Skip to content

Commit

Permalink
added buildUrl property for overriding the url used for the build / run
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc McIntyre authored and Marc McIntyre committed Apr 1, 2022
1 parent 8cd3ba4 commit d3bff70
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 7 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def notifyBitbucket(String state) {
ignoreUnverifiedSSLPeer: true,
buildStatus: state,
buildName: 'Performance Testing',
buildUrl: 'https://my.company.intranet/bitbucket/custom-build-url',
includeBuildNumberInKey: false,
prependParentProjectKey: false,
projectKey: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public class StashNotifier extends Notifier implements SimpleBuildStep {
*/
private String buildName;

/**
* specify a build url to be included in the Bitbucket notification.
* If null, the usual url of the build will be used.
*/
private String buildUrl;

/**
* if true, the build number is included in the Bitbucket notification.
*/
Expand Down Expand Up @@ -182,6 +188,7 @@ public BuildStepMonitor getRequiredMonitorService() {
String commitSha1,
String buildStatus,
String buildName,
String buildUrl,
boolean includeBuildNumberInKey,
String projectKey,
boolean prependParentProjectKey,
Expand All @@ -196,6 +203,7 @@ public BuildStepMonitor getRequiredMonitorService() {
setCommitSha1(commitSha1);
setBuildStatus(buildStatus);
setBuildName(buildName);
setBuildUrl(buildUrl);
setIncludeBuildNumberInKey(includeBuildNumberInKey);
setProjectKey(projectKey);
setPrependParentProjectKey(prependParentProjectKey);
Expand Down Expand Up @@ -272,6 +280,15 @@ public void setBuildName(String buildName) {
this.buildName = buildName;
}

public String getBuildUrl() {
return buildUrl;
}

@DataBoundSetter
public void setBuildUrl(String buildUrl) {
this.buildUrl = buildUrl;
}

public boolean isIncludeBuildNumberInKey() {
return includeBuildNumberInKey;
}
Expand Down Expand Up @@ -888,7 +905,7 @@ private <T extends Credentials> T getCredentials(final Class<T> clazz, final Ite
* or the current build state else.
*
* @param currentBuildStatus the state of the current build
* @return the selected build state
* @return the current build status
*/
protected StashBuildState getPushedBuildStatus(StashBuildState currentBuildStatus) {
if (buildStatus != null) {
Expand Down Expand Up @@ -998,7 +1015,7 @@ private JSONObject createNotificationPayload(
json.put("key", abbreviate(getBuildKey(run, listener), MAX_FIELD_LENGTH));
json.put("name", abbreviate(getBuildName(run), MAX_FIELD_LENGTH));
json.put("description", abbreviate(getBuildDescription(run, state), MAX_FIELD_LENGTH));
json.put("url", abbreviate(DisplayURLProvider.get().getRunURL(run), MAX_URL_FIELD_LENGTH));
json.put("url", abbreviate(getBuildUrl(run), MAX_URL_FIELD_LENGTH));
return json;
}

Expand Down Expand Up @@ -1081,7 +1098,7 @@ protected String getBuildKey(final Run<?, ?> run,
* or get the build name from the {@link Run}.
*
* @param run the run to notify Bitbucket of
* @return the selected build state
* @return the name of the run
*/
protected String getBuildName(final Run<?, ?> run) {
if (buildName != null && buildName.trim().length() > 0) {
Expand All @@ -1091,6 +1108,21 @@ protected String getBuildName(final Run<?, ?> run) {
}
}

/**
* Returns the build url to be pushed. This will select the specifically overwritten build url
* or get the build url from the {@link DisplayURLProvider}.
*
* @param run the run to notify Bitbucket of
* @return the url of the run
*/
protected String getBuildUrl(final Run<?, ?> run) {
if (buildUrl != null && buildUrl.trim().length() > 0) {
return buildUrl;
} else {
return DisplayURLProvider.get().getRunURL(run);
}
}

/**
* Returns the description of the run used for the Bitbucket notification.
* Uses the run description provided by the Jenkins job, if available.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ public class StashNotifierTest {
private static Jenkins jenkins;
private static final HttpNotifierSelector httpNotifierSelector = mock(HttpNotifierSelector.class);
private static final HttpNotifier httpNotifier = mock(HttpNotifier.class);
private static final DisplayURLProvider displayURLProvider = mock(DisplayURLProvider.class);

private static MockedStatic<Jenkins> mockedJenkins;
private static MockedStatic<com.cloudbees.plugins.credentials.CredentialsProvider> mockedCredentialsProvider;
private static MockedStatic<Secret> mockedSecret;
private static MockedStatic<HttpClientBuilder> mockedHttpClientBuilder;
private static MockedStatic<DisplayURLProvider> mockedDisplayURLProvider;

private static StashNotifier buildStashNotifier(String stashBaseUrl) {
return buildStashNotifier(stashBaseUrl, false, false);
Expand All @@ -92,6 +94,7 @@ private static StashNotifier buildStashNotifier(String stashBaseUrl,
null,
null,
null,
null,
true,
"test-project",
true,
Expand All @@ -116,6 +119,7 @@ public static void setUp() throws Exception {
mockedHttpClientBuilder = mockStatic(HttpClientBuilder.class);
mockedCredentialsProvider = mockStatic(
com.cloudbees.plugins.credentials.CredentialsProvider.class);
mockedDisplayURLProvider = mockStatic(DisplayURLProvider.class);

buildListener = mock(BuildListener.class);
jenkins = mock(Jenkins.class);
Expand All @@ -141,6 +145,8 @@ public static void setUp() throws Exception {
List<BuildData> actions = Collections.singletonList(action);

when(Jenkins.get()).thenReturn(jenkins);
when(displayURLProvider.getRunURL(run)).thenReturn("http://localhost");
mockedDisplayURLProvider.when(DisplayURLProvider::get).thenReturn(displayURLProvider);
when(jenkins.getRootUrl()).thenReturn("http://localhost/");
when(build.getEnvironment(buildListener)).thenReturn(environment);
when(action.getLastBuiltRevision()).thenReturn(revision);
Expand Down Expand Up @@ -245,6 +251,7 @@ public void test_build_http_client_https() throws Exception {
null,
null,
null,
null,
true,
null,
false,
Expand Down Expand Up @@ -497,6 +504,7 @@ public void lookupCommitSha1s() {
sha1,
null,
null,
null,
true,
null,
false,
Expand Down Expand Up @@ -526,6 +534,7 @@ private void lookupCommitSha1s_Exception(Exception e) {
sha1,
null,
null,
null,
true,
null,
false,
Expand Down Expand Up @@ -593,6 +602,7 @@ public void test_getPushedBuildState_overwritten() {
null,
state.name(),
null,
null,
true,
null,
true,
Expand All @@ -617,6 +627,7 @@ public void test_getPushedBuildState_not_overwritten() {
null,
null,
null,
null,
true,
null,
true,
Expand Down Expand Up @@ -644,6 +655,7 @@ public void test_getBuildName_overwritten() {
null,
null,
name,
null,
true,
null,
true,
Expand All @@ -670,6 +682,7 @@ public void test_getBuildName_not_overwritten() {
null,
null,
null,
null,
true,
null,
true,
Expand All @@ -684,6 +697,61 @@ public void test_getBuildName_not_overwritten() {
assertThat(buildName, is("default-name"));
}

@Test
public void test_getBuildUrl_overwritten() {
//given
when(displayURLProvider.getRunURL(run)).thenReturn("http://default-url");
String url = "http://custom-url";

sn = new StashNotifier(
"",
"scot",
true,
null,
null,
null,
url,
true,
null,
true,
false,
false,
mock(JenkinsLocationConfiguration.class));

//when
String buildUrl = sn.getBuildUrl(run);

//then
assertThat(buildUrl, is(url));
}

@Test
public void test_getBuildUrl_not_overwritten() {
//given
when(displayURLProvider.getRunURL(run)).thenReturn("http://default-url");

sn = new StashNotifier(
"",
"scot",
true,
null,
null,
null,
null,
true,
null,
true,
false,
false,
mock(JenkinsLocationConfiguration.class));

//when
String buildUrl = sn.getBuildUrl(run);

//then
assertThat(buildUrl, is("http://default-url"));
}

@Test
public void test_getBuildKey() {
//given
Expand All @@ -700,6 +768,7 @@ public void test_getBuildKey() {
null,
null,
"build-name",
null,
true,
key,
true,
Expand Down Expand Up @@ -732,6 +801,7 @@ public void test_getBuildKey_withBuildName() {
null,
null,
buildName,
null,
true,
null,
true,
Expand Down Expand Up @@ -765,6 +835,7 @@ public void test_getRunKey() throws Exception {
null,
null,
null,
null,
true,
key,
true,
Expand Down Expand Up @@ -795,6 +866,7 @@ private void getBuildKey_Exception(Exception e) {
null,
null,
null,
null,
true,
key,
true,
Expand Down Expand Up @@ -829,6 +901,7 @@ private void getRunKey_Exception(Exception e) throws IOException {
null,
null,
null,
null,
true,
key,
true,
Expand Down Expand Up @@ -879,8 +952,6 @@ public void test_getRunKey_MacroEvaluationException() throws Exception {
private NotificationResult notifyStash(int statusCode) throws Exception {
sn = spy(this.sn);
PrintStream logger = mock(PrintStream.class);
DisplayURLProvider displayURLProvider = mock(DisplayURLProvider.class);
when(displayURLProvider.getRunURL(run)).thenReturn("http://localhost");
when(buildListener.getLogger()).thenReturn(logger);
doReturn("someKey1").when(sn).getBuildKey(eq(build), eq(buildListener));
HttpPost httpPost = mock(HttpPost.class);
Expand All @@ -890,8 +961,7 @@ private NotificationResult notifyStash(int statusCode) throws Exception {
when(resp.getStatusLine()).thenReturn(sl);
when(resp.getEntity()).thenReturn(new StringEntity(""));
when(client.execute(eq(httpPost))).thenReturn(resp);
try (MockedStatic<TokenMacro> tokenMacroMock = mockStatic(TokenMacro.class); MockedStatic<DisplayURLProvider> displayURLProviderMock = mockStatic(DisplayURLProvider.class)) {
displayURLProviderMock.when(DisplayURLProvider::get).thenReturn(displayURLProvider);
try (MockedStatic<TokenMacro> tokenMacroMock = mockStatic(TokenMacro.class); ) {
tokenMacroMock.when(() -> TokenMacro.expandAll(any(), any(), any())).thenReturn("http://localhost");
doReturn(client).when(sn).getHttpClient(any(PrintStream.class), any(AbstractBuild.class), anyString());
return sn.notifyStash(logger, build, sha1, buildListener, StashBuildState.FAILED);
Expand Down

0 comments on commit d3bff70

Please sign in to comment.