Skip to content

Commit dc12a4a

Browse files
authored
Fix wrong action IDs in GitLabMergeRequestTrigger.java and add support to "approved" and "unapproved" as well (#252)
1 parent 9ed86bb commit dc12a4a

File tree

4 files changed

+134
-48
lines changed

4 files changed

+134
-48
lines changed

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestTrigger.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class GitLabMergeRequestTrigger extends GitLabMergeRequestSCMEvent {
1111

1212
public static final Logger LOGGER = Logger
13-
.getLogger(GitLabMergeRequestTrigger.class.getName());
13+
.getLogger(GitLabMergeRequestTrigger.class.getName());
1414

1515
public GitLabMergeRequestTrigger(MergeRequestEvent mrEvent, String origin) {
1616
super(mrEvent, origin);
@@ -19,18 +19,18 @@ public GitLabMergeRequestTrigger(MergeRequestEvent mrEvent, String origin) {
1919
@Override
2020
public boolean isMatch(@NonNull GitLabSCMSource source) {
2121
final GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(
22-
null, SCMHeadObserver.none())
23-
.withTraits(source.getTraits());
22+
null, SCMHeadObserver.none())
23+
.withTraits(source.getTraits());
2424

2525
boolean shouldBuild = this.shouldBuild(getPayload(), sourceContext);
2626
LOGGER.log(Level.FINE, "isMatch() result for MR-{0}: {1}",
27-
new Object[]{
28-
getPayload().getObjectAttributes().getIid(),
29-
String.valueOf(shouldBuild)
30-
});
27+
new Object[] {
28+
getPayload().getObjectAttributes().getIid(),
29+
String.valueOf(shouldBuild)
30+
});
3131

3232
return getPayload().getObjectAttributes().getTargetProjectId()
33-
.equals(source.getProjectId()) && shouldBuild;
33+
.equals(source.getProjectId()) && shouldBuild;
3434
}
3535

3636
private boolean shouldBuild(MergeRequestEvent mrEvent, GitLabSCMSourceContext context) {
@@ -67,7 +67,7 @@ private boolean shouldBuild(MergeRequestEvent mrEvent, GitLabSCMSourceContext co
6767

6868
if (!shouldBuild) {
6969
LOGGER.log(Level.FINE, "shouldBuild for MR-{0} set to false due to non-code related updates.",
70-
getPayload().getObjectAttributes().getIid());
70+
getPayload().getObjectAttributes().getIid());
7171
}
7272

7373
if (action.equals("open")) {
@@ -78,12 +78,20 @@ private boolean shouldBuild(MergeRequestEvent mrEvent, GitLabSCMSourceContext co
7878
return context.alwaysBuildMRReOpen();
7979
}
8080

81+
if (action.equals("approval")) {
82+
return !context.alwaysIgnoreMRApproval();
83+
}
84+
85+
if (action.equals("unapproval")) {
86+
return !context.alwaysIgnoreMRUnApproval();
87+
}
88+
8189
if (action.equals("approved")) {
82-
return !context.alwaysIgnoreMRApprove();
90+
return !context.alwaysIgnoreMRApproved();
8391
}
8492

8593
if (action.equals("unapproved")) {
86-
return !context.alwaysIgnoreMRUnApprove();
94+
return !context.alwaysIgnoreMRUnApproved();
8795
}
8896
}
8997

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceContext.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import jenkins.scm.api.trait.SCMSourceContext;
1414

1515
public class GitLabSCMSourceContext
16-
extends SCMSourceContext<GitLabSCMSourceContext, GitLabSCMSourceRequest> {
16+
extends SCMSourceContext<GitLabSCMSourceContext, GitLabSCMSourceRequest> {
1717

1818
private boolean wantBranches;
1919
private boolean wantTags;
@@ -22,10 +22,10 @@ public class GitLabSCMSourceContext
2222

2323
@NonNull
2424
private Set<ChangeRequestCheckoutStrategy> originMRStrategies = EnumSet
25-
.noneOf(ChangeRequestCheckoutStrategy.class);
25+
.noneOf(ChangeRequestCheckoutStrategy.class);
2626
@NonNull
2727
private Set<ChangeRequestCheckoutStrategy> forkMRStrategies = EnumSet
28-
.noneOf(ChangeRequestCheckoutStrategy.class);
28+
.noneOf(ChangeRequestCheckoutStrategy.class);
2929
@NonNull
3030
private GitLabHookRegistration webhookRegistration = GitLabHookRegistration.SYSTEM;
3131
@NonNull
@@ -53,14 +53,18 @@ public class GitLabSCMSourceContext
5353

5454
private boolean alwaysBuildMRReOpen = true;
5555

56-
private boolean alwaysIgnoreMRApprove = false;
56+
private boolean alwaysIgnoreMRApproval = false;
5757

58-
private boolean alwaysIgnoreMRUnApprove = false;
58+
private boolean alwaysIgnoreMRUnApproval = false;
59+
60+
private boolean alwaysIgnoreMRApproved = false;
61+
62+
private boolean alwaysIgnoreMRUnApproved = false;
5963

6064
private boolean alwaysIgnoreNonCodeRelatedUpdates = false;
6165

6266
public GitLabSCMSourceContext(@CheckForNull SCMSourceCriteria criteria,
63-
@NonNull SCMHeadObserver observer) {
67+
@NonNull SCMHeadObserver observer) {
6468
super(criteria, observer);
6569
}
6670

@@ -128,7 +132,9 @@ public final boolean mrCommentTriggerEnabled() {
128132
return mrCommentTriggerEnabled;
129133
}
130134

131-
public final boolean getOnlyTrustedMembersCanTrigger() { return onlyTrustedMembersCanTrigger; }
135+
public final boolean getOnlyTrustedMembersCanTrigger() {
136+
return onlyTrustedMembersCanTrigger;
137+
}
132138

133139
public boolean alwaysBuildMROpen() {
134140
return alwaysBuildMROpen;
@@ -138,12 +144,20 @@ public boolean alwaysBuildMRReOpen() {
138144
return alwaysBuildMRReOpen;
139145
}
140146

141-
public boolean alwaysIgnoreMRApprove() {
142-
return alwaysIgnoreMRApprove;
147+
public boolean alwaysIgnoreMRApproval() {
148+
return alwaysIgnoreMRApproval;
149+
}
150+
151+
public boolean alwaysIgnoreMRUnApproval() {
152+
return alwaysIgnoreMRUnApproval;
153+
}
154+
155+
public boolean alwaysIgnoreMRApproved() {
156+
return alwaysIgnoreMRApproved;
143157
}
144158

145-
public boolean alwaysIgnoreMRUnApprove() {
146-
return alwaysIgnoreMRUnApprove;
159+
public boolean alwaysIgnoreMRUnApproved() {
160+
return alwaysIgnoreMRUnApproved;
147161
}
148162

149163
public boolean alwaysIgnoreNonCodeRelatedUpdates() {
@@ -184,14 +198,14 @@ public GitLabSCMSourceContext wantForkMRs(boolean include) {
184198

185199
@NonNull
186200
public GitLabSCMSourceContext withOriginMRStrategies(
187-
Set<ChangeRequestCheckoutStrategy> strategies) {
201+
Set<ChangeRequestCheckoutStrategy> strategies) {
188202
originMRStrategies.addAll(strategies);
189203
return this;
190204
}
191205

192206
@NonNull
193207
public GitLabSCMSourceContext withForkMRStrategies(
194-
Set<ChangeRequestCheckoutStrategy> strategies) {
208+
Set<ChangeRequestCheckoutStrategy> strategies) {
195209
forkMRStrategies.addAll(strategies);
196210
return this;
197211
}
@@ -262,7 +276,7 @@ public final GitLabSCMSourceContext withBuildStatusNameCustomPart(final String b
262276
@NonNull
263277
@Override
264278
public GitLabSCMSourceRequest newRequest(@NonNull SCMSource source,
265-
@CheckForNull TaskListener listener) {
279+
@CheckForNull TaskListener listener) {
266280
return new GitLabSCMSourceRequest(source, this, listener);
267281
}
268282

@@ -276,13 +290,23 @@ public final GitLabSCMSourceContext withAlwaysBuildMRReOpen(boolean enabled) {
276290
return this;
277291
}
278292

279-
public final GitLabSCMSourceContext withAlwaysIgnoreMRApprove(boolean enabled) {
280-
this.alwaysIgnoreMRApprove = enabled;
293+
public final GitLabSCMSourceContext withalwaysIgnoreMRApproval(boolean enabled) {
294+
this.alwaysIgnoreMRApproval = enabled;
295+
return this;
296+
}
297+
298+
public final GitLabSCMSourceContext withalwaysIgnoreMRUnApproval(boolean enabled) {
299+
this.alwaysIgnoreMRUnApproval = enabled;
300+
return this;
301+
}
302+
303+
public final GitLabSCMSourceContext withalwaysIgnoreMRApproved(boolean enabled) {
304+
this.alwaysIgnoreMRApproved = enabled;
281305
return this;
282306
}
283307

284-
public final GitLabSCMSourceContext withAlwaysIgnoreMRUnApprove(boolean enabled) {
285-
this.alwaysIgnoreMRUnApprove = enabled;
308+
public final GitLabSCMSourceContext withalwaysIgnoreMRUnApproved(boolean enabled) {
309+
this.alwaysIgnoreMRUnApproved = enabled;
286310
return this;
287311
}
288312

src/main/java/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait.java

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,22 @@ public class WebhookListenerBuildConditionsTrait extends SCMSourceTrait {
2525
/**
2626
* Always ignore webhook if it's an approval event
2727
*/
28-
private boolean alwaysIgnoreMRApprove = false;
28+
private boolean alwaysIgnoreMRApproval = false;
2929

3030
/**
3131
* Always ignore webhook if it's an un-approval event
3232
*/
33-
private boolean alwaysIgnoreMRUnApprove = false;
33+
private boolean alwaysIgnoreMRUnApproval = false;
34+
35+
/**
36+
* Always ignore webhook if it's an approved event
37+
*/
38+
private boolean alwaysIgnoreMRApproved = false;
39+
40+
/**
41+
* Always ignore webhook if it's an un-approved event
42+
*/
43+
private boolean alwaysIgnoreMRUnApproved = false;
3444

3545
/**
3646
* Always ignore webhook if it's a non code related update such as title change
@@ -50,10 +60,12 @@ protected void decorateContext(SCMSourceContext<?, ?> context) {
5060
if (context instanceof GitLabSCMSourceContext) {
5161
GitLabSCMSourceContext ctx = (GitLabSCMSourceContext) context;
5262
ctx.withAlwaysBuildMROpen(getAlwaysBuildMROpen())
53-
.withAlwaysBuildMRReOpen(getAlwaysBuildMRReOpen())
54-
.withAlwaysIgnoreMRApprove(getAlwaysIgnoreMRApprove())
55-
.withAlwaysIgnoreMRUnApprove(getAlwaysIgnoreMRUnApprove())
56-
.withAlwaysIgnoreNonCodeRelatedUpdates(getAlwaysIgnoreNonCodeRelatedUpdates());
63+
.withAlwaysBuildMRReOpen(getAlwaysBuildMRReOpen())
64+
.withalwaysIgnoreMRApproval(getalwaysIgnoreMRApproval())
65+
.withalwaysIgnoreMRUnApproval(getalwaysIgnoreMRUnApproval())
66+
.withalwaysIgnoreMRApproved(getalwaysIgnoreMRApproved())
67+
.withalwaysIgnoreMRUnApproved(getalwaysIgnoreMRUnApproved())
68+
.withAlwaysIgnoreNonCodeRelatedUpdates(getAlwaysIgnoreNonCodeRelatedUpdates());
5769
}
5870
}
5971

@@ -104,17 +116,35 @@ public boolean getAlwaysBuildMRReOpen() {
104116
*
105117
* @return false to run build on MR approval
106118
*/
107-
public boolean getAlwaysIgnoreMRApprove() {
108-
return alwaysIgnoreMRApprove;
119+
public boolean getalwaysIgnoreMRApproval() {
120+
return alwaysIgnoreMRApproval;
109121
}
110122

111123
/**
112124
* Run build on MR un-approval
113125
*
114126
* @return false to run build on non-code related MR updates
115127
*/
116-
public boolean getAlwaysIgnoreMRUnApprove() {
117-
return alwaysIgnoreMRUnApprove;
128+
public boolean getalwaysIgnoreMRUnApproval() {
129+
return alwaysIgnoreMRUnApproval;
130+
}
131+
132+
/**
133+
* Run build on MR approved
134+
*
135+
* @return false to run build on MR approved
136+
*/
137+
public boolean getalwaysIgnoreMRApproved() {
138+
return alwaysIgnoreMRApproved;
139+
}
140+
141+
/**
142+
* Run build on MR un-approved
143+
*
144+
* @return false to run build on non-code related MR updates
145+
*/
146+
public boolean getalwaysIgnoreMRUnApproved() {
147+
return alwaysIgnoreMRUnApproved;
118148
}
119149

120150
/**
@@ -143,23 +173,41 @@ public void setAlwaysBuildMRReOpen(boolean alwaysBuildMRReOpen) {
143173
}
144174

145175
/**
146-
* Setter for stapler to set the alwaysIgnoreMRApprove of the WebhookListener
176+
* Setter for stapler to set the alwaysIgnoreMRApproval of the WebhookListener
177+
*/
178+
@DataBoundSetter
179+
public void setalwaysIgnoreMRApproval(boolean alwaysIgnoreMRApproval) {
180+
this.alwaysIgnoreMRApproval = alwaysIgnoreMRApproval;
181+
}
182+
183+
/**
184+
* Setter for stapler to set the alwaysIgnoreMRUnApproval of the WebhookListener
185+
*/
186+
@DataBoundSetter
187+
public void setalwaysIgnoreMRUnApproval(boolean alwaysIgnoreMRUnApproval) {
188+
this.alwaysIgnoreMRUnApproval = alwaysIgnoreMRUnApproval;
189+
}
190+
191+
/**
192+
* Setter for stapler to set the alwaysIgnoreMRApproved of the WebhookListener
147193
*/
148194
@DataBoundSetter
149-
public void setAlwaysIgnoreMRApprove(boolean alwaysIgnoreMRApprove) {
150-
this.alwaysIgnoreMRApprove = alwaysIgnoreMRApprove;
195+
public void setalwaysIgnoreMRApproved(boolean alwaysIgnoreMRApproved) {
196+
this.alwaysIgnoreMRApproved = alwaysIgnoreMRApproved;
151197
}
152198

153199
/**
154-
* Setter for stapler to set the alwaysIgnoreMRUnApprove of the WebhookListener
200+
* Setter for stapler to set the alwaysIgnoreMRUnApproved of the
201+
* WebhookListener
155202
*/
156203
@DataBoundSetter
157-
public void setAlwaysIgnoreMRUnApprove(boolean alwaysIgnoreMRUnApprove) {
158-
this.alwaysIgnoreMRUnApprove = alwaysIgnoreMRUnApprove;
204+
public void setalwaysIgnoreMRUnApproved(boolean alwaysIgnoreMRUnApproved) {
205+
this.alwaysIgnoreMRUnApproved = alwaysIgnoreMRUnApproved;
159206
}
160207

161208
/**
162-
* Setter for stapler to set the alwaysIgnoreNonCodeRelatedUpdates of the WebhookListener
209+
* Setter for stapler to set the alwaysIgnoreNonCodeRelatedUpdates of the
210+
* WebhookListener
163211
*/
164212
@DataBoundSetter
165213
public void setAlwaysIgnoreNonCodeRelatedUpdates(boolean alwaysIgnoreNonCodeRelatedUpdates) {

src/main/resources/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait/config.jelly

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
<f:entry title="${%Always build pipeline on Re-Open MR webhook}" field="alwaysBuildMRReOpen">
77
<f:checkbox default="checked"/>
88
</f:entry>
9-
<f:entry title="${%Ignore build pipeline on MR Approval}" field="alwaysIgnoreMRApprove">
9+
<f:entry title="${%Ignore build pipeline on MR Approval}" field="alwaysIgnoreMRApproval">
1010
<f:checkbox default="checked"/>
1111
</f:entry>
12-
<f:entry title="${%Ignore build pipeline on MR Un-Approval}" field="alwaysIgnoreMRUnApprove">
12+
<f:entry title="${%Ignore build pipeline on MR Un-Approval}" field="alwaysIgnoreMRUnApproval">
13+
<f:checkbox default="checked"/>
14+
</f:entry>
15+
<f:entry title="${%Ignore build pipeline on MR Approved}" field="alwaysIgnoreMRApproved">
16+
<f:checkbox default="checked"/>
17+
</f:entry>
18+
<f:entry title="${%Ignore build pipeline on MR Un-Approved}" field="alwaysIgnoreMRUnApproved">
1319
<f:checkbox default="checked"/>
1420
</f:entry>
1521
<f:entry title="${%Ignore builds for non-code related MR updates}" field="alwaysIgnoreNonCodeRelatedUpdates">

0 commit comments

Comments
 (0)