From 8de7d565a14ed4d1e36d060461a417123e4ae60a Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 19 Mar 2024 18:42:13 +1300 Subject: [PATCH] gitea: optionally support authenticated api --- config.toml.sample | 1 + gitea/apiclient.go | 8 +++++++- main.go | 6 +++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/config.toml.sample b/config.toml.sample index 48b4239..b4bcebb 100644 --- a/config.toml.sample +++ b/config.toml.sample @@ -5,6 +5,7 @@ BindTo="0.0.0.0:5656" [Gitea] URL="https://gitea.com/" Org="gitea" +Token="" # Must have misc:write, org:read, repo:read permission MaxConnections=2 # Use zero for unlimited [Redirect] diff --git a/gitea/apiclient.go b/gitea/apiclient.go index ce06231..ef44f8b 100644 --- a/gitea/apiclient.go +++ b/gitea/apiclient.go @@ -17,12 +17,13 @@ import ( type APIClient struct { urlBase string orgName string + token string apiSem *semaphore.Weighted } // NewAPIClient creates a new Gitea API client for a single Gitea organization. // Set maxConnections to 0 for unlimited concurrent API calls. -func NewAPIClient(urlBase string, orgName string, maxConnections int64) *APIClient { +func NewAPIClient(urlBase string, orgName string, token string, maxConnections int64) *APIClient { if !strings.HasSuffix(urlBase, `/`) { urlBase += `/` @@ -31,6 +32,7 @@ func NewAPIClient(urlBase string, orgName string, maxConnections int64) *APIClie ret := &APIClient{ urlBase: urlBase, orgName: orgName, + token: token, } if maxConnections == 0 { // unlimited @@ -98,6 +100,10 @@ func (ac *APIClient) apiRequest(ctx context.Context, endpoint string, target int return err } + if ac.token != "" { + req.Header.Set(`Authorization`, `token `+ac.token) + } + resp, err := http.DefaultClient.Do(req) if err != nil { return err diff --git a/main.go b/main.go index 7967068..79e695b 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,8 @@ import ( type Config struct { BindTo string Gitea struct { - URL, Org string - MaxConnections int64 + URL, Org, Token string + MaxConnections int64 } Redirect map[string]string Template struct { @@ -55,7 +55,7 @@ func main() { } // Create Gitea API client - app.gitea = gitea.NewAPIClient(app.cfg.Gitea.URL, app.cfg.Gitea.Org, app.cfg.Gitea.MaxConnections) + app.gitea = gitea.NewAPIClient(app.cfg.Gitea.URL, app.cfg.Gitea.Org, app.cfg.Gitea.Token, app.cfg.Gitea.MaxConnections) // Sync worker go app.syncWorker(context.Background())