-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for adding files to an existing gist (for coverage)
- Loading branch information
Showing
5 changed files
with
123 additions
and
14 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
using DotNetReleaser.Changelog; | ||
using DotNetReleaser.Configuration; | ||
using DotNetReleaser.Logging; | ||
using DotNetReleaser.Runners; | ||
using NuGet.Versioning; | ||
using Octokit; | ||
|
||
|
@@ -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) | ||
|
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
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
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
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()); | ||
} | ||
} |
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