2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-15 05:15:03 -05:00
|
|
|
# Worker for processing individual commit messages pushed to a repository.
|
2016-10-07 09:20:57 -04:00
|
|
|
#
|
|
|
|
# Jobs for this worker are scheduled for every commit that is being pushed. As a
|
|
|
|
# result of this the workload of this worker should be kept to a bare minimum.
|
|
|
|
# Consider using an extra worker if you need to add any extra (and potentially
|
|
|
|
# slow) processing of commits.
|
|
|
|
class ProcessCommitWorker
|
2017-11-28 11:08:30 -05:00
|
|
|
include ApplicationWorker
|
2016-10-07 09:20:57 -04:00
|
|
|
|
|
|
|
# project_id - The ID of the project this commit belongs to.
|
|
|
|
# user_id - The ID of the user that pushed the commit.
|
2016-11-24 09:07:44 -05:00
|
|
|
# commit_hash - Hash containing commit details to use for constructing a
|
|
|
|
# Commit object without having to use the Git repository.
|
2016-10-07 09:20:57 -04:00
|
|
|
# default - The data was pushed to the default branch.
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-24 09:07:44 -05:00
|
|
|
def perform(project_id, user_id, commit_hash, default = false)
|
2016-10-07 09:20:57 -04:00
|
|
|
project = Project.find_by(id: project_id)
|
|
|
|
|
|
|
|
return unless project
|
2017-05-18 16:17:58 -04:00
|
|
|
return if commit_exists_in_upstream?(project, commit_hash)
|
2016-10-07 09:20:57 -04:00
|
|
|
|
|
|
|
user = User.find_by(id: user_id)
|
|
|
|
|
|
|
|
return unless user
|
|
|
|
|
2016-11-24 09:07:44 -05:00
|
|
|
commit = build_commit(project, commit_hash)
|
2016-10-07 09:20:57 -04:00
|
|
|
author = commit.author || user
|
|
|
|
|
2018-02-09 10:32:42 -05:00
|
|
|
process_commit_message(project, commit, user, author, default)
|
2016-10-07 09:20:57 -04:00
|
|
|
update_issue_metrics(commit, author)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-10-07 09:20:57 -04:00
|
|
|
|
|
|
|
def process_commit_message(project, commit, user, author, default = false)
|
2018-03-05 09:15:58 -05:00
|
|
|
# Ignore closing references from GitLab-generated commit messages.
|
|
|
|
find_closing_issues = default && !commit.merged_merge_request?(user)
|
|
|
|
closed_issues = find_closing_issues ? commit.closes_issues(user) : []
|
2016-10-07 09:20:57 -04:00
|
|
|
|
2018-02-09 10:32:42 -05:00
|
|
|
close_issues(project, user, author, commit, closed_issues) if closed_issues.any?
|
2016-10-07 09:20:57 -04:00
|
|
|
commit.create_cross_references!(author, closed_issues)
|
|
|
|
end
|
|
|
|
|
|
|
|
def close_issues(project, user, author, commit, issues)
|
|
|
|
# We don't want to run permission related queries for every single issue,
|
2018-02-09 10:32:42 -05:00
|
|
|
# therefore we use IssueCollection here and skip the authorization check in
|
2016-10-07 09:20:57 -04:00
|
|
|
# Issues::CloseService#execute.
|
|
|
|
IssueCollection.new(issues).updatable_by_user(user).each do |issue|
|
2017-06-21 09:48:12 -04:00
|
|
|
Issues::CloseService.new(project, author)
|
2019-05-16 07:59:02 -04:00
|
|
|
.close_issue(issue, closed_via: commit)
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-10-07 09:20:57 -04:00
|
|
|
def update_issue_metrics(commit, author)
|
|
|
|
mentioned_issues = commit.all_references(author).issues
|
|
|
|
|
2017-04-04 06:54:58 -04:00
|
|
|
return if mentioned_issues.empty?
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
Issue::Metrics.where(issue_id: mentioned_issues.map(&:id), first_mentioned_in_commit_at: nil)
|
|
|
|
.update_all(first_mentioned_in_commit_at: commit.committed_date)
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-10-07 09:20:57 -04:00
|
|
|
|
2016-11-24 09:07:44 -05:00
|
|
|
def build_commit(project, hash)
|
|
|
|
date_suffix = '_date'
|
|
|
|
|
|
|
|
# When processing Sidekiq payloads various timestamps are stored as Strings.
|
|
|
|
# Commit in turn expects Time-like instances upon input, so we have to
|
|
|
|
# manually parse these values.
|
|
|
|
hash.each do |key, value|
|
|
|
|
if key.to_s.end_with?(date_suffix) && value.is_a?(String)
|
|
|
|
hash[key] = Time.parse(value)
|
|
|
|
end
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
|
2016-11-24 09:07:44 -05:00
|
|
|
Commit.from_hash(hash, project)
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|
2017-05-18 16:17:58 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-23 11:33:49 -04:00
|
|
|
# Avoid reprocessing commits that already exist in the upstream
|
2017-05-18 16:17:58 -04:00
|
|
|
# when project is forked. This will also prevent duplicated system notes.
|
|
|
|
def commit_exists_in_upstream?(project, commit_hash)
|
2018-07-10 12:11:31 -04:00
|
|
|
upstream_project = project.fork_source
|
|
|
|
|
|
|
|
return false unless upstream_project
|
2017-05-18 16:17:58 -04:00
|
|
|
|
|
|
|
commit_id = commit_hash.with_indifferent_access[:id]
|
|
|
|
upstream_project.commit(commit_id).present?
|
|
|
|
end
|
2016-10-07 09:20:57 -04:00
|
|
|
end
|