Skip to content

Commit 28e8e86

Browse files
authored
Artifact Download: Walk back commits until a valid manifest is found (#3485)
1 parent 83df9b8 commit 28e8e86

1 file changed

Lines changed: 36 additions & 23 deletions

File tree

engine/Tools/SboxBuild/Steps/DownloadPublicArtifacts.cs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Net;
23
using System.Text.Json;
34
using System.Threading;
@@ -14,33 +15,47 @@ internal class DownloadPublicArtifacts( string name ) : Step( name )
1415
private const string BaseUrl = "https://artifacts.sbox.game";
1516
private const int MaxParallelDownloads = 32;
1617
private const int MaxDownloadAttempts = 3;
18+
private const int MaxManifestLookbackCommits = 512;
1719
protected override ExitCode RunInternal()
1820
{
1921
try
2022
{
21-
var commitHash = ResolveCommitHash();
22-
if ( string.IsNullOrWhiteSpace( commitHash ) )
23+
var commitCandidates = ResolveCommitHistory( MaxManifestLookbackCommits );
24+
if ( commitCandidates.Count == 0 )
2325
{
2426
Log.Error( "Unable to determine the commit hash to download artifacts for." );
2527
return ExitCode.Failure;
2628
}
2729

28-
Log.Info( $"Downloading public artifacts for commit {commitHash} from {BaseUrl}" );
29-
3030
using var httpClient = CreateHttpClient();
3131

32-
var manifest = DownloadManifest( httpClient, BaseUrl, commitHash );
33-
if ( manifest is null )
32+
ArtifactManifest manifest = null;
33+
foreach ( var candidate in commitCandidates )
3434
{
35-
return ExitCode.Failure;
35+
var candidateManifest = DownloadManifest( httpClient, BaseUrl, candidate );
36+
if ( candidateManifest is null )
37+
{
38+
continue;
39+
}
40+
41+
if ( !string.Equals( candidateManifest.Commit, candidate, StringComparison.OrdinalIgnoreCase ) )
42+
{
43+
Log.Error( $"Manifest commit {candidateManifest.Commit} does not match requested commit {candidate}." );
44+
return ExitCode.Failure;
45+
}
46+
47+
manifest = candidateManifest;
48+
break;
3649
}
3750

38-
if ( !string.Equals( manifest.Commit, commitHash, StringComparison.OrdinalIgnoreCase ) )
51+
if ( manifest is null )
3952
{
40-
Log.Error( $"Manifest commit {manifest.Commit} does not match requested commit {commitHash}." );
53+
Log.Error( $"Unable to locate a manifest within the last {commitCandidates.Count} commit(s)." );
4154
return ExitCode.Failure;
4255
}
4356

57+
Log.Info( $"Downloading public artifacts for commit {manifest.Commit} from {BaseUrl}" );
58+
4459
if ( manifest.Files.Count == 0 )
4560
{
4661
Log.Warning( "Manifest does not contain any files to download." );
@@ -131,31 +146,29 @@ private static HttpClient CreateHttpClient()
131146
};
132147
}
133148

134-
private static string ResolveCommitHash()
149+
private static IReadOnlyList<string> ResolveCommitHistory( int maxCommits )
135150
{
136-
const string branchName = "master";
137-
string gitCommit = null;
138-
var success = Utility.RunProcess( "git", $"rev-parse {branchName}", onDataReceived: ( _, e ) =>
151+
var commits = new List<string>( Math.Max( maxCommits, 1 ) );
152+
var success = Utility.RunProcess( "git", $"rev-list HEAD --max-count={maxCommits}", onDataReceived: ( _, e ) =>
139153
{
140154
if ( !string.IsNullOrWhiteSpace( e.Data ) )
141155
{
142-
gitCommit ??= e.Data.Trim();
156+
commits.Add( e.Data.Trim() );
143157
}
144158
} );
145159

146160
if ( !success )
147161
{
148-
Log.Error( $"Failed to execute git to resolve commit hash for branch '{branchName}'." );
149-
return null;
162+
Log.Error( "Failed to execute git to resolve commit history for the current branch." );
163+
return Array.Empty<string>();
150164
}
151165

152-
if ( string.IsNullOrWhiteSpace( gitCommit ) )
166+
if ( commits.Count == 0 )
153167
{
154-
Log.Error( $"git returned an empty commit hash for branch '{branchName}'." );
155-
return null;
168+
Log.Error( "git returned no commits for the current branch." );
156169
}
157170

158-
return gitCommit;
171+
return commits;
159172
}
160173

161174
private static ArtifactManifest DownloadManifest( HttpClient httpClient, string baseUrl, string commitHash )
@@ -167,13 +180,13 @@ private static ArtifactManifest DownloadManifest( HttpClient httpClient, string
167180
using var response = httpClient.GetAsync( manifestUrl, HttpCompletionOption.ResponseHeadersRead ).GetAwaiter().GetResult();
168181
if ( response.StatusCode == HttpStatusCode.NotFound )
169182
{
170-
Log.Error( $"Manifest not found for commit {commitHash}." );
183+
Log.Warning( $"Manifest not found for commit {commitHash}." );
171184
return null;
172185
}
173186

174187
if ( !response.IsSuccessStatusCode )
175188
{
176-
Log.Error( $"Failed to download manifest (HTTP {(int)response.StatusCode})." );
189+
Log.Warning( $"Failed to download manifest for commit {commitHash} (HTTP {(int)response.StatusCode})." );
177190
return null;
178191
}
179192

@@ -186,7 +199,7 @@ private static ArtifactManifest DownloadManifest( HttpClient httpClient, string
186199

187200
if ( manifest is null )
188201
{
189-
Log.Error( "Failed to deserialize manifest JSON." );
202+
Log.Warning( $"Failed to deserialize manifest JSON for commit {commitHash}." );
190203
return null;
191204
}
192205

0 commit comments

Comments
 (0)