some background code for manager workspaces

This commit is contained in:
2024-09-06 15:16:19 +02:00
parent ebc57e05d5
commit 9bfd83ee4a
23 changed files with 497 additions and 52 deletions

View File

@@ -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);
}
}

View File

@@ -0,0 +1,12 @@
using ModpackUpdater.Apps.Manager.Api.Model;
using Pilz.Configuration;
namespace ModpackUpdater.Apps.Manager.Features.Workspaces.GitLabRepo;
internal class GitLabRepoWorkspaceConfig : WorkspaceConfig
{
public string InstanceUrl { get; set; } = "https://gitlab.com";
public string? ApiToken { get; set; }
public long RepoId { get; set; } = -1L;
public string RepoBranche { get; set; } = "master";
}

View File

@@ -1,18 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Pilz.UI.Telerik.Dialogs;
namespace ModpackUpdater.Apps.Manager.Features.Workspaces.GitLabRepo;
public partial class GitLabRepoWorkspaceConfigEditor : UserControl
internal partial class GitLabRepoWorkspaceConfigEditor : RadFlyoutBase
{
public GitLabRepoWorkspaceConfigEditor()
private readonly GitLabRepoWorkspaceConfig settings;
public GitLabRepoWorkspaceConfigEditor(GitLabRepoWorkspaceConfig settings)
{
this.settings = settings;
InitializeComponent();
}
}

View File

@@ -1,26 +1,30 @@
using ModpackUpdater.Apps.Manager.Api.Model;
using ModpackUpdater.Apps.Manager.Api.Plugins.Params;
using ModpackUpdater.Apps.Manager.Api.Plugins.Features;
using ModpackUpdater.Apps.Manager.LangRes;
using Pilz.Plugins.Advanced;
using Pilz.UI.Extensions;
using Pilz.UI.Symbols;
using Pilz.UI.Telerik.Dialogs;
namespace ModpackUpdater.Apps.Manager.Features.Workspaces.GitLabRepo;
internal class GitLabRepoWorkspaceFeature : PluginFunction, IPluginFeatureProvider<GitLabRepoWorkspaceFeature>
internal class GitLabRepoWorkspaceFeature : WorkspaceFeature, IPluginFeatureProvider<GitLabRepoWorkspaceFeature>
{
public static GitLabRepoWorkspaceFeature Instance { get; } = new();
public GitLabRepoWorkspaceFeature() : base(FeatureTypes.Workspace, "origin.gitlab", "GitLab repository")
public GitLabRepoWorkspaceFeature() : base("origin.gitlab", "GitLab repository")
{
Icon = AppGlobals.Symbols.GetSvgImage(AppSymbols.github, Pilz.UI.Symbols.SymbolSize.Small);
Icon = AppGlobals.Symbols.GetSvgImage(AppSymbols.gitlab, SymbolSize.Small);
}
protected override object? ExecuteFunction(PluginFunctionParameter? @params)
protected override bool OnConfigure(ref IWorkspace workspace)
{
if (@params is not WorkspaceInitParameters p)
return null;
var settings = workspace?.Config as GitLabRepoWorkspaceConfig ?? new();
/// Open config UI
/// ...
if (RadDialogBase.ShowDialog(new GitLabRepoWorkspaceConfigEditor(settings), TitlesLangRes.GitLabRepoWorkspaceEditor, AppGlobals.Symbols.GetSvgImage(AppSymbols.gitlab, SymbolSize.Small)).Result.IsNotOk())
return false;
return new GitLabRepoWorkspace();
workspace = new GitLabRepoWorkspace(settings);
return true;
}
}

View File

@@ -1,5 +0,0 @@
namespace ModpackUpdater.Apps.Manager.Features.Workspaces.GitLabRepo;
internal class GitLabRepoWorkspaceSettings
{
}