2022-11-21 10:23:25 +09:00
|
|
|
using System;
|
2023-01-16 20:12:51 +09:00
|
|
|
using System.Collections.Generic;
|
2022-11-21 10:23:25 +09:00
|
|
|
using System.Net.Http;
|
2023-01-16 20:12:51 +09:00
|
|
|
using System.Text.RegularExpressions;
|
2022-11-21 10:23:25 +09:00
|
|
|
using System.Threading.Tasks;
|
2023-01-16 20:12:51 +09:00
|
|
|
using ExchangeSharp;
|
2022-11-21 10:23:25 +09:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
2023-01-16 20:12:51 +09:00
|
|
|
using static System.Net.WebRequestMethods;
|
2022-11-21 10:23:25 +09:00
|
|
|
|
|
|
|
namespace BTCPayServer.Plugins
|
|
|
|
{
|
|
|
|
public class PublishedVersion
|
|
|
|
{
|
2023-01-16 20:12:51 +09:00
|
|
|
public class BuildInfoClass
|
|
|
|
{
|
|
|
|
public string gitCommit { get; set; }
|
|
|
|
public string pluginDir { get; set; }
|
|
|
|
public string gitRepository { get; set; }
|
|
|
|
#nullable enable
|
|
|
|
static Regex GithubRepositoryRegex = new Regex("^https://(www\\.)?github\\.com/([^/]+)/([^/]+)/?");
|
|
|
|
public record GithubRepository(string Owner, string RepositoryName)
|
|
|
|
{
|
|
|
|
public string? GetSourceUrl(string commit, string pluginDir)
|
|
|
|
{
|
|
|
|
if (commit is null)
|
|
|
|
return null;
|
|
|
|
return $"https://github.com/{Owner}/{RepositoryName}/tree/{commit}/{pluginDir}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public GithubRepository? GetGithubRepository()
|
|
|
|
{
|
|
|
|
if (gitRepository is null)
|
|
|
|
return null;
|
|
|
|
var match = GithubRepositoryRegex.Match(gitRepository);
|
|
|
|
if (!match.Success)
|
|
|
|
return null;
|
|
|
|
return new GithubRepository(match.Groups[2].Value, match.Groups[3].Value);
|
|
|
|
}
|
|
|
|
#nullable restore
|
|
|
|
[JsonExtensionData]
|
|
|
|
public IDictionary<string, JToken> AdditionalData { get; set; }
|
|
|
|
}
|
2022-11-21 10:23:25 +09:00
|
|
|
public string ProjectSlug { get; set; }
|
|
|
|
public long BuildId { get; set; }
|
2023-01-16 20:12:51 +09:00
|
|
|
public BuildInfoClass BuildInfo { get; set; }
|
2022-11-21 10:23:25 +09:00
|
|
|
public JObject ManifestInfo { get; set; }
|
2022-11-22 19:06:23 +09:00
|
|
|
public string Documentation { get; set; }
|
2022-11-21 10:23:25 +09:00
|
|
|
}
|
|
|
|
public class PluginBuilderClient
|
|
|
|
{
|
|
|
|
HttpClient httpClient;
|
|
|
|
public HttpClient HttpClient => httpClient;
|
|
|
|
public PluginBuilderClient(HttpClient httpClient)
|
|
|
|
{
|
|
|
|
this.httpClient = httpClient;
|
|
|
|
}
|
2023-02-28 12:16:33 +01:00
|
|
|
static JsonSerializerSettings serializerSettings = new() { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() };
|
2022-11-21 10:23:25 +09:00
|
|
|
public async Task<PublishedVersion[]> GetPublishedVersions(string btcpayVersion, bool includePreRelease)
|
|
|
|
{
|
2023-02-28 12:16:33 +01:00
|
|
|
var queryString = $"?includePreRelease={includePreRelease}";
|
2023-04-10 11:07:03 +09:00
|
|
|
if (btcpayVersion is not null)
|
2023-02-28 12:16:33 +01:00
|
|
|
queryString += $"&btcpayVersion={btcpayVersion}&";
|
|
|
|
var result = await httpClient.GetStringAsync($"api/v1/plugins{queryString}");
|
2022-11-21 10:23:25 +09:00
|
|
|
return JsonConvert.DeserializeObject<PublishedVersion[]>(result, serializerSettings) ?? throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|