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

Automatically generate JIRA issue links to Pull Request description #108

Merged
merged 1 commit into from
Jan 26, 2024

Conversation

The-Huginn
Copy link
Collaborator

@The-Huginn The-Huginn commented Jul 28, 2023

Example can be found here: The-Huginn#19

@The-Huginn
Copy link
Collaborator Author

Please review @xstefank

.append("\n");

for (String jira : jiraIssues) {
sb.appendQuote(String.format(RuntimeConstants.BOT_JIRA_LINK_TEMPLATE, jira, jira))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\n can go into format template

}
}

private Set<String> collectJiraIssues(GHPullRequest pullRequest, Pattern jiraPattern) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend parseJiraIssues

}

private Set<String> collectJiraIssues(GHPullRequest pullRequest, Pattern jiraPattern) {
Set<String> jiras = retrieveJirasFromString(pullRequest.getTitle(), jiraPattern);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly parse

@@ -0,0 +1,23 @@
package io.xstefank.wildfly.bot.util;

public class CustomStringBuilder {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can find a better name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wonder if the appendQuote is worth a new class

@@ -298,7 +296,6 @@ void testDirectoriesMentionsNewFileInDiff() throws IOException {
Mockito.verify(mockedPR, Mockito.times(2)).listFiles();
Mockito.verify(mockedPR).comment("/cc @7125767235");
Mockito.verify(mockedPR, Mockito.times(1)).listComments();
Mockito.verifyNoMoreInteractions(mockedPR);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to list individual invocations since we test them all here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would be quite a lot of them and it is possible, that it will grow in the future as well. I think at some point point we will no longer know, where all those invocations were called and adjust the tests to failures.

GHPullRequestCommitDetail.Commit mockCommit = Mockito.mock(GHPullRequestCommitDetail.Commit.class);
Mockito.when(mockCommitDetail.getCommit()).thenReturn(mockCommit);
for (String commitMessage : commitMessages) {
Mockito.when(mockCommit.getMessage()).thenReturn(commitMessage);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this overriding one single commit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think so. At least in chained calls such as Mockito.when(...).thenReturn("1").thenReturn("2") will return at first 1 and on second invocation 2.

Copy link
Collaborator Author

@The-Huginn The-Huginn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushing latest updates

@@ -298,7 +296,6 @@ void testDirectoriesMentionsNewFileInDiff() throws IOException {
Mockito.verify(mockedPR, Mockito.times(2)).listFiles();
Mockito.verify(mockedPR).comment("/cc @7125767235");
Mockito.verify(mockedPR, Mockito.times(1)).listComments();
Mockito.verifyNoMoreInteractions(mockedPR);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would be quite a lot of them and it is possible, that it will grow in the future as well. I think at some point point we will no longer know, where all those invocations were called and adjust the tests to failures.

GHPullRequestCommitDetail.Commit mockCommit = Mockito.mock(GHPullRequestCommitDetail.Commit.class);
Mockito.when(mockCommitDetail.getCommit()).thenReturn(mockCommit);
for (String commitMessage : commitMessages) {
Mockito.when(mockCommit.getMessage()).thenReturn(commitMessage);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think so. At least in chained calls such as Mockito.when(...).thenReturn("1").thenReturn("2") will return at first 1 and on second invocation 2.

@@ -75,6 +84,63 @@ void onPullRequestEdited(@PullRequest.Edited @PullRequest.Opened @PullRequest.Sy

}

/**
* Originally from https://github.com/hibernate/hibernate-github-bot
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can probably drop this as it doesn't add value


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("%s-\\d+", name));
checkPattern = Pattern.compile(String.format("(\\[%1$s-\\d+(,%1$s-\\d+)*\\]|%1$s-\\d+(,%1$s-\\d+)*)(?!%1$s-\\d+)", name));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regexes should be also constants if they are the same, easy to make mistakes

return this;
}

public DescriptionStringBuilder appendQuote(String message) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a Quote and I still don't understand why we need a separate class for this override

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will rename it to appendBlockQuote, as that is what it is called in markdown. which we are using it's syntax for writing comments / editing descriptions. Also it might be easier for future expansion. Currently it is also used in tests and makes the, more readable.


private String appendedMessage = String.format("""
%s\n
> %s%s""" + RuntimeConstants.BOT_MESSAGE_FOOTER, RuntimeConstants.BOT_MESSAGE_HEADER, RuntimeConstants.BOT_JIRA_LINKS_HEADER, "%s");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the %s argument at the end?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise we lose the %s, which is used again. This appendMessage is further used for String.format and the %s contains the body for individual tests.

public void testNonEmptyBodyAppendMessage() throws IOException {
String body = """
This is my
testing\n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just why do you use \n in multiline string? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I replace \n with just an enter key hit and try to compile the wildfly-checkstyle plugin complains

Mockito.when(mockCommit.getMessage()).thenReturn(commitMessage);

processEmptyPullRequestMock(mocks.pullRequest(1352150111));
// We still need to manually mock this commit, as we retrieve the sha as well
processPullRequestMock(mocks.pullRequest(1352150111), mockEmptyFileDetails(), mockEmptyComments(), mockCommitDetails);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have helper methods for commits. Also the comment seems unnecessary

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a comment, why it is needed


GitHubAppTesting.given()
.github(mocks -> {
mocks.configFileFromString("wildfly-bot.yml", wildflyConfigFile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config name should be pulled from constants also in tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it's just most of the test are not doing it anyways.

}

@Test
void incorrectTitleCheckFailMultipleJirasBracketsSecondTest() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? same as previous one?

@The-Huginn
Copy link
Collaborator Author

I will also update other tests

@The-Huginn The-Huginn force-pushed the add-jira-links branch 4 times, most recently from 0be76be to c6880f5 Compare August 2, 2023 07:57

sb.append(RuntimeConstants.BOT_MESSAGE_HEADER)
.append("\n\n")
.append(blockQuoted(RuntimeConstants.BOT_JIRA_LINKS_HEADER));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

header should also be "> " ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted it to be the only one with normal font to explain, what is below in blockquotes. Just as some kind of a title.

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 = """
____
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary if you have the next line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one adds a small little deliminer between the section automatically generated. A thin grey line.

private String appendedMessage = String.format("""
%s

%s%%s%s""", RuntimeConstants.BOT_MESSAGE_HEADER, blockQuoted(RuntimeConstants.BOT_JIRA_LINKS_HEADER), RuntimeConstants.BOT_MESSAGE_FOOTER);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of fun we should write this as "%s\n\n%s%%s%s". (Joke :))

}).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())));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need SB here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be consistent with all the tests, rather then save on this one

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())));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here it makes sense

@The-Huginn The-Huginn force-pushed the add-jira-links branch 2 times, most recently from 9077033 to c32e4b0 Compare August 3, 2023 15:55
@The-Huginn
Copy link
Collaborator Author

We should now link only jiras, which are not in the description and omit updating description fully if all jiras are linked

@xstefank xstefank merged commit 73c1c86 into wildfly:main Jan 26, 2024
1 check passed
@xstefank xstefank removed the hold label Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically add WFLY JIRA issues links
2 participants