From 8e96949e3e4e7ff7ee6f3bb750aa32c05fdeaa62 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 14 Oct 2020 18:31:46 +0200 Subject: [PATCH] [push] new filter option: triggerOnlyIfTagPush (cloud + server) (#128) - 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 --- README.md | 2 ++ .../action/BitBucketPPRAction.java | 4 +++ .../BitBucketPPRServerRepositoryAction.java | 17 +++++++--- .../dsl/BitBucketPPRHookJobDslContext.java | 26 +++++++++++++++ ...itBucketPPRRepositoryPushActionFilter.java | 22 ++++++++++--- ...etPPRServerRepositoryPushActionFilter.java | 33 +++++++++++++++---- .../config.jelly | 8 ++++- .../config.jelly | 8 ++++- 8 files changed, 101 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index ea406854..6d52c0bf 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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) diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRAction.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRAction.java index cb3ed7db..61dad8e2 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRAction.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRAction.java @@ -64,6 +64,10 @@ public default String getTargetBranch() { return null; } + public default String getTargetBranchRefId() { + return null; + } + public default String getType() { return null; } diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java index 789c298a..4ff3b0d3 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java @@ -39,7 +39,8 @@ public class BitBucketPPRServerRepositoryAction extends InvisibleAction implemen private final @Nonnull BitBucketPPRPayload payload; private List scmUrls = new ArrayList<>(2); - private String targetBranch = null; + private String targetBranchName = null; + private String targetBranchRefId = null; private String type; public BitBucketPPRServerRepositoryAction(BitBucketPPRPayload payload) { @@ -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 diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/extensions/dsl/BitBucketPPRHookJobDslContext.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/extensions/dsl/BitBucketPPRHookJobDslContext.java index e6fe2f1b..e16e8b1d 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/extensions/dsl/BitBucketPPRHookJobDslContext.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/extensions/dsl/BitBucketPPRHookJobDslContext.java @@ -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); @@ -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); diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRRepositoryPushActionFilter.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRRepositoryPushActionFilter.java index 7210c6e3..f3989f8c 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRRepositoryPushActionFilter.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRRepositoryPushActionFilter.java @@ -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, @@ -39,6 +39,7 @@ public class BitBucketPPRRepositoryPushActionFilter extends BitBucketPPRReposito public boolean triggerAlsoIfTagPush; public boolean triggerAlsoIfNothingChanged; + public boolean triggerOnlyIfTagPush; public String allowedBranches; public boolean isToApprove; @@ -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; @@ -62,7 +68,9 @@ 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()); @@ -70,6 +78,10 @@ public boolean shouldTriggerBuild(BitBucketPPRAction bitbucketAction) { return false; } + if (this.triggerOnlyIfTagPush && !bitbucketAction.getType().equalsIgnoreCase("TAG")) { + return false; + } + return matches(allowedBranches, bitbucketAction.getTargetBranch(), null); } diff --git a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter.java b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter.java index 8493664d..943f34df 100644 --- a/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter.java +++ b/src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter.java @@ -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, @@ -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; @@ -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; @@ -64,7 +70,9 @@ 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() + "."); @@ -72,9 +80,20 @@ public boolean shouldTriggerBuild(BitBucketPPRAction bitbucketAction) { 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 diff --git a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRRepositoryPushActionFilter/config.jelly b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRRepositoryPushActionFilter/config.jelly index 56fa4a7b..8b7f2316 100644 --- a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRRepositoryPushActionFilter/config.jelly +++ b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRRepositoryPushActionFilter/config.jelly @@ -6,6 +6,12 @@ checked="${instance.triggerAlsoIfTagPush}"> + + + + - \ No newline at end of file + diff --git a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter/config.jelly b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter/config.jelly index 0de00214..d4dfa15c 100644 --- a/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter/config.jelly +++ b/src/main/resources/io/jenkins/plugins/bitbucketpushandpullrequest/filter/repository/BitBucketPPRServerRepositoryPushActionFilter/config.jelly @@ -5,6 +5,12 @@ title="Trigger also if it is a tag push" checked="${instance.triggerAlsoIfTagPush}"> + + + + - \ No newline at end of file +