2018-11-08 19:23:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
# Worker for processing individual commit messages pushed to a repository.
|
2017-08-17 22:00:37 +05:30
|
|
|
#
|
2019-10-12 21:52:04 +05:30
|
|
|
# Jobs for this worker are scheduled for every commit that contains mentionable
|
|
|
|
# references in its message and does not exist in the upstream project. As a
|
2017-08-17 22:00:37 +05:30
|
|
|
# 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.
|
2020-05-24 23:13:21 +05:30
|
|
|
class ProcessCommitWorker
|
2018-03-17 18:26:18 +05:30
|
|
|
include ApplicationWorker
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
data_consistency :always
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
sidekiq_options retry: 3
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
feature_category :source_code_management
|
2020-04-08 14:13:33 +05:30
|
|
|
urgency :high
|
2020-03-13 15:44:24 +05:30
|
|
|
weight 3
|
2020-05-24 23:13:21 +05:30
|
|
|
idempotent!
|
2020-06-23 00:09:42 +05:30
|
|
|
loggable_arguments 2, 3
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# project_id - The ID of the project this commit belongs to.
|
|
|
|
# user_id - The ID of the user that pushed the commit.
|
|
|
|
# commit_hash - Hash containing commit details to use for constructing a
|
|
|
|
# Commit object without having to use the Git repository.
|
|
|
|
# default - The data was pushed to the default branch.
|
|
|
|
def perform(project_id, user_id, commit_hash, default = false)
|
2020-04-08 14:13:33 +05:30
|
|
|
project = Project.id_in(project_id).first
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
return unless project
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
user = User.id_in(user_id).first
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
return unless user
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
commit = Commit.build_from_sidekiq_hash(project, commit_hash)
|
2017-08-17 22:00:37 +05:30
|
|
|
author = commit.author || user
|
|
|
|
|
|
|
|
process_commit_message(project, commit, user, author, default)
|
|
|
|
update_issue_metrics(commit, author)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_commit_message(project, commit, user, author, default = false)
|
2018-03-27 19:54:05 +05:30
|
|
|
# Ignore closing references from GitLab-generated commit messages.
|
|
|
|
find_closing_issues = default && !commit.merged_merge_request?(user)
|
2020-04-08 14:13:33 +05:30
|
|
|
closed_issues = find_closing_issues ? issues_to_close(project, commit, user) : []
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
close_issues(project, user, author, commit, closed_issues) if closed_issues.any?
|
2017-08-17 22:00:37 +05:30
|
|
|
commit.create_cross_references!(author, closed_issues)
|
|
|
|
end
|
|
|
|
|
|
|
|
def close_issues(project, user, author, commit, issues)
|
2022-11-25 23:54:43 +05:30
|
|
|
Issues::CloseWorker.bulk_perform_async_with_contexts(
|
|
|
|
issues,
|
|
|
|
arguments_proc: -> (issue) {
|
|
|
|
[project.id, issue.id, issue.class.to_s, { closed_by: author.id, commit_hash: commit.to_hash }]
|
|
|
|
},
|
|
|
|
context_proc: -> (issue) { { project: project } }
|
|
|
|
)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def issues_to_close(project, commit, user)
|
|
|
|
Gitlab::ClosingIssueExtractor
|
|
|
|
.new(project, user)
|
|
|
|
.closed_by_message(commit.safe_message)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def update_issue_metrics(commit, author)
|
|
|
|
mentioned_issues = commit.all_references(author).issues
|
|
|
|
|
|
|
|
return if mentioned_issues.empty?
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
Issue::Metrics.for_issues(mentioned_issues)
|
|
|
|
.with_first_mention_not_earlier_than(commit.committed_date)
|
2017-09-10 17:25:29 +05:30
|
|
|
.update_all(first_mentioned_in_commit_at: commit.committed_date)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|