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 2, 2023
1 parent ffd0fce commit c6880f5
Show file tree
Hide file tree
Showing 17 changed files with 551 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,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 @@ -65,6 +73,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 @@ -75,6 +85,60 @@ 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) {
originalBody = originalBody.replaceAll("\\r", "");
final int startIndex = originalBody.indexOf(RuntimeConstants.BOT_MESSAGE_HEADER);
sb.append(originalBody.substring(0, startIndex > -1 ? startIndex : originalBody.length()).trim())
.append("\n\n");
}

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_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 @@ -128,11 +192,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
15 changes: 15 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,19 @@ 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 = "* [%1$s](https://issues.redhat.com/browse/%1$s)\n";
}
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() {
}
}
149 changes: 149 additions & 0 deletions src/test/java/io/xstefank/wildfly/bot/PRAppendingMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package io.xstefank.wildfly.bot;

import io.quarkiverse.githubapp.testing.GitHubAppTest;
import io.quarkus.test.junit.QuarkusTest;
import io.xstefank.wildfly.bot.model.RuntimeConstants;
import io.xstefank.wildfly.bot.utils.Action;
import io.xstefank.wildfly.bot.utils.GitHubJson;
import org.junit.jupiter.api.Test;
import org.kohsuke.github.GHEvent;
import org.mockito.Mockito;

import java.io.IOException;

import static io.quarkiverse.githubapp.testing.GitHubAppTesting.given;
import static io.xstefank.wildfly.bot.helper.MockedGHPullRequestProcessor.mockCommits;
import static io.xstefank.wildfly.bot.helper.MockedGHPullRequestProcessor.mockEmptyComments;
import static io.xstefank.wildfly.bot.helper.MockedGHPullRequestProcessor.mockEmptyFileDetails;
import static io.xstefank.wildfly.bot.helper.MockedGHPullRequestProcessor.processPullRequestMock;
import static io.xstefank.wildfly.bot.util.Strings.blockQuoted;

@QuarkusTest
@GitHubAppTest
public class PRAppendingMessage {

private String wildflyConfigFile = """
wildfly:
format:
commit:
enabled: false
""";

private String appendedMessage = String.format("""
%s
%s%%s%s""", RuntimeConstants.BOT_MESSAGE_HEADER, blockQuoted(RuntimeConstants.BOT_JIRA_LINKS_HEADER), RuntimeConstants.BOT_MESSAGE_FOOTER);

@Test
public void testEmptyBodyAppendMessage() throws IOException {
GitHubJson githubJson = GitHubJson.builder("pr-template.json").action(Action.EDITED).title("WFLY-00000 title").body(null).build();

given().github(mocks -> {
mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(wildflyConfigFile);
processPullRequestMock(mocks.pullRequest(githubJson.pullRequest()), mockEmptyFileDetails(), mockEmptyComments(), mockCommits("WFLY-00000 commit"));
}).when().payloadFromString(githubJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> {
StringBuilder sb = new StringBuilder();
Mockito.verify(mocks.pullRequest(githubJson.pullRequest())).setBody(String.format(appendedMessage,
sb.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00000")).toString())));
});

}

@Test
public void testNonEmptyBodyAppendMessage() throws IOException {
String body = """
This is my
testing
body, which
should not be
cleared.""";
GitHubJson githubJson = GitHubJson.builder("pr-template.json").action(Action.EDITED).title("WFLY-00000 title").body(body).build();

given().github(mocks -> {
mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(wildflyConfigFile);
processPullRequestMock(mocks.pullRequest(githubJson.pullRequest()), mockEmptyFileDetails(), mockEmptyComments(), mockCommits("WFLY-00000 commit"));
}).when().payloadFromString(githubJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> {
StringBuilder sb = new StringBuilder();
Mockito.verify(mocks.pullRequest(githubJson.pullRequest())).setBody(String.format(body + "\n\n" + appendedMessage,
sb.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00000")).toString())));
});
}

@Test
public void testEmptyBodyAppendMessageMultipleLinks() throws IOException {
GitHubJson githubJson = GitHubJson.builder("pr-template.json").action(Action.EDITED).title("WFLY-00001 title").body(null).build();

given().github(mocks -> {
mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(wildflyConfigFile);
processPullRequestMock(mocks.pullRequest(githubJson.pullRequest()), mockEmptyFileDetails(), mockEmptyComments(), mockCommits("WFLY-00002 commit"));
}).when().payloadFromString(githubJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> {
StringBuilder sb = new StringBuilder();
Mockito.verify(mocks.pullRequest(githubJson.pullRequest())).setBody(String.format(appendedMessage,
sb.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00001")))
.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00002")).toString())));
});
}

@Test
public void testNonEmptyBodyAppendMessageMultipleLinks() throws IOException {
String body = """
This is my
testing
body, which
should not be
cleared.""";
GitHubJson githubJson = GitHubJson.builder("pr-template.json").action(Action.EDITED).title("WFLY-00001 title").body(body).build();

given().github(mocks -> {
mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(wildflyConfigFile);
processPullRequestMock(mocks.pullRequest(githubJson.pullRequest()), mockEmptyFileDetails(), mockEmptyComments(), mockCommits("WFLY-00002 commit"));
}).when().payloadFromString(githubJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> {
StringBuilder sb = new StringBuilder();
Mockito.verify(mocks.pullRequest(githubJson.pullRequest())).setBody(String.format(body + "\n\n" + appendedMessage,
sb.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00001")))
.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00002")).toString())));
});
}

@Test
public void testEmptyBodyAppendMessageMultipleDifferentLinks() throws IOException {
GitHubJson githubJson = GitHubJson.builder("pr-template.json").action(Action.EDITED).title("WFLY-00001, WFLY-00002 title").body(null).build();

given().github(mocks -> {
mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(wildflyConfigFile);
processPullRequestMock(mocks.pullRequest(githubJson.pullRequest()), mockEmptyFileDetails(), mockEmptyComments(), mockCommits("WFLY-00002 commit", "WFLY-00003 commit"));
}).when().payloadFromString(githubJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> {
StringBuilder sb = new StringBuilder();
Mockito.verify(mocks.pullRequest(githubJson.pullRequest())).setBody(String.format(appendedMessage,
sb.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00001")))
.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00002")))
.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00003")).toString())));
});
}


@Test
public void testNonEmptyBodyAppendMessageMultipleDifferentLinks() throws IOException {
String body = """
This is my
testing
body, which
should not be
cleared.""";
GitHubJson githubJson = GitHubJson.builder("pr-template.json").action(Action.EDITED).title("WFLY-00001, WFLY-00002 title").body(body).build();

given().github(mocks -> {
mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(wildflyConfigFile);
processPullRequestMock(mocks.pullRequest(githubJson.pullRequest()), mockEmptyFileDetails(), mockEmptyComments(), mockCommits("WFLY-00002 commit", "WFLY-00003 commit"));
}).when().payloadFromString(githubJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> {
StringBuilder sb = new StringBuilder();
Mockito.verify(mocks.pullRequest(githubJson.pullRequest())).setBody(String.format(body + "\n\n" + appendedMessage,
sb.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00001")))
.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00002")))
.append(blockQuoted(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, "WFLY-00003")).toString())));
});
}
}
Loading

0 comments on commit c6880f5

Please sign in to comment.