Class: Git::BranchPushService

Inherits:
BaseService show all
Includes:
ChangeParams, Gitlab::Access, Gitlab::Utils::StrongMemoize
Defined in:
app/services/git/branch_push_service.rb

Constant Summary

Constants included from Gitlab::Access

Gitlab::Access::AccessDeniedError, Gitlab::Access::DEVELOPER, Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS, Gitlab::Access::GUEST, Gitlab::Access::MAINTAINER, Gitlab::Access::MAINTAINER_PROJECT_ACCESS, Gitlab::Access::MAINTAINER_SUBGROUP_ACCESS, Gitlab::Access::MINIMAL_ACCESS, Gitlab::Access::NO_ACCESS, Gitlab::Access::NO_ONE_PROJECT_ACCESS, Gitlab::Access::OWNER, Gitlab::Access::OWNER_SUBGROUP_ACCESS, Gitlab::Access::PROTECTION_DEV_CAN_MERGE, Gitlab::Access::PROTECTION_DEV_CAN_PUSH, Gitlab::Access::PROTECTION_FULL, Gitlab::Access::PROTECTION_NONE, Gitlab::Access::REPORTER

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from Gitlab::Access

all_values, human_access, #human_access, #human_access_with_none, human_access_with_none, options, options_with_none, options_with_owner, #owner?, project_creation_level_name, project_creation_options, project_creation_string_options, project_creation_string_values, project_creation_values, protection_options, protection_values, subgroup_creation_options, subgroup_creation_string_options, subgroup_creation_string_values, subgroup_creation_values, sym_options, sym_options_with_owner

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#branch_nameObject


85
86
87
# File 'app/services/git/branch_push_service.rb', line 85

def branch_name
  strong_memoize(:branch_name) { Gitlab::Git.ref_name(ref) }
end

#default_branch?Boolean

Returns:

  • (Boolean)

89
90
91
92
93
# File 'app/services/git/branch_push_service.rb', line 89

def default_branch?
  strong_memoize(:default_branch) do
    [nil, branch_name].include?(project.default_branch)
  end
end

#enqueue_detect_repository_languagesObject


51
52
53
54
55
# File 'app/services/git/branch_push_service.rb', line 51

def enqueue_detect_repository_languages
  return unless default_branch?

  DetectRepositoryLanguagesWorker.perform_async(project.id)
end

#enqueue_update_mrsObject

Update merge requests that may be affected by this push. A new branch could cause the last commit of a merge request to change.


39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/git/branch_push_service.rb', line 39

def enqueue_update_mrs
  return if params[:merge_request_branches]&.exclude?(branch_name)

  UpdateMergeRequestsWorker.perform_async(
    project.id,
    current_user.id,
    oldrev,
    newrev,
    ref
  )
end

#executeObject

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

22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/git/branch_push_service.rb', line 22

def execute
  return unless Gitlab::Git.branch_ref?(ref)

  enqueue_update_mrs
  enqueue_detect_repository_languages

  execute_related_hooks
  perform_housekeeping

  stop_environments
  unlock_artifacts

  true
end

70
71
72
# File 'app/services/git/branch_push_service.rb', line 70

def execute_related_hooks
  BranchHooksService.new(project, current_user, params).execute
end

#perform_housekeepingObject


74
75
76
77
78
79
# File 'app/services/git/branch_push_service.rb', line 74

def perform_housekeeping
  housekeeping = Projects::HousekeepingService.new(project)
  housekeeping.increment!
  housekeeping.execute if housekeeping.needed?
rescue Projects::HousekeepingService::LeaseTaken
end

#removing_branch?Boolean

Returns:

  • (Boolean)

81
82
83
# File 'app/services/git/branch_push_service.rb', line 81

def removing_branch?
  Gitlab::Git.blank_ref?(newrev)
end

#stop_environmentsObject

Only stop environments if the ref is a branch that is being deleted


58
59
60
61
62
# File 'app/services/git/branch_push_service.rb', line 58

def stop_environments
  return unless removing_branch?

  Ci::StopEnvironmentsService.new(project, current_user).execute(branch_name)
end

#unlock_artifactsObject


64
65
66
67
68
# File 'app/services/git/branch_push_service.rb', line 64

def unlock_artifacts
  return unless removing_branch?

  Ci::RefDeleteUnlockArtifactsWorker.perform_async(project.id, current_user.id, ref)
end