diff --git a/go.mod b/go.mod index 2651f7e60..074eb836f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/laszlocph/woodpecker go 1.12 require ( - code.gitea.io/sdk/gitea v0.12.0 + code.gitea.io/sdk/gitea v0.13.1 docker.io/go-docker v1.0.0 github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5 // indirect @@ -34,6 +34,7 @@ require ( github.com/google/go-querystring v1.0.0 // indirect github.com/gorilla/securecookie v1.1.1 github.com/gorilla/websocket v1.4.2 + github.com/hashicorp/go-version v1.2.1 // indirect github.com/jackspirou/syscerts v0.0.0-20160531025014-b68f5469dff1 github.com/joho/godotenv v1.3.0 github.com/kr/pretty v0.2.0 diff --git a/go.sum b/go.sum index 9c74f5684..79adbf4d3 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -code.gitea.io/sdk/gitea v0.12.0 h1:hvDCz4wtFvo7rf5Ebj8tGd4aJ4wLPKX3BKFX9Dk1Pgs= -code.gitea.io/sdk/gitea v0.12.0/go.mod h1:z3uwDV/b9Ls47NGukYM9XhnHtqPh/J+t40lsUrR6JDY= +code.gitea.io/sdk/gitea v0.13.1 h1:Y7bpH2iO6Q0KhhMJfjP/LZ0AmiYITeRQlCD8b0oYqhk= +code.gitea.io/sdk/gitea v0.13.1/go.mod h1:z3uwDV/b9Ls47NGukYM9XhnHtqPh/J+t40lsUrR6JDY= docker.io/go-docker v1.0.0 h1:VdXS/aNYQxyA9wdLD5z8Q8Ro688/hG8HzKxYVEVbE6s= docker.io/go-docker v1.0.0/go.mod h1:7tiAn5a0LFmjbPDbyTPOaTTOuG1ZRNXdPA6RvKY+fpY= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= @@ -129,6 +129,8 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +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/jackspirou/syscerts v0.0.0-20160531025014-b68f5469dff1 h1:9Xm8CKtMZIXgcopfdWk/qZ1rt0HjMgfMR9nxxSeK6vk= github.com/jackspirou/syscerts v0.0.0-20160531025014-b68f5469dff1/go.mod h1:zuHl3Hh+e9P6gmBPvcqR1HjkaWHC/csgyskg6IaFKFo= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= diff --git a/remote/gitea/gitea.go b/remote/gitea/gitea.go index 1a7b2a5d3..5fc9fba27 100644 --- a/remote/gitea/gitea.go +++ b/remote/gitea/gitea.go @@ -136,33 +136,31 @@ func (c *client) Login(res http.ResponseWriter, req *http.Request) (*model.User, return nil, nil } - client := c.newClient() - - // since api does not return token secret, if drone token exists create new one - var accessToken string - client.SetBasicAuth(username, password) - tokens, err := client.ListAccessTokens(gitea.ListAccessTokensOptions{}) - if err == nil { - for _, token := range tokens { - if token.Name == "drone" { - if err := client.DeleteAccessToken(token.ID); err != nil { - return nil, err - } - break - } - } + client, err := c.newClientToken("") + if err != nil { + return nil, err } - token, terr := client.CreateAccessToken( + // since api does not return token secret, if drone token exists create new one + client.SetBasicAuth(username, password) + resp, err := client.DeleteAccessToken("drone") + if err != nil && !(resp != nil && resp.StatusCode == 404) { + return nil, err + } + + token, _, terr := client.CreateAccessToken( gitea.CreateAccessTokenOption{Name: "drone"}, ) if terr != nil { return nil, terr } - accessToken = token.Token + accessToken := token.Token - client = c.newClientToken(accessToken) - account, err := client.GetUserInfo(username) + client, err = c.newClientToken(accessToken) + if err != nil { + return nil, err + } + account, _, err := client.GetUserInfo(username) if err != nil { return nil, err } @@ -182,8 +180,12 @@ func (c *client) Auth(token, secret string) (string, error) { // Teams is supported by the Gitea driver. func (c *client) Teams(u *model.User) ([]*model.Team, error) { - client := c.newClientToken(u.Token) - orgs, err := client.ListMyOrgs(gitea.ListOrgsOptions{}) + client, err := c.newClientToken(u.Token) + if err != nil { + return nil, err + } + + orgs, _, err := client.ListMyOrgs(gitea.ListOrgsOptions{}) if err != nil { return nil, err } @@ -202,8 +204,12 @@ func (c *client) TeamPerm(u *model.User, org string) (*model.Perm, error) { // Repo returns the named Gitea repository. func (c *client) Repo(u *model.User, owner, name string) (*model.Repo, error) { - client := c.newClientToken(u.Token) - repo, err := client.GetRepo(owner, name) + client, err := c.newClientToken(u.Token) + if err != nil { + return nil, err + } + + repo, _, err := client.GetRepo(owner, name) if err != nil { return nil, err } @@ -218,8 +224,12 @@ func (c *client) Repo(u *model.User, owner, name string) (*model.Repo, error) { func (c *client) Repos(u *model.User) ([]*model.Repo, error) { repos := []*model.Repo{} - client := c.newClientToken(u.Token) - all, err := client.ListMyRepos(gitea.ListReposOptions{}) + client, err := c.newClientToken(u.Token) + if err != nil { + return nil, err + } + + all, _, err := client.ListMyRepos(gitea.ListReposOptions{}) if err != nil { return repos, err } @@ -232,8 +242,12 @@ func (c *client) Repos(u *model.User) ([]*model.Repo, error) { // Perm returns the user permissions for the named Gitea repository. func (c *client) Perm(u *model.User, owner, name string) (*model.Perm, error) { - client := c.newClientToken(u.Token) - repo, err := client.GetRepo(owner, name) + client, err := c.newClientToken(u.Token) + if err != nil { + return nil, err + } + + repo, _, err := client.GetRepo(owner, name) if err != nil { return nil, err } @@ -242,8 +256,12 @@ func (c *client) Perm(u *model.User, owner, name string) (*model.Perm, error) { // File fetches the file from the Gitea repository and returns its contents. func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) { - client := c.newClientToken(u.Token) - cfg, err := client.GetFile(r.Owner, r.Name, b.Commit, f) + client, err := c.newClientToken(u.Token) + if err != nil { + return nil, err + } + + cfg, _, err := client.GetFile(r.Owner, r.Name, b.Commit, f) return cfg, err } @@ -253,12 +271,15 @@ func (c *client) Dir(u *model.User, r *model.Repo, b *model.Build, f string) ([] // Status is supported by the Gitea driver. func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link string, proc *model.Proc) error { - client := c.newClientToken(u.Token) + client, err := c.newClientToken(u.Token) + if err != nil { + return err + } status := getStatus(b.Status) desc := getDesc(b.Status) - _, err := client.CreateStatus( + _, _, err = client.CreateStatus( r.Owner, r.Name, b.Commit, @@ -306,24 +327,31 @@ func (c *client) Activate(u *model.User, r *model.Repo, link string) error { Active: true, } - client := c.newClientToken(u.Token) - _, err := client.CreateRepoHook(r.Owner, r.Name, hook) + client, err := c.newClientToken(u.Token) + if err != nil { + return err + } + _, _, err = client.CreateRepoHook(r.Owner, r.Name, hook) return err } // Deactivate deactives the repository be removing repository push hooks from // the Gitea repository. func (c *client) Deactivate(u *model.User, r *model.Repo, link string) error { - client := c.newClientToken(u.Token) + client, err := c.newClientToken(u.Token) + if err != nil { + return err + } - hooks, err := client.ListRepoHooks(r.Owner, r.Name, gitea.ListHooksOptions{}) + hooks, _, err := client.ListRepoHooks(r.Owner, r.Name, gitea.ListHooksOptions{}) if err != nil { return err } hook := matchingHooks(hooks, link) if hook != nil { - return client.DeleteRepoHook(r.Owner, r.Name, hook.ID) + _, err := client.DeleteRepoHook(r.Owner, r.Name, hook.ID) + return err } return nil @@ -336,21 +364,14 @@ func (c *client) Hook(r *http.Request) (*model.Repo, *model.Build, error) { } // helper function to return the Gitea client -func (c *client) newClient() *gitea.Client { - return c.newClientToken("") -} - -// helper function to return the Gitea client -func (c *client) newClientToken(token string) *gitea.Client { - client := gitea.NewClient(c.URL, token) +func (c *client) newClientToken(token string) (*gitea.Client, error) { + httpClient := &http.Client{} if c.SkipVerify { - httpClient := &http.Client{} httpClient.Transport = &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } - client.SetHTTPClient(httpClient) } - return client + return gitea.NewClient(c.URL, gitea.SetToken(token), gitea.SetHTTPClient(httpClient)) } // helper function to return matching hooks.