99 lines
3.7 KiB
C#
99 lines
3.7 KiB
C#
using Gitea.Net.Api;
|
|
using Gitea.Net.Client;
|
|
using Gitea.Net.Model;
|
|
using HttpMethod = System.Net.Http.HttpMethod;
|
|
|
|
namespace Services.Gitea;
|
|
|
|
public class GiteaService : IGiteaService
|
|
{
|
|
private static readonly HttpClient HttpClient = new();
|
|
|
|
public RepositoryApi CreateGiteaClient(GiteaConfiguration configuration)
|
|
{
|
|
if (configuration == null)
|
|
throw new ArgumentNullException(nameof(configuration));
|
|
|
|
var config = new Configuration
|
|
{
|
|
BasePath = configuration.BaseUrl
|
|
};
|
|
|
|
config.ApiKey = new Dictionary<string, string>
|
|
{
|
|
["token"] = configuration.ApiToken
|
|
};
|
|
|
|
return new RepositoryApi(config);
|
|
}
|
|
|
|
public async Task<List<Commit>> GetLastCommitsAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration,
|
|
int limit)
|
|
{
|
|
if (repositoryApi == null) throw new ArgumentNullException(nameof(repositoryApi));
|
|
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
|
|
if (limit <= 0) throw new ArgumentOutOfRangeException(nameof(limit), "Limit must be greater than zero.");
|
|
|
|
var lastCommits = await repositoryApi.RepoGetAllCommitsAsync(configuration.Owner, configuration.Repository,
|
|
configuration.Branch, limit: limit);
|
|
return lastCommits;
|
|
}
|
|
|
|
public async Task<Commit?> GetLastCommitAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(repositoryApi);
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
|
|
var lastCommit = await GetLastCommitsAsync(repositoryApi, configuration, 1);
|
|
return lastCommit.FirstOrDefault();
|
|
}
|
|
|
|
public async Task<List<Branch>> GetAllBranchesAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(repositoryApi);
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
|
|
var branches = await repositoryApi.RepoListBranchesAsync(configuration.Owner, configuration.Repository);
|
|
return branches;
|
|
}
|
|
|
|
public async Task<ContentsResponse> GetFileContentAsync(RepositoryApi repositoryApi,
|
|
GiteaConfiguration configuration,
|
|
string filename)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(repositoryApi);
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
if (string.IsNullOrEmpty(filename)) throw new ArgumentException("Filename cannot be null or empty.", nameof(filename));
|
|
|
|
var repoGetContentsAsync =
|
|
await repositoryApi.RepoGetContentsAsync(configuration.Owner, configuration.Repository, filename);
|
|
return repoGetContentsAsync;
|
|
}
|
|
|
|
public async Task<(bool Status, string Response)> SendRequestAsync(GiteaConfiguration configuration,
|
|
string endpoint, HttpContent content)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
ArgumentNullException.ThrowIfNull(content);
|
|
|
|
if (string.IsNullOrEmpty(endpoint)) throw new ArgumentException("Endpoint cannot be null or empty.", nameof(endpoint));
|
|
|
|
var requestUrl = $"{configuration.Url}/{endpoint}";
|
|
using var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
|
|
request.Content = content;
|
|
|
|
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("token", configuration.ApiToken);
|
|
|
|
try
|
|
{
|
|
var giteaResponse = await HttpClient.SendAsync(request);
|
|
var response = await giteaResponse.Content.ReadAsStringAsync();
|
|
|
|
return (giteaResponse.IsSuccessStatusCode, response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"Error while sending request to Gitea: {ex.Message}", ex);
|
|
}
|
|
}
|
|
} |