1
+ using System ;
2
+ using GitTools . Testing ;
3
+ using LibGit2Sharp ;
4
+ using NUnit . Framework ;
5
+ using Shouldly ;
6
+
7
+ [ TestFixture ]
8
+ public class PullRequestInJenkinsTest
9
+ {
10
+ [ TestCase ]
11
+ public void GivenJenkinsPipelineHasDuplicatedOrigin_VersionIsCalculatedProperly ( )
12
+ {
13
+ string pipelineBranch = "BRANCH_NAME" ;
14
+ string pipelineBranchOrig = Environment . GetEnvironmentVariable ( pipelineBranch ) ;
15
+
16
+ using ( var fixture = new EmptyRepositoryFixture ( ) )
17
+ {
18
+ var remoteRepositoryPath = PathHelper . GetTempPath ( ) ;
19
+ Repository . Init ( remoteRepositoryPath ) ;
20
+ using ( var remoteRepository = new Repository ( remoteRepositoryPath ) )
21
+ {
22
+ remoteRepository . Config . Set ( "user.name" , "Test" ) ;
23
+ remoteRepository . Config . Set ( "user.email" , "[email protected] " ) ;
24
+ fixture . Repository . Network . Remotes . Add ( "origin" , remoteRepositoryPath ) ;
25
+ // Jenkins Pipeline will create a duplicate origin:
26
+ fixture . Repository . Network . Remotes . Add ( "origin1" , remoteRepositoryPath ) ;
27
+ Console . WriteLine ( "Created git repository at {0}" , remoteRepositoryPath ) ;
28
+ remoteRepository . MakeATaggedCommit ( "1.0.3" ) ;
29
+
30
+ var branch = remoteRepository . CreateBranch ( "FeatureBranch" ) ;
31
+ Commands . Checkout ( remoteRepository , branch ) ;
32
+ remoteRepository . MakeCommits ( 2 ) ;
33
+ Commands . Checkout ( remoteRepository , remoteRepository . Head . Tip . Sha ) ;
34
+ //Emulate merge commit
35
+ var mergeCommitSha = remoteRepository . MakeACommit ( ) . Sha ;
36
+ Commands . Checkout ( remoteRepository , "master" ) ; // HEAD cannot be pointing at the merge commit
37
+ remoteRepository . Refs . Add ( "refs/heads/pull/5/head" , new ObjectId ( mergeCommitSha ) ) ;
38
+
39
+ // Checkout PR commit
40
+ Commands . Fetch ( ( Repository ) fixture . Repository , "origin" , new string [ 0 ] , new FetchOptions ( ) , null ) ;
41
+ Commands . Checkout ( fixture . Repository , mergeCommitSha ) ;
42
+ }
43
+
44
+ // Emulating Jenkins environment variable
45
+ Environment . SetEnvironmentVariable ( pipelineBranch , "PR-5" ) ;
46
+ Environment . SetEnvironmentVariable ( "JENKINS_URL" , "url" , EnvironmentVariableTarget . Process ) ;
47
+
48
+ var result = GitVersionHelper . ExecuteIn ( fixture . RepositoryPath ) ;
49
+
50
+ result . ExitCode . ShouldBe ( 0 ) ;
51
+ result . OutputVariables . FullSemVer . ShouldBe ( "1.0.4-PullRequest0005.3" ) ;
52
+
53
+ // Cleanup repository files
54
+ DirectoryHelper . DeleteDirectory ( remoteRepositoryPath ) ;
55
+
56
+ Environment . SetEnvironmentVariable ( pipelineBranch , pipelineBranchOrig ) ;
57
+ Environment . SetEnvironmentVariable ( "JENKINS_URL" , null , EnvironmentVariableTarget . Process ) ;
58
+ }
59
+ }
60
+ }
0 commit comments