-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JENKINS-65741: Implement a new option that will allow to include jenkins url to the commit status #454
base: master
Are you sure you want to change the base?
JENKINS-65741: Implement a new option that will allow to include jenkins url to the commit status #454
Changes from 1 commit
5e4d048
49eae33
6969876
7355bef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Root URL does not need a |
||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There might be double |
||
} | ||
|
||
return key; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method was confusing - it was returning job url but saying it was returning root url, and since I needed a new method that would return a root url - I had to rename this.
This method also never actually uses
build
andcfg
so I am not sure what's the purpose of everything in there except for the last line.