Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-build#3631) Refactor GHA

* Adds GitHub Actions Commands
  * AddPath, fixes cake-build#3627
  * SetEnvironmentVariable, fixes cake-build#3628
  * UploadArtifact, fixes cake-build#3629
* Adds GitHub Actions
  * Runtime Info
  * Runner Name
  * Workflow ActionPath
  * fixes cake-build#3630
* Refactors GitHub Actions Paths, fixes cake-build#3631
* Initial work on BuildSystem integration tests
* Adds GitHub Actions Commands integration tests
  • Loading branch information
devlead committed Oct 25, 2021
1 parent 9ab07c4 commit c8c4dd5
Show file tree
Hide file tree
Showing 26 changed files with 1,326 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ jobs:
with:
target: Run-Integration-Tests
cake-version: tool-manifest

- name: Validate Integration Tests
uses: cake-build/cake-action@master
with:
script-path: tests/integration/Cake.Common/Build/GitHubActions/ValidateGitHubActionsProvider.cake
cake-version: tool-manifest

193 changes: 193 additions & 0 deletions src/Cake.Common.Tests/Fixtures/Build/GitHubActionsCommandsFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Cake.Common.Build.GitHubActions.Commands;
using Cake.Common.Build.GitHubActions.Data;
using Cake.Core;
using Cake.Testing;
using NSubstitute;

namespace Cake.Common.Tests.Fixtures.Build
{
internal sealed class GitHubActionsCommandsFixture : HttpMessageHandler
{
private const string ApiVersion = "6.0-preview";
private const string AcceptHeader = "application/json; api-version=" + ApiVersion;
private const string CreateArtifactUrl = GitHubActionsInfoFixture.ActionRuntimeUrl +
"_apis/pipelines/workflows/34058136/artifacts?api-version=" + ApiVersion + "&artifactName=artifact";
private const string CreateArtifactsUrl = GitHubActionsInfoFixture.ActionRuntimeUrl +
"_apis/pipelines/workflows/34058136/artifacts?api-version=" + ApiVersion + "&artifactName=artifacts";
private const string PutFileUrl = GitHubActionsInfoFixture.ActionRuntimeUrl +
"_apis/resources/Containers/942031?itemPath=artifact%2Fartifact.txt";
private const string CreateArtifactResponse = @"{
""containerId"": 942031,
""size"": -1,
""signedContent"": null,
""fileContainerResourceUrl"": """ + GitHubActionsInfoFixture.ActionRuntimeUrl + @"_apis/resources/Containers/942031"",
""type"": ""actions_storage"",
""name"": ""artifact"",
""url"": """ + GitHubActionsInfoFixture.ActionRuntimeUrl + @"_apis/pipelines/1/runs/7/artifacts?artifactName=artifact"",
""expiresOn"": ""2021-12-14T18:43:29.7431144Z"",
""items"": null
}";
private const string CreateArtifactsResponse = @"{
""containerId"": 942031,
""size"": -1,
""signedContent"": null,
""fileContainerResourceUrl"": """ + GitHubActionsInfoFixture.ActionRuntimeUrl + @"_apis/resources/Containers/942031"",
""type"": ""actions_storage"",
""name"": ""artifact"",
""url"": """ + GitHubActionsInfoFixture.ActionRuntimeUrl + @"_apis/pipelines/1/runs/7/artifacts?artifactName=artifacts"",
""expiresOn"": ""2021-12-14T18:43:29.7431144Z"",
""items"": null
}";

private const string PutDirectoryRootUrl = GitHubActionsInfoFixture.ActionRuntimeUrl +
"_apis/resources/Containers/942031?itemPath=artifacts%2Fartifact.txt";
private const string PutDirectoryFolderAUrl = GitHubActionsInfoFixture.ActionRuntimeUrl +
"_apis/resources/Containers/942031?itemPath=artifacts%2Ffolder_a%2Fartifact.txt";
private const string PutDirectoryFolderBUrl = GitHubActionsInfoFixture.ActionRuntimeUrl +
"_apis/resources/Containers/942031?itemPath=artifacts%2Ffolder_b%2Fartifact.txt";
private const string PutDirectoryFolderBFolderCUrl = GitHubActionsInfoFixture.ActionRuntimeUrl +
"_apis/resources/Containers/942031?itemPath=artifacts%2Ffolder_b%2Ffolder_c%2Fartifact.txt";

private GitHubActionsInfoFixture GitHubActionsInfoFixture { get; }
private ICakeEnvironment Environment { get; }
public FakeFileSystem FileSystem { get; }

public GitHubActionsCommandsFixture()
{
GitHubActionsInfoFixture = new GitHubActionsInfoFixture();
FileSystem = new FakeFileSystem(GitHubActionsInfoFixture.Environment);
FileSystem.CreateDirectory("/opt");
Environment = GitHubActionsInfoFixture.Environment;
}

public GitHubActionsCommands CreateGitHubActionsCommands()
{
return new GitHubActionsCommands(Environment, FileSystem, GitHubActionsInfoFixture.CreateEnvironmentInfo(), CreateClient);
}

public GitHubActionsCommandsFixture WithNoGitHubEnv()
{
Environment.GetEnvironmentVariable("GITHUB_ENV").Returns(null as string);
return this;
}

public GitHubActionsCommandsFixture WithNoGitHubPath()
{
Environment.GetEnvironmentVariable("GITHUB_PATH").Returns(null as string);
return this;
}

private HttpClient CreateClient(string name) => new HttpClient(this);

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Headers.Authorization is null || request.Headers.Authorization.Scheme != "Bearer" || request.Headers.Authorization.Parameter != GitHubActionsInfoFixture.ActionRuntimeToken)
{
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.Unauthorized
};
}

if (!request.Headers.TryGetValues("Accept", out var values) || !values.Contains(AcceptHeader))
{
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.BadRequest
};
}

switch (request)
{
#pragma warning disable SA1013
// FilePath
case
{
RequestUri: { AbsoluteUri: CreateArtifactUrl },
Method: { Method: "POST" },
}:
{
return Ok(new StringContent(CreateArtifactResponse));
}

// DirectoryPath
case
{
RequestUri: { AbsoluteUri: CreateArtifactsUrl },
Method: { Method: "POST" },
}:
{
return Ok(new StringContent(CreateArtifactsResponse));
}

// FilePath
case
{
RequestUri: { AbsoluteUri: PutFileUrl },
Method: { Method: "PUT" }
}:
case
{
RequestUri: { AbsoluteUri: CreateArtifactUrl },
Method: { Method: "PATCH" },
}:

// DirectoryPath
case
{
RequestUri: { AbsoluteUri: PutDirectoryRootUrl },
Method: { Method: "PUT" }
}:
case
{
RequestUri: { AbsoluteUri: PutDirectoryFolderAUrl },
Method: { Method: "PUT" }
}:
case
{
RequestUri: { AbsoluteUri: PutDirectoryFolderBUrl },
Method: { Method: "PUT" }
}:
case
{
RequestUri: { AbsoluteUri: PutDirectoryFolderBFolderCUrl },
Method: { Method: "PUT" }
}:
case
{
RequestUri: { AbsoluteUri: CreateArtifactsUrl },
Method: { Method: "PATCH" },
}:
{
return Ok();
}
#pragma warning restore SA1013

default:
{
await Task.Delay(1, cancellationToken);
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.NotFound
};
}
}
}

private static HttpResponseMessage Ok(HttpContent content = null)
{
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
ReasonPhrase = "OK",
Content = content
};
}
}
}
9 changes: 7 additions & 2 deletions src/Cake.Common.Tests/Fixtures/Build/GitHubActionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@

using Cake.Common.Build.GitHubActions;
using Cake.Core;
using Cake.Core.IO;
using Cake.Testing;
using NSubstitute;

namespace Cake.Common.Tests.Fixtures.Build
{
internal sealed class GitHubActionsFixture
{
public ICakeEnvironment Environment { get; set; }
public ICakeEnvironment Environment { get; }
public IFileSystem FileSystem { get; }

public GitHubActionsFixture()
{
Environment = Substitute.For<ICakeEnvironment>();
Environment.GetEnvironmentVariable("GITHUB_ACTIONS").Returns((string)null);
Environment.WorkingDirectory.Returns("/home/cake");
FileSystem = new FakeFileSystem(Environment);
}

public void IsRunningOnGitHubActions()
Expand All @@ -25,7 +30,7 @@ public void IsRunningOnGitHubActions()

public GitHubActionsProvider CreateGitHubActionsService()
{
return new GitHubActionsProvider(Environment);
return new GitHubActionsProvider(Environment, FileSystem);
}
}
}
17 changes: 16 additions & 1 deletion src/Cake.Common.Tests/Fixtures/Build/GitHubActionsInfoFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ namespace Cake.Common.Tests.Fixtures.Build
{
internal sealed class GitHubActionsInfoFixture
{
public ICakeEnvironment Environment { get; set; }
public const string ActionRuntimeToken = "zht1j5NeW2T5ZsOxncX4CUEiWYhD4ZRwoDghkARk";
public const string ActionRuntimeUrl = "https://pipelines.actions.githubusercontent.com/ip0FyYnZXxdEOcOwPHkRsZJd2x6G5XoT486UsAb0/";
public ICakeEnvironment Environment { get; }

public GitHubActionsInfoFixture()
{
Expand All @@ -19,12 +21,14 @@ public GitHubActionsInfoFixture()
Environment.GetEnvironmentVariable("GITHUB_ACTIONS").Returns("true");
Environment.GetEnvironmentVariable("HOME").Returns("/home/runner");

Environment.GetEnvironmentVariable("RUNNER_NAME").Returns("RunnerName");
Environment.GetEnvironmentVariable("RUNNER_OS").Returns("Linux");
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("GITHUB_ACTION").Returns("run1");
Environment.GetEnvironmentVariable("GITHUB_ACTION_PATH").Returns("/path/to/action");
Environment.GetEnvironmentVariable("GITHUB_ACTOR").Returns("dependabot");
Environment.GetEnvironmentVariable("GITHUB_API_URL").Returns("https://api.github.com");
Environment.GetEnvironmentVariable("GITHUB_BASE_REF").Returns("master");
Expand All @@ -42,6 +46,12 @@ 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("ACTIONS_RUNTIME_TOKEN").Returns(ActionRuntimeToken);
Environment.GetEnvironmentVariable("ACTIONS_RUNTIME_URL").Returns(ActionRuntimeUrl);
Environment.GetEnvironmentVariable("GITHUB_ENV").Returns("/opt/github.env");
Environment.GetEnvironmentVariable("GITHUB_PATH").Returns("/opt/github.path");
Environment.WorkingDirectory.Returns("/home/runner/work/cake/cake");
}

public GitHubActionsRunnerInfo CreateRunnerInfo()
Expand All @@ -63,5 +73,10 @@ public GitHubActionsEnvironmentInfo CreateEnvironmentInfo()
{
return new GitHubActionsEnvironmentInfo(Environment);
}

public GitHubActionsRuntimeInfo CreateRuntimeInfo()
{
return new GitHubActionsRuntimeInfo(Environment);
}
}
}
Loading

0 comments on commit c8c4dd5

Please sign in to comment.