Files
FA/Services/Gitea/GiteaService.cs
Piotr Kus 2e2d8f5df4
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
* Added SendRequestAsync
* Changed method names
2026-01-23 10:40:44 +01:00

71 lines
2.3 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 CreateGiteaClientAsync(string basePath, string giteaApiToken)
{
var config = new Configuration
{
BasePath = basePath,
ApiKey =
{
["token"] = giteaApiToken
}
};
return new RepositoryApi(config);
}
public async Task<List<Commit>> GetLastCommitsAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration,
int limit)
{
var lastCommits = await repositoryApi.RepoGetAllCommitsAsync(configuration.Owner, configuration.Repository,
configuration.Branch, limit: limit);
return lastCommits;
}
public async Task<Commit?> GetLastCommitAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration)
{
var lastCommit = await GetLastCommitsAsync(repositoryApi, configuration, 1);
return lastCommit.FirstOrDefault();
}
public async Task<ContentsResponse> GetFileContentAsync(RepositoryApi repositoryApi,
GiteaConfiguration configuration,
string 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)
{
var requestUrl = $"{configuration.RequestUrl}/{endpoint}";
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl)
{
Content = content,
Headers = { {"Content-Type", "application/json"}, { "Authorization", $"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);
}
}
}