Skip to content

Commit 185e6e8

Browse files
qfccopybara-github
authored andcommitted
Throw ValidationException when +2 is restricted
We should throw validationException instead of reposException when setReview for +2 on the gerrit change is restricted. This is usually a user error. PiperOrigin-RevId: 355635497 Change-Id: I94c387dd0ce70c720bd45d9eb420ed64f656d15d
1 parent 0bb2d7a commit 185e6e8

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

java/com/google/copybara/git/GerritDestination.java

+5
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ private void submitChange(String changeId)
315315
ChangeInfo resultInfo =
316316
gerritApi.submitChange(changeInfo.getChangeId(), new SubmitInput(null));
317317
console.infoFmt("Submitted change : %s/changes/%s", repoUrl, resultInfo.getChangeId());
318+
} catch(RepoException e) {
319+
if (e.getMessage().contains("2 is restricted")) {
320+
throw new ValidationException(e.getMessage(), e);
321+
}
322+
throw e;
318323
}
319324
}
320325

javatests/com/google/copybara/git/GerritDestinationTest.java

+58
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,64 @@ public void gerritSubmit_success() throws Exception {
717717

718718
}
719719

720+
@Test
721+
public void gerritSubmit_plusTwoRestricted() throws Exception {
722+
options.gerrit.gerritChangeId = null;
723+
fetch = "master";
724+
writeFile(workdir, "file", "some content");
725+
url = BASE_URL + "/foo/bar";
726+
repoGitDir = gitUtil.mockRemoteRepo("user:[email protected]/foo/bar").getGitDir();
727+
gitUtil.mockApi(eq("GET"), startsWith(BASE_URL + "/changes/"),
728+
mockResponse(
729+
"["
730+
+ "{"
731+
+ " change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\","
732+
+ " status : \"NEW\""
733+
+ "}]")
734+
);
735+
AtomicBoolean submitCalled = new AtomicBoolean(false);
736+
AtomicBoolean reviewCalled = new AtomicBoolean(false);
737+
gitUtil.mockApi(
738+
eq("POST"),
739+
matches(BASE_URL + "/changes/.*/revisions/.*/review"),
740+
mockResponseWithStatus("Applying label \\\"Code-Review\\\": 2 is restricted\\n\\n", 401));
741+
742+
gitUtil.mockApi(
743+
eq("POST"),
744+
matches(BASE_URL + "/changes/.*/submit"),
745+
mockResponseAndValidateRequest(
746+
"{"
747+
+ " change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\","
748+
+ " status : \"submitted\""
749+
+ "}",
750+
new MockRequestAssertion("Always true with side-effect",
751+
s -> {
752+
submitCalled.set(true);
753+
return true;
754+
}))
755+
);
756+
757+
options.setForce(true);
758+
DummyRevision originRef = new DummyRevision("origin_ref");
759+
GerritDestination destination = destination("submit = True", "gerrit_submit = True");
760+
Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
761+
WriterContext writerContext =
762+
new WriterContext("GerritDestinationTest", "test", false, originRef,
763+
Glob.ALL_FILES.roots());
764+
ValidationException validationException =
765+
assertThrows(ValidationException.class,
766+
() -> destination
767+
.newWriter(writerContext)
768+
.write(
769+
TransformResults.of(workdir, originRef)
770+
.withSummary("Test message")
771+
.withIdentity(originRef.asString()),
772+
glob,
773+
console));
774+
775+
assertThat(validationException).hasMessageThat().contains("2 is restricted");
776+
}
777+
720778
@Test
721779
public void gerritSubmit_noChange() throws Exception {
722780
options.gerrit.gerritChangeId = null;

0 commit comments

Comments
 (0)