-
Notifications
You must be signed in to change notification settings - Fork 28
/
PublishChecksStep.java
204 lines (172 loc) · 6.21 KB
/
PublishChecksStep.java
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
package io.jenkins.plugins.checks.steps;
import edu.hm.hafner.util.VisibleForTesting;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.ListBoxModel;
import io.jenkins.plugins.checks.api.*;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
/**
* Pipeline step to publish customized checks.
*/
@SuppressWarnings("PMD.DataClass")
public class PublishChecksStep extends Step implements Serializable {
private static final long serialVersionUID = 1L;
private String name = StringUtils.EMPTY;
private String summary = StringUtils.EMPTY;
private String title = StringUtils.EMPTY;
private String text = StringUtils.EMPTY;
private String detailsURL = StringUtils.EMPTY;
private ChecksStatus status = ChecksStatus.COMPLETED;
private ChecksConclusion conclusion = ChecksConclusion.SUCCESS;
/**
* Constructor used for pipeline by Stapler.
*/
@DataBoundConstructor
public PublishChecksStep() {
super();
}
@DataBoundSetter
public void setName(final String name) {
this.name = name;
}
@DataBoundSetter
public void setSummary(final String summary) {
this.summary = summary;
}
@DataBoundSetter
public void setTitle(final String title) {
this.title = title;
}
@DataBoundSetter
public void setText(final String text) {
this.text = text;
}
@DataBoundSetter
public void setDetailsURL(final String detailsURL) {
this.detailsURL = detailsURL;
}
@DataBoundSetter
public void setStatus(final ChecksStatus status) {
this.status = status;
}
@DataBoundSetter
public void setConclusion(final ChecksConclusion conclusion) {
this.conclusion = conclusion;
}
public String getName() {
return name;
}
public String getSummary() {
return summary;
}
public String getTitle() {
return StringUtils.defaultIfEmpty(title, name);
}
public String getText() {
return text;
}
public String getDetailsURL() {
return detailsURL;
}
public ChecksStatus getStatus() {
return status;
}
public ChecksConclusion getConclusion() {
return conclusion;
}
@Override
public StepExecution start(final StepContext stepContext) {
return new PublishChecksStepExecution(stepContext, this);
}
/**
* This step's descriptor which defines function name, display name, and context.
*/
@Extension
public static class PublishChecksStepDescriptor extends StepDescriptor {
@Override
public String getFunctionName() {
return "publishChecks";
}
@Override
public Set<? extends Class<?>> getRequiredContext() {
return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(Run.class, TaskListener.class)));
}
@NonNull
@Override
public String getDisplayName() {
return "Publish customized checks to SCM platforms";
}
/**
* Fill the dropdown list model with all {@link ChecksStatus}es.
*
* @return a model with all {@link ChecksStatus}es.
*/
public ListBoxModel doFillStatusItems() {
return asListBoxModel(ChecksStatus.values());
}
/**
* Fill the dropdown list model with all {@link ChecksConclusion}s.
*
* @return a model with all {@link ChecksConclusion}s.
*/
public ListBoxModel doFillConclusionItems() {
return asListBoxModel(ChecksConclusion.values());
}
private ListBoxModel asListBoxModel(final Enum<?>... enums) {
return Arrays.stream(enums)
.map(Enum::name)
.map(name -> new ListBoxModel.Option(asDisplayName(name), name))
.collect(Collectors.toCollection(ListBoxModel::new));
}
private String asDisplayName(final String name) {
return StringUtils.capitalize(name.toLowerCase(Locale.ENGLISH).replace("_", " "));
}
}
/**
* This step's execution to actually publish checks.
*/
static class PublishChecksStepExecution extends SynchronousNonBlockingStepExecution<Void> {
private static final long serialVersionUID = 1L;
private final PublishChecksStep step;
PublishChecksStepExecution(final StepContext context, final PublishChecksStep step) {
super(context);
this.step = step;
}
@Override
protected Void run() throws IOException, InterruptedException {
ChecksPublisherFactory.fromRun(getContext().get(Run.class), getContext().get(TaskListener.class))
.publish(extractChecksDetails());
return null;
}
@VisibleForTesting
ChecksDetails extractChecksDetails() throws IOException, InterruptedException {
// If a checks name has been provided as part of the step, use that.
// If not, check to see if there is an active ChecksInfo context (e.g. from withChecks).
String checksName = StringUtils.defaultIfEmpty(step.getName(),
Optional.ofNullable(getContext().get(ChecksInfo.class))
.map(ChecksInfo::getName)
.orElse(StringUtils.EMPTY)
);
return new ChecksDetails.ChecksDetailsBuilder()
.withName(checksName)
.withStatus(step.getStatus())
.withConclusion(step.getConclusion())
.withDetailsURL(step.getDetailsURL())
.withOutput(new ChecksOutput.ChecksOutputBuilder()
.withTitle(step.getTitle())
.withSummary(step.getSummary())
.withText(step.getText())
.build())
.build();
}
}
}