feat: Allow reading an access token

This commit is contained in:
2024-02-04 13:26:48 +01:00
parent 3747f02ed8
commit 48b6585eba
3 changed files with 44 additions and 3 deletions

View File

@@ -38,6 +38,8 @@ type Repository struct {
}
type GiteaClient struct {
Token string
GetRepository GetRepositoryMethod
HasBranch HasBranchMethod
HasUser HasUserMethod
@@ -46,8 +48,9 @@ type GiteaClient struct {
LookupRepoTXT LookupRepoTXTMethod
}
func NewGiteaClient(giteaUrl string, giteaClient *gitea.Client) GiteaClient {
func NewGiteaClient(giteaUrl string, token string, giteaClient *gitea.Client) GiteaClient {
return GiteaClient{
Token: token,
GetRepository: func(username, repositoryName string) (Repository, error) {
repo, _, err := giteaClient.GetRepo(username, repositoryName)
if err != nil {
@@ -91,6 +94,11 @@ func NewGiteaClient(giteaUrl string, giteaClient *gitea.Client) GiteaClient {
if since != nil {
sinceFormat := since.Format(time.RFC1123)
req.Header.Add("If-Modified-Since", sinceFormat)
// Add authentication, if we have a token
if token != "" {
req.Header.Add("Authorization", "token "+token)
}
}
resp, err := client.Do(req)
if err != nil {