/
GitSCMTest.java
1893 lines (1555 loc) · 89.5 KB
/
GitSCMTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package hudson.plugins.git;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.model.*;
import hudson.plugins.git.GitSCM.BuildChooserContextImpl;
import hudson.plugins.git.GitSCM.DescriptorImpl;
import hudson.plugins.git.browser.GitRepositoryBrowser;
import hudson.plugins.git.browser.GithubWeb;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.impl.*;
import hudson.plugins.git.util.BuildChooserContext;
import hudson.plugins.git.util.BuildChooserContext.ContextCallable;
import hudson.plugins.git.util.BuildData;
import hudson.plugins.git.util.DefaultBuildChooser;
import hudson.plugins.git.util.GitUtils;
import hudson.plugins.parameterizedtrigger.BuildTrigger;
import hudson.plugins.parameterizedtrigger.ResultCondition;
import hudson.remoting.Callable;
import hudson.remoting.Channel;
import hudson.remoting.VirtualChannel;
import hudson.scm.ChangeLogSet;
import hudson.scm.PollingResult;
import hudson.slaves.DumbSlave;
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry;
import hudson.tools.ToolProperty;
import hudson.triggers.SCMTrigger;
import hudson.util.IOException2;
import hudson.util.StreamTaskListener;
import java.io.ByteArrayOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.jenkinsci.plugins.gitclient.*;
import org.jvnet.hudson.test.TestExtension;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.net.URL;
import java.util.*;
import org.eclipse.jgit.transport.RemoteConfig;
import org.jvnet.hudson.test.Issue;
import org.mockito.Mockito;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link GitSCM}.
* @author ishaaq
*/
public class GitSCMTest extends AbstractGitTestCase {
/**
* Basic test - create a GitSCM based project, check it out and build for the first time.
* Next test that polling works correctly, make another commit, check that polling finds it,
* then build it and finally test the build culprits as well as the contents of the workspace.
* @throws Exception if an exception gets thrown.
*/
public void testBasic() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have only one culprit", 1, culprits.size());
assertEquals("", janeDoe.getName(), culprits.iterator().next().getFullName());
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
public void testBasicRemotePoll() throws Exception {
// FreeStyleProject project = setupProject("master", true, false);
FreeStyleProject project = setupProject("master", false, null, null, null, true, null);
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
// ... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have only one culprit", 1, culprits.size());
assertEquals("", janeDoe.getName(), culprits.iterator().next().getFullName());
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
public void testBranchSpecWithRemotesMaster() throws Exception {
FreeStyleProject projectMasterBranch = setupProject("remotes/origin/master", false, null, null, null, true, null);
// create initial commit and build
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(projectMasterBranch, Result.SUCCESS, commitFile1);
}
public void testBranchSpecWithRemotesHierarchical() throws Exception {
FreeStyleProject projectMasterBranch = setupProject("master", false, null, null, null, true, null);
FreeStyleProject projectHierarchicalBranch = setupProject("remotes/origin/rel-1/xy", false, null, null, null, true, null);
// create initial commit
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
// create hierarchical branch, delete master branch, and build
git.branch("rel-1/xy");
git.checkout("rel-1/xy");
git.deleteBranch("master");
build(projectMasterBranch, Result.FAILURE);
build(projectHierarchicalBranch, Result.SUCCESS, commitFile1);
}
public void testBranchSpecUsingTagWithSlash() throws Exception {
FreeStyleProject projectMasterBranch = setupProject("path/tag", false, null, null, null, true, null);
// create initial commit and build
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1 will be tagged with path/tag");
testRepo.git.tag("path/tag", "tag with a slash in the tag name");
build(projectMasterBranch, Result.SUCCESS, commitFile1);
}
public void testBasicIncludedRegion() throws Exception {
FreeStyleProject project = setupProject("master", false, null, null, null, ".*3");
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertFalse("scm polling detected commit2 change, which should not have been included", project.poll(listener).hasChanges());
final String commitFile3 = "commitFile3";
commit(commitFile3, johnDoe, "Commit number 3");
assertTrue("scm polling did not detect commit3 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2, commitFile3);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have two culprit", 2, culprits.size());
PersonIdent[] expected = {johnDoe, janeDoe};
assertCulprits("jane doe and john doe should be the culprits", culprits, expected);
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertTrue(build2.getWorkspace().child(commitFile3).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
public void testIncludedRegionWithDeeperCommits() throws Exception {
FreeStyleProject project = setupProject("master", false, null, null, null, ".*3");
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertFalse("scm polling detected commit2 change, which should not have been included", project.poll(listener).hasChanges());
final String commitFile3 = "commitFile3";
commit(commitFile3, johnDoe, "Commit number 3");
final String commitFile4 = "commitFile4";
commit(commitFile4, janeDoe, "Commit number 4");
assertTrue("scm polling did not detect commit3 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2, commitFile3);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have two culprit", 2, culprits.size());
PersonIdent[] expected = {johnDoe, janeDoe};
assertCulprits("jane doe and john doe should be the culprits", culprits, expected);
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertTrue(build2.getWorkspace().child(commitFile3).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
public void testBasicExcludedRegion() throws Exception {
FreeStyleProject project = setupProject("master", false, null, ".*2", null, null);
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertFalse("scm polling detected commit2 change, which should have been excluded", project.poll(listener).hasChanges());
final String commitFile3 = "commitFile3";
commit(commitFile3, johnDoe, "Commit number 3");
assertTrue("scm polling did not detect commit3 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2, commitFile3);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have two culprit", 2, culprits.size());
PersonIdent[] expected = {johnDoe, janeDoe};
assertCulprits("jane doe and john doe should be the culprits", culprits, expected);
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertTrue(build2.getWorkspace().child(commitFile3).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
public void testCleanBeforeCheckout() throws Exception {
FreeStyleProject p = setupProject("master", false, null, null, "Jane Doe", null);
((GitSCM)p.getScm()).getExtensions().add(new CleanBeforeCheckout());
final String commitFile1 = "commitFile1";
final String commitFile2 = "commitFile2";
commit(commitFile1, johnDoe, janeDoe, "Commit number 1");
commit(commitFile2, johnDoe, janeDoe, "Commit number 2");
final FreeStyleBuild firstBuild = build(p, Result.SUCCESS, commitFile1);
final String branch1 = "Branch1";
final String branch2 = "Branch2";
List<BranchSpec> branches = new ArrayList<BranchSpec>();
branches.add(new BranchSpec("master"));
branches.add(new BranchSpec(branch1));
branches.add(new BranchSpec(branch2));
git.branch(branch1);
git.checkout(branch1);
p.poll(listener).hasChanges();
assertTrue(firstBuild.getLog().contains("Cleaning"));
assertTrue(firstBuild.getLog().indexOf("Cleaning") > firstBuild.getLog().indexOf("Cloning")); //clean should be after clone
assertTrue(firstBuild.getLog().indexOf("Cleaning") < firstBuild.getLog().indexOf("Checking out")); //clean before checkout
assertTrue(firstBuild.getWorkspace().child(commitFile1).exists());
git.checkout(branch1);
final FreeStyleBuild secondBuild = build(p, Result.SUCCESS, commitFile2);
p.poll(listener).hasChanges();
assertTrue(secondBuild.getLog().contains("Cleaning"));
assertTrue(secondBuild.getLog().indexOf("Cleaning") < secondBuild.getLog().indexOf("Fetching upstream changes"));
assertTrue(secondBuild.getWorkspace().child(commitFile2).exists());
}
@Issue("JENKINS-8342")
public void testExcludedRegionMultiCommit() throws Exception {
// Got 2 projects, each one should only build if changes in its own file
FreeStyleProject clientProject = setupProject("master", false, null, ".*serverFile", null, null);
FreeStyleProject serverProject = setupProject("master", false, null, ".*clientFile", null, null);
String initialCommitFile = "initialFile";
commit(initialCommitFile, johnDoe, "initial commit");
build(clientProject, Result.SUCCESS, initialCommitFile);
build(serverProject, Result.SUCCESS, initialCommitFile);
assertFalse("scm polling should not detect any more changes after initial build", clientProject.poll(listener).hasChanges());
assertFalse("scm polling should not detect any more changes after initial build", serverProject.poll(listener).hasChanges());
// Got commits on serverFile, so only server project should build.
commit("myserverFile", johnDoe, "commit first server file");
assertFalse("scm polling should not detect any changes in client project", clientProject.poll(listener).hasChanges());
assertTrue("scm polling did not detect changes in server project", serverProject.poll(listener).hasChanges());
// Got commits on both client and serverFile, so both projects should build.
commit("myNewserverFile", johnDoe, "commit new server file");
commit("myclientFile", johnDoe, "commit first clientfile");
assertTrue("scm polling did not detect changes in client project", clientProject.poll(listener).hasChanges());
assertTrue("scm polling did not detect changes in server project", serverProject.poll(listener).hasChanges());
}
/**
* With multiple branches specified in the project and having commits from a user
* excluded should not build the excluded revisions when another branch changes.
*/
/*
@Issue("JENKINS-8342")
public void testMultipleBranchWithExcludedUser() throws Exception {
final String branch1 = "Branch1";
final String branch2 = "Branch2";
List<BranchSpec> branches = new ArrayList<BranchSpec>();
branches.add(new BranchSpec("master"));
branches.add(new BranchSpec(branch1));
branches.add(new BranchSpec(branch2));
final FreeStyleProject project = setupProject(branches, false, null, null, janeDoe.getName(), null, false, null);
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
// create branches here so we can get back to them later...
git.branch(branch1);
git.branch(branch2);
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, "Commit number 2");
final String commitFile3 = "commitFile3";
commit(commitFile3, johnDoe, "Commit number 3");
assertTrue("scm polling should detect changes in 'master' branch", project.poll(listener).hasChanges());
build(project, Result.SUCCESS, commitFile1, commitFile2);
assertFalse("scm polling should not detect any more changes after last build", project.poll(listener).hasChanges());
// Add excluded commit
final String commitFile4 = "commitFile4";
commit(commitFile4, janeDoe, "Commit number 4");
assertFalse("scm polling detected change in 'master', which should have been excluded", project.poll(listener).hasChanges());
// now jump back...
git.checkout(branch1);
final String branch1File1 = "branch1File1";
commit(branch1File1, janeDoe, "Branch1 commit number 1");
assertFalse("scm polling detected change in 'Branch1', which should have been excluded", project.poll(listener).hasChanges());
// and the other branch...
git.checkout(branch2);
final String branch2File1 = "branch2File1";
commit(branch2File1, janeDoe, "Branch2 commit number 1");
assertFalse("scm polling detected change in 'Branch2', which should have been excluded", project.poll(listener).hasChanges());
final String branch2File2 = "branch2File2";
commit(branch2File2, johnDoe, "Branch2 commit number 2");
assertTrue("scm polling should detect changes in 'Branch2' branch", project.poll(listener).hasChanges());
//... and build it...
build(project, Result.SUCCESS, branch2File1, branch2File2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
// now jump back again...
git.checkout(branch1);
// Commit excluded after non-excluded commit, should trigger build.
final String branch1File2 = "branch1File2";
commit(branch1File2, johnDoe, "Branch1 commit number 2");
final String branch1File3 = "branch1File3";
commit(branch1File3, janeDoe, "Branch1 commit number 3");
assertTrue("scm polling should detect changes in 'Branch1' branch", project.poll(listener).hasChanges());
build(project, Result.SUCCESS, branch1File1, branch1File2, branch1File3);
} */
public void testBasicExcludedUser() throws Exception {
FreeStyleProject project = setupProject("master", false, null, null, "Jane Doe", null);
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertFalse("scm polling detected commit2 change, which should have been excluded", project.poll(listener).hasChanges());
final String commitFile3 = "commitFile3";
commit(commitFile3, johnDoe, "Commit number 3");
assertTrue("scm polling did not detect commit3 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2, commitFile3);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have two culprit", 2, culprits.size());
PersonIdent[] expected = {johnDoe, janeDoe};
assertCulprits("jane doe and john doe should be the culprits", culprits, expected);
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertTrue(build2.getWorkspace().child(commitFile3).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
public void testBasicInSubdir() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
((GitSCM)project.getScm()).getExtensions().add(new RelativeTargetDirectory("subdir"));
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, "subdir", Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, "subdir", Result.SUCCESS,
commitFile2);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have only one culprit", 1, culprits.size());
assertEquals("", janeDoe.getName(), culprits.iterator().next().getFullName());
assertEquals("The workspace should have a 'subdir' subdirectory, but does not.", true,
build2.getWorkspace().child("subdir").exists());
assertEquals("The 'subdir' subdirectory should contain commitFile2, but does not.", true,
build2.getWorkspace().child("subdir").child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
public void testBasicWithSlave() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
project.setAssignedLabel(createSlave().getSelfLabel());
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have only one culprit", 1, culprits.size());
assertEquals("", janeDoe.getName(), culprits.iterator().next().getFullName());
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
// For HUDSON-7547
public void testBasicWithSlaveNoExecutorsOnMaster() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
hudson.setNumExecutors(0);
hudson.setNodes(hudson.getNodes()); // TODO https://github.com/jenkinsci/jenkins/pull/1596 renders this workaround unnecessary
project.setAssignedLabel(createSlave().getSelfLabel());
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have only one culprit", 1, culprits.size());
assertEquals("", janeDoe.getName(), culprits.iterator().next().getFullName());
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
public void testAuthorOrCommitterFalse() throws Exception {
// Test with authorOrCommitter set to false and make sure we get the committer.
FreeStyleProject project = setupSimpleProject("master");
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, janeDoe, "Commit number 1");
final FreeStyleBuild firstBuild = build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, janeDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
final FreeStyleBuild secondBuild = build(project, Result.SUCCESS, commitFile2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final Set<User> secondCulprits = secondBuild.getCulprits();
assertEquals("The build should have only one culprit", 1, secondCulprits.size());
assertEquals("Did not get the committer as the change author with authorOrCommiter==false",
janeDoe.getName(), secondCulprits.iterator().next().getFullName());
}
public void testAuthorOrCommitterTrue() throws Exception {
// Next, test with authorOrCommitter set to true and make sure we get the author.
FreeStyleProject project = setupSimpleProject("master");
((GitSCM)project.getScm()).getExtensions().add(new AuthorInChangelog());
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, janeDoe, "Commit number 1");
final FreeStyleBuild firstBuild = build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, janeDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
final FreeStyleBuild secondBuild = build(project, Result.SUCCESS, commitFile2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final Set<User> secondCulprits = secondBuild.getCulprits();
assertEquals("The build should have only one culprit", 1, secondCulprits.size());
assertEquals("Did not get the author as the change author with authorOrCommiter==true",
johnDoe.getName(), secondCulprits.iterator().next().getFullName());
}
/**
* Method name is self-explanatory.
*/
public void testNewCommitToUntrackedBranchDoesNotTriggerBuild() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
//now create and checkout a new branch:
git.checkout(Constants.HEAD, "untracked");
//.. and commit to it:
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, "Commit number 2");
assertFalse("scm polling should not detect commit2 change because it is not in the branch we are tracking.", project.poll(listener).hasChanges());
}
private String checkoutString(FreeStyleProject project, String envVar) {
return "checkout -f " + getEnvVars(project).get(envVar);
}
public void testEnvVarsAvailable() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
FreeStyleBuild build1 = build(project, Result.SUCCESS, commitFile1);
assertEquals("origin/master", getEnvVars(project).get(GitSCM.GIT_BRANCH));
assertLogContains(getEnvVars(project).get(GitSCM.GIT_BRANCH), build1);
assertLogContains(checkoutString(project, GitSCM.GIT_COMMIT), build1);
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, "Commit number 2");
FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
assertLogNotContains(checkoutString(project, GitSCM.GIT_PREVIOUS_COMMIT), build2);
assertLogContains(checkoutString(project, GitSCM.GIT_PREVIOUS_COMMIT), build1);
assertLogNotContains(checkoutString(project, GitSCM.GIT_PREVIOUS_SUCCESSFUL_COMMIT), build2);
assertLogContains(checkoutString(project, GitSCM.GIT_PREVIOUS_SUCCESSFUL_COMMIT), build1);
}
// For HUDSON-7411
public void testNodeEnvVarsAvailable() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
Node s = createSlave();
setVariables(s, new Entry("TESTKEY", "slaveValue"));
project.setAssignedLabel(s.getSelfLabel());
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertEquals("slaveValue", getEnvVars(project).get("TESTKEY"));
}
/**
* A previous version of GitSCM would only build against branches, not tags. This test checks that that
* regression has been fixed.
*/
public void testGitSCMCanBuildAgainstTags() throws Exception {
final String mytag = "mytag";
FreeStyleProject project = setupSimpleProject(mytag);
build(project, Result.FAILURE); // fail, because there's nothing to be checked out here
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
// Try again. The first build will leave the repository in a bad state because we
// cloned something without even a HEAD - which will mean it will want to re-clone once there is some
// actual data.
build(project, Result.FAILURE); // fail, because there's nothing to be checked out here
//now create and checkout a new branch:
final String tmpBranch = "tmp";
git.branch(tmpBranch);
git.checkout(tmpBranch);
// commit to it
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, "Commit number 2");
assertFalse("scm polling should not detect any more changes since mytag is untouched right now", project.poll(listener).hasChanges());
build(project, Result.FAILURE); // fail, because there's nothing to be checked out here
// tag it, then delete the tmp branch
git.tag(mytag, "mytag initial");
git.checkout("master");
git.deleteBranch(tmpBranch);
// at this point we're back on master, there are no other branches, tag "mytag" exists but is
// not part of "master"
assertTrue("scm polling should detect commit2 change in 'mytag'", project.poll(listener).hasChanges());
build(project, Result.SUCCESS, commitFile2);
assertFalse("scm polling should not detect any more changes after last build", project.poll(listener).hasChanges());
// now, create tmp branch again against mytag:
git.checkout(mytag);
git.branch(tmpBranch);
// another commit:
final String commitFile3 = "commitFile3";
commit(commitFile3, johnDoe, "Commit number 3");
assertFalse("scm polling should not detect any more changes since mytag is untouched right now", project.poll(listener).hasChanges());
// now we're going to force mytag to point to the new commit, if everything goes well, gitSCM should pick the change up:
git.tag(mytag, "mytag moved");
git.checkout("master");
git.deleteBranch(tmpBranch);
// at this point we're back on master, there are no other branches, "mytag" has been updated to a new commit:
assertTrue("scm polling should detect commit3 change in 'mytag'", project.poll(listener).hasChanges());
build(project, Result.SUCCESS, commitFile3);
assertFalse("scm polling should not detect any more changes after last build", project.poll(listener).hasChanges());
}
/**
* Not specifying a branch string in the project implies that we should be polling for changes in
* all branches.
*/
public void testMultipleBranchBuild() throws Exception {
// empty string will result in a project that tracks against changes in all branches:
final FreeStyleProject project = setupSimpleProject("");
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
// create a branch here so we can get back to this point later...
final String fork = "fork";
git.branch(fork);
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, "Commit number 2");
final String commitFile3 = "commitFile3";
commit(commitFile3, johnDoe, "Commit number 3");
assertTrue("scm polling should detect changes in 'master' branch", project.poll(listener).hasChanges());
build(project, Result.SUCCESS, commitFile1, commitFile2);
assertFalse("scm polling should not detect any more changes after last build", project.poll(listener).hasChanges());
// now jump back...
git.checkout(fork);
// add some commits to the fork branch...
final String forkFile1 = "forkFile1";
commit(forkFile1, johnDoe, "Fork commit number 1");
final String forkFile2 = "forkFile2";
commit(forkFile2, johnDoe, "Fork commit number 2");
assertTrue("scm polling should detect changes in 'fork' branch", project.poll(listener).hasChanges());
build(project, Result.SUCCESS, forkFile1, forkFile2);
assertFalse("scm polling should not detect any more changes after last build", project.poll(listener).hasChanges());
}
@Issue("JENKINS-19037")
@SuppressWarnings("ResultOfObjectAllocationIgnored")
public void testBlankRepositoryName() throws Exception {
new GitSCM(null);
}
@Issue("JENKINS-10060")
public void testSubmoduleFixup() throws Exception {
File repo = createTmpDir();
FilePath moduleWs = new FilePath(repo);
org.jenkinsci.plugins.gitclient.GitClient moduleRepo = Git.with(listener, new EnvVars()).in(repo).getClient();
{// first we create a Git repository with submodule
moduleRepo.init();
moduleWs.child("a").touch(0);
moduleRepo.add("a");
moduleRepo.commit("creating a module");
git.addSubmodule(repo.getAbsolutePath(), "module1");
git.commit("creating a super project");
}
// configure two uproject 'u' -> 'd' that's chained together.
FreeStyleProject u = createFreeStyleProject();
FreeStyleProject d = createFreeStyleProject();
u.setScm(new GitSCM(workDir.getPath()));
u.getPublishersList().add(new BuildTrigger(new hudson.plugins.parameterizedtrigger.BuildTriggerConfig(d.getName(), ResultCondition.SUCCESS,
new GitRevisionBuildParameters())));
d.setScm(new GitSCM(workDir.getPath()));
hudson.rebuildDependencyGraph();
FreeStyleBuild ub = assertBuildStatusSuccess(u.scheduleBuild2(0));
System.out.println(ub.getLog());
for (int i=0; (d.getLastBuild()==null || d.getLastBuild().isBuilding()) && i<100; i++) // wait only up to 10 sec to avoid infinite loop
Thread.sleep(100);
FreeStyleBuild db = d.getLastBuild();
assertNotNull("downstream build didn't happen",db);
assertBuildStatusSuccess(db);
}
public void testBuildChooserContext() throws Exception {
final FreeStyleProject p = createFreeStyleProject();
final FreeStyleBuild b = assertBuildStatusSuccess(p.scheduleBuild2(0));
BuildChooserContextImpl c = new BuildChooserContextImpl(p, b, null);
c.actOnBuild(new ContextCallable<Run<?,?>, Object>() {
public Object invoke(Run param, VirtualChannel channel) throws IOException, InterruptedException {
assertSame(param,b);
return null;
}
});
c.actOnProject(new ContextCallable<Job<?,?>, Object>() {
public Object invoke(Job param, VirtualChannel channel) throws IOException, InterruptedException {
assertSame(param,p);
return null;
}
});
DumbSlave s = createOnlineSlave();
assertEquals(p.toString(), s.getChannel().call(new BuildChooserContextTestCallable(c)));
}
private static class BuildChooserContextTestCallable implements Callable<String,IOException> {
private final BuildChooserContext c;
public BuildChooserContextTestCallable(BuildChooserContext c) {
this.c = c;
}
public String call() throws IOException {
try {
return c.actOnProject(new ContextCallable<Job<?,?>, String>() {
public String invoke(Job<?,?> param, VirtualChannel channel) throws IOException, InterruptedException {
assertTrue(channel instanceof Channel);
assertTrue(Hudson.getInstance()!=null);
return param.toString();
}
});
} catch (InterruptedException e) {
throw new IOException2(e);
}
}
}
// eg: "jane doe and john doe should be the culprits", culprits, [johnDoe, janeDoe])
static public void assertCulprits(String assertMsg, Set<User> actual, PersonIdent[] expected)
{
Collection<String> fullNames = Collections2.transform(actual, new Function<User,String>() {
public String apply(User u)
{
return u.getFullName();
}
});
for(PersonIdent p : expected)
{
assertTrue(assertMsg, fullNames.contains(p.getName()));
}
}
public void testEmailCommitter() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
// setup global config
final DescriptorImpl descriptor = (DescriptorImpl) project.getScm().getDescriptor();
descriptor.setCreateAccountBasedOnEmail(true);
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
final FreeStyleBuild build = build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
final PersonIdent jeffDoe = new PersonIdent("Jeff Doe", "jeff@doe.com");
commit(commitFile2, jeffDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have only one culprit", 1, culprits.size());
User culprit = culprits.iterator().next();
assertEquals("", jeffDoe.getEmailAddress(), culprit.getId());
assertEquals("", jeffDoe.getName(), culprit.getFullName());
assertBuildStatusSuccess(build);
}
public void testFetchFromMultipleRepositories() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
TestGitRepo secondTestRepo = new TestGitRepo("second", this, listener);
List<UserRemoteConfig> remotes = new ArrayList<UserRemoteConfig>();
remotes.addAll(testRepo.remoteConfigs());
remotes.addAll(secondTestRepo.remoteConfigs());
project.setScm(new GitSCM(
remotes,
Collections.singletonList(new BranchSpec("master")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null,
Collections.<GitSCMExtension>emptyList()));
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
final String commitFile2 = "commitFile2";
secondTestRepo.commit(commitFile2, janeDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
@Issue("JENKINS-25639")
public void testCommitDetectedOnlyOnceInMultipleRepositories() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
TestGitRepo secondTestRepo = new TestGitRepo("secondRepo", this, listener);
List<UserRemoteConfig> remotes = new ArrayList<UserRemoteConfig>();
remotes.addAll(testRepo.remoteConfigs());
remotes.addAll(secondTestRepo.remoteConfigs());
GitSCM gitSCM = new GitSCM(
remotes,
Collections.singletonList(new BranchSpec("origin/master")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null,
Collections.<GitSCMExtension>emptyList());
project.setScm(gitSCM);
commit("commitFile1", johnDoe, "Commit number 1");
FreeStyleBuild build = build(project, Result.SUCCESS, "commitFile1");
commit("commitFile2", johnDoe, "Commit number 2");
git = Git.with(listener, new EnvVars()).in(build.getWorkspace()).getClient();
for (RemoteConfig remoteConfig : gitSCM.getRepositories()) {
git.fetch_().from(remoteConfig.getURIs().get(0), remoteConfig.getFetchRefSpecs());
}
Collection<Revision> candidateRevisions = ((DefaultBuildChooser) (gitSCM).getBuildChooser()).getCandidateRevisions(false, "origin/master", git, listener, project.getLastBuild().getAction(BuildData.class), null);
assertEquals(1, candidateRevisions.size());
}
public void testMerge() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
GitSCM scm = new GitSCM(
createRemoteRepositories(),
Collections.singletonList(new BranchSpec("*")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null,
Collections.<GitSCMExtension>emptyList());
scm.getExtensions().add(new PreBuildMerge(new UserMergeOptions("origin", "integration", "default", MergeCommand.GitPluginFastForwardMode.FF)));
project.setScm(scm);
// create initial commit and then run the build against it:
commit("commitFileBase", johnDoe, "Initial Commit");
testRepo.git.branch("integration");
build(project, Result.SUCCESS, "commitFileBase");
testRepo.git.checkout(null, "topic1");
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
final FreeStyleBuild build1 = build(project, Result.SUCCESS, commitFile1);
assertTrue(build1.getWorkspace().child(commitFile1).exists());
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
// do what the GitPublisher would do
testRepo.git.deleteBranch("integration");
testRepo.git.checkout("topic1", "integration");
testRepo.git.checkout("master", "topic2");
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}
@Issue("JENKINS-20392")
public void testMergeChangelog() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
GitSCM scm = new GitSCM(
createRemoteRepositories(),
Collections.singletonList(new BranchSpec("*")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null,
Collections.<GitSCMExtension>emptyList());
scm.getExtensions().add(new PreBuildMerge(new UserMergeOptions("origin", "integration", "default", MergeCommand.GitPluginFastForwardMode.FF)));
project.setScm(scm);
// create initial commit and then run the build against it:
// Here the changelog is by default empty (because changelog for first commit is always empty
commit("commitFileBase", johnDoe, "Initial Commit");
testRepo.git.branch("integration");
build(project, Result.SUCCESS, "commitFileBase");
// Create second commit and run build
// Here the changelog should contain exactly this one new commit
testRepo.git.checkout("master", "topic2");
final String commitFile2 = "commitFile2";
String commitMessage = "Commit number 2";
commit(commitFile2, johnDoe, commitMessage);
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
ChangeLogSet<? extends ChangeLogSet.Entry> changeLog = build2.getChangeSet();
assertEquals("Changelog should contain one item", 1, changeLog.getItems().length);
GitChangeSet singleChange = (GitChangeSet) changeLog.getItems()[0];
assertEquals("Changelog should contain commit number 2", commitMessage, singleChange.getComment().trim());
}
public void testMergeWithSlave() throws Exception {
FreeStyleProject project = setupSimpleProject("master");
project.setAssignedLabel(createSlave().getSelfLabel());
GitSCM scm = new GitSCM(
createRemoteRepositories(),
Collections.singletonList(new BranchSpec("*")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null,
Collections.<GitSCMExtension>emptyList());
scm.getExtensions().add(new PreBuildMerge(new UserMergeOptions("origin", "integration", null, null)));
project.setScm(scm);
// create initial commit and then run the build against it:
commit("commitFileBase", johnDoe, "Initial Commit");
testRepo.git.branch("integration");
build(project, Result.SUCCESS, "commitFileBase");
testRepo.git.checkout(null, "topic1");
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
final FreeStyleBuild build1 = build(project, Result.SUCCESS, commitFile1);
assertTrue(build1.getWorkspace().child(commitFile1).exists());
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
// do what the GitPublisher would do
testRepo.git.deleteBranch("integration");
testRepo.git.checkout("topic1", "integration");
testRepo.git.checkout("master", "topic2");