Skip to content

Commit

Permalink
Merge pull request #243 from westarne/feature/buildState-buildName
Browse files Browse the repository at this point in the history
Make build state and build name overwritable
  • Loading branch information
scaytrase committed Aug 28, 2019
2 parents c5c1d2e + 57411fe commit 873e674
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 16 deletions.
21 changes: 18 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ node {
}
```

Or you could as well use

```groovy
checkout scm
notifyBitbucket(buildStatus: 'INPROGRESS') // Notifies the Bitbucket instance of an INPROGRESS build
try {
// Do stuff
notifyBitbucket(buildStatus: 'SUCCESSFUL') // Notifies the Bitbucket instance of an SUCCESSFUL build
} catch(err) {
// Do clean up
notifyBitbucket(buildStatus: 'FAILED') // Notifies the Bitbucket instance of an FAILED build
}
```

In situations where an advanced setup is required the following can be used:

```groovy
Expand All @@ -98,15 +114,14 @@ node {
}
def notifyBitbucket(String state) {
if ('SUCCESS' == state || 'FAILED' == state) {
currentBuild.result = state // Set result of currentBuild !Important!
}
notifyBitbucket(
commitSha1: 'commit',
credentialsId: '00000000-1111-2222-3333-123456789abc',
disableInprogressNotification: false,
considerUnstableAsSuccess: true,
ignoreUnverifiedSSLPeer: true,
buildStatus: state,
buildName: 'Performance Testing',
includeBuildNumberInKey: false,
prependParentProjectKey: false,
projectKey: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.*;
import com.cloudbees.plugins.credentials.common.CertificateCredentials;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.*;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.ProxyConfiguration;
import hudson.model.*;
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
Expand All @@ -43,9 +48,10 @@
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.*;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
Expand All @@ -54,16 +60,12 @@
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
Expand Down Expand Up @@ -121,6 +123,18 @@ public class StashNotifier extends Notifier implements SimpleBuildStep {
*/
private final String commitSha1;

/**
* specify a specific build state to be pushed.
* If null, the current build result will be used.
*/
private final StashBuildState buildStatus;

/**
* specify a build name to be included in the Bitbucket notification.
* If null, the usual full project name will be used.
*/
private final String buildName;

/**
* if true, the build number is included in the Bitbucket notification.
*/
Expand Down Expand Up @@ -160,6 +174,8 @@ public BuildStepMonitor getRequiredMonitorService() {
String credentialsId,
boolean ignoreUnverifiedSSLPeer,
String commitSha1,
String buildStatus,
String buildName,
boolean includeBuildNumberInKey,
String projectKey,
boolean prependParentProjectKey,
Expand All @@ -174,6 +190,16 @@ public BuildStepMonitor getRequiredMonitorService() {
this.credentialsId = credentialsId;
this.ignoreUnverifiedSSLPeer = ignoreUnverifiedSSLPeer;
this.commitSha1 = commitSha1;

StashBuildState overwrittenBuildState = null;
try {
overwrittenBuildState = StashBuildState.valueOf(buildStatus);
} catch (Exception e) {
// ignore unknown or null values
}
this.buildStatus = overwrittenBuildState;

this.buildName = buildName;
this.includeBuildNumberInKey = includeBuildNumberInKey;
this.projectKey = projectKey;
this.prependParentProjectKey = prependParentProjectKey;
Expand All @@ -188,6 +214,8 @@ public StashNotifier(
String credentialsId,
boolean ignoreUnverifiedSSLPeer,
String commitSha1,
String buildStatus,
String buildName,
boolean includeBuildNumberInKey,
String projectKey,
boolean prependParentProjectKey,
Expand All @@ -199,6 +227,8 @@ public StashNotifier(
credentialsId,
ignoreUnverifiedSSLPeer,
commitSha1,
buildStatus,
buildName,
includeBuildNumberInKey,
projectKey,
prependParentProjectKey,
Expand Down Expand Up @@ -232,6 +262,14 @@ public String getCommitSha1() {
return commitSha1;
}

public StashBuildState getBuildStatus() {
return buildStatus;
}

public String getBuildName() {
return buildName;
}

public boolean getIncludeBuildNumberInKey() {
return includeBuildNumberInKey;
}
Expand Down Expand Up @@ -736,7 +774,8 @@ protected NotificationResult notifyStash(
final String commitSha1,
final TaskListener listener,
final StashBuildState state) throws Exception {
HttpEntity stashBuildNotificationEntity = newStashBuildNotificationEntity(run, state, listener);
StashBuildState buildStatus = getPushedBuildStatus(state);
HttpEntity stashBuildNotificationEntity = newStashBuildNotificationEntity(run, buildStatus, listener);

String stashURL = expandStashURL(run, listener);

Expand Down Expand Up @@ -821,6 +860,21 @@ protected <C extends Credentials> List<C> lookupCredentials(Class<C> type, ItemG
return CredentialsProvider.lookupCredentials(type, itemGroup, authentication, domainRequirements);
}

/**
* Returns the build state to be pushed. This will select the specifically overwritten build state
* or the current build state else.
*
* @param currentBuildStatus the state of the current build
* @return the selected build state
*/
protected StashBuildState getPushedBuildStatus(StashBuildState currentBuildStatus) {
if (buildStatus != null) {
return buildStatus;
} else {
return currentBuildStatus;
}
}

/**
* Returns the HTTP POST request ready to be sent to the Bitbucket build API for
* the given run and change set.
Expand Down Expand Up @@ -901,7 +955,7 @@ private HttpEntity newStashBuildNotificationEntity(

json.put("key", abbreviate(getBuildKey(run, listener), MAX_FIELD_LENGTH));

json.put("name", abbreviate(run.getFullDisplayName(), 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));
Expand Down Expand Up @@ -938,6 +992,10 @@ private String getDefaultBuildKey(final Run<?, ?> run) {
}
key.append('-').append(getRootUrl());

if (buildName != null && buildName.trim().length() > 0) {
key.append('-').append(buildName);
}

return key.toString();
}

Expand Down Expand Up @@ -979,6 +1037,21 @@ protected String getBuildKey(final Run<?, ?> run,
return StringEscapeUtils.escapeJavaScript(key.toString());
}

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

/**
* Returns the description of the run used for the Bitbucket notification.
* Uses the run description provided by the Jenkins job, if available.
Expand Down

0 comments on commit 873e674

Please sign in to comment.