forked from cake-build/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.cake
77 lines (60 loc) · 3.03 KB
/
github.cake
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
#addin "nuget:https://api.nuget.org/v3/index.json?package=Octokit&version=0.50.0"
#addin "nuget:https://api.nuget.org/v3/index.json?package=NuGet.Versioning&version=5.11.0"
using Octokit;
using NuGet.Versioning;
static CakeGitHubReleaseInfo _cakeGitHubReleaseInfo;
public static CakeGitHubReleaseInfo GetCakeGitHubReleaseInfo(this ICakeContext context, DirectoryPath releaseDir)
{
if (!(_cakeGitHubReleaseInfo is null))
{
return _cakeGitHubReleaseInfo;
}
// We cache the latest Cake release information in a file to speed up local development and also
// to decrease the chance of getting rate limited by GitHub when running unauthenticated
var cachedGitHubDir = context.MakeAbsolute(releaseDir.Combine("cake-github"));
var cachedReleaseInfoFile = cachedGitHubDir.CombineWithFilePath($"{nameof(CakeGitHubReleaseInfo)}.yml");
if (context.FileExists(cachedReleaseInfoFile))
{
context.Verbose("Retrieving Cake release information from cached file {0}...", cachedReleaseInfoFile);
_cakeGitHubReleaseInfo = context.DeserializeYamlFromFile<CakeGitHubReleaseInfo>(cachedReleaseInfoFile);
LogGitHubReleaseInfo(context, _cakeGitHubReleaseInfo);
return _cakeGitHubReleaseInfo;
}
context.Information("Retrieving Cake release information from GitHub...");
var client = new GitHubClient(new ProductHeaderValue("cake-build-website"));
var gitHubAccessToken = context.EnvironmentVariable("git_access_token");
if (!string.IsNullOrWhiteSpace(gitHubAccessToken))
{
client.Credentials = new Credentials(gitHubAccessToken);
}
var allCakeReleases = client.Repository.Release.GetAll("cake-build", "cake")
.GetAwaiter()
.GetResult()
.Where(r => !r.Draft && r.PublishedAt.HasValue && r.Name.StartsWith("v", StringComparison.Ordinal))
.OrderByDescending(r => new NuGetVersion(r.Name.Substring(1)))
.ToList();
var latestCakeRelease = allCakeReleases
.First();
_cakeGitHubReleaseInfo = new CakeGitHubReleaseInfo
{
LatestReleaseName = latestCakeRelease.Name.TrimStart('v'),
LatestReleaseUrl = latestCakeRelease.HtmlUrl,
LatestReleaseZipUrl = latestCakeRelease.ZipballUrl,
};
LogGitHubReleaseInfo(context, _cakeGitHubReleaseInfo);
context.EnsureDirectoryExists(cachedReleaseInfoFile.GetDirectory());
context.SerializeYamlToFile<CakeGitHubReleaseInfo>(cachedReleaseInfoFile, _cakeGitHubReleaseInfo);
return _cakeGitHubReleaseInfo;
}
private static void LogGitHubReleaseInfo(ICakeContext context, CakeGitHubReleaseInfo releaseInfo)
{
context.Information("Cake Latest Release Name: {0}", releaseInfo.LatestReleaseName);
context.Information("Cake Latest Release Url: {0}", releaseInfo.LatestReleaseUrl);
context.Information("Cake Latest Release Zip Url: {0}", releaseInfo.LatestReleaseZipUrl);
}
public class CakeGitHubReleaseInfo
{
public string LatestReleaseName { get; set; }
public string LatestReleaseUrl { get; set; }
public string LatestReleaseZipUrl { get; set; }
}