Skip to content

Commit

Permalink
Always append link to the bot's repo in format check processor
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Huginn committed Mar 25, 2024
1 parent 4e71e9a commit 1f0aac8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static io.xstefank.wildfly.bot.model.RuntimeConstants.BOT_REPO_REF_FOOTER;
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;
Expand Down Expand Up @@ -93,6 +94,9 @@ void pullRequestFormatCheck(
githubProcessor.commitStatusError(pullRequest, CHECK_NAME, "Failed checks: " + String.join(", ", errors.keySet()));
}
githubProcessor.formatComment(pullRequest, FAILED_FORMAT_COMMENT, errors.values());

String updatedBody = pullRequest.getBody() + BOT_REPO_REF_FOOTER.formatted(wildFlyBotConfig.githubName());
pullRequest.setBody(updatedBody);
}

void postDependabotInfo(@PullRequest.Opened GHEventPayload.PullRequest pullRequestPayload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class RuntimeConstants {
public static final String BOT_MESSAGE_FOOTER = """
\n<--- END OF WILDFLY GITHUB BOT REPORT --->""";

public static final String BOT_REPO_REF_FOOTER = """
\n\nMore information about the [%s](https://github.com/wildfly/wildfly-github-bot)""";

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";
Expand Down
36 changes: 32 additions & 4 deletions src/test/java/io/xstefank/wildfly/bot/PRAppendingMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

import io.quarkiverse.githubapp.testing.GitHubAppTest;
import io.quarkus.test.junit.QuarkusTest;
import io.xstefank.wildfly.bot.config.WildFlyBotConfig;
import io.xstefank.wildfly.bot.model.RuntimeConstants;
import io.xstefank.wildfly.bot.utils.Action;
import io.xstefank.wildfly.bot.utils.MockedGHPullRequest;
import io.xstefank.wildfly.bot.utils.PullRequestJson;
import io.xstefank.wildfly.bot.utils.TestConstants;
import io.xstefank.wildfly.bot.utils.Util;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.kohsuke.github.GHEvent;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

import java.io.IOException;

import static io.quarkiverse.githubapp.testing.GitHubAppTesting.given;
import static io.xstefank.wildfly.bot.model.RuntimeConstants.BOT_JIRA_LINK_COMMENT_TEMPLATE;
import static io.xstefank.wildfly.bot.model.RuntimeConstants.BOT_REPO_REF_FOOTER;
import static io.xstefank.wildfly.bot.util.Strings.blockQuoted;

@QuarkusTest
Expand All @@ -35,6 +38,9 @@ public class PRAppendingMessageTest {
%s%%s%s""", RuntimeConstants.BOT_MESSAGE_HEADER, blockQuoted(RuntimeConstants.BOT_JIRA_LINKS_HEADER),
RuntimeConstants.BOT_MESSAGE_FOOTER);

@Inject
WildFlyBotConfig wildFlyBotConfig;

PullRequestJson pullRequestJson;

MockedGHPullRequest mockedContext;
Expand All @@ -56,7 +62,6 @@ public void testEmptyBodyAppendMessage() throws IOException {
sb.append(blockQuoted(
String.format(RuntimeConstants.BOT_JIRA_LINK_COMMENT_TEMPLATE, "WFLY-00000")))));
});

}

@Test
Expand Down Expand Up @@ -244,8 +249,31 @@ public void testBodyContainingAllJirasAppendMessageMultipleDifferentLinks() thro
.commit("WFLY-00003 commit");
given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext))
.when().payloadFromString(pullRequestJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> {
Mockito.verify(mocks.pullRequest(pullRequestJson.id()), Mockito.times(0))
.setBody(ArgumentMatchers.anyString());
Mockito.verify(mocks.pullRequest(pullRequestJson.id()))
.setBody(body + BOT_REPO_REF_FOOTER.formatted(wildFlyBotConfig.githubName()));
});
}

@Test
public void testRepoRefFooterAppendedMessage() throws IOException {
pullRequestJson = PullRequestJson.builder(TestConstants.VALID_PR_TEMPLATE_JSON)
.action(Action.EDITED)
.title("WFLY-00000 title")
.description(null)
.build();

String jiraLinkDescription = String.format(appendedMessage,
blockQuoted(BOT_JIRA_LINK_COMMENT_TEMPLATE.formatted("WFLY-00000")));
// even as the description is set, it's after the start, thus we need to mock it's content to match
mockedContext = MockedGHPullRequest.builder(pullRequestJson.id())
.describtion(jiraLinkDescription)
.commit("WFLY-00000 commit");
given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext))
.when()
.payloadFromString(pullRequestJson.jsonString()).event(GHEvent.PULL_REQUEST)
.then().github(mocks -> {
String repoRef = jiraLinkDescription + BOT_REPO_REF_FOOTER.formatted(wildFlyBotConfig.githubName());
Mockito.verify(mocks.pullRequest(pullRequestJson.id())).setBody(repoRef);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class MockedGHPullRequest extends Mockable {

private final long pullRequest;
private Set<String> prFiles = new LinkedHashSet<>();
private String description;
private final List<Tuple2<String, String>> comments = new ArrayList<>();
private Set<String> reviewers = new LinkedHashSet<>();
private Set<String> prLabels = new LinkedHashSet<>();
Expand All @@ -52,6 +53,11 @@ public MockedGHPullRequest comment(String comment, String author) {
return this;
}

public MockedGHPullRequest describtion(String description) {
this.description = description;
return this;
}

public MockedGHPullRequest commit(MockedCommit commit) {
this.commits.add(commit);
return this;
Expand Down Expand Up @@ -148,6 +154,10 @@ public AtomicLong mock(GitHubMockContext mocks, AtomicLong idGenerator) throws I

Mockito.when(pullRequest.isDraft()).thenReturn(isDraft);

if (description != null) {
Mockito.when(pullRequest.getBody()).thenReturn(description);
}

return idGenerator;
}
}

0 comments on commit 1f0aac8

Please sign in to comment.