Skip to content

Commit

Permalink
[push] new filter option: triggerOnlyIfTagPush (cloud + server) (#128)
Browse files Browse the repository at this point in the history
- was necessary due to different payload for cloud on tag push
- add dsl snippets
- for server only: support matching on refId, e.g. "refs/tags/*"
- update docs
  • Loading branch information
Patrick authored Oct 14, 2020
1 parent 0f62752 commit 8e96949
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ bitbucketTriggers {
// For Bitbucket Cloud
repositoryPushAction(triggerAlsoIfTagPush: boolean, triggerAlsoIfNothingChanged: boolean, allowedBranches: String)
repositoryPushAction(triggerAlsoIfTagPush: boolean, triggerAlsoIfNothingChanged: boolean, allowedBranches: String, isToApprove: boolean)
repositoryPushAction(triggerAlsoIfTagPush: boolean, triggerAlsoIfNothingChanged: boolean, allowedBranches: String, isToApprove: boolean, triggerOnlyIfTagPush: boolean)
pullRequestApprovedAction(onlyIfReviewersApproved: boolean)
pullRequestApprovedAction(onlyIfReviewersApproved: boolean, allowedBranches: String)
Expand Down Expand Up @@ -178,6 +179,7 @@ bitbucketTriggers {
// note: flag `isToApprove` has no effect yet
repositoryServerPushAction(triggerAlsoIfTagPush: boolean, triggerAlsoIfNothingChanged: boolean, allowedBranches: String)
repositoryServerPushAction(triggerAlsoIfTagPush: boolean, triggerAlsoIfNothingChanged: boolean, allowedBranches: String, isToApprove: boolean)
repositoryServerPushAction(triggerAlsoIfTagPush: boolean, triggerAlsoIfNothingChanged: boolean, allowedBranches: String, isToApprove: boolean, triggerOnlyIfTagPush: boolean)
pullRequestServerApprovedAction(onlyIfReviewersApproved: boolean)
pullRequestServerApprovedAction(onlyIfReviewersApproved: boolean, allowedBranches: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public default String getTargetBranch() {
return null;
}

public default String getTargetBranchRefId() {
return null;
}

public default String getType() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class BitBucketPPRServerRepositoryAction extends InvisibleAction implemen

private final @Nonnull BitBucketPPRPayload payload;
private List<String> scmUrls = new ArrayList<>(2);
private String targetBranch = null;
private String targetBranchName = null;
private String targetBranchRefId = null;
private String type;

public BitBucketPPRServerRepositoryAction(BitBucketPPRPayload payload) {
Expand All @@ -59,20 +60,26 @@ public BitBucketPPRServerRepositoryAction(BitBucketPPRPayload payload) {

for (BitBucketPPRServerChange change : payload.getServerChanges()) {
if (change.getRefId() != null) {
this.targetBranch = change.getRefId();
this.type = change.getType();
this.targetBranchName = change.getRef().getDisplayId();
this.targetBranchRefId = change.getRefId();
this.type = change.getRef().getType();
break;
}
}

LOGGER.log(Level.INFO,
() -> "Received commit hook notification from server for destination branch: " + this.targetBranch);
() -> "Received commit hook notification from server for destination branch: " + this.targetBranchName);
LOGGER.log(Level.INFO, () -> "Received commit hook type from server: " + this.type);
}

@Override
public String getTargetBranch() {
return targetBranch;
return targetBranchName;
}

@Override
public String getTargetBranchRefId() {
return targetBranchRefId;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public void repositoryPushAction(boolean triggerAlsoIfTagPush,
triggers.add(repositoryTriggerFilter);
}

public void repositoryPushAction(boolean triggerAlsoIfTagPush,
boolean triggerAlsoIfNothingChanged, String allowedBranches, boolean isToApprove,
boolean triggerOnlyIfTagPush) {
BitBucketPPRRepositoryPushActionFilter repositoryPushActionFilter =
new BitBucketPPRRepositoryPushActionFilter(triggerAlsoIfTagPush,
triggerAlsoIfNothingChanged, allowedBranches);
repositoryPushActionFilter.setIsToApprove(isToApprove);
repositoryPushActionFilter.setTriggerOnlyIfTagPush(triggerOnlyIfTagPush);
BitBucketPPRRepositoryTriggerFilter repositoryTriggerFilter =
new BitBucketPPRRepositoryTriggerFilter(repositoryPushActionFilter);
triggers.add(repositoryTriggerFilter);
}

public void pullRequestApprovedAction(boolean onlyIfReviewersApproved, String allowedBranches) {
BitBucketPPRPullRequestApprovedActionFilter pullRequestApprovedActionFilter =
new BitBucketPPRPullRequestApprovedActionFilter(onlyIfReviewersApproved);
Expand Down Expand Up @@ -270,6 +283,19 @@ public void repositoryServerPushAction(boolean triggerAlsoIfTagPush,
triggers.add(repositoryServerTriggerFilter);
}

public void repositoryServerPushAction(boolean triggerAlsoIfTagPush,
boolean triggerAlsoIfNothingChanged, String allowedBranches, boolean isToApprove,
boolean triggerOnlyIfTagPush) {
BitBucketPPRServerRepositoryPushActionFilter repositoryServerPushActionFilter =
new BitBucketPPRServerRepositoryPushActionFilter(triggerAlsoIfTagPush,
triggerAlsoIfNothingChanged, allowedBranches);
repositoryServerPushActionFilter.setIsToApprove(isToApprove);
repositoryServerPushActionFilter.setTriggerOnlyIfTagPush(triggerOnlyIfTagPush);
BitBucketPPRRepositoryTriggerFilter repositoryServerTriggerFilter =
new BitBucketPPRRepositoryTriggerFilter(repositoryServerPushActionFilter);
triggers.add(repositoryServerTriggerFilter);
}

public void pullRequestServerApprovedAction(boolean onlyIfReviewersApproved) {
BitBucketPPRPullRequestServerApprovedActionFilter pullRequestServerApprovedActionFilter =
new BitBucketPPRPullRequestServerApprovedActionFilter(onlyIfReviewersApproved);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*******************************************************************************
* The MIT License
*
*
* Copyright (C) 2019, CloudBees, Inc.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Expand Down Expand Up @@ -39,6 +39,7 @@ public class BitBucketPPRRepositoryPushActionFilter extends BitBucketPPRReposito

public boolean triggerAlsoIfTagPush;
public boolean triggerAlsoIfNothingChanged;
public boolean triggerOnlyIfTagPush;
public String allowedBranches;
public boolean isToApprove;

Expand All @@ -50,6 +51,11 @@ public BitBucketPPRRepositoryPushActionFilter(boolean triggerAlsoIfTagPush,
this.allowedBranches = allowedBranches;
}

@DataBoundSetter
public void setTriggerOnlyIfTagPush(boolean triggerOnlyIfTagPush) {
this.triggerOnlyIfTagPush = triggerOnlyIfTagPush;
}

@DataBoundSetter
public void setIsToApprove(boolean isToApprove) {
this.isToApprove = isToApprove;
Expand All @@ -62,14 +68,20 @@ public boolean shouldTriggerBuild(BitBucketPPRAction bitbucketAction) {

if (!bitbucketAction.getType().equalsIgnoreCase("BRANCH")
&& !bitbucketAction.getType().equalsIgnoreCase("named_branch")
&& !bitbucketAction.getType().equalsIgnoreCase("UPDATE") && !this.triggerAlsoIfTagPush) {
&& !bitbucketAction.getType().equalsIgnoreCase("UPDATE")
&& !bitbucketAction.getType().equalsIgnoreCase("TAG")
&& !this.triggerAlsoIfTagPush) {
logger.info(
"Neither bitbucketAction type is BRANCH, nor UPDATE, nor trigger on tag push is set: "
+ bitbucketAction.getType());

return false;
}

if (this.triggerOnlyIfTagPush && !bitbucketAction.getType().equalsIgnoreCase("TAG")) {
return false;
}

return matches(allowedBranches, bitbucketAction.getTargetBranch(), null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*******************************************************************************
* The MIT License
*
*
* Copyright (C) 2020, CloudBees, Inc.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Expand Down Expand Up @@ -40,6 +40,7 @@ public class BitBucketPPRServerRepositoryPushActionFilter
Logger.getLogger(BitBucketPPRServerRepositoryPushActionFilter.class.getName());

public boolean triggerAlsoIfTagPush;
public boolean triggerOnlyIfTagPush;
public boolean triggerAlsoIfNothingChanged;
public String allowedBranches;
public boolean isToApprove;
Expand All @@ -52,6 +53,11 @@ public BitBucketPPRServerRepositoryPushActionFilter(boolean triggerAlsoIfTagPush
this.allowedBranches = allowedBranches;
}

@DataBoundSetter
public void setTriggerOnlyIfTagPush(boolean triggerOnlyIfTagPush) {
this.triggerOnlyIfTagPush = triggerOnlyIfTagPush;
}

@DataBoundSetter
public void setIsToApprove(boolean isToApprove) {
this.isToApprove = isToApprove;
Expand All @@ -64,17 +70,30 @@ public boolean shouldTriggerBuild(BitBucketPPRAction bitbucketAction) {

if (!bitbucketAction.getType().equalsIgnoreCase("BRANCH")
&& !bitbucketAction.getType().equalsIgnoreCase("named_branch")
&& !bitbucketAction.getType().equalsIgnoreCase("UPDATE") && !this.triggerAlsoIfTagPush) {
&& !bitbucketAction.getType().equalsIgnoreCase("UPDATE")
&& !bitbucketAction.getType().equalsIgnoreCase("TAG")
&& !this.triggerAlsoIfTagPush) {
LOGGER.info(
() -> "Neither bitbucketActionType is BRANCH, nor UPDATE, nor trigger on tag push is set for bitbucket type: "
+ bitbucketAction.getType() + ".");

return false;
}

LOGGER.log(Level.FINEST, "the target branch is: {0}.", bitbucketAction.getTargetBranch());
if (this.triggerOnlyIfTagPush && !bitbucketAction.getType().equalsIgnoreCase("TAG")) {
return false;
}

String target = null;
if (bitbucketAction.getType().equalsIgnoreCase("TAG")) {
target = bitbucketAction.getTargetBranchRefId();
} else {
target = bitbucketAction.getTargetBranch();
}

LOGGER.log(Level.FINEST, "the target branch is: {0}.", target);
LOGGER.log(Level.FINEST, "The allowed branches are: {0}.", allowedBranches);
return matches(allowedBranches, bitbucketAction.getTargetBranch(), null);
return matches(allowedBranches, target, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
checked="${instance.triggerAlsoIfTagPush}">
</f:checkbox>
</f:entry>
<f:entry>
<f:checkbox name="triggerOnlyIfTagPush"
title="Trigger only if it is a tag push"
checked="${instance.triggerOnlyIfTagPush}">
</f:checkbox>
</f:entry>
<f:entry>
<f:checkbox name="triggerAlsoIfNothingChanged"
title="Trigger also if nothing has changed in the repo"
Expand All @@ -21,4 +27,4 @@
<f:entry title="Allowed Branches" field="allowed">
<f:textbox field="allowedBranches" />
</f:entry>
</j:jelly>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
title="Trigger also if it is a tag push"
checked="${instance.triggerAlsoIfTagPush}">
</f:checkbox>
</f:entry>
<f:entry>
<f:checkbox name="triggerOnlyIfTagPush"
title="Trigger only if it is a tag push"
checked="${instance.triggerOnlyIfTagPush}">
</f:checkbox>
</f:entry>
<f:entry>
<f:checkbox name="triggerAlsoIfNothingChanged"
Expand All @@ -21,4 +27,4 @@
<f:entry title="Allowed Branches" field="allowed">
<f:textbox field="allowedBranches" />
</f:entry>
</j:jelly>
</j:jelly>

0 comments on commit 8e96949

Please sign in to comment.