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 Jul 28, 2023
1 parent ffd0fce commit ff3fb9a
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
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;

@RequestScoped
Expand Down Expand Up @@ -65,6 +71,8 @@ void onPullRequestEdited(@PullRequest.Edited @PullRequest.Opened @PullRequest.Sy
}
}

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

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

}

/**
* Originally from https://github.com/hibernate/hibernate-github-bot
*/
private void generateAppendedMessage(GHPullRequest pullRequest, Pattern projectPattern) throws IOException {
Set<String> jiraIssues = collectJiraIssues(pullRequest, projectPattern);
if (jiraIssues.isEmpty()) {
LOG.debugf("No JIRA issues found for Pull Request [#%s]: \"%s\"", pullRequest.getNumber(), pullRequest.getTitle());
}

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

if (originalBody != null) {
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(RuntimeConstants.BOT_JIRA_LINKS_HEADER)
.append("\n");

for (String jira : jiraIssues) {
sb.append(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, jira, jira))
.append("\n");
}

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> collectJiraIssues(GHPullRequest pullRequest, Pattern jiraPattern) {
Set<String> jiras = retrieveJirasFromString(pullRequest.getTitle(), jiraPattern);
PagedIterable<GHPullRequestCommitDetail> commits = pullRequest.listCommits();
for (GHPullRequestCommitDetail commit : commits) {
String commitMessage = commit.getCommit().getMessage();
jiras.addAll(retrieveJirasFromString(commitMessage, jiraPattern));
}
return jiras;
}

private Set<String> retrieveJirasFromString(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
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ 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 BOT_MESSAGE_HEADER = "THIS SECTION IS AUTOMATICALLY GENERATED AND APPENDED BY WILDFLY GITHUB BOT. ANY MANUAL CHANGES WILL BE LOST.";

public static final String BOT_JIRA_LINKS_HEADER = "Wildfly Github Bot issue links:";

public static final String BOT_JIRA_LINK_TEMPLATE = "* [%s](https://issues.redhat.com/browse/%s)";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ 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 projectPattern = Pattern.compile(String.format("%s-\\d+", 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));
projectPattern = Pattern.compile(String.format("%s-\\d+", name));
}

public Pattern getProjectPattern() {
Expand Down
24 changes: 24 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,28 @@ public static boolean find(String pattern, String string) {
.find();
}

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

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

/**
* Allowed patterns are 2 following examples:
* WFLY-00000 ...
* [WFLY-00000 ...
*/
if (!string.startsWith(matcher.group()) &&
!string.startsWith("[" + matcher.group())) {
return false;
}

return true;
}

private Patterns() {
}}
Loading

0 comments on commit ff3fb9a

Please sign in to comment.