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 3, 2023
1 parent d679fb2 commit c32e4b0
Show file tree
Hide file tree
Showing 17 changed files with 626 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@
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;

@RequestScoped
public class PullRequestFormatProcessor {

Expand Down Expand Up @@ -64,6 +72,8 @@ void onPullRequestEdited(@PullRequest.Edited @PullRequest.Opened @PullRequest.Sy
}
}

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

if (errors.isEmpty()) {
githubCommitProcessor.commitStatusSuccess(pullRequest, CHECK_NAME, "Valid");
deleteFormatComment(pullRequest);
Expand All @@ -74,6 +84,80 @@ void onPullRequestEdited(@PullRequest.Edited @PullRequest.Opened @PullRequest.Sy

}

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 @@ -127,11 +211,11 @@ private List<Check> initializeChecks(WildFlyConfigFile wildflyConfigFile) {
}

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

if (wildflyConfigFile.wildfly.format.commit.enabled) {
checks.add(new CommitMessagesCheck(new RegexDefinition(wildflyConfigFile.wildfly.getProjectPattern(), wildflyConfigFile.wildfly.format.commit.message)));
checks.add(new CommitMessagesCheck(new RegexDefinition(wildflyConfigFile.wildfly.getCheckPattern(), wildflyConfigFile.wildfly.format.commit.message)));
}

if (wildflyConfigFile.wildfly.format.description != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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;

public class CommitMessagesCheck implements Check {
Expand All @@ -31,9 +31,7 @@ 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)) {
return String.format("For commit: \"%s\" (%s) - %s" , commitMessage, commit.getSha(), this.message);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/xstefank/wildfly/bot/format/TitleCheck.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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;

public class TitleCheck implements Check {
Expand All @@ -21,8 +21,7 @@ public TitleCheck(RegexDefinition title) {

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

Expand Down
17 changes: 17 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 @@ -9,4 +9,21 @@ public class RuntimeConstants {
public static final String DEFAULT_TITLE_MESSAGE = "Wrong content of the title";

public static final String DEFAULT_PROJECT_KEY = "WFLY";

public static final String ISSUE_PATTERN = "%s-\\d+";

public static final String CHECK_PATTERN = "\\[%1$s-\\d+(,%1$s-\\d+)*\\]|%1$s-\\d+(,%1$s-\\d+)*";

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);
}
14 changes: 10 additions & 4 deletions src/main/java/io/xstefank/wildfly/bot/model/WildFlyConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ public class WildFlyConfigFile {

public static final class WildFlyConfig {

private Pattern projectPattern = Pattern.compile(String.format("\\[%s-\\d+]\\s+.*|%s-\\d+\\s+.*", RuntimeConstants.DEFAULT_PROJECT_KEY, RuntimeConstants.DEFAULT_PROJECT_KEY));
private Pattern issuePattern = Pattern.compile(String.format(RuntimeConstants.ISSUE_PATTERN, RuntimeConstants.DEFAULT_PROJECT_KEY));
private Pattern checkPattern = Pattern.compile(String.format(RuntimeConstants.CHECK_PATTERN, RuntimeConstants.DEFAULT_PROJECT_KEY));

public List<WildFlyRule> rules;

public Format format = new Format();

public void setProjectKey(String name) {
projectPattern = Pattern.compile(String.format("\\[%s-\\d+]\\s+.*|%s-\\d+\\s+.*", name, name));
issuePattern = Pattern.compile(String.format(RuntimeConstants.ISSUE_PATTERN, name));
checkPattern = Pattern.compile(String.format(RuntimeConstants.CHECK_PATTERN, name));
}

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

public Pattern getCheckPattern() {
return checkPattern;
}

public List<String> emails;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/xstefank/wildfly/bot/util/Patterns.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.xstefank.wildfly.bot.util;

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

/**
Expand All @@ -19,5 +20,20 @@ public static boolean find(String pattern, String string) {
.find();
}

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

string = string.replaceAll("\\s", "");

Matcher matcher = pattern.matcher(string);
if (!matcher.find()) {
return false;
}

return string.startsWith(matcher.group()) && !matcher.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 c32e4b0

Please sign in to comment.