start a big rework
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using NGitLab;
|
||||
using NGitLab.Models;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.Updating.Administration.Integrations;
|
||||
|
||||
public static class GitLabSnippetExtension
|
||||
{
|
||||
private static IGitLabClient GetClient(GitLabSnippetConfig config)
|
||||
{
|
||||
return new GitLabClient(config.GitLabUrl, config.PersonalAccessToken);
|
||||
}
|
||||
|
||||
private static bool LoadSnippet(UpdateServerManager manager)
|
||||
{
|
||||
var httpClient = new HttpClient();
|
||||
var glClient = GetClient(manager.Config.GitLabSnippetConfig);
|
||||
|
||||
if (glClient.Snippets.Get(manager.Config.GitLabSnippetConfig.ProjectId, manager.Config.GitLabSnippetConfig.SnippetId) is not Snippet snippet
|
||||
|| snippet.Files.FirstOrDefault(f => f.Path == manager.Config.GitLabSnippetConfig.SnippetFilePath) is not SnippetFile file
|
||||
|| httpClient.GetStringAsync(snippet.Files[1].RawUrl).Result is not string content)
|
||||
return false;
|
||||
|
||||
manager.UpdateInfo = UpdateInfo.Parse(content);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool SaveSnippet(UpdateServerManager manager)
|
||||
{
|
||||
var glClient = GetClient(manager.Config.GitLabSnippetConfig);
|
||||
|
||||
if (glClient == null)
|
||||
return false;
|
||||
|
||||
glClient.Snippets.Update(new SnippetProjectUpdate
|
||||
{
|
||||
SnippetId = manager.Config.GitLabSnippetConfig.SnippetId,
|
||||
ProjectId = manager.Config.GitLabSnippetConfig.ProjectId,
|
||||
Files = [
|
||||
new SnippetUpdateFile
|
||||
{
|
||||
Action = SnippetUpdateFileAction.Update,
|
||||
FilePath = manager.Config.GitLabSnippetConfig.SnippetFilePath,
|
||||
Content = manager.UpdateInfo.ToString(),
|
||||
}
|
||||
],
|
||||
Visibility = VisibilityLevel.Public,
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Task<bool> ReadInfoFromGitLabSnippet(this UpdateServerManager manager)
|
||||
{
|
||||
return Task.Run(() => LoadSnippet(manager));
|
||||
}
|
||||
|
||||
public static Task<bool> SaveInfoToGitLabSnippet(this UpdateServerManager manager)
|
||||
{
|
||||
return Task.Run(() => SaveSnippet(manager));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user