2019-03-22 17:01:48 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Git
|
2019-03-28 14:59:24 +00:00
|
|
|
class BranchPushService < ::BaseService
|
2019-03-22 17:01:48 +00:00
|
|
|
include Gitlab::Access
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2019-10-03 15:07:07 +00:00
|
|
|
include ChangeParams
|
2019-03-22 17:01:48 +00:00
|
|
|
|
|
|
|
# This method will be called after each git update
|
|
|
|
# and only if the provided user and project are present in GitLab.
|
|
|
|
#
|
|
|
|
# All callbacks for post receive action should be placed here.
|
|
|
|
#
|
|
|
|
# Next, this method:
|
|
|
|
# 1. Creates the push event
|
|
|
|
# 2. Updates merge requests
|
|
|
|
# 3. Recognizes cross-references from commit messages
|
|
|
|
# 4. Executes the project's webhooks
|
|
|
|
# 5. Executes the project's services
|
|
|
|
# 6. Checks if the project's main language has changed
|
|
|
|
#
|
|
|
|
def execute
|
2019-10-03 15:07:07 +00:00
|
|
|
return unless Gitlab::Git.branch_ref?(ref)
|
2019-03-28 14:59:24 +00:00
|
|
|
|
|
|
|
enqueue_update_mrs
|
|
|
|
enqueue_detect_repository_languages
|
|
|
|
|
2019-03-22 17:01:48 +00:00
|
|
|
execute_related_hooks
|
|
|
|
|
2019-03-28 14:59:24 +00:00
|
|
|
stop_environments
|
2020-07-14 12:09:14 +00:00
|
|
|
unlock_artifacts
|
2019-03-22 17:01:48 +00:00
|
|
|
|
2019-03-28 14:59:24 +00:00
|
|
|
true
|
2019-03-22 17:01:48 +00:00
|
|
|
end
|
|
|
|
|
2019-03-28 14:59:24 +00:00
|
|
|
# Update merge requests that may be affected by this push. A new branch
|
|
|
|
# could cause the last commit of a merge request to change.
|
|
|
|
def enqueue_update_mrs
|
2020-04-21 15:21:10 +00:00
|
|
|
return if params[:merge_request_branches]&.exclude?(branch_name)
|
|
|
|
|
2019-03-28 14:59:24 +00:00
|
|
|
UpdateMergeRequestsWorker.perform_async(
|
|
|
|
project.id,
|
|
|
|
current_user.id,
|
2019-10-03 15:07:07 +00:00
|
|
|
oldrev,
|
|
|
|
newrev,
|
|
|
|
ref
|
2019-03-28 14:59:24 +00:00
|
|
|
)
|
2019-03-22 17:01:48 +00:00
|
|
|
end
|
|
|
|
|
2019-03-28 14:59:24 +00:00
|
|
|
def enqueue_detect_repository_languages
|
|
|
|
return unless default_branch?
|
2019-03-22 17:01:48 +00:00
|
|
|
|
2019-05-04 09:11:06 +00:00
|
|
|
DetectRepositoryLanguagesWorker.perform_async(project.id)
|
2019-03-22 17:01:48 +00:00
|
|
|
end
|
|
|
|
|
2019-03-28 14:59:24 +00:00
|
|
|
# Only stop environments if the ref is a branch that is being deleted
|
|
|
|
def stop_environments
|
|
|
|
return unless removing_branch?
|
2019-03-22 17:01:48 +00:00
|
|
|
|
2021-08-06 18:09:57 +00:00
|
|
|
Environments::StopService.new(project, current_user).execute_for_branch(branch_name)
|
2019-03-22 17:01:48 +00:00
|
|
|
end
|
|
|
|
|
2020-07-14 12:09:14 +00:00
|
|
|
def unlock_artifacts
|
|
|
|
return unless removing_branch?
|
|
|
|
|
|
|
|
Ci::RefDeleteUnlockArtifactsWorker.perform_async(project.id, current_user.id, ref)
|
|
|
|
end
|
|
|
|
|
2019-03-22 17:01:48 +00:00
|
|
|
def execute_related_hooks
|
2019-03-28 14:59:24 +00:00
|
|
|
BranchHooksService.new(project, current_user, params).execute
|
2019-03-22 17:01:48 +00:00
|
|
|
end
|
|
|
|
|
2019-03-28 14:59:24 +00:00
|
|
|
def removing_branch?
|
2019-10-03 15:07:07 +00:00
|
|
|
Gitlab::Git.blank_ref?(newrev)
|
2019-03-22 17:01:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def branch_name
|
2019-10-03 15:07:07 +00:00
|
|
|
strong_memoize(:branch_name) { Gitlab::Git.ref_name(ref) }
|
2019-03-22 17:01:48 +00:00
|
|
|
end
|
|
|
|
|
2019-03-28 14:59:24 +00:00
|
|
|
def default_branch?
|
|
|
|
strong_memoize(:default_branch) do
|
|
|
|
[nil, branch_name].include?(project.default_branch)
|
2019-03-22 17:01:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Git::BranchPushService.prepend_mod_with('Git::BranchPushService')
|