-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
82 lines (73 loc) · 3.25 KB
/
Program.cs
File metadata and controls
82 lines (73 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.Net.Http.Json;
using YsfInstall;
var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN")!;
var repoUrl = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY_URL")!;
Console.WriteLine($"Url:{repoUrl}");
Console.WriteLine($"Token:{token.Substring(0, 10)}...");
var handler = new HttpClientHandler()
{
AllowAutoRedirect = false
};
var httpClient = new HttpClient(handler)
{
Timeout = TimeSpan.FromMinutes(20)
};
var req = new HttpRequestMessage(HttpMethod.Get, $"{repoUrl}/actions/artifacts");
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("token", token);
req.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
req.Headers.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("GitHubApiClient", "1.0"));
Console.WriteLine("Getting artifacts...");
var httpRes = await httpClient.SendAsync(req);
if (httpRes is null)
{
Console.WriteLine("Failed to get artifacts");
return;
}
if (!httpRes.IsSuccessStatusCode)
{
Console.WriteLine($"Failed response code: {httpRes.StatusCode}");
return;
}
var response = await httpRes.Content.ReadFromJsonAsync<GetArtifacts>();
if (null == response)
{
Console.WriteLine("Failed to parse artifacts response");
return;
}
var namePrefix = Environment.GetEnvironmentVariable("ARTIFACT_NAME_PREFIX")!;
var branch = Environment.GetEnvironmentVariable("GITHUB_BRANCH");
var commit = Environment.GetEnvironmentVariable("GITHUB_COMMIT");
var artifact = response.Artifacts
.Where(a => a.Name.StartsWith(namePrefix) && a.Expired == false)
.Where(a => string.IsNullOrEmpty(branch) || a.WorkflowRun.HeadBranch == branch)
.Where(a => string.IsNullOrEmpty(commit) || a.WorkflowRun.HeadSha == commit)
.OrderByDescending(a => a.CreatedAt)
.FirstOrDefault();
if (null == artifact)
{
Console.WriteLine("No valid artifact found");
return;
}
var downloadUrl = artifact.ArchiveDownloadUrl;
Console.WriteLine($"Downloading artifact {artifact.Name} from {downloadUrl}");
var req2 = new HttpRequestMessage(HttpMethod.Get, downloadUrl);
req2.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("token", token);
req2.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
req2.Headers.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("GitHubApiClient", "1.0"));
var downloadResponse = await httpClient.SendAsync(req2);
if (downloadResponse.StatusCode == System.Net.HttpStatusCode.Redirect)
{
downloadUrl = downloadResponse.Headers.Location!.ToString();
Console.WriteLine($"Downloading artifact {artifact.Name} from redirect {downloadUrl}");
downloadResponse = await httpClient.GetAsync(downloadUrl);
} else if (!downloadResponse.IsSuccessStatusCode)
{
Console.WriteLine($"Failed to download artifact: {downloadResponse.StatusCode}");
return;
}
var path = Environment.GetEnvironmentVariable("DOWNLOAD_PATH") ?? "/app/output";
var fileName = $"{artifact.Name}.zip";
var downloadPath = Path.Combine(path, fileName);
await using var fs = new FileStream(downloadPath, FileMode.Create, FileAccess.Write);
await downloadResponse.Content.CopyToAsync(fs);
Console.WriteLine($"Artifact downloaded to {downloadPath}");