Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH3764: Add missing GitHub Actions properties #3765

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Cake.Common.Tests/Fixtures/Build/GitHubActionsInfoFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public GitHubActionsInfoFixture()
Environment.GetEnvironmentVariable("RUNNER_TEMP").Returns("/home/runner/work/_temp");
Environment.GetEnvironmentVariable("RUNNER_TOOL_CACHE").Returns("/opt/hostedtoolcache");
Environment.GetEnvironmentVariable("RUNNER_WORKSPACE").Returns("/home/runner/work/cake");
Environment.GetEnvironmentVariable("ImageOS").Returns("ubuntu20");
Environment.GetEnvironmentVariable("ImageVersion").Returns("20211209.3");
Environment.GetEnvironmentVariable("RUNNER_USER").Returns("runner");

Environment.GetEnvironmentVariable("GITHUB_ACTION").Returns("run1");
Environment.GetEnvironmentVariable("GITHUB_ACTION_PATH").Returns("/path/to/action");
Expand All @@ -46,6 +49,9 @@ public GitHubActionsInfoFixture()
Environment.GetEnvironmentVariable("GITHUB_SHA").Returns("d1e4f990f57349334368c8253382abc63be02d73");
Environment.GetEnvironmentVariable("GITHUB_WORKFLOW").Returns("Build");
Environment.GetEnvironmentVariable("GITHUB_WORKSPACE").Returns("/home/runner/work/cake/cake");
Environment.GetEnvironmentVariable("GITHUB_RUN_ATTEMPT").Returns("2");
Environment.GetEnvironmentVariable("GITHUB_REF_PROTECTED").Returns("true");
Environment.GetEnvironmentVariable("GITHUB_REF_NAME").Returns("main");

Environment.GetEnvironmentVariable("ACTIONS_RUNTIME_TOKEN").Returns(ActionRuntimeToken);
Environment.GetEnvironmentVariable("ACTIONS_RUNTIME_URL").Returns(ActionRuntimeUrl);
Expand All @@ -54,13 +60,15 @@ public GitHubActionsInfoFixture()
Environment.WorkingDirectory.Returns("/home/runner/work/cake/cake");
}

public GitHubActionsRunnerInfo CreateRunnerInfo()
public GitHubActionsRunnerInfo CreateRunnerInfo(string architecture = null)
{
Environment.GetEnvironmentVariable("RUNNER_ARCH").Returns(architecture);
return new GitHubActionsRunnerInfo(Environment);
}

public GitHubActionsWorkflowInfo CreateWorkflowInfo()
public GitHubActionsWorkflowInfo CreateWorkflowInfo(string refType = null)
{
Environment.GetEnvironmentVariable("GITHUB_REF_TYPE").Returns(refType);
return new GitHubActionsWorkflowInfo(Environment);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Build.GitHubActions.Data;
using Cake.Common.Tests.Fixtures.Build;
using Xunit;

Expand Down Expand Up @@ -89,5 +90,74 @@ public void Should_Return_Correct_Value()
Assert.Equal("/home/runner/work/cake", result);
}
}

public sealed class TheImageOSProperty
{
[Fact]
public void Should_Return_Correct_Value()
{
// Given
var info = new GitHubActionsInfoFixture().CreateRunnerInfo();

// When
var result = info.ImageOS;

// Then
Assert.Equal("ubuntu20", result);
}
}

public sealed class TheImageVersionProperty
{
[Fact]
public void Should_Return_Correct_Value()
{
// Given
var info = new GitHubActionsInfoFixture().CreateRunnerInfo();

// When
var result = info.ImageVersion;

// Then
Assert.Equal("20211209.3", result);
}
}

public sealed class TheUserProperty
{
[Fact]
public void Should_Return_Correct_Value()
{
// Given
var info = new GitHubActionsInfoFixture().CreateRunnerInfo();

// When
var result = info.User;

// Then
Assert.Equal("runner", result);
}
}

public sealed class TheArchitectureProperty
{
[Theory]
[InlineData("X86", GitHubActionsArchitecture.X86)]
[InlineData("X64", GitHubActionsArchitecture.X64)]
[InlineData("ARM", GitHubActionsArchitecture.ARM)]
[InlineData("ARM64", GitHubActionsArchitecture.ARM64)]
[InlineData("", GitHubActionsArchitecture.Unknown)]
public void Should_Return_Correct_Value(string value, GitHubActionsArchitecture expected)
{
// Given
var info = new GitHubActionsInfoFixture().CreateRunnerInfo(architecture: value);

// When
var result = info.Architecture;

// Then
Assert.Equal(expected, result);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Common.Build.GitHubActions.Data;
using Cake.Common.Tests.Fixtures.Build;
using Xunit;

Expand Down Expand Up @@ -312,5 +313,72 @@ public void Should_Return_Correct_Value()
Assert.Equal("/home/runner/work/cake/cake", result);
}
}

public sealed class TheAttemptProperty
{
[Fact]
public void Should_Return_Correct_Value()
{
// Given
var info = new GitHubActionsInfoFixture().CreateWorkflowInfo();

// When
var result = info.Attempt;

// Then
Assert.Equal(2, result);
}
}

public sealed class TheRefProtectedProperty
{
[Fact]
public void Should_Return_Correct_Value()
{
// Given
var info = new GitHubActionsInfoFixture().CreateWorkflowInfo();

// When
var result = info.RefProtected;

// Then
Assert.True(result);
}
}

public sealed class TheRefNameProperty
{
[Fact]
public void Should_Return_Correct_Value()
{
// Given
var info = new GitHubActionsInfoFixture().CreateWorkflowInfo();

// When
var result = info.RefName;

// Then
Assert.Equal("main", result);
}
}

public sealed class TheRefTypeProperty
{
[Theory]
[InlineData("branch", GitHubActionsRefType.Branch)]
[InlineData("tag", GitHubActionsRefType.Tag)]
[InlineData("", GitHubActionsRefType.Unknown)]
public void Should_Return_Correct_Value(string value, GitHubActionsRefType expected)
{
// Given
var info = new GitHubActionsInfoFixture().CreateWorkflowInfo(refType: value);

// When
var result = info.RefType;

// Then
Assert.Equal(expected, result);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Cake.Common.Build.GitHubActions.Data
{
/// <summary>
/// The GitHub Actions Architecture.
/// </summary>
public enum GitHubActionsArchitecture
{
/// <summary>
/// Unknown.
/// </summary>
Unknown,

/// <summary>
/// X86.
/// </summary>
X86,

/// <summary>
/// X64.
/// </summary>
X64,

/// <summary>
/// ARM.
/// </summary>
ARM,

/// <summary>
/// ARM64.
/// </summary>
ARM64
}
}
27 changes: 27 additions & 0 deletions src/Cake.Common/Build/GitHubActions/Data/GitHubActionsRefType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Cake.Common.Build.GitHubActions.Data
{
/// <summary>
/// The GitHub Actions Ref Type.
/// </summary>
public enum GitHubActionsRefType
{
/// <summary>
/// Unknown.
/// </summary>
Unknown,

/// <summary>
/// Branch.
/// </summary>
Branch,

/// <summary>
/// Tag.
/// </summary>
Tag
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,45 @@ public GitHubActionsRunnerInfo(ICakeEnvironment environment)
/// The runner workspace directory path.
/// </value>
public DirectoryPath Workspace => GetEnvironmentDirectoryPath("RUNNER_WORKSPACE");

/// <summary>
/// Gets the runner image OS on hosted agents.
/// </summary>
/// <value>
/// The runner image OS on hosted agents.
/// </value>
public string ImageOS => GetEnvironmentString("ImageOS");

/// <summary>
/// Gets the runner image version on hosted agents.
/// </summary>
/// <value>
/// The runner image version on hosted agents.
/// </value>
public string ImageVersion => GetEnvironmentString("ImageVersion");

/// <summary>
/// Gets the runner user name.
/// </summary>
/// <value>
/// The runner user name.
/// </value>
public string User => GetEnvironmentString("RUNNER_USER");

/// <summary>
/// Gets the runner architecture of the runner executing the job.
/// </summary>
/// <value>
/// The runner architecture of the runner executing the job. Possible values are X86, X64, ARM, and ARM64.
/// </value>
public GitHubActionsArchitecture Architecture => GetEnvironmentString("RUNNER_ARCH")
?.ToUpperInvariant() switch
{
"X86" => GitHubActionsArchitecture.X86,
"X64" => GitHubActionsArchitecture.X64,
"ARM" => GitHubActionsArchitecture.ARM,
"ARM64" => GitHubActionsArchitecture.ARM64,
_ => GitHubActionsArchitecture.Unknown
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,43 @@ public GitHubActionsWorkflowInfo(ICakeEnvironment environment)
/// The GitHub workspace directory path.
/// </value>
public DirectoryPath Workspace => GetEnvironmentDirectoryPath("GITHUB_WORKSPACE");

/// <summary>
/// Gets the number of attempts for current run.
/// </summary>
/// <value>
/// The attempt number for current run.
/// </value>
public int Attempt => GetEnvironmentInteger("GITHUB_RUN_ATTEMPT");

/// <summary>
/// Gets a value indicating whether if branch protections are configured for the ref that triggered the workflow run.
/// </summary>
/// <value>
/// Value whether if branch protections are configured for the ref that triggered the workflow run.
/// </value>
public bool RefProtected => GetEnvironmentBoolean("GITHUB_REF_PROTECTED");

/// <summary>
/// Gets the branch or tag name that triggered the workflow run.
/// </summary>
/// <value>
/// The branch or tag name that triggered the workflow run.
/// </value>
public string RefName => GetEnvironmentString("GITHUB_REF_NAME");

/// <summary>
/// Gets the type of ref that triggered the workflow run.
/// </summary>
/// <value>
/// The type of ref that triggered the workflow run. Valid values are branch or tag.
/// </value>
public GitHubActionsRefType RefType => GetEnvironmentString("GITHUB_REF_TYPE")
?.ToLowerInvariant() switch
{
"branch" => GitHubActionsRefType.Branch,
"tag" => GitHubActionsRefType.Tag,
_ => GitHubActionsRefType.Unknown
};
}
}
Loading