2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Git
|
2019-07-31 22:56:46 +05:30
|
|
|
class BranchPushService < ::BaseService
|
2019-07-07 11:18:12 +05:30
|
|
|
include Gitlab::Access
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
# 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-07-31 22:56:46 +05:30
|
|
|
return unless Gitlab::Git.branch_ref?(params[:ref])
|
|
|
|
|
|
|
|
enqueue_update_mrs
|
|
|
|
enqueue_detect_repository_languages
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
execute_related_hooks
|
|
|
|
perform_housekeeping
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
stop_environments
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
true
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
# 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
|
|
|
|
UpdateMergeRequestsWorker.perform_async(
|
|
|
|
project.id,
|
|
|
|
current_user.id,
|
|
|
|
params[:oldrev],
|
|
|
|
params[:newrev],
|
|
|
|
params[:ref]
|
|
|
|
)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def enqueue_detect_repository_languages
|
|
|
|
return unless default_branch?
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
DetectRepositoryLanguagesWorker.perform_async(project.id)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
# Only stop environments if the ref is a branch that is being deleted
|
|
|
|
def stop_environments
|
|
|
|
return unless removing_branch?
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
Ci::StopEnvironmentsService.new(project, current_user).execute(branch_name)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def update_remote_mirrors
|
|
|
|
return unless project.has_remote_mirror?
|
|
|
|
|
|
|
|
project.mark_stuck_remote_mirrors_as_failed!
|
|
|
|
project.update_remote_mirrors
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_related_hooks
|
2019-07-31 22:56:46 +05:30
|
|
|
BranchHooksService.new(project, current_user, params).execute
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def perform_housekeeping
|
|
|
|
housekeeping = Projects::HousekeepingService.new(project)
|
|
|
|
housekeeping.increment!
|
|
|
|
housekeeping.execute if housekeeping.needed?
|
|
|
|
rescue Projects::HousekeepingService::LeaseTaken
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def removing_branch?
|
|
|
|
Gitlab::Git.blank_ref?(params[:newrev])
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def branch_name
|
2019-07-31 22:56:46 +05:30
|
|
|
strong_memoize(:branch_name) { Gitlab::Git.ref_name(params[:ref]) }
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def default_branch?
|
|
|
|
strong_memoize(:default_branch) do
|
|
|
|
[nil, branch_name].include?(project.default_branch)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|