-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathghprcomment.java
More file actions
291 lines (252 loc) · 12.6 KB
/
ghprcomment.java
File metadata and controls
291 lines (252 loc) · 12.6 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21+
//DEPS org.kohsuke:github-api:2.0-rc.5
//DEPS one.util:streamex:0.8.4
//DEPS me.tongfei:progressbar:0.10.2
//DEPS org.jline:jline-terminal:4.0.0
//DEPS org.eclipse.collections:eclipse-collections:13.0.0
//DEPS info.picocli:picocli:4.7.7
//DEPS org.yaml:snakeyaml:2.6
//DEPS org.jooq:jool:0.9.15
//DEPS org.tinylog:tinylog-api:2.7.0
//DEPS org.tinylog:tinylog-impl:2.7.0
//DEPS org.tinylog:slf4j-tinylog:2.7.0 // because of jgit
//FILES tinylog.properties
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.SequencedCollection;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jooq.lambda.Unchecked;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHWorkflowJob;
import org.kohsuke.github.GHWorkflowRun;
import org.kohsuke.github.GitHub;
import org.tinylog.Logger;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Command(name = "ghprcomment",
version = "ghprcomment 2024-08-22",
mixinStandardHelpOptions = true,
sortSynopsis = false)
public class ghprcomment implements Callable<Integer> {
public static final int REPOSITORY_REFERENCE_ERROR = 1;
private static final int GH_PR_COMMENT_YAML_NOT_FOUND = 2;
private final String CONFIG_FILE_NAME = "ghprcomment";
private final String MAGIC_COMMENT = "<!-- created by ghprcomment -->";
private final Pattern MAGIC_COMMENT_V2 = Pattern.compile("<!-- ghprcomment (\\S+) -->");
private final Pattern MAGIC_COMMENT_V3 = Pattern.compile(
"<!-- ghprcomment\\n(?<workflowName>.+?)\\n(?<jobName>.+?)\\n-->"
);
private final List<Path> SEARCH_PATHS = List.of(Path.of("."), Path.of(".github"));
private final List<Path> CONFIG_PATHS = SEARCH_PATHS.stream()
.flatMap(path -> Stream.of(path.resolve(CONFIG_FILE_NAME + ".yaml"), path.resolve(CONFIG_FILE_NAME + ".yml")))
.toList();
@Option(names = {"-r", "--repository"}, description = "The GitHub repository in the form owner/repository. E.g., JabRef/jabref", required = true)
private String repository;
@Option(names = {"-w", "--workflow-run-id"}, required = true)
private Long workflowRunId;
// PR_NUMBER: ${{ github.event.number }}
@Option(names = {"-p", "--pr-number"}, required = true)
private Integer pullRequestNumber;
public static void main(String... args) {
CommandLine commandLine = new CommandLine(new ghprcomment());
commandLine.parseArgs(args);
int exitCode = commandLine.execute(args);
System.exit(exitCode);
}
@Override
public Integer call() throws Exception {
Optional<Path> configPath = CONFIG_PATHS.stream()
.filter(Files::exists)
.findFirst();
if (configPath.isEmpty()) {
Logger.error("{} not found. Searched at {}.", CONFIG_FILE_NAME, CONFIG_PATHS);
return GH_PR_COMMENT_YAML_NOT_FOUND;
}
Logger.info("Connecting to {}...", repository);
GitHub gitHub = GitHub.connect();
GHRepository gitHubRepository;
try {
gitHubRepository = gitHub.getRepository(repository);
// We fetch the pull request early to ensure that the number is valid
Logger.info("Pull Request number: {}", pullRequestNumber);
GHPullRequest pullRequest = gitHubRepository.getPullRequest(pullRequestNumber);
GHWorkflowRun workflowRun = gitHubRepository.getWorkflowRun(workflowRunId);
String runWorkflowName = workflowRun.getName();
Logger.info("workflowRunId: {} ({})", workflowRunId, runWorkflowName);
Set<String> failedJobs = workflowRun.listAllJobs().toList().stream()
.filter(job -> job.getConclusion() == GHWorkflowRun.Conclusion.FAILURE)
.map(GHWorkflowJob::getName)
.collect(Collectors.toSet());
Logger.info("Failed jobs: {}", failedJobs);
List<FailureComment> failureComments = getFailureComments(configPath.get(), runWorkflowName);
Logger.debug("# failure comments: {}", failureComments.size());
Logger.debug("Failure comments: {}",
failureComments.stream()
.map(fc -> fc.jobName)
.collect(Collectors.joining(", "))
);
Logger.trace("Failure comments: {}", failureComments);
Optional<FailureComment> commentToPost = failureComments.stream()
.filter(fc -> failedJobs.contains(fc.jobName))
.findFirst();
Logger.debug("Found comment: {}", commentToPost);
// Delete all previous comments
// And collect all already posted comments
Set<String> commentedFailedJobs = new HashSet<>();
pullRequest.getComments().forEach(Unchecked.consumer(comment -> {
String body = comment.getBody();
Logger.trace("Comment body: {}", body);
Matcher matcherV2 = MAGIC_COMMENT_V2.matcher(body);
Matcher matcherV3 = MAGIC_COMMENT_V3.matcher(body);
if (body.contains(MAGIC_COMMENT)) {
Logger.debug("Found V1 match - deleting {}", comment.getId());
comment.delete();
} else if (matcherV2.find()) {
Logger.debug("Found V1 match - deleting {}", comment.getId());
comment.delete();
} else if (matcherV3.find()) {
String workflowName = matcherV3.group("workflowName");
if (runWorkflowName.equals(workflowName)) {
String jobName = matcherV3.group("jobName");
Logger.debug("Found a match of workflow '{}' / job '{}'", workflowName, jobName);
if (failedJobs.contains(jobName)) {
Logger.debug("Still fails: {}", jobName);
boolean isCommentToPost = commentToPost.map(FailureComment::jobName)
.filter(jobName::equals)
.isPresent();
if (isCommentToPost) {
Logger.debug("Comment to post - deleting (to enable repost later)");
comment.delete();
} else {
Logger.debug("Comment already posted; and not most important to comment - keeping comment as is");
commentedFailedJobs.add(jobName);
}
} else {
Logger.debug("Found a match - job not failing any more - deleting {}", comment.getId());
comment.delete();
}
}
}
}));
Logger.debug("Commented failed jobs: {}", commentedFailedJobs);
if (failedJobs.isEmpty()) {
// Do nothing if all previous comments have been deleted - and no new comments need to be posted
Logger.info("No failed jobs found. Exiting.");
return 0;
}
SequencedCollection<FailureComment> commentsToPost = new LinkedHashSet<>();
commentToPost.ifPresent(commentsToPost::add);
commentToPost.ifPresent(fc -> Logger.debug("Comment to post for failed job: {}", fc.jobName));
// Add all "failed" always comments
failureComments.stream()
.filter(fc -> fc.always)
.filter(fc -> failedJobs.contains(fc.jobName))
.forEach(commentsToPost::add);
Optional<String> theLabel = getLabel(configPath.get());
Logger.debug("Label found", theLabel);
theLabel.ifPresent(Unchecked.consumer(label -> {
if (commentsToPost.isEmpty()) {
Logger.debug("Removing label.");
pullRequest.removeLabel(label);
} else {
Logger.debug("Adding label.");
pullRequest.addLabels(label);
}
}));
commentsToPost.forEach(Unchecked.consumer(fc -> {
if (!commentedFailedJobs.contains(fc.jobName)) {
// Post only if comment not already posted
postComment(fc.message, runWorkflowName, fc.jobName, pullRequest);
Logger.info("Posting comment for failed job {}", fc.jobName);
} else {
Logger.debug("Comment already posted for job {}", fc.jobName);
}
}));
} catch (IllegalArgumentException e) {
Logger.error("Error in repository reference {}", repository);
return REPOSITORY_REFERENCE_ERROR;
}
return 0;
}
private void postComment(String message, String workflowName, String jobName, GHPullRequest pullRequest) throws Exception {
Logger.debug("workflowName: {}, jobName: {}", workflowName, jobName);
if (message == null) {
Logger.error("Skipping empty message");
return;
}
Logger.trace("message: {}", message);
String body = String.format("%s\n\n<!-- ghprcomment\n%s\n%s\n-->", message, workflowName, jobName);
Logger.trace("Creating PR comment {}...", body);
pullRequest.comment(body);
}
private Optional<String> getLabel(Path yamlFile) throws IOException {
LoaderOptions options = new LoaderOptions();
Yaml yaml = new Yaml(options);
List<Map<String, String>> failureComments;
try (InputStream inputStream = Files.newInputStream(yamlFile)) {
failureComments = yaml.load(inputStream);
}
return failureComments
.stream()
.filter(comment -> comment.get("label") != null)
.map(comment -> comment.get("label"))
.findAny();
}
private static List<FailureComment> getFailureComments(Path yamlFile, String workflowName) throws IOException {
LoaderOptions options = new LoaderOptions();
Yaml yaml = new Yaml(options);
// SnakeYAML 2.2 cannot handle records (returns a LinkedList instead of record)
// We do the conversion "by hand"
List<Map<String, String>> failureComments;
try (InputStream inputStream = Files.newInputStream(yamlFile)) {
failureComments = yaml.load(inputStream);
}
Logger.trace("failureComments {}", failureComments);
List<FailureComment> result = failureComments
.stream()
.filter(comment -> comment.get("jobName") != null)
.filter(comment -> {
String jobsWorkflow = comment.get("workflowName");
return jobsWorkflow == null || workflowName.equals(jobsWorkflow);
})
.map(map -> {
boolean alwaysValue = false;
Object always = map.get("always");
if (always == null) {
alwaysValue = false;
} else if (always instanceof Boolean theValue) {
alwaysValue = theValue;
} else if (always instanceof String theValue) {
alwaysValue = Boolean.parseBoolean(theValue);
} else {
Logger.error("Unknown always value: {}", always);
}
return new FailureComment(map.get("jobName"), map.get("message"), alwaysValue);
}).toList();
Logger.trace("result {}", result);
return result;
}
record FailureComment(
String jobName,
String message,
boolean always) {
}
}