forked from wildfly/wildfly-github-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommitMessagesCheck.java
More file actions
79 lines (67 loc) · 2.66 KB
/
CommitMessagesCheck.java
File metadata and controls
79 lines (67 loc) · 2.66 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
package org.wildfly.bot.format;
import org.wildfly.bot.model.RegexDefinition;
import org.wildfly.bot.util.Patterns;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHPullRequestCommitDetail;
import org.kohsuke.github.PagedIterable;
import java.util.regex.Pattern;
import java.io.IOException;
import static org.wildfly.bot.model.RuntimeConstants.DEPENDABOT;
public class CommitMessagesCheck implements Check {
private static final int MAX_COMMIT_MESSAGE_LENGTH = 75;
private static final String ELLIPSIS = "...";
private final Pattern pattern;
private final String message;
public CommitMessagesCheck(RegexDefinition description) {
if (description.pattern == null) {
throw new IllegalArgumentException("Input argument cannot be null");
}
pattern = description.pattern;
message = description.message;
}
@Override
public String check(GHPullRequest pullRequest) throws IOException {
if (pullRequest.getUser().getLogin().equals(DEPENDABOT)) {
// Skip for Dependabot for now
return null;
}
PagedIterable<GHPullRequestCommitDetail> commits = pullRequest.listCommits();
if (commits != null) {
boolean oneMatched = false;
for (GHPullRequestCommitDetail commit : commits) {
if (commit.getCommit() != null) {
String commitMessage = commit.getCommit().getMessage();
if (commitMessage.isEmpty()) {
return commit.getSha() + ": Commit message is Empty";
}
if (Patterns.matches(pattern, commitMessage)) {
oneMatched = true;
break;
}
}
}
if (!oneMatched) {
return formatMessageWithDetailsIfNeeded(String.format(this.message, pattern.pattern()));
}
}
return null;
}
@Override
public String getName() {
return "commit";
}
private String formatMessageWithDetailsIfNeeded(String message) {
if (message.length() > MAX_COMMIT_MESSAGE_LENGTH) {
return new StringBuilder()
.append("<details>")
.append("<summary>")
.append(message.substring(0, MAX_COMMIT_MESSAGE_LENGTH - ELLIPSIS.length()))
.append(ELLIPSIS)
.append("</summary>")
.append(message.substring(MAX_COMMIT_MESSAGE_LENGTH - ELLIPSIS.length()))
.append("</details>")
.toString();
}
return message;
}
}