Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
(GH-102) Add ability to correctly set pull request on github-actions
Browse files Browse the repository at this point in the history
fixes #102
  • Loading branch information
AdmiringWorm committed Jun 9, 2020
1 parent 62af77d commit fba2511
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void Branch_Should_Be_Empty_When_Environment_Variable_Does_Not_Exist()
// Given
var ga = new Mock<GitHubAction>() { CallBase = true };
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_REF")).Returns(string.Empty);
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_HEAD_REF")).Returns(string.Empty);
var githubAction = ga.Object;

// When
Expand All @@ -22,12 +23,29 @@ public void Branch_Should_Be_Empty_When_Environment_Variable_Does_Not_Exist()
branch.Should().BeEmpty();
}

[Fact]
public void Branch_Should_Be_Set_From_Head_Ref_When_Environment_Variable_Exist()
{
// Given
var ga = new Mock<GitHubAction>() { CallBase = true };
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_REF")).Returns("refs/pull/234/merge");
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_HEAD_REF")).Returns("develop");
var githubAction = ga.Object;

// When
var branch = githubAction.Branch;

// Then
branch.Should().Be("develop");
}

[Fact]
public void Branch_Should_Be_Set_When_Enviornment_Variable_Exits()
{
// Given
var ga = new Mock<GitHubAction>() { CallBase = true };
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_REF")).Returns("ref/heads/develop");
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_REF")).Returns("refs/heads/develop");
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_HEAD_REF")).Returns(string.Empty);
var githubAction = ga.Object;

// When
Expand Down Expand Up @@ -128,6 +146,39 @@ public void Detecter_Should_Be_True_When_Actions_Environment_Variable_Exist_And_
detecter.Should().BeTrue();
}

[Fact]
public void PR_Should_Not_Be_Empty_When_Environment_Variables_Exist()
{
// Given
var ga = new Mock<GitHubAction>() { CallBase = true };
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_HEAD_REF")).Returns("patch-2");
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_REF")).Returns("refs/pull/7/merge");
var githubAction = ga.Object;

// When
var pr = githubAction.Pr;
var branch = githubAction.Branch;

// Then
pr.Should().Be("7");
branch.Should().Be("patch-2");
}

[Fact]
public void PR_Should_Not_be_Set_If_Head_Ref_Is_Empyt()
{
// Given
var ga = new Mock<GitHubAction>() { CallBase = true };
ga.Setup(s => s.GetEnvironmentVariable("GITHUB_HEAD_REF")).Returns(string.Empty);
var githubAction = ga.Object;

// When
var pr = githubAction.Pr;

// THen
pr.Should().BeEmpty();
}

[Fact]
public void Service_Should_Be_Set_To_GitHubActions()
{
Expand Down
1 change: 1 addition & 0 deletions Source/Codecov.Tool/Codecov.Tool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<CodeAnalysisRuleSet>.\Codecov.ruleset</CodeAnalysisRuleSet>
<PackageId>Codecov.Tool</PackageId>
<ToolCommandName>codecov</ToolCommandName>
Expand Down
1 change: 1 addition & 0 deletions Source/Codecov/Codecov.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup Condition="'$(TargetFrameworks)' == ''">
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifiers>win7-x64;win7-x86;linux-x64;osx-x64</RuntimeIdentifiers>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ internal class GitHubAction : ContinuousIntegrationServer
private readonly Lazy<string> _branch;
private readonly Lazy<string> _commit;
private readonly Lazy<bool> _detecter;
private readonly Lazy<string> _pr;
private readonly Lazy<string> _slug;

public GitHubAction()
{
_branch = new Lazy<string>(LoadBranch);
_commit = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_SHA"));
_detecter = new Lazy<bool>(() => CheckEnvironmentVariables("GITHUB_ACTIONS") || !string.IsNullOrWhiteSpace(GetEnvironmentVariable("GITHUB_ACTION")));
_pr = new Lazy<string>(LoadPullRequest);
_slug = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_REPOSITORY"));
}

Expand All @@ -23,15 +25,69 @@ public GitHubAction()

public override bool Detecter => _detecter.Value;

public override string Pr => _pr.Value;

public override string Service => "github-actions";

public override string Slug => _slug.Value;

private static string ExtractSubstring(string text, string prefix, string postfix)
{
if (string.IsNullOrWhiteSpace(text))
{
return string.Empty;
}

var textLength = text.Length;

var startIndex = string.IsNullOrEmpty(prefix) ? 0 : text.IndexOf(prefix);
var endIndex = string.IsNullOrEmpty(postfix) ? textLength : text.IndexOf(postfix);
if (startIndex == -1)
{
startIndex = 0;
}
else if (!string.IsNullOrEmpty(prefix))
{
startIndex += prefix.Length;
}

if (endIndex == -1)
{
endIndex = textLength;
}

return text.Substring(startIndex, endIndex - startIndex);
}

private string LoadBranch()
{
var headRef = GetEnvironmentVariable("GITHUB_HEAD_REF");
if (!string.IsNullOrWhiteSpace(headRef))
{
return headRef;
}

var branch = GetEnvironmentVariable("GITHUB_REF");

return string.IsNullOrWhiteSpace(branch) ? string.Empty : branch.StartsWith("ref/heads/") ? branch.Substring(10) : branch;
return ExtractSubstring(branch, "refs/heads/", null);
}

private string LoadPullRequest()
{
var headRef = GetEnvironmentVariable("GITHUB_HEAD_REF");
if (string.IsNullOrEmpty(headRef))
{
return string.Empty;
}

var branchRef = GetEnvironmentVariable("GITHUB_REF");

if (string.IsNullOrEmpty(branchRef))
{
return string.Empty;
}

return ExtractSubstring(branchRef, "refs/pull/", "/merge");
}
}
}

0 comments on commit fba2511

Please sign in to comment.