forked from cake-build/cake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
26 changed files
with
1,326 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
src/Cake.Common.Tests/Fixtures/Build/GitHubActionsCommandsFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.