Skip to content

Commit

Permalink
Pre-Release 23.3.3 artifacts | +semver: patch
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed Jun 11, 2024
1 parent 73c4742 commit 8c0180a
Showing 1 changed file with 19 additions and 56 deletions.
75 changes: 19 additions & 56 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,10 @@ var artifactsFile = packagesDir + File("artifacts.txt");
var releaseNotesFile = packagesDir + File("ReleaseNotes.md");
var releaseNotes = new List<string>();

// stable releases
var tagsUrl = "https://api.github.com/repos/ThreeMammals/ocelot/releases/tags/";
var nugetFeedStableKey = EnvironmentVariable("OCELOT_NUGET_API_KEY_3Mammals");
var nugetFeedStableUploadUrl = "https://www.nuget.org/api/v2/package";
var nugetFeedStableSymbolsUploadUrl = "https://www.nuget.org/api/v2/package";

// internal build variables - don't change these.
string committedVersion = "0.0.0-dev";
GitVersion versioning = null;
bool IsTechnicalRelease = true;
//string gitHubUsername = "raman-m";
//string gitHubToken = Environment.GetEnvironmentVariable("OCELOT_GITHUB_API_KEY_2");
bool IsTechnicalRelease = false;

var target = Argument("target", "Default");
var slnFile = (target == Release) ? $"./Ocelot.{Release}.sln" : "./Ocelot.sln";
Expand Down Expand Up @@ -490,7 +482,7 @@ Task("PublishGitHubRelease")
.IsDependentOn("CreateArtifacts")
.Does(() =>
{
//if (!IsRunningOnCircleCI()) return;
if (!IsRunningOnCircleCI()) return;

dynamic release = CreateGitHubRelease();
var path = packagesDir.ToString() + @"/**/*";
Expand Down Expand Up @@ -524,7 +516,7 @@ Task("DownloadGitHubReleaseArtifacts")
System.Threading.Thread.Sleep(5000);
EnsureDirectoryExists(packagesDir);

var releaseUrl = tagsUrl + versioning.NuGetVersion;
var releaseUrl = "https://api.github.com/repos/ThreeMammals/ocelot/releases/tags/" + versioning.NuGetVersion;
var releaseInfo = await GetResourceAsync(releaseUrl);
var assets_url = Newtonsoft.Json.Linq.JObject.Parse(releaseInfo)
.Value<string>("assets_url");
Expand Down Expand Up @@ -555,6 +547,10 @@ Task("PublishToNuget")

if (IsRunningOnCircleCI())
{
// stable releases
var nugetFeedStableKey = EnvironmentVariable("OCELOT_NUGET_API_KEY_3Mammals");
var nugetFeedStableUploadUrl = "https://www.nuget.org/api/v2/package";
var nugetFeedStableSymbolsUploadUrl = "https://www.nuget.org/api/v2/package";
PublishPackages(packagesDir, artifactsFile, nugetFeedStableKey, nugetFeedStableUploadUrl, nugetFeedStableSymbolsUploadUrl);
}
});
Expand Down Expand Up @@ -593,13 +589,10 @@ private GitVersion GetNuGetVersionForCommit()
private void PersistVersion(string committedVersion, string newVersion)
{
Information(string.Format("We'll search all csproj files for {0} and replace with {1}...", committedVersion, newVersion));

var projectFiles = GetFiles("./**/*.csproj");

foreach(var projectFile in projectFiles)
{
var file = projectFile.ToString();

Information(string.Format("Updating {0}...", file));

var updatedProjectFile = System.IO.File.ReadAllText(file)
Expand All @@ -612,22 +605,18 @@ private void PersistVersion(string committedVersion, string newVersion)
/// Publishes code and symbols packages to nuget feed, based on contents of artifacts file
private void PublishPackages(ConvertableDirectoryPath packagesDir, ConvertableFilePath artifactsFile, string feedApiKey, string codeFeedUrl, string symbolFeedUrl)
{
Information("Publishing to NuGet...");
Information("PublishPackages: Publishing to NuGet...");
var artifacts = System.IO.File
.ReadAllLines(artifactsFile)
.Distinct();

foreach(var artifact in artifacts)
{
if (artifact == "ReleaseNotes.md")
{
continue;
}

var codePackage = packagesDir + File(artifact);
Information("Pushing package " + codePackage + "...");

Information("Calling DotNetNuGetPush");
Information("PublishPackages: Pushing package " + codePackage + "...");
DotNetNuGetPush(
codePackage,
new DotNetNuGetPushSettings { ApiKey = feedApiKey, Source = codeFeedUrl }
Expand All @@ -638,46 +627,31 @@ private void PublishPackages(ConvertableDirectoryPath packagesDir, ConvertableFi
private void SetupGitHubClient(System.Net.Http.HttpClient client)
{
string token = Environment.GetEnvironmentVariable("OCELOT_GITHUB_API_KEY_2");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"Bearer",
token // Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(gitHubPassword)) // token actually
);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
client.DefaultRequestHeaders.Add("User-Agent", "Ocelot Release");
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github+json");
client.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2022-11-28");
}

private dynamic CreateGitHubRelease()
{
//var json = $"{{ \"tag_name\": \"{versioning.NuGetVersion}\", \"target_commitish\": \"main\", \"name\": \"{versioning.NuGetVersion}\", \"body\": \"{ReleaseNotesAsJson()}\", \"draft\": true, \"prerelease\": true }}";
var json = $"{{ \"tag_name\": \"{versioning.NuGetVersion}\", \"target_commitish\": \"main\", \"name\": \"{versioning.NuGetVersion}\", \"body\": \"{ReleaseNotesAsJson()}\", \"draft\": true, \"prerelease\": true, \"generate_release_notes\": false }}";
var content = new System.Net.Http.StringContent(json, System.Text.Encoding.UTF8, "application/json");

using (var client = new System.Net.Http.HttpClient())
{
// client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
// "Basic",
// Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{gitHubUsername}:{gitHubPassword}"))
// );
// client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
// "Bearer",
// Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(gitHubPassword)) // token actually
// );
// client.DefaultRequestHeaders.Add("User-Agent", "Ocelot Release");
// client.DefaultRequestHeaders.Add("Accept", "application/vnd.github+json");
// client.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2022-11-28");
SetupGitHubClient(client);
var result = client.PostAsync("https://api.github.com/repos/ThreeMammals/Ocelot/releases", content)
.Result;
if(result.StatusCode != System.Net.HttpStatusCode.Created)
var result = client.PostAsync("https://api.github.com/repos/ThreeMammals/Ocelot/releases", content).Result;
if (result.StatusCode != System.Net.HttpStatusCode.Created)
{
throw new Exception("CreateGitHubRelease result.StatusCode = " + result.StatusCode);
var msg = "CreateGitHubRelease: StatusCode = " + result.StatusCode;
Information(msg);
throw new Exception(msg);
}
var releaseData = result.Content.ReadAsStringAsync().Result;
dynamic releaseJSON = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(releaseData);
Information("CreateGitHubRelease: Release ID is " + releaseJSON.id);
//releaseId = test.id;
return releaseJSON; //.id;
return releaseJSON;
}
}

Expand All @@ -694,18 +668,13 @@ private void UploadFileToGitHubRelease(dynamic release, FilePath file)

using (var client = new System.Net.Http.HttpClient())
{
// client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
// "Basic",
// Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{gitHubUsername}:{gitHubPassword}"))
// );
// client.DefaultRequestHeaders.Add("User-Agent", "Ocelot Release");
SetupGitHubClient(client);
int releaseId = release.id;
var fileName = file.GetFilename();
string uploadUrl = release.upload_url.ToString();
Information($"UploadFileToGitHubRelease: uploadUrl is {uploadUrl}");
string[] parts = uploadUrl.Split('{');
uploadUrl = parts[0] + $"?name={fileName}"; // $"https://uploads.github.com/repos/ThreeMammals/Ocelot/releases/{releaseId}/assets?name={fileName}"
string[] parts = uploadUrl.Replace("{", "").Split(',');
uploadUrl = parts[0] + "=" + fileName; // $"https://uploads.github.com/repos/ThreeMammals/Ocelot/releases/{releaseId}/assets?name={fileName}"
Information($"UploadFileToGitHubRelease: uploadUrl is {uploadUrl}");
var result = client.PostAsync(uploadUrl, content).Result;
if (result.StatusCode != System.Net.HttpStatusCode.Created)
Expand All @@ -719,20 +688,14 @@ private void UploadFileToGitHubRelease(dynamic release, FilePath file)
private void CompleteGitHubRelease(dynamic release)
{
int releaseId = release.id;
string url = release.url;
string url = release.url.ToString();
var json = $"{{ \"tag_name\": \"{versioning.NuGetVersion}\", \"target_commitish\": \"main\", \"name\": \"{versioning.NuGetVersion}\", \"body\": \"{ReleaseNotesAsJson()}\", \"draft\": false, \"prerelease\": false }}";
var request = new System.Net.Http.HttpRequestMessage(new System.Net.Http.HttpMethod("Patch"), url); // $"https://api.github.com/repos/ThreeMammals/Ocelot/releases/{releaseId}");
request.Content = new System.Net.Http.StringContent(json, System.Text.Encoding.UTF8, "application/json");

using (var client = new System.Net.Http.HttpClient())
{
// client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
// "Basic",
// Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{gitHubUsername}:{gitHubPassword}"))
// );
// client.DefaultRequestHeaders.Add("User-Agent", "Ocelot Release");
SetupGitHubClient(client);

var result = client.SendAsync(request).Result;
if (result.StatusCode != System.Net.HttpStatusCode.OK)
{
Expand Down

0 comments on commit 8c0180a

Please sign in to comment.