From 5e4d048416c9c9461ba4a224963003535e6554aa Mon Sep 17 00:00:00 2001 From: Dee Date: Fri, 28 May 2021 11:41:39 -0700 Subject: [PATCH 1/2] JENKINS-65741: Implement a new option that will allow to include jenkins url to the bitbucket commit status --- .../BitbucketBuildStatusNotifications.java | 43 +++++++++++++------ ...itbucketBuildStatusNotificationsTrait.java | 21 ++++++++- .../bitbucket/BitbucketSCMSourceContext.java | 26 +++++++++++ .../config.jelly | 3 ++ 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotifications.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotifications.java index 1656a953b..ca5463b80 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotifications.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotifications.java @@ -63,11 +63,15 @@ public class BitbucketBuildStatusNotifications { private static final String STOPPED_STATE = "STOPPED"; private static final String INPROGRESS_STATE = "INPROGRESS"; - private static String getRootURL(@NonNull Run build) { + private static String getRootURL() { + return DisplayURLProvider.get().getRoot(); + } + + private static String getRunURL(@NonNull Run build) { JenkinsLocationConfiguration cfg = JenkinsLocationConfiguration.get(); if (cfg == null || cfg.getUrl() == null) { - throw new IllegalStateException("Could not determine Jenkins URL."); + throw new IllegalStateException("Could not determine job URL."); } return DisplayURLProvider.get().getRunURL(build); @@ -110,9 +114,11 @@ private static void createStatus(@NonNull Run build, @NonNull TaskListener return; } - String url; + String rootUrl, url; try { - url = getRootURL(build); + rootUrl = getRootURL(); + + url = getRunURL(build); checkURL(url, bitbucket); } catch (IllegalStateException e) { listener.getLogger().println("Can not determine Jenkins root URL " + @@ -123,10 +129,15 @@ private static void createStatus(@NonNull Run build, @NonNull TaskListener return; } + BitbucketSCMSource source = (BitbucketSCMSource) s; + BitbucketSCMSourceContext sourceContext = new BitbucketSCMSourceContext(null, SCMHeadObserver.none()) + .withTraits(source.getTraits()); + String name = build.getFullDisplayName(); // use the build number as the display name of the status BitbucketBuildStatus status; Result result = build.getResult(); String buildDescription = build.getDescription(); + String statusDescriptionSuffix = ""; String statusDescription; String state; if (Result.SUCCESS.equals(result)) { @@ -135,9 +146,6 @@ private static void createStatus(@NonNull Run build, @NonNull TaskListener } else if (Result.UNSTABLE.equals(result)) { statusDescription = StringUtils.defaultIfBlank(buildDescription, "This commit has test failures."); - BitbucketSCMSource source = (BitbucketSCMSource) s; - BitbucketSCMSourceContext sourceContext = new BitbucketSCMSourceContext(null, SCMHeadObserver.none()) - .withTraits(source.getTraits()); if (sourceContext.sendSuccessNotificationForUnstableBuild()) { state = SUCCESSFUL_STATE; } else { @@ -157,7 +165,10 @@ private static void createStatus(@NonNull Run build, @NonNull TaskListener statusDescription = StringUtils.defaultIfBlank(buildDescription, "The build is in progress..."); state = INPROGRESS_STATE; } - status = new BitbucketBuildStatus(hash, statusDescription, state, url, key, name); + if (sourceContext.buildStatusIncludeJenkinsURL()) { + statusDescriptionSuffix = "\n" + rootUrl; + } + status = new BitbucketBuildStatus(hash, statusDescription + statusDescriptionSuffix, state, url, key, name); new BitbucketChangesetCommentNotifier(bitbucket).buildStatus(status); if (result != null) { listener.getLogger().println("[Bitbucket] Build result notified"); @@ -171,6 +182,8 @@ private static void createStatus(@NonNull Run build, @NonNull TaskListener private static void sendNotifications(BitbucketSCMSource source, Run build, TaskListener listener) throws IOException, InterruptedException { + String rootUrl = getRootURL(); + BitbucketSCMSourceContext sourceContext = new BitbucketSCMSourceContext(null, SCMHeadObserver.none()).withTraits(source.getTraits()); if (sourceContext.notificationsDisabled()) { @@ -193,11 +206,13 @@ private static void sendNotifications(BitbucketSCMSource source, Run build if (r instanceof PullRequestSCMRevision) { listener.getLogger().println("[Bitbucket] Notifying pull request build result"); PullRequestSCMHead head = (PullRequestSCMHead) r.getHead(); - key = getBuildKey(build, head.getOriginName(), shareBuildKeyBetweenBranchAndPR); + key = getBuildKey(build, rootUrl, head.getOriginName(), + shareBuildKeyBetweenBranchAndPR, sourceContext.buildStatusIncludeJenkinsURL()); bitbucket = source.buildBitbucketClient(head); } else { listener.getLogger().println("[Bitbucket] Notifying commit build result"); - key = getBuildKey(build, r.getHead().getName(), shareBuildKeyBetweenBranchAndPR); + key = getBuildKey(build, rootUrl, r.getHead().getName(), + shareBuildKeyBetweenBranchAndPR, sourceContext.buildStatusIncludeJenkinsURL()); bitbucket = source.buildBitbucketClient(); } createStatus(build, listener, bitbucket, key, hash); @@ -215,8 +230,8 @@ private static String getHash(@CheckForNull SCMRevision revision) { return null; } - private static String getBuildKey(@NonNull Run build, String branch, - boolean shareBuildKeyBetweenBranchAndPR) { + private static String getBuildKey(@NonNull Run build, String rootUrl, String branch, + boolean shareBuildKeyBetweenBranchAndPR, boolean includeJenkinsURL) { // When the ExcludeOriginPRBranchesSCMHeadFilter filter is active, we want the // build status key to be the same between the branch project and the PR project. @@ -231,6 +246,10 @@ private static String getBuildKey(@NonNull Run build, String branch, key = build.getParent().getFullName(); // use the job full name as the key for the status } + if (includeJenkinsURL) { + key = rootUrl + "/" + key; + } + return key; } diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotificationsTrait.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotificationsTrait.java index 3d3a122eb..8470de06b 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotificationsTrait.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotificationsTrait.java @@ -45,6 +45,11 @@ public class BitbucketBuildStatusNotificationsTrait extends SCMSourceTrait { */ private boolean sendSuccessNotificationForUnstableBuild; + /** + * Include Jenkins URL as a status key/name + */ + private boolean includeJenkinsURL; + /** * Constructor. * @@ -68,12 +73,26 @@ public boolean getSendSuccessNotificationForUnstableBuild() { return this.sendSuccessNotificationForUnstableBuild; } + @DataBoundSetter + public void setIncludeJenkinsURL(boolean isIncludeJenkinsURL) { + includeJenkinsURL = isIncludeJenkinsURL; + } + + /** + * @return {@code true} if commit status should include Jenkins URL + */ + public boolean getIncludeJenkinsURL() { + return this.includeJenkinsURL; + } + /** * {@inheritDoc} */ @Override protected void decorateContext(SCMSourceContext context) { - ((BitbucketSCMSourceContext) context).withSendSuccessNotificationForUnstableBuild(getSendSuccessNotificationForUnstableBuild()); + ((BitbucketSCMSourceContext) context) + .withSendSuccessNotificationForUnstableBuild(getSendSuccessNotificationForUnstableBuild()) + .withBuildStatusIncludeJenkinsURL(getIncludeJenkinsURL()); } /** diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceContext.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceContext.java index c0985d26a..d237fe1a3 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceContext.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceContext.java @@ -91,6 +91,11 @@ public class BitbucketSCMSourceContext extends SCMSourceContext + + + \ No newline at end of file From 6969876c7e06d0b4dd5ee5ab034f5bec3a6d6613 Mon Sep 17 00:00:00 2001 From: Dee Date: Mon, 21 Jun 2021 19:18:03 -0700 Subject: [PATCH 2/2] JENKINS-65741: Update for v7.13+ --- .../bitbucket/BitbucketBuildStatusNotifications.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotifications.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotifications.java index ca5463b80..34bc63b1c 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotifications.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotifications.java @@ -137,6 +137,7 @@ private static void createStatus(@NonNull Run build, @NonNull TaskListener BitbucketBuildStatus status; Result result = build.getResult(); String buildDescription = build.getDescription(); + String statusNamePrefix = ""; String statusDescriptionSuffix = ""; String statusDescription; String state; @@ -166,9 +167,13 @@ private static void createStatus(@NonNull Run build, @NonNull TaskListener state = INPROGRESS_STATE; } if (sourceContext.buildStatusIncludeJenkinsURL()) { - statusDescriptionSuffix = "\n" + rootUrl; + statusNamePrefix = rootUrl + " ยป "; + statusDescriptionSuffix = " @ " + rootUrl; } - status = new BitbucketBuildStatus(hash, statusDescription + statusDescriptionSuffix, state, url, key, name); + status = new BitbucketBuildStatus(hash, + statusDescription + statusDescriptionSuffix, + state, url, key, + statusNamePrefix + name); new BitbucketChangesetCommentNotifier(bitbucket).buildStatus(status); if (result != null) { listener.getLogger().println("[Bitbucket] Build result notified");