Skip to content

Commit f482645

Browse files
committed
Merge branch '34927-protect-manual-actions-on-tags' into 'master'
Protect manual actions against protected tag too Closes #34927 See merge request !12908
2 parents 786879e + 7426e61 commit f482645

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

app/policies/ci/build_policy.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ class BuildPolicy < CommitStatusPolicy
33
condition(:protected_action) do
44
next false unless @subject.action?
55

6-
!::Gitlab::UserAccess
7-
.new(@user, project: @subject.project)
8-
.can_merge_to_branch?(@subject.ref)
6+
access = ::Gitlab::UserAccess.new(@user, project: @subject.project)
7+
8+
if @subject.tag?
9+
!access.can_create_tag?(@subject.ref)
10+
else
11+
!access.can_merge_to_branch?(@subject.ref)
12+
end
913
end
1014

1115
rule { protected_action }.prevent :update_build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Protect manual actions against protected tag too
3+
merge_request: 12908
4+
author:

spec/policies/ci/build_policy_spec.rb

+38-6
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,7 @@
103103
project.add_developer(user)
104104
end
105105

106-
context 'when branch build is assigned to is protected' do
107-
before do
108-
create(:protected_branch, :no_one_can_push,
109-
name: 'some-ref', project: project)
110-
end
111-
106+
shared_examples 'protected ref' do
112107
context 'when build is a manual action' do
113108
let(:build) do
114109
create(:ci_build, :manual, ref: 'some-ref', pipeline: pipeline)
@@ -130,6 +125,43 @@
130125
end
131126
end
132127

128+
context 'when build is against a protected branch' do
129+
before do
130+
create(:protected_branch, :no_one_can_push,
131+
name: 'some-ref', project: project)
132+
end
133+
134+
it_behaves_like 'protected ref'
135+
end
136+
137+
context 'when build is against a protected tag' do
138+
before do
139+
create(:protected_tag, :no_one_can_create,
140+
name: 'some-ref', project: project)
141+
142+
build.update(tag: true)
143+
end
144+
145+
it_behaves_like 'protected ref'
146+
end
147+
148+
context 'when build is against a protected tag but it is not a tag' do
149+
before do
150+
create(:protected_tag, :no_one_can_create,
151+
name: 'some-ref', project: project)
152+
end
153+
154+
context 'when build is a manual action' do
155+
let(:build) do
156+
create(:ci_build, :manual, ref: 'some-ref', pipeline: pipeline)
157+
end
158+
159+
it 'includes ability to update build' do
160+
expect(policy).to be_allowed :update_build
161+
end
162+
end
163+
end
164+
133165
context 'when branch build is assigned to is not protected' do
134166
context 'when build is a manual action' do
135167
let(:build) { create(:ci_build, :manual, pipeline: pipeline) }

0 commit comments

Comments
 (0)