forked from jenkinsci/jira-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionCreator.java
More file actions
109 lines (87 loc) · 3.57 KB
/
VersionCreator.java
File metadata and controls
109 lines (87 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package hudson.plugins.jira;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
import static org.apache.commons.lang.StringUtils.isEmpty;
import hudson.model.BuildListener;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.jira.extension.ExtendedVersion;
/**
* Performs an action which creates new Jira version.
*/
class VersionCreator {
private static final Logger LOGGER = Logger.getLogger(VersionCreator.class.getName());
private boolean failIfAlreadyExists = true;
private String jiraVersion;
private String jiraProjectKey;
public VersionCreator setFailIfAlreadyExists(boolean failIfAlreadyExists) {
this.failIfAlreadyExists = failIfAlreadyExists;
return this;
}
public VersionCreator setJiraVersion(String jiraVersion) {
this.jiraVersion = jiraVersion;
return this;
}
public VersionCreator setJiraProjectKey(String jiraProjectKey) {
this.jiraProjectKey = jiraProjectKey;
return this;
}
protected boolean perform(
Job<?, ?> project, Run<?, ?> build, TaskListener listener) {
String realVersion = null;
String realProjectKey = null;
try {
realVersion = build.getEnvironment(listener).expand(jiraVersion);
realProjectKey = build.getEnvironment(listener).expand(jiraProjectKey);
if (isEmpty(realVersion)) {
throw new IllegalArgumentException("No version specified");
}
if (isEmpty(realProjectKey)) {
throw new IllegalArgumentException("No project specified");
}
String finalRealVersion = realVersion;
JiraSession session = getSiteForProject(project).getSession(project);
List<ExtendedVersion> existingVersions =
Optional.ofNullable(session.getVersions(realProjectKey)).orElse(Collections.emptyList());
// check if version already exists
if (existingVersions.stream().anyMatch(v -> v.getName().equals(finalRealVersion))) {
listener.getLogger().println(Messages.JiraVersionCreator_VersionExists(realVersion, realProjectKey));
if (failIfAlreadyExists) {
if (listener instanceof BuildListener) {
((BuildListener) listener).finished(Result.FAILURE);
}
return false;
}
// version exists but we don't fail the build
return true;
}
listener.getLogger().println(Messages.JiraVersionCreator_CreatingVersion(realVersion, realProjectKey));
addVersion(realVersion, realProjectKey, session);
return true;
} catch (Exception e) {
e.printStackTrace(
listener.fatalError("Unable to add version %s to Jira project %s", realVersion, realProjectKey, e));
}
return false;
}
/**
* Creates given version in given project
* @param version version name
* @param projectKey project key
* @param session session
*/
protected void addVersion(String version, String projectKey, JiraSession session) {
if (session == null) {
LOGGER.warning("Jira session could not be established");
return;
}
session.addVersion(version, projectKey);
}
protected JiraSite getSiteForProject(Job<?, ?> project) {
return JiraSite.get(project);
}
}