Skip to content

Commit

Permalink
Fix wrong action IDs in GitLabMergeRequestTrigger.java and add suppor…
Browse files Browse the repository at this point in the history
…t to "approved" and "unapproved" as well (#252)
  • Loading branch information
mifitous authored Nov 29, 2022
1 parent 9ed86bb commit dc12a4a
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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")) {
Expand All @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import jenkins.scm.api.trait.SCMSourceContext;

public class GitLabSCMSourceContext
extends SCMSourceContext<GitLabSCMSourceContext, GitLabSCMSourceRequest> {
extends SCMSourceContext<GitLabSCMSourceContext, GitLabSCMSourceRequest> {

private boolean wantBranches;
private boolean wantTags;
Expand All @@ -22,10 +22,10 @@ public class GitLabSCMSourceContext

@NonNull
private Set<ChangeRequestCheckoutStrategy> originMRStrategies = EnumSet
.noneOf(ChangeRequestCheckoutStrategy.class);
.noneOf(ChangeRequestCheckoutStrategy.class);
@NonNull
private Set<ChangeRequestCheckoutStrategy> forkMRStrategies = EnumSet
.noneOf(ChangeRequestCheckoutStrategy.class);
.noneOf(ChangeRequestCheckoutStrategy.class);
@NonNull
private GitLabHookRegistration webhookRegistration = GitLabHookRegistration.SYSTEM;
@NonNull
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -184,14 +198,14 @@ public GitLabSCMSourceContext wantForkMRs(boolean include) {

@NonNull
public GitLabSCMSourceContext withOriginMRStrategies(
Set<ChangeRequestCheckoutStrategy> strategies) {
Set<ChangeRequestCheckoutStrategy> strategies) {
originMRStrategies.addAll(strategies);
return this;
}

@NonNull
public GitLabSCMSourceContext withForkMRStrategies(
Set<ChangeRequestCheckoutStrategy> strategies) {
Set<ChangeRequestCheckoutStrategy> strategies) {
forkMRStrategies.addAll(strategies);
return this;
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -104,17 +116,35 @@ public boolean getAlwaysBuildMRReOpen() {
*
* @return false to run build on MR approval
*/
public boolean getAlwaysIgnoreMRApprove() {
return alwaysIgnoreMRApprove;
public boolean getalwaysIgnoreMRApproval() {
return alwaysIgnoreMRApproval;
}

/**
* Run build on MR un-approval
*
* @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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
<f:entry title="${%Always build pipeline on Re-Open MR webhook}" field="alwaysBuildMRReOpen">
<f:checkbox default="checked"/>
</f:entry>
<f:entry title="${%Ignore build pipeline on MR Approval}" field="alwaysIgnoreMRApprove">
<f:entry title="${%Ignore build pipeline on MR Approval}" field="alwaysIgnoreMRApproval">
<f:checkbox default="checked"/>
</f:entry>
<f:entry title="${%Ignore build pipeline on MR Un-Approval}" field="alwaysIgnoreMRUnApprove">
<f:entry title="${%Ignore build pipeline on MR Un-Approval}" field="alwaysIgnoreMRUnApproval">
<f:checkbox default="checked"/>
</f:entry>
<f:entry title="${%Ignore build pipeline on MR Approved}" field="alwaysIgnoreMRApproved">
<f:checkbox default="checked"/>
</f:entry>
<f:entry title="${%Ignore build pipeline on MR Un-Approved}" field="alwaysIgnoreMRUnApproved">
<f:checkbox default="checked"/>
</f:entry>
<f:entry title="${%Ignore builds for non-code related MR updates}" field="alwaysIgnoreNonCodeRelatedUpdates">
Expand Down

0 comments on commit dc12a4a

Please sign in to comment.