From dc12a4a064349814682eae6b959a784365aec39c Mon Sep 17 00:00:00 2001 From: Michael Fitoussi <16884890+mifitous@users.noreply.github.com> Date: Tue, 29 Nov 2022 17:24:54 +0200 Subject: [PATCH] Fix wrong action IDs in GitLabMergeRequestTrigger.java and add support to "approved" and "unapproved" as well (#252) --- .../GitLabMergeRequestTrigger.java | 30 ++++--- .../GitLabSCMSourceContext.java | 60 ++++++++++---- .../WebhookListenerBuildConditionsTrait.java | 82 +++++++++++++++---- .../config.jelly | 10 ++- 4 files changed, 134 insertions(+), 48 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestTrigger.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestTrigger.java index 71e006a5..61a72b87 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestTrigger.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestTrigger.java @@ -10,7 +10,7 @@ public class GitLabMergeRequestTrigger extends GitLabMergeRequestSCMEvent { public static final Logger LOGGER = Logger - .getLogger(GitLabMergeRequestTrigger.class.getName()); + .getLogger(GitLabMergeRequestTrigger.class.getName()); public GitLabMergeRequestTrigger(MergeRequestEvent mrEvent, String origin) { super(mrEvent, origin); @@ -19,18 +19,18 @@ public GitLabMergeRequestTrigger(MergeRequestEvent mrEvent, String origin) { @Override public boolean isMatch(@NonNull GitLabSCMSource source) { final GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext( - null, SCMHeadObserver.none()) - .withTraits(source.getTraits()); + null, SCMHeadObserver.none()) + .withTraits(source.getTraits()); boolean shouldBuild = this.shouldBuild(getPayload(), sourceContext); LOGGER.log(Level.FINE, "isMatch() result for MR-{0}: {1}", - new Object[]{ - getPayload().getObjectAttributes().getIid(), - String.valueOf(shouldBuild) - }); + new Object[] { + getPayload().getObjectAttributes().getIid(), + String.valueOf(shouldBuild) + }); return getPayload().getObjectAttributes().getTargetProjectId() - .equals(source.getProjectId()) && shouldBuild; + .equals(source.getProjectId()) && shouldBuild; } private boolean shouldBuild(MergeRequestEvent mrEvent, GitLabSCMSourceContext context) { @@ -67,7 +67,7 @@ private boolean shouldBuild(MergeRequestEvent mrEvent, GitLabSCMSourceContext co if (!shouldBuild) { LOGGER.log(Level.FINE, "shouldBuild for MR-{0} set to false due to non-code related updates.", - getPayload().getObjectAttributes().getIid()); + getPayload().getObjectAttributes().getIid()); } if (action.equals("open")) { @@ -78,12 +78,20 @@ private boolean shouldBuild(MergeRequestEvent mrEvent, GitLabSCMSourceContext co return context.alwaysBuildMRReOpen(); } + if (action.equals("approval")) { + return !context.alwaysIgnoreMRApproval(); + } + + if (action.equals("unapproval")) { + return !context.alwaysIgnoreMRUnApproval(); + } + if (action.equals("approved")) { - return !context.alwaysIgnoreMRApprove(); + return !context.alwaysIgnoreMRApproved(); } if (action.equals("unapproved")) { - return !context.alwaysIgnoreMRUnApprove(); + return !context.alwaysIgnoreMRUnApproved(); } } diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceContext.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceContext.java index 7ac5bb99..b9e2d283 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceContext.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceContext.java @@ -13,7 +13,7 @@ import jenkins.scm.api.trait.SCMSourceContext; public class GitLabSCMSourceContext - extends SCMSourceContext { + extends SCMSourceContext { private boolean wantBranches; private boolean wantTags; @@ -22,10 +22,10 @@ public class GitLabSCMSourceContext @NonNull private Set originMRStrategies = EnumSet - .noneOf(ChangeRequestCheckoutStrategy.class); + .noneOf(ChangeRequestCheckoutStrategy.class); @NonNull private Set forkMRStrategies = EnumSet - .noneOf(ChangeRequestCheckoutStrategy.class); + .noneOf(ChangeRequestCheckoutStrategy.class); @NonNull private GitLabHookRegistration webhookRegistration = GitLabHookRegistration.SYSTEM; @NonNull @@ -53,14 +53,18 @@ public class GitLabSCMSourceContext private boolean alwaysBuildMRReOpen = true; - private boolean alwaysIgnoreMRApprove = false; + private boolean alwaysIgnoreMRApproval = false; - private boolean alwaysIgnoreMRUnApprove = false; + private boolean alwaysIgnoreMRUnApproval = false; + + private boolean alwaysIgnoreMRApproved = false; + + private boolean alwaysIgnoreMRUnApproved = false; private boolean alwaysIgnoreNonCodeRelatedUpdates = false; public GitLabSCMSourceContext(@CheckForNull SCMSourceCriteria criteria, - @NonNull SCMHeadObserver observer) { + @NonNull SCMHeadObserver observer) { super(criteria, observer); } @@ -128,7 +132,9 @@ public final boolean mrCommentTriggerEnabled() { return mrCommentTriggerEnabled; } - public final boolean getOnlyTrustedMembersCanTrigger() { return onlyTrustedMembersCanTrigger; } + public final boolean getOnlyTrustedMembersCanTrigger() { + return onlyTrustedMembersCanTrigger; + } public boolean alwaysBuildMROpen() { return alwaysBuildMROpen; @@ -138,12 +144,20 @@ public boolean alwaysBuildMRReOpen() { return alwaysBuildMRReOpen; } - public boolean alwaysIgnoreMRApprove() { - return alwaysIgnoreMRApprove; + public boolean alwaysIgnoreMRApproval() { + return alwaysIgnoreMRApproval; + } + + public boolean alwaysIgnoreMRUnApproval() { + return alwaysIgnoreMRUnApproval; + } + + public boolean alwaysIgnoreMRApproved() { + return alwaysIgnoreMRApproved; } - public boolean alwaysIgnoreMRUnApprove() { - return alwaysIgnoreMRUnApprove; + public boolean alwaysIgnoreMRUnApproved() { + return alwaysIgnoreMRUnApproved; } public boolean alwaysIgnoreNonCodeRelatedUpdates() { @@ -184,14 +198,14 @@ public GitLabSCMSourceContext wantForkMRs(boolean include) { @NonNull public GitLabSCMSourceContext withOriginMRStrategies( - Set strategies) { + Set strategies) { originMRStrategies.addAll(strategies); return this; } @NonNull public GitLabSCMSourceContext withForkMRStrategies( - Set strategies) { + Set strategies) { forkMRStrategies.addAll(strategies); return this; } @@ -262,7 +276,7 @@ public final GitLabSCMSourceContext withBuildStatusNameCustomPart(final String b @NonNull @Override public GitLabSCMSourceRequest newRequest(@NonNull SCMSource source, - @CheckForNull TaskListener listener) { + @CheckForNull TaskListener listener) { return new GitLabSCMSourceRequest(source, this, listener); } @@ -276,13 +290,23 @@ public final GitLabSCMSourceContext withAlwaysBuildMRReOpen(boolean enabled) { return this; } - public final GitLabSCMSourceContext withAlwaysIgnoreMRApprove(boolean enabled) { - this.alwaysIgnoreMRApprove = enabled; + public final GitLabSCMSourceContext withalwaysIgnoreMRApproval(boolean enabled) { + this.alwaysIgnoreMRApproval = enabled; + return this; + } + + public final GitLabSCMSourceContext withalwaysIgnoreMRUnApproval(boolean enabled) { + this.alwaysIgnoreMRUnApproval = enabled; + return this; + } + + public final GitLabSCMSourceContext withalwaysIgnoreMRApproved(boolean enabled) { + this.alwaysIgnoreMRApproved = enabled; return this; } - public final GitLabSCMSourceContext withAlwaysIgnoreMRUnApprove(boolean enabled) { - this.alwaysIgnoreMRUnApprove = enabled; + public final GitLabSCMSourceContext withalwaysIgnoreMRUnApproved(boolean enabled) { + this.alwaysIgnoreMRUnApproved = enabled; return this; } diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait.java index ae35832a..93d1f0ca 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait.java @@ -25,12 +25,22 @@ public class WebhookListenerBuildConditionsTrait extends SCMSourceTrait { /** * Always ignore webhook if it's an approval event */ - private boolean alwaysIgnoreMRApprove = false; + private boolean alwaysIgnoreMRApproval = false; /** * Always ignore webhook if it's an un-approval event */ - private boolean alwaysIgnoreMRUnApprove = false; + private boolean alwaysIgnoreMRUnApproval = false; + + /** + * Always ignore webhook if it's an approved event + */ + private boolean alwaysIgnoreMRApproved = false; + + /** + * Always ignore webhook if it's an un-approved event + */ + private boolean alwaysIgnoreMRUnApproved = false; /** * Always ignore webhook if it's a non code related update such as title change @@ -50,10 +60,12 @@ protected void decorateContext(SCMSourceContext context) { if (context instanceof GitLabSCMSourceContext) { GitLabSCMSourceContext ctx = (GitLabSCMSourceContext) context; ctx.withAlwaysBuildMROpen(getAlwaysBuildMROpen()) - .withAlwaysBuildMRReOpen(getAlwaysBuildMRReOpen()) - .withAlwaysIgnoreMRApprove(getAlwaysIgnoreMRApprove()) - .withAlwaysIgnoreMRUnApprove(getAlwaysIgnoreMRUnApprove()) - .withAlwaysIgnoreNonCodeRelatedUpdates(getAlwaysIgnoreNonCodeRelatedUpdates()); + .withAlwaysBuildMRReOpen(getAlwaysBuildMRReOpen()) + .withalwaysIgnoreMRApproval(getalwaysIgnoreMRApproval()) + .withalwaysIgnoreMRUnApproval(getalwaysIgnoreMRUnApproval()) + .withalwaysIgnoreMRApproved(getalwaysIgnoreMRApproved()) + .withalwaysIgnoreMRUnApproved(getalwaysIgnoreMRUnApproved()) + .withAlwaysIgnoreNonCodeRelatedUpdates(getAlwaysIgnoreNonCodeRelatedUpdates()); } } @@ -104,8 +116,8 @@ public boolean getAlwaysBuildMRReOpen() { * * @return false to run build on MR approval */ - public boolean getAlwaysIgnoreMRApprove() { - return alwaysIgnoreMRApprove; + public boolean getalwaysIgnoreMRApproval() { + return alwaysIgnoreMRApproval; } /** @@ -113,8 +125,26 @@ public boolean getAlwaysIgnoreMRApprove() { * * @return false to run build on non-code related MR updates */ - public boolean getAlwaysIgnoreMRUnApprove() { - return alwaysIgnoreMRUnApprove; + public boolean getalwaysIgnoreMRUnApproval() { + return alwaysIgnoreMRUnApproval; + } + + /** + * Run build on MR approved + * + * @return false to run build on MR approved + */ + public boolean getalwaysIgnoreMRApproved() { + return alwaysIgnoreMRApproved; + } + + /** + * Run build on MR un-approved + * + * @return false to run build on non-code related MR updates + */ + public boolean getalwaysIgnoreMRUnApproved() { + return alwaysIgnoreMRUnApproved; } /** @@ -143,23 +173,41 @@ public void setAlwaysBuildMRReOpen(boolean alwaysBuildMRReOpen) { } /** - * Setter for stapler to set the alwaysIgnoreMRApprove of the WebhookListener + * Setter for stapler to set the alwaysIgnoreMRApproval of the WebhookListener + */ + @DataBoundSetter + public void setalwaysIgnoreMRApproval(boolean alwaysIgnoreMRApproval) { + this.alwaysIgnoreMRApproval = alwaysIgnoreMRApproval; + } + + /** + * Setter for stapler to set the alwaysIgnoreMRUnApproval of the WebhookListener + */ + @DataBoundSetter + public void setalwaysIgnoreMRUnApproval(boolean alwaysIgnoreMRUnApproval) { + this.alwaysIgnoreMRUnApproval = alwaysIgnoreMRUnApproval; + } + + /** + * Setter for stapler to set the alwaysIgnoreMRApproved of the WebhookListener */ @DataBoundSetter - public void setAlwaysIgnoreMRApprove(boolean alwaysIgnoreMRApprove) { - this.alwaysIgnoreMRApprove = alwaysIgnoreMRApprove; + public void setalwaysIgnoreMRApproved(boolean alwaysIgnoreMRApproved) { + this.alwaysIgnoreMRApproved = alwaysIgnoreMRApproved; } /** - * Setter for stapler to set the alwaysIgnoreMRUnApprove of the WebhookListener + * Setter for stapler to set the alwaysIgnoreMRUnApproved of the + * WebhookListener */ @DataBoundSetter - public void setAlwaysIgnoreMRUnApprove(boolean alwaysIgnoreMRUnApprove) { - this.alwaysIgnoreMRUnApprove = alwaysIgnoreMRUnApprove; + public void setalwaysIgnoreMRUnApproved(boolean alwaysIgnoreMRUnApproved) { + this.alwaysIgnoreMRUnApproved = alwaysIgnoreMRUnApproved; } /** - * Setter for stapler to set the alwaysIgnoreNonCodeRelatedUpdates of the WebhookListener + * Setter for stapler to set the alwaysIgnoreNonCodeRelatedUpdates of the + * WebhookListener */ @DataBoundSetter public void setAlwaysIgnoreNonCodeRelatedUpdates(boolean alwaysIgnoreNonCodeRelatedUpdates) { diff --git a/src/main/resources/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait/config.jelly b/src/main/resources/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait/config.jelly index 876e4dbc..81646fda 100644 --- a/src/main/resources/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait/config.jelly +++ b/src/main/resources/io/jenkins/plugins/gitlabbranchsource/WebhookListenerBuildConditionsTrait/config.jelly @@ -6,10 +6,16 @@ - + - + + + + + + +