Skip to content

Commit bbaf92c

Browse files
gitfoolarturcic
authored andcommitted
Fix GitHub Actions current branch and prevent fetch
1 parent 0e9e479 commit bbaf92c

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,42 @@ public void CanApplyToCurrentContextShouldBeFalseWhenEnvironmentVariableIsNotSet
5858
}
5959

6060
[Test]
61-
public void GetCurrentBranchShouldGetBranchIfSet()
61+
public void GetCurrentBranchShouldHandleBranches()
6262
{
6363
// Arrange
64-
const string expected = "actionsBranch";
64+
environment.SetEnvironmentVariable("GITHUB_REF", "refs/heads/master");
6565

66-
environment.SetEnvironmentVariable("GITHUB_REF", $"refs/heads/{expected}");
66+
// Act
67+
var result = buildServer.GetCurrentBranch(false);
68+
69+
// Assert
70+
result.ShouldBe("refs/heads/master");
71+
}
72+
73+
[Test]
74+
public void GetCurrentBranchShouldHandleTags()
75+
{
76+
// Arrange
77+
environment.SetEnvironmentVariable("GITHUB_REF", "refs/tags/1.0.0");
6778

6879
// Act
6980
var result = buildServer.GetCurrentBranch(false);
7081

7182
// Assert
72-
result.ShouldBe(expected);
83+
result.ShouldBe("refs/tags/1.0.0");
7384
}
7485

7586
[Test]
76-
public void GetCurrentBranchShouldNotMatchTag()
87+
public void GetCurrentBranchShouldHandlePullRequests()
7788
{
7889
// Arrange
79-
environment.SetEnvironmentVariable("GITHUB_REF", $"refs/tags/v1.0.0");
90+
environment.SetEnvironmentVariable("GITHUB_REF", "refs/pull/1/merge");
8091

8192
// Act
8293
var result = buildServer.GetCurrentBranch(false);
8394

8495
// Assert
85-
result.ShouldBeNull();
96+
result.ShouldBe("refs/pull/1/merge");
8697
}
8798

8899
[TestCase("Something", "1.0.0",

src/GitVersionCore/BuildServers/GitHubActions.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,9 @@ public override string[] GenerateSetParameterMessage(string name, string value)
4242

4343
public override string GetCurrentBranch(bool usingDynamicRepos)
4444
{
45-
// GITHUB_REF
46-
// The branch or tag ref that triggered the workflow.
47-
// For example, refs/heads/feature-branch-1. If neither a branch or
48-
// tag is available for the event type, the variable will not exist.
49-
50-
var value = Environment.GetEnvironmentVariable("GITHUB_REF");
51-
if (!string.IsNullOrWhiteSpace(value))
52-
{
53-
const string refsHeads = "refs/heads/";
54-
55-
if (value.StartsWith(refsHeads))
56-
{
57-
value = value.Substring(refsHeads.Length);
58-
return value;
59-
}
60-
}
61-
62-
return base.GetCurrentBranch(usingDynamicRepos);
45+
return Environment.GetEnvironmentVariable("GITHUB_REF");
6346
}
6447

65-
public override bool PreventFetch() => false;
48+
public override bool PreventFetch() => true;
6649
}
6750
}

0 commit comments

Comments
 (0)