Skip to content
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

Implement setters instead of constructor for optional parameters #246

Merged
merged 6 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 105 additions & 66 deletions src/main/java/org/jenkinsci/plugins/stashNotifier/StashNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,61 +104,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;

// public members ----------------------------------------------------------

Expand All @@ -181,105 +181,144 @@ public BuildStepMonitor getRequiredMonitorService() {
boolean considerUnstableAsSuccess,
JenkinsLocationConfiguration globalConfig
) {
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);
}

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;
StashNotifier(
Copy link
Collaborator

@darxriggs darxriggs Feb 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking if the number of constructors can be reduced even more? Is this one really required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about that, and neither am I that the test cases existing as well as the ones I can do locally cover all potential use cases. So I've kept it to be on the safe side.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is a new one and another one has been removed.

In other plugins it's often handled the way to keep all old constructors for backward compatibility and annotate them with @Deprecated. That's something the maintainers should decide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you're right. Then I'm not totally sure, what the constructor was meant for. I think it's just the old DataBoundConstructor without all of the optional parameters. So I could just move the logic in the no-argument constructor.

Copy link
Contributor Author

@westarne westarne Apr 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was some time out of the country and noticed that I did not push the change before. Done that now.

JenkinsLocationConfiguration globalConfig
) {
this.globalConfig = globalConfig;
}

@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
) {
public StashNotifier() {
this(
stashServerBaseUrl,
credentialsId,
ignoreUnverifiedSSLPeer,
commitSha1,
buildStatus,
buildName,
includeBuildNumberInKey,
projectKey,
prependParentProjectKey,
disableInprogressNotification,
considerUnstableAsSuccess,
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 = stashServerBaseUrl != null && stashServerBaseUrl.endsWith("/")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringUtils#stripEnd could be used.

? stashServerBaseUrl.substring(0, stashServerBaseUrl.length() - 1)
: 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) {
scaytrase marked this conversation as resolved.
Show resolved Hide resolved
StashBuildState overwrittenBuildState = null;
try {
overwrittenBuildState = StashBuildState.valueOf(buildStatus);
} catch (Exception e) {
// ignore unknown or null values
}
this.buildStatus = overwrittenBuildState;
}

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;
}

@Override
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
return disableInprogressNotification || processJenkinsEvent(build, null, listener, StashBuildState.INPROGRESS);
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">
scaytrase marked this conversation as resolved.
Show resolved Hide resolved
<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>