-
Notifications
You must be signed in to change notification settings - Fork 28
/
BuildStatusChecksPublisher.java
234 lines (208 loc) · 7.98 KB
/
BuildStatusChecksPublisher.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package io.jenkins.plugins.checks.status;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.*;
import hudson.model.listeners.RunListener;
import hudson.model.listeners.SCMListener;
import hudson.model.queue.QueueListener;
import hudson.scm.SCM;
import hudson.scm.SCMRevisionState;
import io.jenkins.plugins.checks.api.*;
import io.jenkins.plugins.checks.api.ChecksDetails.ChecksDetailsBuilder;
import io.jenkins.plugins.util.JenkinsFacade;
import org.jenkinsci.plugins.workflow.actions.LabelAction;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.flow.GraphListener;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
/**
* A publisher which publishes different statuses through the checks API based on the stage of the {@link Queue.Item}
* or {@link Run}.
*/
public final class BuildStatusChecksPublisher {
private static final Logger LOGGER = Logger.getLogger(BuildStatusChecksPublisher.class.getName());
private BuildStatusChecksPublisher() {
}
private static final JenkinsFacade JENKINS = new JenkinsFacade();
private static final AbstractStatusChecksProperties DEFAULT_PROPERTIES = new DefaultStatusCheckProperties();
private static void publish(final ChecksPublisher publisher, final ChecksStatus status,
final ChecksConclusion conclusion, final String name, @CheckForNull final ChecksOutput output) {
ChecksDetailsBuilder builder = new ChecksDetailsBuilder()
.withName(name)
.withStatus(status)
.withConclusion(conclusion);
if (output != null) {
builder.withOutput(output);
}
publisher.publish(builder.build());
}
@Deprecated
private static Optional<StatusChecksProperties> findDeprecatedProperties(final Job<?, ?> job) {
return JENKINS.getExtensionsFor(StatusChecksProperties.class)
.stream()
.filter(p -> p.isApplicable(job))
.findFirst();
}
private static AbstractStatusChecksProperties findProperties(final Job<?, ?> job) {
return JENKINS.getExtensionsFor(AbstractStatusChecksProperties.class)
.stream()
.filter(p -> p.isApplicable(job))
.findFirst()
.orElse(DEFAULT_PROPERTIES);
}
static Optional<String> getChecksName(final Run<?, ?> run) {
return getChecksName(run.getParent());
}
static Optional<String> getChecksName(final Job<?, ?> job) {
return Stream.of(
findDeprecatedProperties(job)
.filter(p -> !p.isSkip(job))
.map(p -> p.getName(job)),
Optional.of(findProperties(job))
.filter(p -> !p.isSkipped(job))
.map(p -> p.getName(job))
)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
}
@CheckForNull
static ChecksOutput getOutput(final Run run) {
if (run instanceof WorkflowRun) {
FlowExecution execution = ((WorkflowRun) run).getExecution();
if (execution != null) {
return getOutput(run, execution);
}
}
return null;
}
static ChecksOutput getOutput(final Run run, final FlowExecution execution) {
return new FlowExecutionAnalyzer(run, execution).extractOutput();
}
/**
* {@inheritDoc}
*
* <p>
* Listens to the queue and publishes checks in "queued" state for entering items.
* </p>
*/
@Extension
public static class JobScheduledListener extends QueueListener {
/**
* {@inheritDoc}
*
* <p>
* When a job enters queue, creates the check on "queued".
* </p>
*/
@Override
public void onEnterWaiting(final Queue.WaitingItem wi) {
if (!(wi.task instanceof Job)) {
return;
}
final Job job = (Job) wi.task;
getChecksName(job).ifPresent(checksName -> publish(ChecksPublisherFactory.fromJob(job, TaskListener.NULL),
ChecksStatus.QUEUED, ChecksConclusion.NONE, checksName, null));
}
}
/**
* {@inheritDoc}
*
* <p>
* Listens to the SCM checkout and publishes checks.
* </p>
*/
@Extension
public static class JobCheckoutListener extends SCMListener {
/**
* {@inheritDoc}
* <p>
* When checkout finished, update the check to "in progress".
* </p>
*/
@Override
public void onCheckout(final Run<?, ?> run, final SCM scm, final FilePath workspace,
final TaskListener listener, @CheckForNull final File changelogFile,
@CheckForNull final SCMRevisionState pollingBaseline) {
getChecksName(run).ifPresent(checksName -> publish(ChecksPublisherFactory.fromRun(run, listener),
ChecksStatus.IN_PROGRESS, ChecksConclusion.NONE, checksName, null));
}
}
/**
* {@inheritDoc}
*
* <p>
* Listens to the run and publishes checks.
* </p>
*/
@Extension
public static class JobCompletedListener extends RunListener<Run<?, ?>> {
/**
* {@inheritDoc}
*
* <p>
* When a job completes, completes the check.
* </p>
*/
@Override
public void onCompleted(final Run run, @CheckForNull final TaskListener listener) {
getChecksName(run).ifPresent(checksName -> publish(ChecksPublisherFactory.fromRun(run, listener),
ChecksStatus.COMPLETED, extractConclusion(run), checksName, getOutput(run)));
}
private ChecksConclusion extractConclusion(final Run<?, ?> run) {
Result result = run.getResult();
if (result == null) {
throw new IllegalStateException("No result when the run completes, run: " + run.toString());
}
if (result.isBetterOrEqualTo(Result.SUCCESS)) {
return ChecksConclusion.SUCCESS;
}
else if (result.isBetterOrEqualTo(Result.UNSTABLE) || result.isBetterOrEqualTo(Result.FAILURE)) {
return ChecksConclusion.FAILURE;
}
else if (result.isBetterOrEqualTo(Result.NOT_BUILT)) {
return ChecksConclusion.SKIPPED;
}
else if (result.isBetterOrEqualTo(Result.ABORTED)) {
return ChecksConclusion.CANCELED;
}
else {
throw new IllegalStateException("Unsupported run result: " + result);
}
}
}
/**
* {@inheritDoc}
*
* <p>
* As a job progresses, record a representation of the flow graph.
* </p>
*/
@Extension
public static class ChecksGraphListener implements GraphListener {
@Override
public void onNewHead(final FlowNode node) {
if (node.getAction(LabelAction.class) == null) {
// It's not a branch or stage node, so let's not worry about updating.
return;
}
Run<?, ?> run;
try {
run = (Run) node.getExecution().getOwner().getExecutable();
}
catch (IOException e) {
LOGGER.log(Level.WARNING, "Unable to find Run from flow node.", e);
return;
}
getChecksName(run).ifPresent(checksName -> publish(ChecksPublisherFactory.fromRun(run, TaskListener.NULL),
ChecksStatus.IN_PROGRESS, ChecksConclusion.NONE, checksName, getOutput(run, node.getExecution())));
}
}
}