Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WFLY-777 title #181

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@
import org.jboss.logging.Logger;
import org.kohsuke.github.GHEventPayload;
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.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.model.RuntimeConstants.DEPENDABOT;
import static io.xstefank.wildfly.bot.model.RuntimeConstants.FAILED_FORMAT_COMMENT;
import static io.xstefank.wildfly.bot.util.Strings.blockQuoted;

@RequestScoped
public class PullRequestFormatProcessor {
Expand Down Expand Up @@ -68,6 +76,8 @@ void pullRequestFormatCheck(
}
}

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

if (errors.isEmpty()) {
githubProcessor.commitStatusSuccess(pullRequest, CHECK_NAME, "Valid");
} else {
Expand Down Expand Up @@ -96,6 +106,81 @@ 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 List<Check> initializeChecks(WildFlyConfigFile wildflyConfigFile) {
List<Check> checks = new ArrayList<>();

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 @@ -14,6 +14,19 @@ public class RuntimeConstants {

public static final String LABEL_FIX_ME = "fix-me";

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]";

public static final String PROJECT_PATTERN_REGEX = "%s-\\d+";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public void setProjectKey(String key) {
projectPattern = Pattern.compile(String.format(PROJECT_PATTERN_REGEX, key), Pattern.DOTALL);
}

public Pattern getIssuePattern() {
return projectPattern;
}

public Pattern getProjectPattern() {
return projectPattern;
}
Expand Down
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
Loading