debian-mirror-gitlab/app/services/projects/protect_default_branch_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
2 KiB
Ruby
Raw Normal View History

2019-03-02 22:35:43 +05:30
# frozen_string_literal: true
module Projects
# Service class that can be used to execute actions necessary after creating a
# default branch.
class ProtectDefaultBranchService
attr_reader :project, :default_branch_protection
# @param [Project] project
def initialize(project)
@project = project
@default_branch_protection = Gitlab::Access::BranchProtection
2020-04-08 14:13:33 +05:30
.new(project.namespace.default_branch_protection)
2019-03-02 22:35:43 +05:30
end
def execute
protect_default_branch if default_branch
end
def protect_default_branch
# Ensure HEAD points to the default branch in case it is not master
project.change_head(default_branch)
2021-09-30 23:02:18 +05:30
create_protected_branch if protect_branch? && !protected_branch_exists?
2019-03-02 22:35:43 +05:30
end
def create_protected_branch
params = {
name: default_branch,
push_access_levels_attributes: [{ access_level: push_access_level }],
merge_access_levels_attributes: [{ access_level: merge_access_level }]
}
# The creator of the project is always allowed to create protected
# branches, so we skip the authorization check in this service class.
ProtectedBranches::CreateService
.new(project, project.creator, params)
.execute(skip_authorization: true)
end
def protect_branch?
default_branch_protection.any? &&
!ProtectedBranch.protected?(project, default_branch)
end
2021-09-30 23:02:18 +05:30
def protected_branch_exists?
2023-05-27 22:25:52 +05:30
project.all_protected_branches.find_by_name(default_branch).present?
2021-09-30 23:02:18 +05:30
end
2019-03-02 22:35:43 +05:30
def default_branch
project.default_branch
end
def push_access_level
if default_branch_protection.developer_can_push?
Gitlab::Access::DEVELOPER
else
Gitlab::Access::MAINTAINER
end
end
def merge_access_level
if default_branch_protection.developer_can_merge?
Gitlab::Access::DEVELOPER
else
Gitlab::Access::MAINTAINER
end
end
end
end
2021-10-27 15:23:28 +05:30
Projects::ProtectDefaultBranchService.prepend_mod