Forked from arian2ashk/gist:6dbf7d505c1ec26025ab2d38cbed0c06
Created
October 19, 2023 09:25
-
-
Save mimicscott/257cc9c3b3172cf8cd134b5c7ce5772c to your computer and use it in GitHub Desktop.
Github Release utils for Nuke Build
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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using Microsoft.AspNetCore.StaticFiles; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using Nuke.Common; | |
using Nuke.Common.IO; | |
using Nuke.Common.Tools.GitHub; | |
using Nuke.Common.Utilities.Collections; | |
using Octokit; | |
using Serilog; | |
public static class Utils | |
{ | |
public static byte[] GetAsset(string repoOwner, string repoName, string assetName, string? uiReleaseTag) | |
{ | |
Log.Information("Getting UI asset '{AssetName}' from repo {RepoOwner}/{RepoName}", assetName, repoOwner, | |
repoName); | |
var uiRelease = string.IsNullOrWhiteSpace(uiReleaseTag) | |
? GitHubTasks.GitHubClient.Repository.Release.GetLatest(repoOwner, repoName).Result | |
: GitHubTasks.GitHubClient.Repository.Release.Get(repoOwner, repoName, uiReleaseTag).Result; | |
var uiAsset = uiRelease.Assets.First(x => x.Name == assetName); | |
var downloadedAsset = GitHubTasks.GitHubClient.Connection.Get<byte[]>(new Uri(uiAsset.Url), | |
new Dictionary<string, string>(), "application/octet-stream").Result; | |
Log.Information("Download Completed for asset {AssetName} of {ReleaseName}", assetName, uiRelease.Name); | |
return downloadedAsset.Body; | |
} | |
public static void SaveFile(AbsolutePath path, byte[] file) | |
{ | |
if (path.Exists()) | |
{ | |
return; | |
} | |
Log.Information("Saving file to path {Path}", path); | |
File.WriteAllBytes(path, file); | |
Log.Information("File saved to path {Path}", path); | |
} | |
public static void SetGithubCredentials(string authToken) => | |
GitHubTasks.GitHubClient = new GitHubClient(new ProductHeaderValue(nameof(NukeBuild))) | |
{ | |
Credentials = new Credentials(authToken) | |
}; | |
static void UploadReleaseAssetToGithub(this Release release, AbsolutePath asset) | |
{ | |
if (!asset.Exists()) | |
{ | |
return; | |
} | |
Log.Information("Started Uploading {FileName} to the release", Path.GetFileName(asset)); | |
if (!new FileExtensionContentTypeProvider().TryGetContentType(asset, out var assetContentType)) | |
{ | |
assetContentType = "application/x-binary"; | |
} | |
var releaseAssetUpload = new ReleaseAssetUpload | |
{ | |
ContentType = assetContentType, FileName = Path.GetFileName(asset), RawData = File.OpenRead(asset) | |
}; | |
var _ = GitHubTasks.GitHubClient.Repository.Release.UploadAsset(release, releaseAssetUpload).Result; | |
Log.Information("Done Uploading {FileName} to the release", Path.GetFileName(asset)); | |
} | |
public static Release UploadDirectory(this Release release, AbsolutePath directory) | |
{ | |
if (directory.GlobDirectories("*").Count > 0) | |
{ | |
Log.Warning("Only files on the root of {Directory} directory will be uploaded as release assets", | |
directory); | |
} | |
directory.GlobFiles("*").ForEach(release.UploadReleaseAssetToGithub); | |
return release; | |
} | |
public static Release CreateRelease(string repoOwner, string repoName, string tagName, | |
string? version, string? commitSha, bool isPrerelease) | |
{ | |
Log.Information("Creating release for tag {TagName}", tagName); | |
var newRelease = new NewRelease(tagName) | |
{ | |
TargetCommitish = commitSha, | |
Draft = true, | |
Name = $"Release version {version}", | |
Prerelease = isPrerelease, | |
Body = "" | |
}; | |
return GitHubTasks.GitHubClient.Repository.Release.Create(repoOwner, repoName, newRelease).Result; | |
} | |
public static Release Publish(this Release release, string repoOwner, string repoName) => | |
GitHubTasks.GitHubClient.Repository.Release | |
.Edit(repoOwner, repoName, release.Id, new ReleaseUpdate {Draft = false}).Result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment