Skip to content

Commit 7404b0b

Browse files
rantoniukCopilot
andauthored
feat: option to not fail the build if version already exists (#1145)
* feat: add failIfAlreadyExists flag to version creator * handle null from XStream readResolve and refactor to fluent interface * chore: spotless:apply * add tests for failIfAlreadyExists flag in JiraVersionCreatorBuilder --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]>
1 parent 6672f0a commit 7404b0b

File tree

12 files changed

+202
-32
lines changed

12 files changed

+202
-32
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ General rules:
44

55
- check the [general Jenkins development guide](https://www.jenkins.io/doc/developer/book/)
66
- make sure to provide tests
7+
- when adding new fields, make sure to [include backward-compatibility](https://www.jenkins.io/doc/developer/persistence/backward-compatibility/) and tests for that
78
- mark the Pull Request as _draft_ initially, to make sure all the checks pass correctly, then convert it to non-draft.
89

910
## Setting up your environment
@@ -22,20 +23,19 @@ brew install pre-commit && pre-commit install --install-hooks
2223

2324
Use [docker-compose](./docker-compose.yml) to run a local Jenkins instance with the plugin installed. The configuration includes local volumes for both: Jenkins and ssh-agent, so you can easily test the plugin in a clean environment.
2425

25-
```bash
2626

2727
### Atlassian sources import
2828

29-
To resolve some binary compatibility issues [JENKINS-48357](https://issues.jenkins-ci.org/browse/JENKINS-48357),
29+
To resolve [some binary compatibility issues](https://github.com/jenkinsci/jira-plugin/pull/140),
3030
the sources from the artifact [com.atlassian.httpclient:atlassian-httpclient-plugin:0.23](https://packages.atlassian.com/maven-external/com/atlassian/httpclient/atlassian-httpclient-plugin/0.23.0/)
3131
has been imported in the project to have control over http(s) protocol transport layer.
3232
The downloaded sources didn't have any license headers but based on the [pom](https://packages.atlassian.com/maven-external/com/atlassian/httpclient/atlassian-httpclient-plugin/0.23.0/atlassian-httpclient-plugin-0.23.0.pom)
33-
sources are Apache License (see pom in src/main/resources/atlassian-httpclient-plugin-0.23.0.pom)
33+
sources are Apache License (see pom in src/main/resources/atlassian-httpclient-plugin-0.23.0.pom)
3434

3535
### Testing
3636

3737
There is a [Jira Cloud](https://jenkins-jira-plugin.atlassian.net/) test instance that the changes can be tested against, official maintainers are admins that can grant access for testing to PR submitters on a need-to-have basis.
3838

3939
### Releasing the plugin
4040

41-
Make sure you have your `~/.m2/settings.xml` configured accordingly - refer to [releasing Jenkins plugins](https://www.jenkins.io/doc/developer/publishing/releasing/).
41+
See [releasing Jenkins plugins](https://www.jenkins.io/doc/developer/publishing/releasing-manually/).

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
[![Jenkins CI](https://ci.jenkins.io/buildStatus/icon?job=Plugins/jira-plugin/master)](https://ci.jenkins.io/job/Plugins/job/jira-plugin/)
77
[![Contributors](https://img.shields.io/github/contributors/jenkinsci/jira-plugin.svg)](https://github.com/jenkinsci/jira-plugin/graphs/contributors)
88

9-
See user documentation at [https://jenkinsci.github.io/jira-plugin/](https://jenkinsci.github.io/jira-plugin/)
9+
1. See user documentation at [https://jenkinsci.github.io/jira-plugin/](https://jenkinsci.github.io/jira-plugin/).
10+
1. Use [Declarative pipelines](https://www.jenkins.io/doc/book/pipeline/#declarative-versus-scripted-pipeline-syntax).
11+
1. Check [jira plugin steps reference](https://www.jenkins.io/doc/pipeline/steps/jira/).
1012

1113
## i18n
1214

@@ -22,10 +24,3 @@ See user documentation at [https://jenkinsci.github.io/jira-plugin/](https://jen
2224

2325
This plugin uses [CrowdIn platform](https://jenkins.crowdin.com/jira-plugin) as the frontend to manage translations. If you would like to contribute translation of this plugin in your language, you're most welcome! For details, see [jenkins.io CrowdIn introduction](https://www.jenkins.io/doc/developer/crowdin/translating-plugins/).
2426

25-
## Contributing
26-
27-
There have been many developers involved in the development of this plugin and there are many downstream users who depend on it. Tests help us assure that we're delivering a reliable plugin and that we've communicated our intent to other developers in a way that they can detect when they run tests.
28-
29-
- each change should be covered by appropriate unit tests
30-
- in case it is not testable via a unit test, it should be tested against a real Jira instance - possibly both Jira Server and Jira Cloud. There is a [Jira Cloud test instance](https://jenkins-jira-plugin.atlassian.net/) that we are using for testing the plugin releases - let us know in the Pull Request in case you need access for testing
31-

src/main/java/hudson/plugins/jira/JiraVersionCreator.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@
1111
import hudson.tasks.Publisher;
1212
import net.sf.json.JSONObject;
1313
import org.kohsuke.stapler.DataBoundConstructor;
14+
import org.kohsuke.stapler.DataBoundSetter;
1415
import org.kohsuke.stapler.StaplerRequest2;
1516

1617
/**
1718
* A build step which creates new Jira version
1819
*
1920
* @author Artem Koshelev [email protected]
20-
* @deprecated Replaced by {@link JiraVersionCreatorBuilder}. Read its description to see why.
21-
* Kept for backward compatibility.
21+
* @deprecated Replaced by {@link JiraVersionCreatorBuilder}. Read its
22+
* description to see why. Kept for backward compatibility.
2223
*/
2324
@Deprecated
2425
public class JiraVersionCreator extends Notifier {
26+
2527
private String jiraVersion;
2628
private String jiraProjectKey;
29+
private Boolean failIfAlreadyExists = true;
2730

2831
@DataBoundConstructor
2932
public JiraVersionCreator(String jiraVersion, String jiraProjectKey) {
@@ -52,16 +55,37 @@ public void setJiraProjectKey(String jiraProjectKey) {
5255
this.jiraProjectKey = jiraProjectKey;
5356
}
5457

58+
public boolean isFailIfAlreadyExists() {
59+
return failIfAlreadyExists;
60+
}
61+
62+
@DataBoundSetter
63+
public void setFailIfAlreadyExists(boolean failIfAlreadyExists) {
64+
this.failIfAlreadyExists = failIfAlreadyExists;
65+
}
66+
5567
@Override
5668
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
57-
return new VersionCreator().perform(build.getProject(), jiraVersion, jiraProjectKey, build, listener);
69+
VersionCreator versionCreator = new VersionCreator();
70+
versionCreator.setFailIfAlreadyExists(failIfAlreadyExists);
71+
versionCreator.setJiraVersion(jiraVersion);
72+
versionCreator.setJiraProjectKey(jiraProjectKey);
73+
return versionCreator.perform(build.getProject(), build, listener);
5874
}
5975

6076
@Override
6177
public BuildStepDescriptor<Publisher> getDescriptor() {
6278
return DESCRIPTOR;
6379
}
6480

81+
protected Object readResolve() {
82+
if (failIfAlreadyExists == null) {
83+
setFailIfAlreadyExists(true);
84+
}
85+
86+
return this;
87+
}
88+
6589
@Extension
6690
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
6791

src/main/java/hudson/plugins/jira/JiraVersionCreatorBuilder.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.sf.json.JSONObject;
1313
import org.jenkinsci.Symbol;
1414
import org.kohsuke.stapler.DataBoundConstructor;
15+
import org.kohsuke.stapler.DataBoundSetter;
1516
import org.kohsuke.stapler.StaplerRequest2;
1617

1718
/**
@@ -25,6 +26,7 @@ public class JiraVersionCreatorBuilder extends Builder implements SimpleBuildSte
2526

2627
private String jiraVersion;
2728
private String jiraProjectKey;
29+
private Boolean failIfAlreadyExists = true;
2830

2931
@DataBoundConstructor
3032
public JiraVersionCreatorBuilder(String jiraVersion, String jiraProjectKey) {
@@ -53,9 +55,30 @@ public void setJiraProjectKey(String jiraProjectKey) {
5355
this.jiraProjectKey = jiraProjectKey;
5456
}
5557

58+
public boolean isFailIfAlreadyExists() {
59+
return failIfAlreadyExists;
60+
}
61+
62+
protected Object readResolve() {
63+
if (failIfAlreadyExists == null) {
64+
setFailIfAlreadyExists(true);
65+
}
66+
67+
return this;
68+
}
69+
70+
@DataBoundSetter
71+
public void setFailIfAlreadyExists(boolean failIfAlreadyExists) {
72+
this.failIfAlreadyExists = failIfAlreadyExists;
73+
}
74+
5675
@Override
5776
public void perform(Run<?, ?> run, EnvVars env, TaskListener listener) {
58-
new VersionCreator().perform(run.getParent(), jiraVersion, jiraProjectKey, run, listener);
77+
VersionCreator versionCreator = new VersionCreator();
78+
versionCreator.setFailIfAlreadyExists(failIfAlreadyExists);
79+
versionCreator.setJiraVersion(jiraVersion);
80+
versionCreator.setJiraProjectKey(jiraProjectKey);
81+
versionCreator.perform(run.getParent(), run, listener);
5982
}
6083

6184
@Override

src/main/java/hudson/plugins/jira/VersionCreator.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,28 @@ class VersionCreator {
2020

2121
private static final Logger LOGGER = Logger.getLogger(VersionCreator.class.getName());
2222

23-
protected boolean perform(
24-
Job<?, ?> project, String jiraVersion, String jiraProjectKey, Run<?, ?> build, TaskListener listener) {
23+
private boolean failIfAlreadyExists = true;
24+
25+
private String jiraVersion;
26+
27+
private String jiraProjectKey;
28+
29+
public VersionCreator setFailIfAlreadyExists(boolean failIfAlreadyExists) {
30+
this.failIfAlreadyExists = failIfAlreadyExists;
31+
return this;
32+
}
33+
34+
public VersionCreator setJiraVersion(String jiraVersion) {
35+
this.jiraVersion = jiraVersion;
36+
return this;
37+
}
38+
39+
public VersionCreator setJiraProjectKey(String jiraProjectKey) {
40+
this.jiraProjectKey = jiraProjectKey;
41+
return this;
42+
}
43+
44+
protected boolean perform(Job<?, ?> project, Run<?, ?> build, TaskListener listener) {
2545
String realVersion = null;
2646
String realProjectKey = null;
2747

@@ -42,13 +62,17 @@ protected boolean perform(
4262
List<ExtendedVersion> existingVersions =
4363
Optional.ofNullable(session.getVersions(realProjectKey)).orElse(Collections.emptyList());
4464

45-
// past logic to fail the build if the version already exists
65+
// check if version already exists
4666
if (existingVersions.stream().anyMatch(v -> v.getName().equals(finalRealVersion))) {
4767
listener.getLogger().println(Messages.JiraVersionCreator_VersionExists(realVersion, realProjectKey));
48-
if (listener instanceof BuildListener) {
49-
((BuildListener) listener).finished(Result.FAILURE);
68+
if (failIfAlreadyExists) {
69+
if (listener instanceof BuildListener) {
70+
((BuildListener) listener).finished(Result.FAILURE);
71+
}
72+
return false;
5073
}
51-
return false;
74+
// version exists but we don't fail the build
75+
return true;
5276
}
5377

5478
listener.getLogger().println(Messages.JiraVersionCreator_CreatingVersion(realVersion, realProjectKey));
@@ -59,9 +83,6 @@ protected boolean perform(
5983
listener.fatalError("Unable to add version %s to Jira project %s", realVersion, realProjectKey, e));
6084
}
6185

62-
if (listener instanceof BuildListener) {
63-
((BuildListener) listener).finished(Result.FAILURE);
64-
}
6586
return false;
6687
}
6788

src/main/resources/hudson/plugins/jira/JiraVersionCreator/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
<f:entry title="${%Jira Project Key}" field="jiraProjectKey">
77
<f:textbox/>
88
</f:entry>
9+
<f:entry title="${%Fail if version already exists}" field="failIfAlreadyExists">
10+
<f:checkbox default="true"/>
11+
</f:entry>
912
</j:jelly>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
When checked (default), the build will fail if the version already exists in Jira.
3+
When unchecked, the build will continue successfully even if the version already exists.
4+
</div>

src/main/resources/hudson/plugins/jira/JiraVersionCreatorBuilder/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
<f:entry title="${%Jira Project Key}" field="jiraProjectKey">
77
<f:textbox/>
88
</f:entry>
9+
<f:entry title="${%Fail if version already exists}" field="failIfAlreadyExists">
10+
<f:checkbox default="true"/>
11+
</f:entry>
912
</j:jelly>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
When checked (default), the build will fail if the version already exists in Jira.
3+
When unchecked, the build will continue successfully even if the version already exists.
4+
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
Creates a new version in Jira in the project with the given key. If the version already exists, the build will fail.
2+
Creates a new version in Jira in the project with the given key. By default, if the version already exists, the build will fail. This behavior can be changed by unchecking the "Fail if version already exists" option.
33
</div>

0 commit comments

Comments
 (0)