|
10 | 10 | using DotNetReleaser.Changelog;
|
11 | 11 | using DotNetReleaser.Configuration;
|
12 | 12 | using DotNetReleaser.Logging;
|
| 13 | +using DotNetReleaser.Runners; |
13 | 14 | using NuGet.Versioning;
|
14 | 15 | using Octokit;
|
15 | 16 |
|
@@ -90,18 +91,87 @@ public async Task CreateOrUpdateGist(string gistId, string fileName, string cont
|
90 | 91 | _log.Info($"No need to update the gist {gistId} as the content is the same.");
|
91 | 92 | return;
|
92 | 93 | }
|
| 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); |
93 | 100 | }
|
94 | 101 | else
|
95 | 102 | {
|
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 | + } |
99 | 139 |
|
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 | + } |
105 | 175 | }
|
106 | 176 |
|
107 | 177 | private async Task<List<(RepositoryTag, NuGetVersion)>> GetAllReleaseTagsImpl(string user, string repo, string tagPrefix)
|
|
0 commit comments