All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
* Changed method names
71 lines
2.3 KiB
C#
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);
|
|
}
|
|
}
|
|
} |