forked from jenkinsci/jira-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJiraVersionParameterDefinition.java
More file actions
228 lines (194 loc) · 7.41 KB
/
JiraVersionParameterDefinition.java
File metadata and controls
228 lines (194 loc) · 7.41 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package hudson.plugins.jira.versionparameter;
import com.atlassian.jira.rest.client.api.RestClientException;
import com.atlassian.jira.rest.client.api.domain.Version;
import hudson.Extension;
import hudson.cli.CLICommand;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.plugins.jira.JiraSession;
import hudson.plugins.jira.JiraSite;
import hudson.plugins.jira.Messages;
import hudson.util.ListBoxModel;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.interceptor.RequirePOST;
public class JiraVersionParameterDefinition extends ParameterDefinition {
private static final long serialVersionUID = 4232979892748310160L;
private String projectKey;
private boolean showReleased = false;
private boolean showArchived = false;
private boolean showUnreleased = false;
private Pattern pattern = null;
@DataBoundConstructor
public JiraVersionParameterDefinition(
String name,
String description,
String jiraProjectKey,
String jiraReleasePattern,
String jiraShowReleased,
String jiraShowArchived,
String jiraShowUnreleased) {
super(name);
setDescription(description);
setJiraProjectKey(jiraProjectKey);
setJiraReleasePattern(jiraReleasePattern);
setJiraShowReleased(jiraShowReleased);
setJiraShowArchived(jiraShowArchived);
setShowUnreleased(jiraShowUnreleased);
}
@Override
public ParameterValue createValue(StaplerRequest2 req) {
String[] values = req.getParameterValues(getName());
if (values == null || values.length != 1 || values[0].isEmpty()) {
return null;
}
return new JiraVersionParameterValue(getName(), values[0]);
}
@Override
public ParameterValue createValue(StaplerRequest2 req, JSONObject formData) {
JiraVersionParameterValue value = req.bindJSON(JiraVersionParameterValue.class, formData);
return value;
}
@Override
public ParameterValue createValue(CLICommand command, String value) throws IOException, InterruptedException {
return new JiraVersionParameterValue(getName(), value);
}
public List<JiraVersionParameterDefinition.Result> getVersions() throws IOException, RestClientException {
Job<?, ?> contextJob = Stapler.getCurrentRequest2().findAncestorObject(Job.class);
return getVersions(contextJob);
}
List<JiraVersionParameterDefinition.Result> getVersions(Job<?, ?> contextJob) {
JiraSite site = JiraSite.get(contextJob);
if (site == null) {
throw new IllegalStateException(
"Jira site needs to be configured in the project " + contextJob.getFullDisplayName());
}
JiraSession session = site.getSession(contextJob);
if (session == null) {
throw new IllegalStateException("Remote access for Jira isn't configured in Jenkins");
}
return session.getVersions(projectKey).stream()
.sorted(VersionComparator.INSTANCE)
.filter(this::match)
.map(Result::new)
.collect(Collectors.toList());
}
private boolean match(Version version) {
// Match regex if it exists
if (pattern != null) {
if (!pattern.matcher(version.getName()).matches()) {
return false;
}
}
boolean isReleased = version.isReleased();
boolean isArchived = version.isArchived();
boolean showAllVersions = !showReleased && !showUnreleased && !showArchived;
if (showAllVersions) {
return true;
}
if (showReleased && isReleased && !isArchived) {
return true;
}
if (showArchived && isArchived) {
return true;
}
if (showUnreleased && !isReleased && !isArchived) {
return true;
}
return false;
}
public String getJiraReleasePattern() {
if (pattern == null) {
return "";
}
return pattern.pattern();
}
public void setJiraReleasePattern(String pattern) {
if (pattern == null || pattern.isEmpty()) {
this.pattern = null;
} else {
this.pattern = Pattern.compile(pattern);
}
}
public String getJiraProjectKey() {
return projectKey;
}
public void setJiraProjectKey(String projectKey) {
this.projectKey = projectKey;
}
public String getJiraShowReleased() {
return Boolean.toString(showReleased);
}
public void setJiraShowReleased(String showReleased) {
this.showReleased = Boolean.parseBoolean(showReleased);
}
public String getJiraShowArchived() {
return Boolean.toString(showArchived);
}
public void setJiraShowArchived(String showArchived) {
this.showArchived = Boolean.parseBoolean(showArchived);
}
public String getJiraShowUnreleased() {
return Boolean.toString(showUnreleased);
}
public void setShowUnreleased(String jiraShowUnreleased) {
this.showUnreleased = Boolean.parseBoolean(jiraShowUnreleased);
}
@Extension
@Symbol("jiraReleaseVersion")
public static class DescriptorImpl extends ParameterDescriptor {
@Override
public String getDisplayName() {
return "Jira Release Version Parameter";
}
@RequirePOST
public ListBoxModel doFillVersionItems(@AncestorInPath Job<?, ?> job, @QueryParameter String name) {
ListBoxModel items = new ListBoxModel();
if (job.hasPermission(Item.BUILD)) {
ParametersDefinitionProperty prop = job.getProperty(ParametersDefinitionProperty.class);
if (prop != null) {
ParameterDefinition def = prop.getParameterDefinition(name);
if (def instanceof JiraVersionParameterDefinition jiraVersionDef) {
List<JiraVersionParameterDefinition.Result> issueValues = jiraVersionDef.getVersions(job);
issueValues.forEach(it -> items.add(it.name));
}
}
}
if (items.isEmpty()) {
items.add(Messages.JiraVersionParameterDefinition_NoVersionsMatchedSearch(), "");
}
return items;
}
}
public static class Result {
public final String name;
public final Long id;
public Result(final Version version) {
this.name = version.getName();
this.id = version.getId();
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Result result = (Result) o;
return Objects.equals(name, result.name) && Objects.equals(id, result.id);
}
@Override
public int hashCode() {
return Objects.hash(name, id);
}
}
}