From 3ee0dee61ba342bc7977328fa21e7c14c2593823 Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 1 Nov 2022 21:50:49 +0100 Subject: [PATCH] Start using gitea-sdk where possible --- gitea.go | 5 ++-- go.mod | 2 ++ go.sum | 6 +++++ pkg/gitea/gitea.go | 57 ++++++++++++---------------------------------- 4 files changed, 25 insertions(+), 45 deletions(-) diff --git a/gitea.go b/gitea.go index 0dbaac5..fea0da0 100644 --- a/gitea.go +++ b/gitea.go @@ -41,9 +41,10 @@ func (Middleware) CaddyModule() caddy.ModuleInfo { // Provision provisions gitea client. func (m *Middleware) Provision(ctx caddy.Context) error { - m.Client = gitea.NewClient(m.Server, m.Token, m.GiteaPages) + var err error + m.Client, err = gitea.NewClient(m.Server, m.Token, m.GiteaPages) - return nil + return err } // Validate implements caddy.Validator. diff --git a/go.mod b/go.mod index 34f56ce..17d037a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/42wim/caddy-gitea go 1.19 require ( + code.gitea.io/sdk/gitea v0.15.1 github.com/BurntSushi/toml v1.2.1 github.com/alecthomas/chroma v0.10.0 github.com/caddyserver/caddy/v2 v2.6.2 @@ -45,6 +46,7 @@ require ( github.com/google/cel-go v0.12.5 // indirect github.com/google/pprof v0.0.0-20221010195024-131d412537ea // indirect github.com/google/uuid v1.3.0 // indirect + github.com/hashicorp/go-version v1.2.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/huandu/xstrings v1.3.2 // indirect github.com/imdario/mergo v0.3.13 // indirect diff --git a/go.sum b/go.sum index 69ca91a..1e5d4fa 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,9 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= +code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE= +code.gitea.io/sdk/gitea v0.15.1 h1:WJreC7YYuxbn0UDaPuWIe/mtiNKTvLN8MLkaw71yx/M= +code.gitea.io/sdk/gitea v0.15.1/go.mod h1:klY2LVI3s3NChzIk/MzMn7G1FHrfU7qd63iSMVoHRBA= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek= filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= @@ -254,6 +257,8 @@ github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8 github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= github.com/gorilla/mux v1.4.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/groob/finalizer v0.0.0-20170707115354-4c2ed49aabda/go.mod h1:MyndkAZd5rUMdNogn35MWXBX1UiBigrU8eTj8DoAC2c= +github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= +github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= @@ -837,6 +842,7 @@ golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20200227222343-706bc42d1f0d/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= golang.org/x/tools v0.0.0-20200312045724-11d5b4c81c7d/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= +golang.org/x/tools v0.0.0-20200325010219-a49f79bcc224/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= diff --git a/pkg/gitea/gitea.go b/pkg/gitea/gitea.go index 048ed2f..3c7098d 100644 --- a/pkg/gitea/gitea.go +++ b/pkg/gitea/gitea.go @@ -2,7 +2,6 @@ package gitea import ( "bytes" - "encoding/json" "errors" "fmt" "io" @@ -12,6 +11,7 @@ import ( "strings" "sync" + gclient "code.gitea.io/sdk/gitea" "github.com/spf13/viper" ) @@ -19,25 +19,25 @@ type Client struct { serverURL string token string giteapages string + gc *gclient.Client } -type pagesConfig struct { -} - -type topicsResponse struct { - Topics []string `json:"topics"` -} - -func NewClient(serverURL, token, giteapages string) *Client { +func NewClient(serverURL, token, giteapages string) (*Client, error) { if giteapages == "" { giteapages = "gitea-pages" } + gc, err := gclient.NewClient(serverURL, gclient.SetToken(token), gclient.SetGiteaVersion("")) + if err != nil { + return nil, err + } + return &Client{ serverURL: serverURL, token: token, + gc: gc, giteapages: giteapages, - } + }, nil } func (c *Client) Open(name, ref string) (fs.File, error) { @@ -86,6 +86,8 @@ func (c *Client) getRawFileOrLFS(owner, repo, filepath, ref string) ([]byte, err err error ) + // TODO: make pr for go-sdk + // gitea sdk doesn't support "media" type for lfs/non-lfs giteaURL, err = url.JoinPath(c.serverURL+"/api/v1/repos/", owner, repo, "media", filepath) if err != nil { return nil, err @@ -150,39 +152,8 @@ func handleMD(res []byte) ([]byte, error) { } func (c *Client) repoTopics(owner, repo string) ([]string, error) { - var ( - giteaURL string - err error - ) - - giteaURL, err = url.JoinPath(c.serverURL+"/api/v1/repos/", owner, repo, "topics") - if err != nil { - return nil, err - } - - req, err := http.NewRequest(http.MethodGet, giteaURL, nil) - if err != nil { - return nil, err - } - - req.Header.Add("Authorization", "token "+c.token) - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return nil, err - } - - res, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - defer resp.Body.Close() - - t := topicsResponse{} - json.Unmarshal(res, &t) - - return t.Topics, nil + repos, _, err := c.gc.ListRepoTopics(owner, repo, gclient.ListRepoTopicsOptions{}) + return repos, err } func (c *Client) allowsPages(owner, repo string) bool {