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

Always append link to the bot's repo in format check processor #193

Merged
merged 1 commit into from
Mar 25, 2024
Merged
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 @@ -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;
}
}
Loading