some background code for manager workspaces
This commit is contained in:
@@ -1,7 +1,67 @@
|
||||
using ModpackUpdater.Apps.Manager.Api.Model;
|
||||
using NGitLab;
|
||||
using NGitLab.Models;
|
||||
using System.Text.Encodings.Web;
|
||||
|
||||
namespace ModpackUpdater.Apps.Manager.Features.Workspaces.GitLabRepo;
|
||||
|
||||
internal class GitLabRepoWorkspace : IWorkspace
|
||||
internal class GitLabRepoWorkspace(GitLabRepoWorkspaceConfig config) : IWorkspace
|
||||
{
|
||||
public WorkspaceConfig Config => ConfigX;
|
||||
|
||||
public GitLabRepoWorkspaceConfig ConfigX { get; } = config;
|
||||
|
||||
public IGitLabClient Gitlab { get; } = new GitLabClient(config.InstanceUrl, config.ApiToken);
|
||||
|
||||
public InstallInfos? InstallInfos { get; private set; }
|
||||
|
||||
public UpdateInfos? UpdateInfos { get; private set; }
|
||||
|
||||
public async Task<bool> Load()
|
||||
{
|
||||
InstallInfos = InstallInfos.Parse(await GetContent(ConfigX.FileLocationInstallJson));
|
||||
UpdateInfos = UpdateInfos.Parse(await GetContent(ConfigX.FileLocationUpdateJson));
|
||||
return InstallInfos != null && UpdateInfos != null;
|
||||
}
|
||||
|
||||
public async Task<bool> Save()
|
||||
{
|
||||
if (InstallInfos != null)
|
||||
await SaveContent(ConfigX.FileLocationInstallJson, InstallInfos.ToString());
|
||||
|
||||
if (UpdateInfos != null)
|
||||
await SaveContent(ConfigX.FileLocationUpdateJson, UpdateInfos.ToString());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<string> GetContent(string path)
|
||||
{
|
||||
var pathUrl = UrlEncoder.Default.Encode(path);
|
||||
var repoId = new ProjectId(ConfigX.RepoId);
|
||||
var repo = Gitlab.GetRepository(repoId);
|
||||
var data = await repo.Files.GetAsync(pathUrl, ConfigX.RepoBranche);
|
||||
return data.DecodedContent;
|
||||
}
|
||||
|
||||
private Task<bool> SaveContent(string path, string content)
|
||||
{
|
||||
var pathUrl = UrlEncoder.Default.Encode(path);
|
||||
var repoId = new ProjectId(ConfigX.RepoId);
|
||||
var repo = Gitlab.GetRepository(repoId);
|
||||
var update = new FileUpsert
|
||||
{
|
||||
Branch = ConfigX.RepoBranche,
|
||||
CommitMessage = "update " + Path.GetFileName(pathUrl),
|
||||
RawContent = content,
|
||||
Path = pathUrl,
|
||||
};
|
||||
|
||||
if (repo.Files.FileExists(pathUrl, ConfigX.RepoBranche))
|
||||
repo.Files.Update(update);
|
||||
else
|
||||
repo.Files.Create(update);
|
||||
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user