File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
GitVersion.Core.Tests/BuildAgents
GitVersion.Core/BuildAgents Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ public void GetCurrentBranchShouldHandleBranches()
52
52
{
53
53
// Arrange
54
54
this . environment . SetEnvironmentVariable ( "BUILDKITE_BRANCH" , MainBranch ) ;
55
+ this . environment . SetEnvironmentVariable ( "BUILDKITE_PULL_REQUEST" , "false" ) ;
55
56
56
57
// Act
57
58
var result = this . buildServer . GetCurrentBranch ( false ) ;
@@ -60,6 +61,20 @@ public void GetCurrentBranchShouldHandleBranches()
60
61
result . ShouldBe ( MainBranch ) ;
61
62
}
62
63
64
+ [ Test ]
65
+ public void GetCurrentBranchShouldHandlePullRequests ( )
66
+ {
67
+ // Arrange
68
+ this . environment . SetEnvironmentVariable ( "BUILDKITE_BRANCH" , "feature/new" ) ;
69
+ this . environment . SetEnvironmentVariable ( "BUILDKITE_PULL_REQUEST" , "55" ) ;
70
+
71
+ // Act
72
+ var result = this . buildServer . GetCurrentBranch ( false ) ;
73
+
74
+ // Assert
75
+ result . ShouldBe ( "refs/pull/55/head" ) ;
76
+ }
77
+
63
78
[ Test ]
64
79
public void GetSetParameterMessageShouldReturnEmptyArray ( )
65
80
{
Original file line number Diff line number Diff line change @@ -21,7 +21,20 @@ public override string GenerateSetVersionMessage(VersionVariables variables) =>
21
21
public override string [ ] GenerateSetParameterMessage ( string name , string value ) =>
22
22
Array . Empty < string > ( ) ; // There is no equivalent function in BuildKite.
23
23
24
- public override string ? GetCurrentBranch ( bool usingDynamicRepos ) => Environment . GetEnvironmentVariable ( "BUILDKITE_BRANCH" ) ;
24
+ public override string ? GetCurrentBranch ( bool usingDynamicRepos )
25
+ {
26
+ var pullRequest = Environment . GetEnvironmentVariable ( "BUILDKITE_PULL_REQUEST" ) ;
27
+ if ( string . IsNullOrEmpty ( pullRequest ) || pullRequest == "false" )
28
+ {
29
+ return Environment . GetEnvironmentVariable ( "BUILDKITE_BRANCH" ) ;
30
+ }
31
+ else
32
+ {
33
+ // For pull requests BUILDKITE_BRANCH refers to the head, so adjust the
34
+ // branch name for pull request versioning to function as expected
35
+ return string . Format ( "refs/pull/{0}/head" , pullRequest ) ;
36
+ }
37
+ }
25
38
26
39
public override bool PreventFetch ( ) => true ;
27
40
}
You can’t perform that action at this time.
0 commit comments