-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathDescriptionSetterPublisher.java
More file actions
191 lines (161 loc) · 5.92 KB
/
Copy pathDescriptionSetterPublisher.java
File metadata and controls
191 lines (161 loc) · 5.92 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
package hudson.plugins.descriptionsetter;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Launcher;
import hudson.Util;
import hudson.matrix.MatrixAggregatable;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import java.io.IOException;
import java.io.ObjectStreamException;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest2;
/**
* The DescriptionSetterPublisher allows the description of a build to be set as
* a post-build action, after the build has completed.
*
*/
public class DescriptionSetterPublisher extends Recorder implements MatrixAggregatable {
private final String regexp;
private final String regexpForFailed;
private final String description;
private final String descriptionForFailed;
private final boolean setForMatrix;
private final boolean appendMode;
private final boolean envVariable;
@Deprecated
private transient boolean setForFailed = false;
@Deprecated
private transient boolean explicitNotRegexp = false;
@DataBoundConstructor
public DescriptionSetterPublisher(
String regexp,
String regexpForFailed,
String description,
String descriptionForFailed,
boolean setForMatrix,
boolean appendMode,
boolean envVariable) {
this.regexp = regexp;
this.regexpForFailed = regexpForFailed;
this.description = Util.fixEmptyAndTrim(description);
this.descriptionForFailed = Util.fixEmptyAndTrim(descriptionForFailed);
this.setForMatrix = setForMatrix;
this.appendMode = appendMode;
this.envVariable = envVariable;
}
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException {
Result result = build.getResult();
boolean useUnstable = (regexpForFailed != null || descriptionForFailed != null)
&& result != null
&& result.isWorseThan(Result.UNSTABLE);
return DescriptionSetterHelper.setDescription(
build,
listener,
useUnstable ? regexpForFailed : regexp,
useUnstable ? descriptionForFailed : description,
appendMode,
envVariable);
}
private Object readResolve() throws ObjectStreamException {
if (explicitNotRegexp) {
return new DescriptionSetterPublisher(
null, null, regexp, setForFailed ? regexpForFailed : null, false, false, true);
} else {
return this;
}
}
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public DescriptorImpl() {
super(DescriptionSetterPublisher.class);
}
@Override
public String getDisplayName() {
return Messages.DescriptionSetter_DisplayName();
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
@Override
public Publisher newInstance(StaplerRequest2 req, @NonNull JSONObject formData) throws FormException {
if (req == null) {
return null;
}
return req.bindJSON(DescriptionSetterPublisher.class, formData);
}
public boolean isMatrixProject(AbstractProject project) {
return project instanceof MatrixProject;
}
}
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}
@Deprecated
public boolean isExplicitNotRegexp() {
return explicitNotRegexp;
}
public String getRegexp() {
return regexp;
}
@Deprecated
public boolean isSetForFailed() {
return setForFailed;
}
public String getRegexpForFailed() {
return regexpForFailed;
}
public String getDescription() {
return description;
}
public String getDescriptionForFailed() {
return descriptionForFailed;
}
public MatrixAggregator createAggregator(final MatrixBuild build, Launcher launcher, final BuildListener listener) {
if (!isSetForMatrix()) {
return null;
}
return new MatrixAggregator(build, launcher, listener) {
@Override
public boolean endRun(MatrixRun run) throws InterruptedException, IOException {
if (build.getDescription() == null && run.getDescription() != null) {
build.setDescription(run.getDescription());
} else if (build.getDescription() != null && run.getDescription() != null) {
String oldDescr = build.getDescription();
/* Don't want a runtime dependency on OWASP markup formatter plugin */
String formatter =
Jenkins.get().getMarkupFormatter().getClass().getName();
String htmlBreak = formatter.contains("HtmlMarkup") ? "<br>" : " ";
String newDescr = oldDescr + htmlBreak + run.getDescription();
build.setDescription(newDescr);
}
return true;
}
};
}
public boolean isSetForMatrix() {
return setForMatrix;
}
public boolean isAppendMode() {
return appendMode;
}
}