Skip to content

Commit

Permalink
Added 'skip-i18n-push' label handling to GithubPRInfoCommand (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
maallen authored and aurambaj committed Sep 2, 2023
1 parent 35283ab commit 4f90412
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,27 @@ public class GithubPRInfoCommand extends Command {
description = "The Github repository owner")
String owner;

@Parameter(
names = {"--skip-i18n-push-label"},
arity = 1,
required = false,
description = "Github label name that is used to trigger skipping i18n push")
String skipI18NPushLabel = "skip-i18n-push";

@Parameter(
names = {"--skip-i18n-push-comment"},
arity = 1,
required = false,
description =
"Comment added to PR to indicate that the push to the Mojito backend will be skipped")
String skipI18NPushComment =
":warning: I18N strings will not be pushed to Mojito as '%s' label is applied to this PR.";

@Override
public void execute() throws CommandException {

skipI18NPushComment = String.format(skipI18NPushComment, skipI18NPushLabel);

if (githubClients == null) {
throw new CommandException(
"Github must be configured with properties: l10n.githubClients.<client>.appId, l10n.githubClients.<client>.key and l10n.githubClients.<client>.owner");
Expand Down Expand Up @@ -94,11 +112,26 @@ public void execute() throws CommandException {
} else {
consoleWriterAnsiCodeEnabledFalse.a("MOJITO_SKIP_I18N_CHECKS=false").println();
}

if (github.isLabelAppliedToPR(repository, prNumber, skipI18NPushLabel)) {
addPushSkippedComment(prComments, github);
consoleWriterAnsiCodeEnabledFalse.a("MOJITO_SKIP_I18N_PUSH=true").println();
} else {
consoleWriterAnsiCodeEnabledFalse.a("MOJITO_SKIP_I18N_PUSH=false").println();
}

} catch (GithubException e) {
throw new CommandException(e);
}
}

private void addPushSkippedComment(List<GHIssueComment> prComments, GithubClient github) {
if (!prComments.stream()
.anyMatch(ghIssueComment -> ghIssueComment.getBody().contains(skipI18NPushComment))) {
github.addCommentToPR(repository, prNumber, skipI18NPushComment);
}
}

private static boolean isSkipChecks(List<GHIssueComment> prComments) {
return prComments.stream()
.anyMatch(ghIssueComment -> ghIssueComment.getBody().contains(SKIP_I18N_CHECKS_FLAG));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void testExecute() {
verify(consoleWriterMock, times(1)).a("MOJITO_GITHUB_AUTHOR_USERNAME=");
verify(consoleWriterMock, times(1)).a("some");
verify(consoleWriterMock, times(1)).a("MOJITO_SKIP_I18N_CHECKS=false");
verify(consoleWriterMock, times(1)).a("MOJITO_SKIP_I18N_PUSH=false");
}

@Test
Expand All @@ -76,6 +77,48 @@ public void testExecuteWithChecksSkipped() throws IOException {
verify(consoleWriterMock, times(1)).a("some");
verify(consoleWriterMock, times(1)).a("MOJITO_SKIP_I18N_CHECKS=true");
verify(ghIssueCommentMock, times(1)).createReaction(ReactionContent.PLUS_ONE);
verify(consoleWriterMock, times(1)).a("MOJITO_SKIP_I18N_PUSH=false");
}

@Test
public void testExecuteWithPushSkipped() {
when(githubMock.isLabelAppliedToPR("testRepo", 1, "skip-i18n-push")).thenReturn(true);
githubPRInfoCommand.execute();
verify(consoleWriterMock, times(1)).a("MOJITO_GITHUB_BASE_COMMIT=");
verify(consoleWriterMock, times(1)).a("baseSha");
verify(consoleWriterMock, times(1)).a("MOJITO_GITHUB_AUTHOR_EMAIL=");
verify(consoleWriterMock, times(1)).a("[email protected]");
verify(consoleWriterMock, times(1)).a("MOJITO_GITHUB_AUTHOR_USERNAME=");
verify(consoleWriterMock, times(1)).a("some");
verify(consoleWriterMock, times(1)).a("MOJITO_SKIP_I18N_CHECKS=false");
verify(consoleWriterMock, times(1)).a("MOJITO_SKIP_I18N_PUSH=true");
verify(githubMock, times(1))
.addCommentToPR(
"testRepo",
1,
":warning: I18N strings will not be pushed to Mojito as 'skip-i18n-push' label is applied to this PR.");
}

@Test
public void testExecuteWithPushSkippedOnlyCommentsOnce() {
when(githubMock.isLabelAppliedToPR("testRepo", 1, "skip-i18n-push")).thenReturn(true);
when(ghIssueCommentMock.getBody())
.thenReturn(
":warning: I18N strings will not be pushed to Mojito as 'skip-i18n-push' label is applied to this PR.");
githubPRInfoCommand.execute();
verify(consoleWriterMock, times(1)).a("MOJITO_GITHUB_BASE_COMMIT=");
verify(consoleWriterMock, times(1)).a("baseSha");
verify(consoleWriterMock, times(1)).a("MOJITO_GITHUB_AUTHOR_EMAIL=");
verify(consoleWriterMock, times(1)).a("[email protected]");
verify(consoleWriterMock, times(1)).a("MOJITO_GITHUB_AUTHOR_USERNAME=");
verify(consoleWriterMock, times(1)).a("some");
verify(consoleWriterMock, times(1)).a("MOJITO_SKIP_I18N_CHECKS=false");
verify(consoleWriterMock, times(1)).a("MOJITO_SKIP_I18N_PUSH=true");
verify(githubMock, times(0))
.addCommentToPR(
"testRepo",
1,
":warning: I18N strings will not be pushed to Mojito as 'skip-i18n-push' label is applied to this PR.");
}

@Test(expected = CommandException.class)
Expand Down

0 comments on commit 4f90412

Please sign in to comment.