70
70
import java .io .IOException ;
71
71
import java .nio .file .Files ;
72
72
import java .nio .file .Path ;
73
+ import java .util .List ;
73
74
import java .util .Map ;
74
75
import java .util .Map .Entry ;
75
76
import java .util .Optional ;
@@ -142,7 +143,7 @@ public void testNoCommandLineReference() throws Exception {
142
143
143
144
@ Test
144
145
public void testGitResolvePullRequest () throws Exception {
145
- mockPullRequestAndIssue (123 , "open" , "foo: yes" , "bar: yes" );
146
+ mockPullRequestAndIssue ("open" , 123 , "foo: yes" , "bar: yes" );
146
147
checkResolve (
147
148
githubPrOrigin (
148
149
"url = 'https://github.com/google/example'" ,
@@ -153,7 +154,7 @@ public void testGitResolvePullRequest() throws Exception {
153
154
154
155
@ Test
155
156
public void testGitResolveWithGitDescribe () throws Exception {
156
- mockPullRequestAndIssue (123 , "open" , "foo: yes" , "bar: yes" );
157
+ mockPullRequestAndIssue ("open" , 123 , "foo: yes" , "bar: yes" );
157
158
GitRepository remote = gitUtil .mockRemoteRepo ("github.com/google/example" );
158
159
addFiles (remote , "first change" , ImmutableMap .<String , String >builder ()
159
160
.put (123 + ".txt" , "" ).build ());
@@ -172,7 +173,7 @@ public void testGitResolveWithGitDescribe() throws Exception {
172
173
173
174
@ Test
174
175
public void testGitResolvePullRequestNumber () throws Exception {
175
- mockPullRequestAndIssue (123 , "open" , "foo: yes" , "bar: yes" );
176
+ mockPullRequestAndIssue ("open" , 123 , "foo: yes" , "bar: yes" );
176
177
checkResolve (
177
178
githubPrOrigin (
178
179
"url = 'https://github.com/google/example'" ,
@@ -188,7 +189,7 @@ public void testEmptyUrl() {
188
189
189
190
@ Test
190
191
public void testGitResolvePullRequestRawRef () throws Exception {
191
- mockPullRequestAndIssue (123 , "open" , "foo: yes" , "bar: yes" );
192
+ mockPullRequestAndIssue ("open" , 123 , "foo: yes" , "bar: yes" );
192
193
checkResolve (
193
194
githubPrOrigin (
194
195
"url = 'https://github.com/google/example'" ,
@@ -199,7 +200,7 @@ public void testGitResolvePullRequestRawRef() throws Exception {
199
200
200
201
@ Test
201
202
public void testGitResolveSha1 () throws Exception {
202
- mockPullRequestAndIssue (123 , "open" );
203
+ mockPullRequestAndIssue ("open" , 123 );
203
204
204
205
GitHubPROrigin origin = githubPrOrigin (
205
206
"url = 'https://github.com/google/example'" );
@@ -215,13 +216,13 @@ public void testGitResolveSha1() throws Exception {
215
216
216
217
@ Test
217
218
public void testGitResolveNoLabelsRequired () throws Exception {
218
- mockPullRequestAndIssue (125 , "open" , "bar: yes" );
219
+ mockPullRequestAndIssue ("open" , 125 , "bar: yes" );
219
220
checkResolve (
220
221
githubPrOrigin ("url = 'https://github.com/google/example'" , "required_labels = []" ),
221
222
"125" ,
222
223
125 );
223
224
224
- mockPullRequestAndIssue (126 , "open" );
225
+ mockPullRequestAndIssue ("open" , 126 );
225
226
226
227
checkResolve (
227
228
githubPrOrigin ("url = 'https://github.com/google/example'" , "required_labels = []" ),
@@ -231,7 +232,7 @@ public void testGitResolveNoLabelsRequired() throws Exception {
231
232
232
233
@ Test
233
234
public void testGitResolveRequiredLabelsNotFound () throws Exception {
234
- mockPullRequestAndIssue (125 , "open" , "bar: yes" );
235
+ mockPullRequestAndIssue ("open" , 125 , "bar: yes" );
235
236
EmptyChangeException thrown =
236
237
assertThrows (
237
238
EmptyChangeException .class ,
@@ -252,7 +253,7 @@ public void testGitResolveRequiredLabelsNotFound() throws Exception {
252
253
@ Test
253
254
public void testGitResolveRequiredLabelsNotFound_forceMigrate () throws Exception {
254
255
options .githubPrOrigin .forceImport = true ;
255
- mockPullRequestAndIssue (125 , "open" , "bar: yes" );
256
+ mockPullRequestAndIssue ("open" , 125 , "bar: yes" );
256
257
checkResolve (
257
258
githubPrOrigin (
258
259
"url = 'https://github.com/google/example'" ,
@@ -264,13 +265,13 @@ public void testGitResolveRequiredLabelsNotFound_forceMigrate() throws Exception
264
265
@ Test
265
266
public void testLimitByBranch () throws Exception {
266
267
// This should work since it returns a PR for master.
267
- mockPullRequestAndIssue (125 , "open" , "bar: yes" );
268
+ mockPullRequestAndIssue ("open" , 125 , "bar: yes" );
268
269
checkResolve (
269
270
githubPrOrigin ("url = 'https://github.com/google/example'" , "branch = 'master'" ),
270
271
"125" ,
271
272
125 );
272
273
273
- mockPullRequestAndIssue (126 , "open" , "bar: yes" );
274
+ mockPullRequestAndIssue ("open" , 126 , "bar: yes" );
274
275
EmptyChangeException e =
275
276
assertThrows (
276
277
EmptyChangeException .class ,
@@ -288,7 +289,7 @@ public void testLimitByBranch() throws Exception {
288
289
289
290
@ Test
290
291
public void testGitResolveRequiredLabelsRetried () throws Exception {
291
- mockPullRequest (125 , "open" );
292
+ mockPullRequest (125 , "open" , "master" );
292
293
293
294
mockIssue (
294
295
125 ,
@@ -310,7 +311,7 @@ public void testGitResolveRequiredLabelsRetried() throws Exception {
310
311
311
312
@ Test
312
313
public void testGitResolveRequiredLabelsNotRetryable () throws Exception {
313
- mockPullRequestAndIssue (125 , "open" );
314
+ mockPullRequestAndIssue ("open" , 125 );
314
315
EmptyChangeException thrown =
315
316
assertThrows (
316
317
EmptyChangeException .class ,
@@ -330,7 +331,7 @@ public void testGitResolveRequiredLabelsNotRetryable() throws Exception {
330
331
331
332
@ Test
332
333
public void testAlreadyClosed_default () throws Exception {
333
- mockPullRequestAndIssue (125 , "closed" , "foo: yes" );
334
+ mockPullRequestAndIssue ("closed" , 125 , "foo: yes" );
334
335
EmptyChangeException thrown =
335
336
assertThrows (
336
337
EmptyChangeException .class ,
@@ -342,7 +343,7 @@ public void testAlreadyClosed_default() throws Exception {
342
343
343
344
@ Test
344
345
public void testAlreadyClosed_only_open () throws Exception {
345
- mockPullRequestAndIssue (125 , "closed" , "foo: yes" );
346
+ mockPullRequestAndIssue ("closed" , 125 , "foo: yes" );
346
347
EmptyChangeException thrown =
347
348
assertThrows (
348
349
EmptyChangeException .class ,
@@ -357,14 +358,14 @@ public void testAlreadyClosed_only_open() throws Exception {
357
358
@ Test
358
359
public void testAlreadyClosed_only_open_forceMigration () throws Exception {
359
360
options .githubPrOrigin .forceImport = true ;
360
- mockPullRequestAndIssue (125 , "closed" , "foo: yes" );
361
+ mockPullRequestAndIssue ("closed" , 125 , "foo: yes" );
361
362
checkResolve (
362
363
githubPrOrigin ("url = 'https://github.com/google/example', state = 'OPEN'" ), "125" , 125 );
363
364
}
364
365
365
366
@ Test
366
367
public void testAlreadyClosed_only_closed () throws Exception {
367
- mockPullRequestAndIssue (125 , "open" , "foo: yes" );
368
+ mockPullRequestAndIssue ("open" , 125 , "foo: yes" );
368
369
EmptyChangeException thrown =
369
370
assertThrows (
370
371
EmptyChangeException .class ,
@@ -378,7 +379,7 @@ public void testAlreadyClosed_only_closed() throws Exception {
378
379
379
380
@ Test
380
381
public void testGitResolveRequiredLabelsMixed () throws Exception {
381
- mockPullRequestAndIssue (125 , "open" , "foo: yes" , "bar: yes" );
382
+ mockPullRequestAndIssue ("open" , 125 , "foo: yes" , "bar: yes" );
382
383
checkResolve (
383
384
githubPrOrigin (
384
385
"url = 'https://github.com/google/example'" ,
@@ -420,7 +421,7 @@ public void testChanges() throws Exception {
420
421
.put ("other.txt" , "" ).build ());
421
422
remote .simpleCommand ("update-ref" , GitHubUtil .asMergeRef (123 ), remote .parseRef ("HEAD" ));
422
423
423
- mockPullRequestAndIssue (123 , "open" );
424
+ mockPullRequestAndIssue ("open" , 123 );
424
425
425
426
GitHubPROrigin origin = githubPrOrigin (
426
427
"url = 'https://github.com/google/example'" );
@@ -465,7 +466,7 @@ public void testCheckout() throws Exception {
465
466
.put ("other.txt" , "" ).build ());
466
467
remote .simpleCommand ("update-ref" , GitHubUtil .asMergeRef (123 ), remote .parseRef ("HEAD" ));
467
468
468
- mockPullRequestAndIssue (123 , "open" );
469
+ mockPullRequestAndIssue ("open" , 123 );
469
470
470
471
GitHubPROrigin origin = githubPrOrigin (
471
472
"url = 'https://github.com/google/example'" ,
@@ -513,7 +514,7 @@ public void testCheckout() throws Exception {
513
514
"use_merge = True" ,
514
515
"baseline_from_branch = True" );
515
516
516
- mockPullRequestAndIssue (123 , "open" );
517
+ mockPullRequestAndIssue ("open" , 123 );
517
518
518
519
GitRevision mergePrRevision = origin .resolve ("123" );
519
520
@@ -563,7 +564,7 @@ public void testHookForGitHubPr() throws Exception {
563
564
String prHeadSha1 = remote .parseRef ("HEAD" );
564
565
remote .simpleCommand ("update-ref" , GitHubUtil .asHeadRef (123 ), prHeadSha1 );
565
566
566
- mockPullRequestAndIssue (123 , "open" );
567
+ mockPullRequestAndIssue ("open" , 123 );
567
568
gitUtil .mockApi (
568
569
eq ("POST" ),
569
570
startsWith ("https://api.github.com/repos/google/example/statuses/" ),
@@ -715,7 +716,7 @@ private GitRevision checkReviewApprovers(String... configLines)
715
716
String prHeadSha1 = remote .parseRef ("HEAD" );
716
717
remote .simpleCommand ("update-ref" , GitHubUtil .asHeadRef (123 ), prHeadSha1 );
717
718
718
- mockPullRequestAndIssue (123 , "open" );
719
+ mockPullRequestAndIssue ("open" , 123 );
719
720
720
721
gitUtil .mockApi (
721
722
"GET" ,
@@ -792,27 +793,30 @@ private String toJson(Object obj) {
792
793
@ Test
793
794
public void testMerge () throws Exception {
794
795
GitRepository remote = withTmpWorktree (gitUtil .mockRemoteRepo ("github.com/google/example" ));
796
+
795
797
addFiles (remote , "base" , ImmutableMap .<String , String >builder ()
796
798
.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" );
799
803
addFiles (remote , "one" , ImmutableMap .<String , String >builder ()
800
804
.put ("a.txt" , "" ).put ("b.txt" , "" ).build ());
801
805
addFiles (remote , "two" , ImmutableMap .<String , String >builder ()
802
806
.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 ()
805
809
.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 " ));
809
813
810
814
811
815
GitHubPROrigin origin = githubPrOrigin (
812
816
"url = 'https://github.com/google/example'" ,
813
817
"use_merge = True" );
814
818
815
- mockPullRequestAndIssue (123 , "open" );
819
+ mockPullRequestAndIssue ("open" , "primary" , 123 );
816
820
817
821
origin .newReader (Glob .ALL_FILES , authoring ).checkout (origin .resolve ("123" ), workdir );
818
822
@@ -832,14 +836,14 @@ public void testMerge() throws Exception {
832
836
remote .resolveReference (GitHubUtil .asHeadRef (123 )).getSha1 ()));
833
837
834
838
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'" );
841
845
// 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 " ));
843
847
844
848
assertThat (Lists .transform (
845
849
reader .changes (/*fromRef=*/ null , origin .resolve ("123" )).getChanges (),
@@ -855,7 +859,7 @@ public void testCheckout_noMergeRef() throws Exception {
855
859
String prHeadSha1 = remote .parseRef ("HEAD" );
856
860
remote .simpleCommand ("update-ref" , GitHubUtil .asHeadRef (123 ), prHeadSha1 );
857
861
858
- mockPullRequestAndIssue (123 , "open " );
862
+ mockPullRequestAndIssue ("open" , 123 , "master " );
859
863
860
864
// Now try with merge ref
861
865
GitHubPROrigin origin = githubPrOrigin (
@@ -916,20 +920,27 @@ private GitHubPROrigin githubPrOrigin(String... lines) throws ValidationExceptio
916
920
+ " " + Joiner .on (",\n " ).join (lines ) + ",\n )" );
917
921
}
918
922
919
- public void mockPullRequestAndIssue (int prNumber , String state , String ... labels )
923
+ public void mockPullRequestAndIssue (String state , int prNumber , String ... labels )
920
924
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 );
922
933
mockIssue (prNumber , issueResponse (prNumber , state , labels ));
923
934
}
924
935
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 );
927
938
}
928
939
929
940
/** 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 (
933
944
"{\n "
934
945
+ " \" id\" : 1,\n "
935
946
+ " \" number\" : "
@@ -948,8 +959,8 @@ public static void mockPullRequest(GitTestUtil gitUtil, int prNumber, String sta
948
959
+ " \" ref\" : \" example-branch\" \n "
949
960
+ " },\n "
950
961
+ " \" base\" : {\n "
951
- + " \" label\" : \" google:master \" ,\n "
952
- + " \" ref\" : \" master \" \n "
962
+ + " \" label\" : \" google:%1$s \" ,\n "
963
+ + " \" ref\" : \" %1$s \" \n "
953
964
+ " },\n "
954
965
+ " \" user\" : {\n "
955
966
+ " \" login\" : \" some_user\" \n "
@@ -962,7 +973,7 @@ public static void mockPullRequest(GitTestUtil gitUtil, int prNumber, String sta
962
973
+ " \" login\" : \" assignee2\" \n "
963
974
+ " }\n "
964
975
+ " ]\n "
965
- + "}" ;
976
+ + "}" , primaryBranch ) ;
966
977
gitUtil .mockApi (
967
978
eq ("GET" ),
968
979
eq ("https://api.github.com/repos/google/example/pulls/" + prNumber ),
0 commit comments