forked from jenkinsci/jira-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJiraIssueParameterDefinition.java
More file actions
184 lines (159 loc) · 6.49 KB
/
JiraIssueParameterDefinition.java
File metadata and controls
184 lines (159 loc) · 6.49 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
/*
* Copyright 2011-2012 Insider Guides, Inc., MeetMe, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hudson.plugins.jira.listissuesparameter;
import static hudson.Util.fixNull;
import com.atlassian.jira.rest.client.api.RestClientException;
import com.atlassian.jira.rest.client.api.domain.Issue;
import com.atlassian.jira.rest.client.api.domain.IssueField;
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.util.ListBoxModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.interceptor.RequirePOST;
public class JiraIssueParameterDefinition extends ParameterDefinition {
private static final long serialVersionUID = 3927562542249244416L;
private String jiraIssueFilter;
private String altSummaryFields;
@DataBoundConstructor
public JiraIssueParameterDefinition(String name, String description, String jiraIssueFilter) {
super(name, description);
this.jiraIssueFilter = jiraIssueFilter;
}
@Override
public ParameterValue createValue(StaplerRequest2 req) {
String[] values = req.getParameterValues(getName());
if (values == null || values.length != 1) {
return null;
}
return new JiraIssueParameterValue(getName(), values[0]);
}
@Override
public ParameterValue createValue(StaplerRequest2 req, JSONObject formData) {
JiraIssueParameterValue value = req.bindJSON(JiraIssueParameterValue.class, formData);
return value;
}
@Override
public ParameterValue createValue(CLICommand command, String value) throws IOException, InterruptedException {
return new JiraIssueParameterValue(getName(), value);
}
public List<JiraIssueParameterDefinition.Result> getIssues()
throws IOException, TimeoutException, RestClientException {
Job<?, ?> job = Stapler.getCurrentRequest2().findAncestorObject(Job.class);
return getIssues(job);
}
public List<JiraIssueParameterDefinition.Result> getIssues(Job<?, ?> job) {
JiraSite site = JiraSite.get(job);
if (site == null) {
throw new IllegalStateException(
"Jira site needs to be configured in the project " + job.getFullDisplayName());
}
JiraSession session = site.getSession(job);
if (session == null) {
throw new IllegalStateException("Remote access for Jira isn't configured in Jenkins");
}
List<Issue> issues = session.getIssuesFromJqlSearch(jiraIssueFilter);
List<Result> issueValues = new ArrayList<>();
for (Issue issue : fixNull(issues)) {
issueValues.add(new Result(issue, this.altSummaryFields));
}
return issueValues;
}
public String getJiraIssueFilter() {
return jiraIssueFilter;
}
public void setJiraIssueFilter(String jiraIssueFilter) {
this.jiraIssueFilter = jiraIssueFilter;
}
public String getAltSummaryFields() {
return altSummaryFields;
}
@DataBoundSetter
public void setAltSummaryFields(String altSummaryFields) {
this.altSummaryFields = altSummaryFields;
}
@Extension
@Symbol("jiraIssue")
public static class DescriptorImpl extends ParameterDescriptor {
@Override
public String getDisplayName() {
return "Jira Issue Parameter";
}
@RequirePOST
public ListBoxModel doFillValueItems(@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 JiraIssueParameterDefinition jiraIssueDef) {
List<Result> issueValues = jiraIssueDef.getIssues(job);
issueValues.forEach(it -> items.add(it.summary, it.key));
}
}
}
return items;
}
}
public static class Result {
public final String key;
public final String summary;
public Result(final Issue issue) {
this(issue, null);
}
public Result(final Issue issue, String altSummaryFields) {
this.key = issue.getKey();
if (StringUtils.isEmpty(altSummaryFields)) {
this.summary = issue.getSummary();
} else {
String[] fields = altSummaryFields.split(",");
StringBuilder sb = new StringBuilder();
for (String f : fields) {
String fn = f.trim();
if (StringUtils.isNotEmpty(fn)) {
IssueField field = issue.getFieldByName(fn);
if (field != null && field.getValue() != null) {
String fv = field.getValue().toString();
if (StringUtils.isNotEmpty(fv)) {
sb.append(fv);
sb.append(' ');
}
}
}
}
this.summary = sb.toString().trim();
}
}
}
}