summaryrefslogtreecommitdiff
path: root/app/services/protected_branches/create_service.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/protected_branches/create_service.rb')
-rw-r--r--app/services/protected_branches/create_service.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/app/services/protected_branches/create_service.rb b/app/services/protected_branches/create_service.rb
new file mode 100644
index 00000000000..6150a2a83c9
--- /dev/null
+++ b/app/services/protected_branches/create_service.rb
@@ -0,0 +1,27 @@
+module ProtectedBranches
+ class CreateService < BaseService
+ attr_reader :protected_branch
+
+ def execute
+ raise Gitlab::Access::AccessDeniedError unless can?(current_user, :admin_project, project)
+
+ protected_branch = project.protected_branches.new(params)
+
+ ProtectedBranch.transaction do
+ protected_branch.save!
+
+ if protected_branch.push_access_level.blank?
+ protected_branch.create_push_access_level!(access_level: Gitlab::Access::MASTER)
+ end
+
+ if protected_branch.merge_access_level.blank?
+ protected_branch.create_merge_access_level!(access_level: Gitlab::Access::MASTER)
+ end
+ end
+
+ protected_branch
+ rescue ActiveRecord::RecordInvalid
+ protected_branch
+ end
+ end
+end