Skip to content

Commit

Permalink
Automatically generate JIRA issue links to Pull Request description b…
Browse files Browse the repository at this point in the history
…ody.
  • Loading branch information
The-Huginn committed Aug 15, 2023
1 parent 2349384 commit f4c28ea
Show file tree
Hide file tree
Showing 18 changed files with 668 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHIssueComment;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHPullRequestCommitDetail;
import org.kohsuke.github.PagedIterable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static io.xstefank.wildfly.bot.util.Strings.blockQuoted;
import static io.xstefank.wildfly.bot.model.RuntimeConstants.DEPENDABOT;

@RequestScoped
Expand Down Expand Up @@ -67,6 +74,8 @@ void verifyFormat(@PullRequest.Edited @PullRequest.Opened @PullRequest.Synchroni
}
}

generateAppendedMessage(pullRequest, wildflyConfigFile.wildfly.getIssuePattern());

if (errors.isEmpty()) {
githubCommitProcessor.commitStatusSuccess(pullRequest, CHECK_NAME, "Valid");
deleteFormatComment(pullRequest);
Expand All @@ -83,7 +92,7 @@ void postDependabotInfo(@PullRequest.Opened GHEventPayload.PullRequest pullReque
if (pullRequest.getUser().getLogin().equals(DEPENDABOT)) {
LOG.infof("Dependabot Pull Request [#%d] detected.", pullRequest.getNumber());
String comment = ("WildFly Bot recognized this PR as dependabot dependency update. Please create a %s issue" +
" and add its ID to the title and its link to the description.").formatted(wildflyConfigFile.wildfly.projectKey);
" and add its ID to the title and its link to the description.").formatted(wildflyConfigFile.wildfly.projectKey);
if (wildFlyBotConfig.isDryRun()) {
LOG.infof("Pull request #%d - Add new comment %s", pullRequest.getNumber(), comment);
} else {
Expand All @@ -93,6 +102,80 @@ void postDependabotInfo(@PullRequest.Opened GHEventPayload.PullRequest pullReque

}

private void generateAppendedMessage(GHPullRequest pullRequest, Pattern projectPattern) throws IOException {
Set<String> jiraIssues = parseJiraIssues(pullRequest, projectPattern);
if (jiraIssues.isEmpty()) {
LOG.debugf("No JIRA issues found for Pull Request [#%s]: \"%s\"", pullRequest.getNumber(), pullRequest.getTitle());
}

String originalBody = pullRequest.getBody();
final StringBuilder sb = new StringBuilder();

if (originalBody != null) {
final String trimmedOriginalBody = originalBody.replaceAll("\\r", "");
final int startIndex = trimmedOriginalBody.indexOf(RuntimeConstants.BOT_MESSAGE_HEADER);
sb.append(trimmedOriginalBody.substring(0, startIndex > -1 ? startIndex : trimmedOriginalBody.length()).trim())
.append("\n\n");

// we create links, search for ones contained in the original body
Set<String> jiraLinks = jiraIssues.stream()
.map(s -> String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, s))
.collect(Collectors.toSet());

// collect back jira issues from links, which are missing in the description
jiraIssues = jiraLinks.stream()
.filter(s -> !trimmedOriginalBody.contains(s))
.map(s -> {
Matcher matcher = projectPattern.matcher(s);
matcher.find();
return matcher.group();
})
.collect(Collectors.toSet());
}

if (jiraIssues.isEmpty()) {
return;
}

sb.append(RuntimeConstants.BOT_MESSAGE_HEADER)
.append("\n\n")
.append(blockQuoted(RuntimeConstants.BOT_JIRA_LINKS_HEADER));

for (String jira : jiraIssues) {

sb.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_COMMENT_TEMPLATE, jira)));
}

sb.append(RuntimeConstants.BOT_MESSAGE_FOOTER);
String newBody = sb.toString();

if (wildFlyBotConfig.isDryRun()) {
LOG.infof("Pull Request #%s - Updated PR body:\n%s", newBody);
} else {
pullRequest.setBody(newBody);
}
}

private Set<String> parseJiraIssues(GHPullRequest pullRequest, Pattern jiraPattern) {
Set<String> jiras = parseJirasFromString(pullRequest.getTitle(), jiraPattern);
PagedIterable<GHPullRequestCommitDetail> commits = pullRequest.listCommits();
for (GHPullRequestCommitDetail commit : commits) {
String commitMessage = commit.getCommit().getMessage();
jiras.addAll(parseJirasFromString(commitMessage, jiraPattern));
}
return jiras;
}

private Set<String> parseJirasFromString(String fromString, Pattern jiraPattern) {
Set<String> jiras = new TreeSet<>();
Matcher matcher = jiraPattern.matcher(fromString);
while (matcher.find()) {
jiras.add(matcher.group());
}

return jiras;
}

private void deleteFormatComment(GHPullRequest pullRequest) throws IOException {
formatComment(pullRequest, null);
}
Expand Down Expand Up @@ -147,7 +230,7 @@ private List<Check> initializeChecks(WildFlyConfigFile wildflyConfigFile) {
}

if (wildflyConfigFile.wildfly.format.title.enabled) {
checks.add(new TitleCheck(new RegexDefinition(wildflyConfigFile.wildfly.getProjectPatternAllowingPrefix(),
checks.add(new TitleCheck(new RegexDefinition(wildflyConfigFile.wildfly.getProjectPattern(),
wildflyConfigFile.wildfly.format.title.message)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package io.xstefank.wildfly.bot.format;

import io.xstefank.wildfly.bot.model.RegexDefinition;
import io.xstefank.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.util.regex.Matcher;
import java.io.IOException;

import static io.xstefank.wildfly.bot.model.RuntimeConstants.DEFAULT_COMMIT_MESSAGE;
import static io.xstefank.wildfly.bot.model.RuntimeConstants.DEPENDABOT;

public class CommitMessagesCheck implements Check {

private Pattern pattern;
private String message;
private final Pattern pattern;
private final String message;

public CommitMessagesCheck(RegexDefinition description) {
if (description.pattern == null) {
Expand All @@ -41,18 +41,17 @@ public String check(GHPullRequest pullRequest) throws IOException {
return commit.getSha() + ": Commit message is Empty";
}

Matcher matcher = pattern.matcher(commitMessage);

if (matcher.matches()) {
if (Patterns.matches(pattern, commitMessage)) {
oneMatched = true;
break;
}
}
}
if (!oneMatched) {
return String.format(this.message, pattern.pattern());
}
}
}

return null;
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/io/xstefank/wildfly/bot/format/TitleCheck.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package io.xstefank.wildfly.bot.format;

import io.xstefank.wildfly.bot.model.RegexDefinition;
import io.xstefank.wildfly.bot.util.Patterns;
import org.kohsuke.github.GHPullRequest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.xstefank.wildfly.bot.model.RuntimeConstants.DEFAULT_TITLE_MESSAGE;

public class TitleCheck implements Check {

private Pattern pattern;
private String message;
private final Pattern pattern;
private final String message;

public TitleCheck(RegexDefinition title) {
if (title.pattern == null) {
Expand All @@ -23,9 +23,8 @@ public TitleCheck(RegexDefinition title) {

@Override
public String check(GHPullRequest pullRequest) {
Matcher matcher = pattern.matcher(pullRequest.getTitle());
if (!matcher.matches()) {
return String.format(message, pattern.pattern());
if (!Patterns.matches(pattern, pullRequest.getTitle())) {
return message.formatted(pattern.pattern());
}

return null;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/xstefank/wildfly/bot/model/RuntimeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,18 @@ public class RuntimeConstants {

public static final String DEFAULT_PROJECT_KEY = "WFLY";

public static final String BOT_MESSAGE_HEADER = """
____
<--- THIS SECTION IS AUTOMATICALLY GENERATED BY WILDFLY GITHUB BOT. ANY MANUAL CHANGES WILL BE LOST. --->""";

public static final String BOT_MESSAGE_FOOTER = """
\n<--- END OF WILDFLY GITHUB BOT REPORT --->""";

public static final String BOT_JIRA_LINKS_HEADER = "Wildfly issue links:\n";

public static final String BOT_JIRA_LINK_TEMPLATE = "https://issues.redhat.com/browse/%1$s";

public static final String BOT_JIRA_LINK_COMMENT_TEMPLATE = String.format("* [%%1$s](%s)\n", BOT_JIRA_LINK_TEMPLATE);

public static final String DEPENDABOT = "dependabot[bot]";
}
24 changes: 11 additions & 13 deletions src/main/java/io/xstefank/wildfly/bot/model/WildFlyConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@

public class WildFlyConfigFile {

public static final String PROJECT_PATTERN_REGEX = "\\[%s-\\d+]\\s+.*|%s-\\d+\\s+.*";
public static final String PROJECT_PATTERN_REGEX_PREFIXED = ".*\\[%s-\\d+]\\s+.*|.*%s-\\d+\\s+.*";
public static final String ISSUE_PATTERN_REGEX = "%s-\\d+";

public static final String PROJECT_PATTERN_REGEX = "%1$s-\\d+(,%1$s-\\d+)*";

public WildFlyConfig wildfly;

public static final class WildFlyConfig {

private Pattern projectPattern = Pattern.compile(String.format(PROJECT_PATTERN_REGEX,
DEFAULT_PROJECT_KEY, DEFAULT_PROJECT_KEY), Pattern.DOTALL);

private Pattern projectPatternPrefixed = Pattern.compile(String.format(PROJECT_PATTERN_REGEX_PREFIXED,
DEFAULT_PROJECT_KEY, DEFAULT_PROJECT_KEY), Pattern.DOTALL);
private Pattern issuePattern = Pattern.compile(ISSUE_PATTERN_REGEX.formatted(DEFAULT_PROJECT_KEY), Pattern.DOTALL);
private Pattern projectPattern = Pattern.compile(PROJECT_PATTERN_REGEX.formatted(DEFAULT_PROJECT_KEY), Pattern.DOTALL);

public List<WildFlyRule> rules;

Expand All @@ -34,16 +32,16 @@ public static final class WildFlyConfig {

public void setProjectKey(String key) {
projectKey = key;
projectPattern = Pattern.compile(String.format(PROJECT_PATTERN_REGEX, key, key), Pattern.DOTALL);
projectPatternPrefixed = Pattern.compile(String.format(PROJECT_PATTERN_REGEX_PREFIXED, key, key), Pattern.DOTALL);
issuePattern = Pattern.compile(String.format(ISSUE_PATTERN_REGEX, key));
projectPattern = Pattern.compile(String.format(PROJECT_PATTERN_REGEX, key));
}

public Pattern getProjectPattern() {
return projectPattern;
public Pattern getIssuePattern() {
return issuePattern;
}

public Pattern getProjectPatternAllowingPrefix() {
return projectPatternPrefixed;
public Pattern getProjectPattern() {
return projectPattern;
}

public List<String> emails;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/xstefank/wildfly/bot/util/Patterns.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ public static boolean find(String pattern, String string) {
.find();
}

public static boolean matches(Pattern pattern, String string) {
if (Strings.isBlank(string)) {
return false;
}

return pattern.matcher(string).find();
}

private Patterns() {
}}
4 changes: 4 additions & 0 deletions src/main/java/io/xstefank/wildfly/bot/util/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public static boolean isBlank(String string) {
return string == null || string.trim().isEmpty();
}

public static String blockQuoted(String line) {
return "> " + line;
}

private Strings() {
}
}
Loading

0 comments on commit f4c28ea

Please sign in to comment.