Skip to content

Commit 9e9a7df

Browse files
committed
Add support for adding files to an existing gist (for coverage)
1 parent a3b35e6 commit 9e9a7df

File tree

5 files changed

+123
-14
lines changed

5 files changed

+123
-14
lines changed

src/dotnet-releaser/DevHosting/GitHubDevHosting.cs

+78-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using DotNetReleaser.Changelog;
1111
using DotNetReleaser.Configuration;
1212
using DotNetReleaser.Logging;
13+
using DotNetReleaser.Runners;
1314
using NuGet.Versioning;
1415
using Octokit;
1516

@@ -90,18 +91,87 @@ public async Task CreateOrUpdateGist(string gistId, string fileName, string cont
9091
_log.Info($"No need to update the gist {gistId} as the content is the same.");
9192
return;
9293
}
94+
95+
// Update the file
96+
GistUpdate gistUpdate = new GistUpdate();
97+
//gistUpdate.Files.Add();
98+
gistUpdate.Files.Add(fileName, new GistFileUpdate() { NewFileName = fileName, Content = content });
99+
await _client.Gist.Edit(gistId, gistUpdate);
93100
}
94101
else
95102
{
96-
_log.Warn($"Cannot update gist {gistId} as it does not contain the required file {fileName}");
97-
return;
98-
}
103+
if (!gist.GitPushUrl.StartsWith("https://"))
104+
{
105+
_log.Warn($"The gist URL {gist.GitPushUrl} is not a standard gist URL and cannot be updated.");
106+
return;
107+
}
108+
109+
var uri = new Uri(gist.GitPushUrl);
110+
var gitCloneUrl = $"git@{uri.Host}:{uri.PathAndQuery.TrimStart('/')}";
111+
112+
var gistTempDirectory = Directory.CreateTempSubdirectory("dotnet-releaser-gist");
113+
try
114+
{
115+
var result = await GitRunner.Run("clone", new[] { gitCloneUrl, gistTempDirectory.FullName });
116+
117+
if (result.HasErrors)
118+
{
119+
_log.Error($"Unable to clone the gist {gistId} to {gistTempDirectory.FullName}. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
120+
return;
121+
}
122+
123+
var gistFile = Path.Combine(gistTempDirectory.FullName, fileName);
124+
await File.WriteAllTextAsync(gistFile, content);
125+
126+
result = await GitRunner.Run("config", new[] { "--local", "user.email", "[email protected]" }, gistTempDirectory.FullName);
127+
if (result.HasErrors)
128+
{
129+
_log.Error($"Unable to set the user.email for the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
130+
return;
131+
}
132+
133+
result = await GitRunner.Run("config", new[] { "--local", "user.name", "GitHub Action" }, gistTempDirectory.FullName);
134+
if (result.HasErrors)
135+
{
136+
_log.Error($"Unable to set the user.name for the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
137+
return;
138+
}
99139

100-
// Update the file
101-
GistUpdate gistUpdate = new GistUpdate();
102-
//gistUpdate.Files.Add();
103-
gistUpdate.Files.Add(fileName, new GistFileUpdate() { NewFileName = fileName, Content = content });
104-
await _client.Gist.Edit(gistId, gistUpdate);
140+
result = await GitRunner.Run("add", new[] { "." }, gistTempDirectory.FullName);
141+
if (result.HasErrors)
142+
{
143+
_log.Error($"Unable to add the file {fileName} to the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
144+
return;
145+
}
146+
147+
result = await GitRunner.Run("commit", new[] { "-m", $"Upload new file {fileName}" }, gistTempDirectory.FullName);
148+
if (result.HasErrors)
149+
{
150+
_log.Error($"Unable to commit the file {fileName} to the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
151+
return;
152+
}
153+
154+
result = await GitRunner.Run("push", Array.Empty<string>(), gistTempDirectory.FullName);
155+
if (result.HasErrors)
156+
{
157+
_log.Error($"Unable to push the file {fileName} to the git repository. ExitCode: {result.CommandResult.ExitCode}, Output: {result.Output}");
158+
return;
159+
}
160+
}
161+
finally
162+
{
163+
try
164+
{
165+
gistTempDirectory.Delete(true);
166+
}
167+
catch
168+
{
169+
// ignore
170+
}
171+
}
172+
173+
_log.Warn($"New file uploaded to gist {gistId}");
174+
}
105175
}
106176

107177
private async Task<List<(RepositoryTag, NuGetVersion)>> GetAllReleaseTagsImpl(string user, string repo, string tagPrefix)

src/dotnet-releaser/Runners/DotNetRunner.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public DotNetRunner(string command) : base(command)
88
{
99
}
1010

11-
public async Task<DotNetResult> Run()
11+
public async Task<CommandResulExtended> Run()
1212
{
1313
return await RunImpl();
1414
}

src/dotnet-releaser/Runners/DotNetRunnerBase.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace DotNetReleaser.Runners;
1111

12-
public record DotNetResult(CommandResult CommandResult, string CommandLine, string Output)
12+
public record CommandResulExtended(CommandResult CommandResult, string CommandLine, string Output)
1313
{
1414
public bool HasErrors => CommandResult.ExitCode != 0;
1515
}
@@ -43,7 +43,7 @@ protected DotNetRunnerBase(string command)
4343

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

46-
protected async Task<DotNetResult> RunImpl()
46+
protected async Task<CommandResulExtended> RunImpl()
4747
{
4848
return await Run(Command, ComputeArguments(), ComputeProperties(), WorkingDirectory);
4949
}
@@ -83,7 +83,7 @@ private static string GetPropertyValueAsString(object value)
8383
return value.ToString() ?? string.Empty;
8484
}
8585

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

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

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

108108
protected virtual void Dispose(bool disposing)
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
using CliWrap;
6+
using System.Collections.Generic;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using System;
10+
using CliWrap.Builders;
11+
12+
namespace DotNetReleaser.Runners;
13+
14+
public static class GitRunner
15+
{
16+
public static async Task<CommandResulExtended> Run(string command, IEnumerable<string> args, string? workingDirectory = null)
17+
{
18+
var stdOutAndErrorBuffer = new StringBuilder();
19+
20+
var argsBuilder = new ArgumentsBuilder();
21+
argsBuilder.Add(command);
22+
foreach (var arg in args)
23+
{
24+
argsBuilder.Add(arg);
25+
}
26+
var arguments = argsBuilder.Build();
27+
28+
var wrap = Cli.Wrap("git")
29+
.WithArguments(arguments)
30+
.WithWorkingDirectory(workingDirectory ?? Environment.CurrentDirectory)
31+
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutAndErrorBuffer))
32+
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdOutAndErrorBuffer))
33+
.WithValidation(CommandResultValidation.None)
34+
.ExecuteAsync();
35+
36+
var result = await wrap.ConfigureAwait(false);
37+
return new CommandResulExtended(result, $"git {arguments}", stdOutAndErrorBuffer.ToString());
38+
}
39+
}

src/dotnet-releaser/Runners/MSBuildProgram.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace DotNetReleaser.Runners;
1212

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

1515
public class MSBuildRunner : DotNetRunnerBase
1616
{

0 commit comments

Comments
 (0)