77using Cake . Common . Build . GitHubActions . Commands ;
88using Cake . Common . Build . GitHubActions . Data ;
99using Cake . Core ;
10+ using Cake . Core . IO ;
1011using Cake . Testing ;
1112using NSubstitute ;
1213
@@ -16,6 +17,8 @@ internal sealed class GitHubActionsCommandsFixture : HttpMessageHandler
1617 {
1718 private const string ApiVersion = "6.0-preview" ;
1819 private const string AcceptHeader = "application/json; api-version=" + ApiVersion ;
20+ private const string AcceptGzip = "application/octet-stream; api-version=" + ApiVersion ;
21+ private const string AcceptEncodingGzip = "gzip" ;
1922 private const string CreateArtifactUrl = GitHubActionsInfoFixture . ActionRuntimeUrl +
2023 "_apis/pipelines/workflows/34058136/artifacts?api-version=" + ApiVersion + "&artifactName=artifact" ;
2124 private const string CreateArtifactsUrl = GitHubActionsInfoFixture . ActionRuntimeUrl +
@@ -54,6 +57,54 @@ internal sealed class GitHubActionsCommandsFixture : HttpMessageHandler
5457 private const string PutDirectoryFolderBFolderCUrl = GitHubActionsInfoFixture . ActionRuntimeUrl +
5558 "_apis/resources/Containers/942031?itemPath=artifacts%2Ffolder_b%2Ffolder_c%2Fartifact.txt" ;
5659
60+ private const string GetArtifactResourceUrl = GitHubActionsInfoFixture . ActionRuntimeUrl +
61+ "_apis/pipelines/workflows/34058136/artifacts?api-version=6.0-preview&artifactName=artifact" ;
62+ private const string FileContainerResourceUrl = GitHubActionsInfoFixture . ActionRuntimeUrl + @"_apis/resources/Containers/4794789" ;
63+ private const string GetArtifactResourceResponse = @"{
64+ ""count"": 1,
65+ ""value"": [
66+ {
67+ ""containerId"": 4794789,
68+ ""size"": 4,
69+ ""signedContent"": null,
70+ ""fileContainerResourceUrl"": """ + FileContainerResourceUrl + @""",
71+ ""type"": ""actions_storage"",
72+ ""name"": ""artifact"",
73+ ""url"": """ + GitHubActionsInfoFixture . ActionRuntimeUrl + @"_apis/pipelines/1/runs/7/artifacts?artifactName=artifact"",
74+ ""expiresOn"": ""2022-03-16T08:22:01.5699067Z"",
75+ ""items"": null
76+ }
77+ ]
78+ }" ;
79+
80+ private const string GetContainerItemResourcesUrl = FileContainerResourceUrl + "?itemPath=artifact" ;
81+ private const string GetContainerItemResourcesResponse = @"{
82+ ""count"": 1,
83+ ""value"": [
84+ {
85+ ""containerId"": 4794789,
86+ ""scopeIdentifier"": ""00000000-0000-0000-0000-000000000000"",
87+ ""path"": ""artifact/test.txt"",
88+ ""itemType"": ""file"",
89+ ""status"": ""created"",
90+ ""fileLength"": 4,
91+ ""fileEncoding"": 1,
92+ ""fileType"": 1,
93+ ""dateCreated"": ""2021-12-16T09:05:18.803Z"",
94+ ""dateLastModified"": ""2021-12-16T09:05:18.907Z"",
95+ ""createdBy"": ""2daeb16b-86ae-4e46-ba89-92a8aa076e52"",
96+ ""lastModifiedBy"": ""2daeb16b-86ae-4e46-ba89-92a8aa076e52"",
97+ ""itemLocation"": """ + GetContainerItemResourcesUrl + @"%2Ftest.txt&metadata=True"",
98+ ""contentLocation"": """ + GetContainerItemResourcesUrl + @"%2Ftest.txt"",
99+ ""fileId"": 1407,
100+ ""contentId"": """"
101+ }
102+ ]
103+ }" ;
104+
105+ private const string DownloadItemResourceUrl = GetContainerItemResourcesUrl + "%2Ftest.txt" ;
106+ private const string DownloadItemResourceResponse = "Cake" ;
107+
57108 private GitHubActionsInfoFixture GitHubActionsInfoFixture { get ; }
58109 private ICakeEnvironment Environment { get ; }
59110 public FakeFileSystem FileSystem { get ; }
@@ -71,6 +122,12 @@ public GitHubActionsCommands CreateGitHubActionsCommands()
71122 return new GitHubActionsCommands ( Environment , FileSystem , GitHubActionsInfoFixture . CreateEnvironmentInfo ( ) , CreateClient ) ;
72123 }
73124
125+ public GitHubActionsCommandsFixture WithWorkingDirectory ( DirectoryPath workingDirectory )
126+ {
127+ Environment . WorkingDirectory = workingDirectory ;
128+ return this ;
129+ }
130+
74131 public GitHubActionsCommandsFixture WithNoGitHubEnv ( )
75132 {
76133 Environment . GetEnvironmentVariable ( "GITHUB_ENV" ) . Returns ( null as string ) ;
@@ -95,18 +152,26 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
95152 } ;
96153 }
97154
98- if ( ! request . Headers . TryGetValues ( "Accept" , out var values ) || ! values . Contains ( AcceptHeader ) )
155+ if (
156+ ! request . Headers . TryGetValues ( "Accept" , out var values )
157+ || ! values . Contains ( AcceptHeader ) )
99158 {
100- return new HttpResponseMessage
159+ if ( request . RequestUri . AbsoluteUri != DownloadItemResourceUrl
160+ || ! values . Contains ( AcceptGzip )
161+ || ! request . Headers . TryGetValues ( "Accept-Encoding" , out var encodingValues )
162+ || ! encodingValues . Contains ( AcceptEncodingGzip ) )
101163 {
102- StatusCode = HttpStatusCode . BadRequest
103- } ;
164+ return new HttpResponseMessage
165+ {
166+ StatusCode = HttpStatusCode . BadRequest
167+ } ;
168+ }
104169 }
105170
106171 switch ( request )
107172 {
108173#pragma warning disable SA1013
109- // FilePath
174+ // Create Artifact FilePath
110175 case
111176 {
112177 RequestUri : { AbsoluteUri : CreateArtifactUrl } ,
@@ -116,7 +181,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
116181 return Ok ( new StringContent ( CreateArtifactResponse ) ) ;
117182 }
118183
119- // DirectoryPath
184+ // Create Artifact DirectoryPath
120185 case
121186 {
122187 RequestUri : { AbsoluteUri : CreateArtifactsUrl } ,
@@ -126,7 +191,37 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
126191 return Ok ( new StringContent ( CreateArtifactsResponse ) ) ;
127192 }
128193
129- // FilePath
194+ // Download Artifact - Get Artifact Container Resource
195+ case
196+ {
197+ RequestUri : { AbsoluteUri : GetArtifactResourceUrl } ,
198+ Method : { Method : "GET" }
199+ } :
200+ {
201+ return Ok ( new StringContent ( GetArtifactResourceResponse ) ) ;
202+ }
203+
204+ // Download Artifact - Get Artifact Container Item Resource
205+ case
206+ {
207+ RequestUri : { AbsoluteUri : GetContainerItemResourcesUrl } ,
208+ Method : { Method : "GET" }
209+ } :
210+ {
211+ return Ok ( new StringContent ( GetContainerItemResourcesResponse ) ) ;
212+ }
213+
214+ // Download Artifact - DownloadItemResource
215+ case
216+ {
217+ RequestUri : { AbsoluteUri : DownloadItemResourceUrl } ,
218+ Method : { Method : "GET" }
219+ } :
220+ {
221+ return Ok ( new StringContent ( DownloadItemResourceResponse ) ) ;
222+ }
223+
224+ // Put FilePath
130225 case
131226 {
132227 RequestUri : { AbsoluteUri : PutFileUrl } ,
@@ -138,7 +233,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
138233 Method : { Method : "PATCH" } ,
139234 } :
140235
141- // DirectoryPath
236+ // Put DirectoryPath
142237 case
143238 {
144239 RequestUri : { AbsoluteUri : PutDirectoryRootUrl } ,
0 commit comments