Skip to content

Commit

Permalink
Add support for adding files to an existing gist (for coverage)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Feb 8, 2024
1 parent a3b35e6 commit 9e9a7df
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 14 deletions.
86 changes: 78 additions & 8 deletions src/dotnet-releaser/DevHosting/GitHubDevHosting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using DotNetReleaser.Changelog;
using DotNetReleaser.Configuration;
using DotNetReleaser.Logging;
using DotNetReleaser.Runners;
using NuGet.Versioning;
using Octokit;

Expand Down Expand Up @@ -90,18 +91,87 @@ public async Task CreateOrUpdateGist(string gistId, string fileName, string cont
_log.Info($"No need to update the gist {gistId} as the content is the same.");
return;
}

// Update the file
GistUpdate gistUpdate = new GistUpdate();
//gistUpdate.Files.Add();
gistUpdate.Files.Add(fileName, new GistFileUpdate() { NewFileName = fileName, Content = content });
await _client.Gist.Edit(gistId, gistUpdate);
}
else
{
_log.Warn($"Cannot update gist {gistId} as it does not contain the required file {fileName}");
return;
}
if (!gist.GitPushUrl.StartsWith("https://"))
{
_log.Warn($"The gist URL {gist.GitPushUrl} is not a standard gist URL and cannot be updated.");
return;
}

var uri = new Uri(gist.GitPushUrl);
var gitCloneUrl = $"git@{uri.Host}:{uri.PathAndQuery.TrimStart('/')}";

var gistTempDirectory = Directory.CreateTempSubdirectory("dotnet-releaser-gist");
try
{
var result = await GitRunner.Run("clone", new[] { gitCloneUrl, gistTempDirectory.FullName });

if (result.HasErrors)
{
_log.Error($"Unable to clone the gist {gistId} to {gistTempDirectory.FullName}. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
return;
}

var gistFile = Path.Combine(gistTempDirectory.FullName, fileName);
await File.WriteAllTextAsync(gistFile, content);

result = await GitRunner.Run("config", new[] { "--local", "user.email", "[email protected]" }, gistTempDirectory.FullName);
if (result.HasErrors)
{
_log.Error($"Unable to set the user.email for the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
return;
}

result = await GitRunner.Run("config", new[] { "--local", "user.name", "GitHub Action" }, gistTempDirectory.FullName);
if (result.HasErrors)
{
_log.Error($"Unable to set the user.name for the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
return;
}

// Update the file
GistUpdate gistUpdate = new GistUpdate();
//gistUpdate.Files.Add();
gistUpdate.Files.Add(fileName, new GistFileUpdate() { NewFileName = fileName, Content = content });
await _client.Gist.Edit(gistId, gistUpdate);
result = await GitRunner.Run("add", new[] { "." }, gistTempDirectory.FullName);
if (result.HasErrors)
{
_log.Error($"Unable to add the file {fileName} to the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
return;
}

result = await GitRunner.Run("commit", new[] { "-m", $"Upload new file {fileName}" }, gistTempDirectory.FullName);
if (result.HasErrors)
{
_log.Error($"Unable to commit the file {fileName} to the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
return;
}

result = await GitRunner.Run("push", Array.Empty<string>(), gistTempDirectory.FullName);
if (result.HasErrors)
{
_log.Error($"Unable to push the file {fileName} to the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
return;
}
}
finally
{
try
{
gistTempDirectory.Delete(true);
}
catch
{
// ignore
}
}

_log.Warn($"New file uploaded to gist {gistId}");
}
}

private async Task<List<(RepositoryTag, NuGetVersion)>> GetAllReleaseTagsImpl(string user, string repo, string tagPrefix)
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-releaser/Runners/DotNetRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public DotNetRunner(string command) : base(command)
{
}

public async Task<DotNetResult> Run()
public async Task<CommandResulExtended> Run()
{
return await RunImpl();
}
Expand Down
8 changes: 4 additions & 4 deletions src/dotnet-releaser/Runners/DotNetRunnerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace DotNetReleaser.Runners;

public record DotNetResult(CommandResult CommandResult, string CommandLine, string Output)
public record CommandResulExtended(CommandResult CommandResult, string CommandLine, string Output)
{
public bool HasErrors => CommandResult.ExitCode != 0;
}
Expand Down Expand Up @@ -43,7 +43,7 @@ protected DotNetRunnerBase(string command)

protected virtual IReadOnlyDictionary<string, object> ComputeProperties() => Properties;

protected async Task<DotNetResult> RunImpl()
protected async Task<CommandResulExtended> RunImpl()
{
return await Run(Command, ComputeArguments(), ComputeProperties(), WorkingDirectory);
}
Expand Down Expand Up @@ -83,7 +83,7 @@ private static string GetPropertyValueAsString(object value)
return value.ToString() ?? string.Empty;
}

private async Task<DotNetResult> Run(string command, IEnumerable<string> args, IReadOnlyDictionary<string, object>? properties = null, string? workingDirectory = null)
private async Task<CommandResulExtended> Run(string command, IEnumerable<string> args, IReadOnlyDictionary<string, object>? properties = null, string? workingDirectory = null)
{
var stdOutAndErrorBuffer = new StringBuilder();

Expand All @@ -102,7 +102,7 @@ private async Task<DotNetResult> Run(string command, IEnumerable<string> args, I
RunAfterStart?.Invoke();

var result = await wrap.ConfigureAwait(false);
return new DotNetResult(result, $"dotnet {arguments}",stdOutAndErrorBuffer.ToString());
return new CommandResulExtended(result, $"dotnet {arguments}",stdOutAndErrorBuffer.ToString());
}

protected virtual void Dispose(bool disposing)
Expand Down
39 changes: 39 additions & 0 deletions src/dotnet-releaser/Runners/GitRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

using CliWrap;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System;
using CliWrap.Builders;

namespace DotNetReleaser.Runners;

public static class GitRunner
{
public static async Task<CommandResulExtended> Run(string command, IEnumerable<string> args, string? workingDirectory = null)
{
var stdOutAndErrorBuffer = new StringBuilder();

var argsBuilder = new ArgumentsBuilder();
argsBuilder.Add(command);
foreach (var arg in args)
{
argsBuilder.Add(arg);
}
var arguments = argsBuilder.Build();

var wrap = Cli.Wrap("git")
.WithArguments(arguments)
.WithWorkingDirectory(workingDirectory ?? Environment.CurrentDirectory)
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutAndErrorBuffer))
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdOutAndErrorBuffer))
.WithValidation(CommandResultValidation.None)
.ExecuteAsync();

var result = await wrap.ConfigureAwait(false);
return new CommandResulExtended(result, $"git {arguments}", stdOutAndErrorBuffer.ToString());
}
}
2 changes: 1 addition & 1 deletion src/dotnet-releaser/Runners/MSBuildProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace DotNetReleaser.Runners;

public record MSBuildResult(CommandResult CommandResult, string CommandLine, string Output, Dictionary<string, List<ITaskItem>> TargetOutputs) : DotNetResult(CommandResult, CommandLine, Output);
public record MSBuildResult(CommandResult CommandResult, string CommandLine, string Output, Dictionary<string, List<ITaskItem>> TargetOutputs) : CommandResulExtended(CommandResult, CommandLine, Output);

public class MSBuildRunner : DotNetRunnerBase
{
Expand Down

0 comments on commit 9e9a7df

Please sign in to comment.