-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathDescriptionSetterHelper.java
More file actions
160 lines (139 loc) · 5.77 KB
/
Copy pathDescriptionSetterHelper.java
File metadata and controls
160 lines (139 loc) · 5.77 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
package hudson.plugins.descriptionsetter;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.StringParameterValue;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jenkins.model.Jenkins;
/**
* Helper class that performs common functionality used by both
* DescriptionSetterBuilder and DescriptionSetterPublisher.
*
*/
public class DescriptionSetterHelper {
private static final String LOG_PREFIX = "[description-setter]";
/**
* Sets the description on the given build based on the specified regular
* expression and description.
*
* @param build the build whose description to set.
* @param listener the build listener to report events to.
* @param regexp the regular expression to apply to the build log.
* @param description the description to set.
* @return true, regardless of if the regular expression matched and a
* description could be set or not.
* @throws InterruptedException if the build is interrupted by the user.
*/
public static boolean setDescription(
AbstractBuild<?, ?> build, BuildListener listener, String regexp, String description)
throws InterruptedException {
return setDescription(build, listener, regexp, description, true, true);
}
/**
* Sets the description on the given build based on the specified regular
* expression and description.
*
* @param build the build whose description to set.
* @param listener the build listener to report events to.
* @param regexp the regular expression to apply to the build log.
* @param description the description to set.
* @param appendMode if true, description is added to the current one
* @param envVariable if true, environment variable containing the
* description is set.
* @return true, regardless of if the regular expression matched and a
* description could be set or not.
* @throws InterruptedException if the build is interrupted by the user.
*/
public static boolean setDescription(
AbstractBuild<?, ?> build,
BuildListener listener,
String regexp,
String description,
boolean appendMode,
boolean envVariable)
throws InterruptedException {
try {
String result = null;
Matcher matcher = parseLog(build, regexp);
if (matcher != null) {
result = getExpandedDescription(matcher, description);
result = build.getEnvironment(listener).expand(result);
} else {
if (regexp == null && description != null) {
result = description;
}
}
if (result == null) {
listener.getLogger().println(LOG_PREFIX + " Could not determine description.");
return true;
}
result = urlify(result);
build.addAction(new DescriptionSetterAction(result));
/* Don't want a runtime dependency on OWASP markup formatter plugin */
String formatter = Jenkins.get().getMarkupFormatter().getClass().getName();
String htmlBreak = formatter.contains("HtmlMarkup") ? "<br>" : " ";
if (build.getDescription() == null) build.setDescription(result);
else build.setDescription((appendMode ? build.getDescription() + htmlBreak : "") + result);
if (envVariable) {
setEnvironmentVariable(result, build);
}
listener.getLogger().println(LOG_PREFIX + " Description set: " + result);
} catch (IOException e) {
e.printStackTrace(listener.error(LOG_PREFIX + " Error while parsing logs for description-setter"));
}
return true;
}
private static void setEnvironmentVariable(String result, AbstractBuild<?, ?> build) {
List<ParameterValue> params = new ArrayList<ParameterValue>();
params.add(new StringParameterValue("DESCRIPTION_SETTER_DESCRIPTION", result));
build.addAction(new ParametersAction(params));
}
private static Matcher parseLog(AbstractBuild<?, ?> build, String regexp) throws IOException {
if (regexp == null) {
return null;
}
// Assume default encoding and text files
String line;
Pattern pattern = Pattern.compile(regexp);
try (BufferedReader reader = new BufferedReader(build.getLogReader())) {
while ((line = reader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
return matcher;
}
}
}
return null;
}
private static String getExpandedDescription(Matcher matcher, String description) {
String result = description;
if (result == null) {
if (matcher.groupCount() == 0) {
result = "\\0";
} else {
result = "\\1";
}
}
// Expand all groups: 1..Count, as well as 0 for the entire pattern
for (int i = matcher.groupCount(); i >= 0; i--) {
result = result.replace("\\" + i, matcher.group(i) == null ? "" : matcher.group(i));
}
return result;
}
private static String urlify(String text) {
try {
new URL(text);
return "<a href=\"%s\">%s</a>".formatted(text, text);
} catch (MalformedURLException e) {
return text;
}
}
}