diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/GithubPRInfoCommand.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/GithubPRInfoCommand.java index c3e0061712..367e89ae03 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/GithubPRInfoCommand.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/GithubPRInfoCommand.java @@ -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..appId, l10n.githubClients..key and l10n.githubClients..owner"); @@ -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 prComments, GithubClient github) { + if (!prComments.stream() + .anyMatch(ghIssueComment -> ghIssueComment.getBody().contains(skipI18NPushComment))) { + github.addCommentToPR(repository, prNumber, skipI18NPushComment); + } + } + private static boolean isSkipChecks(List prComments) { return prComments.stream() .anyMatch(ghIssueComment -> ghIssueComment.getBody().contains(SKIP_I18N_CHECKS_FLAG)); diff --git a/cli/src/test/java/com/box/l10n/mojito/cli/command/GithubPRInfoCommandTest.java b/cli/src/test/java/com/box/l10n/mojito/cli/command/GithubPRInfoCommandTest.java index 0c8f9d1baf..511323d0ec 100644 --- a/cli/src/test/java/com/box/l10n/mojito/cli/command/GithubPRInfoCommandTest.java +++ b/cli/src/test/java/com/box/l10n/mojito/cli/command/GithubPRInfoCommandTest.java @@ -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 @@ -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("some@email.com"); + 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("some@email.com"); + 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)