Skip to content

Commit 8cff730

Browse files
hsudhofcopybara-github
authored andcommitted
Allow merge message to contain destination branch
Git 2.28 stopped treating the "main" branch specially for the message. However, 6e6029a82acdb3d785eef15dc798f4d86c4ea445 added it back as an option. To be able to handle both cases, allow either in the test. Adjust the test to use a non-default branch. Fixes #132 PiperOrigin-RevId: 326092156 Change-Id: I77d1c57ffe4b8d4ae702563e3a25f5a4656d65e5
1 parent a6765cb commit 8cff730

File tree

1 file changed

+60
-49
lines changed

1 file changed

+60
-49
lines changed

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

+60-49
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import java.io.IOException;
7171
import java.nio.file.Files;
7272
import java.nio.file.Path;
73+
import java.util.List;
7374
import java.util.Map;
7475
import java.util.Map.Entry;
7576
import java.util.Optional;
@@ -142,7 +143,7 @@ public void testNoCommandLineReference() throws Exception {
142143

143144
@Test
144145
public void testGitResolvePullRequest() throws Exception {
145-
mockPullRequestAndIssue(123, "open", "foo: yes", "bar: yes");
146+
mockPullRequestAndIssue("open", 123, "foo: yes", "bar: yes");
146147
checkResolve(
147148
githubPrOrigin(
148149
"url = 'https://github.com/google/example'",
@@ -153,7 +154,7 @@ public void testGitResolvePullRequest() throws Exception {
153154

154155
@Test
155156
public void testGitResolveWithGitDescribe() throws Exception {
156-
mockPullRequestAndIssue(123, "open", "foo: yes", "bar: yes");
157+
mockPullRequestAndIssue("open", 123, "foo: yes", "bar: yes");
157158
GitRepository remote = gitUtil.mockRemoteRepo("github.com/google/example");
158159
addFiles(remote, "first change", ImmutableMap.<String, String>builder()
159160
.put(123 + ".txt", "").build());
@@ -172,7 +173,7 @@ public void testGitResolveWithGitDescribe() throws Exception {
172173

173174
@Test
174175
public void testGitResolvePullRequestNumber() throws Exception {
175-
mockPullRequestAndIssue(123, "open", "foo: yes", "bar: yes");
176+
mockPullRequestAndIssue("open", 123, "foo: yes", "bar: yes");
176177
checkResolve(
177178
githubPrOrigin(
178179
"url = 'https://github.com/google/example'",
@@ -188,7 +189,7 @@ public void testEmptyUrl() {
188189

189190
@Test
190191
public void testGitResolvePullRequestRawRef() throws Exception {
191-
mockPullRequestAndIssue(123, "open", "foo: yes", "bar: yes");
192+
mockPullRequestAndIssue("open", 123, "foo: yes", "bar: yes");
192193
checkResolve(
193194
githubPrOrigin(
194195
"url = 'https://github.com/google/example'",
@@ -199,7 +200,7 @@ public void testGitResolvePullRequestRawRef() throws Exception {
199200

200201
@Test
201202
public void testGitResolveSha1() throws Exception {
202-
mockPullRequestAndIssue(123, "open");
203+
mockPullRequestAndIssue("open", 123);
203204

204205
GitHubPROrigin origin = githubPrOrigin(
205206
"url = 'https://github.com/google/example'");
@@ -215,13 +216,13 @@ public void testGitResolveSha1() throws Exception {
215216

216217
@Test
217218
public void testGitResolveNoLabelsRequired() throws Exception {
218-
mockPullRequestAndIssue(125, "open", "bar: yes");
219+
mockPullRequestAndIssue("open", 125, "bar: yes");
219220
checkResolve(
220221
githubPrOrigin("url = 'https://github.com/google/example'", "required_labels = []"),
221222
"125",
222223
125);
223224

224-
mockPullRequestAndIssue(126, "open");
225+
mockPullRequestAndIssue("open", 126);
225226

226227
checkResolve(
227228
githubPrOrigin("url = 'https://github.com/google/example'", "required_labels = []"),
@@ -231,7 +232,7 @@ public void testGitResolveNoLabelsRequired() throws Exception {
231232

232233
@Test
233234
public void testGitResolveRequiredLabelsNotFound() throws Exception {
234-
mockPullRequestAndIssue(125, "open", "bar: yes");
235+
mockPullRequestAndIssue("open", 125, "bar: yes");
235236
EmptyChangeException thrown =
236237
assertThrows(
237238
EmptyChangeException.class,
@@ -252,7 +253,7 @@ public void testGitResolveRequiredLabelsNotFound() throws Exception {
252253
@Test
253254
public void testGitResolveRequiredLabelsNotFound_forceMigrate() throws Exception {
254255
options.githubPrOrigin.forceImport = true;
255-
mockPullRequestAndIssue(125, "open", "bar: yes");
256+
mockPullRequestAndIssue("open", 125, "bar: yes");
256257
checkResolve(
257258
githubPrOrigin(
258259
"url = 'https://github.com/google/example'",
@@ -264,13 +265,13 @@ public void testGitResolveRequiredLabelsNotFound_forceMigrate() throws Exception
264265
@Test
265266
public void testLimitByBranch() throws Exception {
266267
// This should work since it returns a PR for master.
267-
mockPullRequestAndIssue(125, "open", "bar: yes");
268+
mockPullRequestAndIssue("open", 125, "bar: yes");
268269
checkResolve(
269270
githubPrOrigin("url = 'https://github.com/google/example'", "branch = 'master'"),
270271
"125",
271272
125);
272273

273-
mockPullRequestAndIssue(126, "open", "bar: yes");
274+
mockPullRequestAndIssue("open", 126, "bar: yes");
274275
EmptyChangeException e =
275276
assertThrows(
276277
EmptyChangeException.class,
@@ -288,7 +289,7 @@ public void testLimitByBranch() throws Exception {
288289

289290
@Test
290291
public void testGitResolveRequiredLabelsRetried() throws Exception {
291-
mockPullRequest(125, "open");
292+
mockPullRequest(125, "open", "master");
292293

293294
mockIssue(
294295
125,
@@ -310,7 +311,7 @@ public void testGitResolveRequiredLabelsRetried() throws Exception {
310311

311312
@Test
312313
public void testGitResolveRequiredLabelsNotRetryable() throws Exception {
313-
mockPullRequestAndIssue(125, "open");
314+
mockPullRequestAndIssue("open", 125);
314315
EmptyChangeException thrown =
315316
assertThrows(
316317
EmptyChangeException.class,
@@ -330,7 +331,7 @@ public void testGitResolveRequiredLabelsNotRetryable() throws Exception {
330331

331332
@Test
332333
public void testAlreadyClosed_default() throws Exception {
333-
mockPullRequestAndIssue(125, "closed", "foo: yes");
334+
mockPullRequestAndIssue("closed", 125, "foo: yes");
334335
EmptyChangeException thrown =
335336
assertThrows(
336337
EmptyChangeException.class,
@@ -342,7 +343,7 @@ public void testAlreadyClosed_default() throws Exception {
342343

343344
@Test
344345
public void testAlreadyClosed_only_open() throws Exception {
345-
mockPullRequestAndIssue(125, "closed", "foo: yes");
346+
mockPullRequestAndIssue("closed", 125, "foo: yes");
346347
EmptyChangeException thrown =
347348
assertThrows(
348349
EmptyChangeException.class,
@@ -357,14 +358,14 @@ public void testAlreadyClosed_only_open() throws Exception {
357358
@Test
358359
public void testAlreadyClosed_only_open_forceMigration() throws Exception {
359360
options.githubPrOrigin.forceImport = true;
360-
mockPullRequestAndIssue(125, "closed", "foo: yes");
361+
mockPullRequestAndIssue("closed", 125, "foo: yes");
361362
checkResolve(
362363
githubPrOrigin("url = 'https://github.com/google/example', state = 'OPEN'"), "125", 125);
363364
}
364365

365366
@Test
366367
public void testAlreadyClosed_only_closed() throws Exception {
367-
mockPullRequestAndIssue(125, "open", "foo: yes");
368+
mockPullRequestAndIssue("open", 125, "foo: yes");
368369
EmptyChangeException thrown =
369370
assertThrows(
370371
EmptyChangeException.class,
@@ -378,7 +379,7 @@ public void testAlreadyClosed_only_closed() throws Exception {
378379

379380
@Test
380381
public void testGitResolveRequiredLabelsMixed() throws Exception {
381-
mockPullRequestAndIssue(125, "open", "foo: yes", "bar: yes");
382+
mockPullRequestAndIssue("open", 125, "foo: yes", "bar: yes");
382383
checkResolve(
383384
githubPrOrigin(
384385
"url = 'https://github.com/google/example'",
@@ -420,7 +421,7 @@ public void testChanges() throws Exception {
420421
.put("other.txt", "").build());
421422
remote.simpleCommand("update-ref", GitHubUtil.asMergeRef(123), remote.parseRef("HEAD"));
422423

423-
mockPullRequestAndIssue(123, "open");
424+
mockPullRequestAndIssue("open", 123);
424425

425426
GitHubPROrigin origin = githubPrOrigin(
426427
"url = 'https://github.com/google/example'");
@@ -465,7 +466,7 @@ public void testCheckout() throws Exception {
465466
.put("other.txt", "").build());
466467
remote.simpleCommand("update-ref", GitHubUtil.asMergeRef(123), remote.parseRef("HEAD"));
467468

468-
mockPullRequestAndIssue(123, "open");
469+
mockPullRequestAndIssue("open", 123);
469470

470471
GitHubPROrigin origin = githubPrOrigin(
471472
"url = 'https://github.com/google/example'",
@@ -513,7 +514,7 @@ public void testCheckout() throws Exception {
513514
"use_merge = True",
514515
"baseline_from_branch = True");
515516

516-
mockPullRequestAndIssue(123, "open");
517+
mockPullRequestAndIssue("open", 123);
517518

518519
GitRevision mergePrRevision = origin.resolve("123");
519520

@@ -563,7 +564,7 @@ public void testHookForGitHubPr() throws Exception {
563564
String prHeadSha1 = remote.parseRef("HEAD");
564565
remote.simpleCommand("update-ref", GitHubUtil.asHeadRef(123), prHeadSha1);
565566

566-
mockPullRequestAndIssue(123, "open");
567+
mockPullRequestAndIssue("open", 123);
567568
gitUtil.mockApi(
568569
eq("POST"),
569570
startsWith("https://api.github.com/repos/google/example/statuses/"),
@@ -715,7 +716,7 @@ private GitRevision checkReviewApprovers(String... configLines)
715716
String prHeadSha1 = remote.parseRef("HEAD");
716717
remote.simpleCommand("update-ref", GitHubUtil.asHeadRef(123), prHeadSha1);
717718

718-
mockPullRequestAndIssue(123, "open");
719+
mockPullRequestAndIssue("open", 123);
719720

720721
gitUtil.mockApi(
721722
"GET",
@@ -792,27 +793,30 @@ private String toJson(Object obj) {
792793
@Test
793794
public void testMerge() throws Exception {
794795
GitRepository remote = withTmpWorktree(gitUtil.mockRemoteRepo("github.com/google/example"));
796+
795797
addFiles(remote, "base", ImmutableMap.<String, String>builder()
796798
.put("a.txt", "").build());
797-
remote.simpleCommand("branch", "foo");
798-
remote.forceCheckout("foo");
799+
remote.simpleCommand("branch", "testMerge");
800+
remote.simpleCommand("branch", "primary");
801+
802+
remote.forceCheckout("testMerge");
799803
addFiles(remote, "one", ImmutableMap.<String, String>builder()
800804
.put("a.txt", "").put("b.txt", "").build());
801805
addFiles(remote, "two", ImmutableMap.<String, String>builder()
802806
.put("a.txt", "").put("b.txt", "").put("c.txt", "").build());
803-
remote.forceCheckout("master");
804-
addFiles(remote, "master change", ImmutableMap.<String, String>builder()
807+
remote.forceCheckout("primary");
808+
addFiles(remote, "primary change", ImmutableMap.<String, String>builder()
805809
.put("a.txt", "").put("d.txt", "").build());
806-
remote.simpleCommand("merge", "foo");
807-
remote.simpleCommand("update-ref", GitHubUtil.asHeadRef(123), remote.parseRef("foo"));
808-
remote.simpleCommand("update-ref", GitHubUtil.asMergeRef(123), remote.parseRef("master"));
810+
remote.simpleCommand("merge", "testMerge");
811+
remote.simpleCommand("update-ref", GitHubUtil.asHeadRef(123), remote.parseRef("testMerge"));
812+
remote.simpleCommand("update-ref", GitHubUtil.asMergeRef(123), remote.parseRef("primary"));
809813

810814

811815
GitHubPROrigin origin = githubPrOrigin(
812816
"url = 'https://github.com/google/example'",
813817
"use_merge = True");
814818

815-
mockPullRequestAndIssue(123, "open");
819+
mockPullRequestAndIssue("open", "primary", 123);
816820

817821
origin.newReader(Glob.ALL_FILES, authoring).checkout(origin.resolve("123"), workdir);
818822

@@ -832,14 +836,14 @@ public void testMerge() throws Exception {
832836
remote.resolveReference(GitHubUtil.asHeadRef(123)).getSha1()));
833837

834838
Reader<GitRevision> reader = origin.newReader(Glob.ALL_FILES, authoring);
835-
assertThat(
836-
Lists.transform(
837-
reader.changes(/*fromRef=*/ null, mergeRevision).getChanges(),
838-
Change::getMessage))
839-
.isEqualTo(Lists.newArrayList("base\n", "one\n", "two\n", "Merge branch 'foo'\n"));
840-
839+
List<String> msgs = Lists.transform(
840+
reader.changes(/*fromRef=*/ null, mergeRevision).getChanges(),
841+
Change::getMessage);
842+
assertThat(msgs).hasSize(4);
843+
assertThat(msgs).containsAtLeast("base\n", "one\n", "two\n");
844+
assertThat(msgs.get(3)).contains("Merge branch 'testMerge'");
841845
// Simulate fast-forward
842-
remote.simpleCommand("update-ref", GitHubUtil.asMergeRef(123), remote.parseRef("foo"));
846+
remote.simpleCommand("update-ref", GitHubUtil.asMergeRef(123), remote.parseRef("testMerge"));
843847

844848
assertThat(Lists.transform(
845849
reader.changes(/*fromRef=*/null, origin.resolve("123")).getChanges(),
@@ -855,7 +859,7 @@ public void testCheckout_noMergeRef() throws Exception {
855859
String prHeadSha1 = remote.parseRef("HEAD");
856860
remote.simpleCommand("update-ref", GitHubUtil.asHeadRef(123), prHeadSha1);
857861

858-
mockPullRequestAndIssue(123, "open");
862+
mockPullRequestAndIssue("open", 123, "master");
859863

860864
// Now try with merge ref
861865
GitHubPROrigin origin = githubPrOrigin(
@@ -916,20 +920,27 @@ private GitHubPROrigin githubPrOrigin(String... lines) throws ValidationExceptio
916920
+ " " + Joiner.on(",\n ").join(lines) + ",\n)");
917921
}
918922

919-
public void mockPullRequestAndIssue(int prNumber, String state, String... labels)
923+
public void mockPullRequestAndIssue(String state, int prNumber, String... labels)
920924
throws IOException {
921-
mockPullRequest(prNumber, state);
925+
mockPullRequestAndIssue(state, "master", prNumber, labels);
926+
}
927+
928+
public void mockPullRequestAndIssue(
929+
String state, String primaryBranch, int prNumber, String... labels)
930+
throws IOException {
931+
// TODO(hsudhof): PrimaryBranch should not assume this value as
932+
mockPullRequest(prNumber, state, primaryBranch);
922933
mockIssue(prNumber, issueResponse(prNumber, state, labels));
923934
}
924935

925-
private void mockPullRequest(int prNumber, String state) throws IOException {
926-
mockPullRequest(gitUtil, prNumber, state);
936+
private void mockPullRequest(int prNumber, String state, String primaryBranch) {
937+
mockPullRequest(gitUtil, prNumber, state, primaryBranch);
927938
}
928939

929940
/** Used internally */
930-
public static void mockPullRequest(GitTestUtil gitUtil, int prNumber, String state)
931-
throws IOException {
932-
String content =
941+
public static void mockPullRequest(
942+
GitTestUtil gitUtil, int prNumber, String state, String primaryBranch) {
943+
String content = String.format(
933944
"{\n"
934945
+ " \"id\": 1,\n"
935946
+ " \"number\": "
@@ -948,8 +959,8 @@ public static void mockPullRequest(GitTestUtil gitUtil, int prNumber, String sta
948959
+ " \"ref\": \"example-branch\"\n"
949960
+ " },\n"
950961
+ " \"base\": {\n"
951-
+ " \"label\": \"google:master\",\n"
952-
+ " \"ref\": \"master\"\n"
962+
+ " \"label\": \"google:%1$s\",\n"
963+
+ " \"ref\": \"%1$s\"\n"
953964
+ " },\n"
954965
+ " \"user\": {\n"
955966
+ " \"login\": \"some_user\"\n"
@@ -962,7 +973,7 @@ public static void mockPullRequest(GitTestUtil gitUtil, int prNumber, String sta
962973
+ " \"login\": \"assignee2\"\n"
963974
+ " }\n"
964975
+ " ]\n"
965-
+ "}";
976+
+ "}", primaryBranch);
966977
gitUtil.mockApi(
967978
eq("GET"),
968979
eq("https://api.github.com/repos/google/example/pulls/" + prNumber),

0 commit comments

Comments
 (0)