Skip to content

Commit

Permalink
Merge pull request #246 from westarne/feature/optional-parameters
Browse files Browse the repository at this point in the history
Implement setters instead of constructor for optional parameters
  • Loading branch information
scaytrase authored Jan 22, 2022
2 parents 640dbc5 + a6e1355 commit 5d1ba63
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 71 deletions.
172 changes: 101 additions & 71 deletions src/main/java/org/jenkinsci/plugins/stashNotifier/StashNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;

/**
* Notifies a configured Atlassian Bitbucket server instance of build results
Expand All @@ -106,61 +109,61 @@ public class StashNotifier extends Notifier implements SimpleBuildStep {
/**
* base url of Bitbucket server, e. g. <tt>http://localhost:7990</tt>.
*/
private final String stashServerBaseUrl;
private String stashServerBaseUrl;

/**
* The id of the credentials to use.
*/
private final String credentialsId;
private String credentialsId;

/**
* if true, ignore exception thrown in case of an unverified SSL peer.
*/
private final boolean ignoreUnverifiedSSLPeer;
private boolean ignoreUnverifiedSSLPeer;

/**
* specify the commit from config
*/
private final String commitSha1;
private String commitSha1;

/**
* specify a specific build state to be pushed.
* If null, the current build result will be used.
*/
private final StashBuildState buildStatus;
private 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;
private String buildName;

/**
* if true, the build number is included in the Bitbucket notification.
*/
private final boolean includeBuildNumberInKey;
private boolean includeBuildNumberInKey;

/**
* specify project key manually
*/
private final String projectKey;
private String projectKey;

/**
* append parent project key to key formation
*/
private final boolean prependParentProjectKey;
private boolean prependParentProjectKey;

/**
* whether to send INPROGRESS notification at the build start
*/
private final boolean disableInprogressNotification;
private boolean disableInprogressNotification;

/**
* whether to consider UNSTABLE builds as failures or success
*/
private final boolean considerUnstableAsSuccess;
private boolean considerUnstableAsSuccess;

private final JenkinsLocationConfiguration globalConfig;
private JenkinsLocationConfiguration globalConfig;

/**
* gives us the desired {@link HttpNotifier}. Transient because
Expand Down Expand Up @@ -189,105 +192,132 @@ public BuildStepMonitor getRequiredMonitorService() {
boolean considerUnstableAsSuccess,
JenkinsLocationConfiguration globalConfig
) {

this.stashServerBaseUrl = stashServerBaseUrl != null && stashServerBaseUrl.endsWith("/")
? stashServerBaseUrl.substring(0, stashServerBaseUrl.length() - 1)
: stashServerBaseUrl;
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;
this.disableInprogressNotification = disableInprogressNotification;
this.considerUnstableAsSuccess = considerUnstableAsSuccess;
this.globalConfig = globalConfig;
setStashServerBaseUrl(stashServerBaseUrl);
setCredentialsId(credentialsId);
setIgnoreUnverifiedSSLPeer(ignoreUnverifiedSSLPeer);
setCommitSha1(commitSha1);
setBuildStatus(buildStatus);
setBuildName(buildName);
setIncludeBuildNumberInKey(includeBuildNumberInKey);
setProjectKey(projectKey);
setPrependParentProjectKey(prependParentProjectKey);
setDisableInprogressNotification(disableInprogressNotification);
setConsiderUnstableAsSuccess(considerUnstableAsSuccess);
}

@DataBoundConstructor
public StashNotifier(
String stashServerBaseUrl,
String credentialsId,
boolean ignoreUnverifiedSSLPeer,
String commitSha1,
String buildStatus,
String buildName,
boolean includeBuildNumberInKey,
String projectKey,
boolean prependParentProjectKey,
boolean disableInprogressNotification,
boolean considerUnstableAsSuccess
) {
this(
stashServerBaseUrl,
credentialsId,
ignoreUnverifiedSSLPeer,
commitSha1,
buildStatus,
buildName,
includeBuildNumberInKey,
projectKey,
prependParentProjectKey,
disableInprogressNotification,
considerUnstableAsSuccess,
JenkinsLocationConfiguration.get()
);
public StashNotifier() {
this.globalConfig = JenkinsLocationConfiguration.get();
}

public boolean isDisableInprogressNotification() {
return disableInprogressNotification;
public String getStashServerBaseUrl() {
return stashServerBaseUrl;
}

public boolean isConsiderUnstableAsSuccess() {
return considerUnstableAsSuccess;
@DataBoundSetter
public void setStashServerBaseUrl(String stashServerBaseUrl) {
this.stashServerBaseUrl = StringUtils.stripEnd(stashServerBaseUrl, "/");
}

public String getCredentialsId() {
return credentialsId;
}

public String getStashServerBaseUrl() {
return stashServerBaseUrl;
@DataBoundSetter
public void setCredentialsId(String credentialsId) {
this.credentialsId = credentialsId;
}

public boolean getIgnoreUnverifiedSSLPeer() {
public boolean isIgnoreUnverifiedSSLPeer() {
return ignoreUnverifiedSSLPeer;
}

@DataBoundSetter
public void setIgnoreUnverifiedSSLPeer(boolean ignoreUnverifiedSSLPeer) {
this.ignoreUnverifiedSSLPeer = ignoreUnverifiedSSLPeer;
}

public String getCommitSha1() {
return commitSha1;
}

@DataBoundSetter
public void setCommitSha1(String commitSha1) {
this.commitSha1 = commitSha1;
}

public StashBuildState getBuildStatus() {
return buildStatus;
}

public void setBuildStatus(StashBuildState buildStatus) {
this.buildStatus = buildStatus;
}

@DataBoundSetter
public void setBuildStatus(String buildStatus) {
try {
this.buildStatus = StashBuildState.valueOf(buildStatus);
} catch (Exception e) {
// ignore unknown or null values
}
}

public String getBuildName() {
return buildName;
}

public boolean getIncludeBuildNumberInKey() {
@DataBoundSetter
public void setBuildName(String buildName) {
this.buildName = buildName;
}

public boolean isIncludeBuildNumberInKey() {
return includeBuildNumberInKey;
}

@DataBoundSetter
public void setIncludeBuildNumberInKey(boolean includeBuildNumberInKey) {
this.includeBuildNumberInKey = includeBuildNumberInKey;
}

public String getProjectKey() {
return projectKey;
}

public boolean getPrependParentProjectKey() {
@DataBoundSetter
public void setProjectKey(String projectKey) {
this.projectKey = projectKey;
}

public boolean isPrependParentProjectKey() {
return prependParentProjectKey;
}

@DataBoundSetter
public void setPrependParentProjectKey(boolean prependParentProjectKey) {
this.prependParentProjectKey = prependParentProjectKey;
}

public boolean isDisableInprogressNotification() {
return disableInprogressNotification;
}

@DataBoundSetter
public void setDisableInprogressNotification(boolean disableInprogressNotification) {
this.disableInprogressNotification = disableInprogressNotification;
}

public boolean isConsiderUnstableAsSuccess() {
return considerUnstableAsSuccess;
}

@DataBoundSetter
public void setConsiderUnstableAsSuccess(boolean considerUnstableAsSuccess) {
this.considerUnstableAsSuccess = considerUnstableAsSuccess;
}

@Inject
void setHttpNotifierSelector(HttpNotifierSelector httpNotifierSelector) {
this.httpNotifierSelector = httpNotifierSelector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<f:entry title="Commit SHA-1" field="commitSha1">
<f:textbox />
</f:entry>
<f:entry title="Build Name" field="buildName">
<f:textbox />
</f:entry>
<f:entry title="Build Status" field="buildStatus">
<f:textbox />
</f:entry>
<f:entry title="Ignore unverified SSL certificates" field="ignoreUnverifiedSSLPeer">
<f:checkbox />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
If not empty, this will overwrite the default build name sent to Bitbucket.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
If not empty, this status will be sent to Bitbucket instead of the <code>currentBuild.result</code>.
</div>

0 comments on commit 5d1ba63

Please sign in to comment.