Update Gitea SDK to v0.13.0 and refactor

This commit is contained in:
6543 2020-09-16 08:48:09 +02:00
parent d7a61cebf4
commit 02c957bcdd
No known key found for this signature in database
GPG key ID: A1CA74D27FD13271
3 changed files with 70 additions and 49 deletions

2
go.mod
View file

@ -3,7 +3,7 @@ module github.com/laszlocph/woodpecker
go 1.12 go 1.12
require ( require (
code.gitea.io/sdk/gitea v0.12.0 code.gitea.io/sdk/gitea v0.13.0
docker.io/go-docker v1.0.0 docker.io/go-docker v1.0.0
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5 // indirect github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5 // indirect

4
go.sum
View file

@ -1,7 +1,7 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 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= 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.13.0 h1:iHognp8ZMhMFLooUUNZFpm8IHaC9qoHJDvAE5vTm5aw=
code.gitea.io/sdk/gitea v0.12.0/go.mod h1:z3uwDV/b9Ls47NGukYM9XhnHtqPh/J+t40lsUrR6JDY= code.gitea.io/sdk/gitea v0.13.0/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 h1:VdXS/aNYQxyA9wdLD5z8Q8Ro688/hG8HzKxYVEVbE6s=
docker.io/go-docker v1.0.0/go.mod h1:7tiAn5a0LFmjbPDbyTPOaTTOuG1ZRNXdPA6RvKY+fpY= 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= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=

View file

@ -136,33 +136,31 @@ func (c *client) Login(res http.ResponseWriter, req *http.Request) (*model.User,
return nil, nil return nil, nil
} }
client := c.newClient() client, err := c.newClientToken("")
if err != nil {
// 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 return nil, err
} }
break
} // 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( token, _, terr := client.CreateAccessToken(
gitea.CreateAccessTokenOption{Name: "drone"}, gitea.CreateAccessTokenOption{Name: "drone"},
) )
if terr != nil { if terr != nil {
return nil, terr return nil, terr
} }
accessToken = token.Token accessToken := token.Token
client = c.newClientToken(accessToken) client, err = c.newClientToken(accessToken)
account, err := client.GetUserInfo(username) if err != nil {
return nil, err
}
account, _, err := client.GetUserInfo(username)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -182,8 +180,12 @@ func (c *client) Auth(token, secret string) (string, error) {
// Teams is supported by the Gitea driver. // Teams is supported by the Gitea driver.
func (c *client) Teams(u *model.User) ([]*model.Team, error) { func (c *client) Teams(u *model.User) ([]*model.Team, error) {
client := c.newClientToken(u.Token) client, err := c.newClientToken(u.Token)
orgs, err := client.ListMyOrgs(gitea.ListOrgsOptions{}) if err != nil {
return nil, err
}
orgs, _, err := client.ListMyOrgs(gitea.ListOrgsOptions{})
if err != nil { if err != nil {
return nil, err 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. // Repo returns the named Gitea repository.
func (c *client) Repo(u *model.User, owner, name string) (*model.Repo, error) { func (c *client) Repo(u *model.User, owner, name string) (*model.Repo, error) {
client := c.newClientToken(u.Token) client, err := c.newClientToken(u.Token)
repo, err := client.GetRepo(owner, name) if err != nil {
return nil, err
}
repo, _, err := client.GetRepo(owner, name)
if err != nil { if err != nil {
return nil, err 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) { func (c *client) Repos(u *model.User) ([]*model.Repo, error) {
repos := []*model.Repo{} repos := []*model.Repo{}
client := c.newClientToken(u.Token) client, err := c.newClientToken(u.Token)
all, err := client.ListMyRepos(gitea.ListReposOptions{}) if err != nil {
return nil, err
}
all, _, err := client.ListMyRepos(gitea.ListReposOptions{})
if err != nil { if err != nil {
return repos, err 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. // Perm returns the user permissions for the named Gitea repository.
func (c *client) Perm(u *model.User, owner, name string) (*model.Perm, error) { func (c *client) Perm(u *model.User, owner, name string) (*model.Perm, error) {
client := c.newClientToken(u.Token) client, err := c.newClientToken(u.Token)
repo, err := client.GetRepo(owner, name) if err != nil {
return nil, err
}
repo, _, err := client.GetRepo(owner, name)
if err != nil { if err != nil {
return nil, err 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. // 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) { func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) {
client := c.newClientToken(u.Token) client, err := c.newClientToken(u.Token)
cfg, err := client.GetFile(r.Owner, r.Name, b.Commit, f) if err != nil {
return nil, err
}
cfg, _, err := client.GetFile(r.Owner, r.Name, b.Commit, f)
return cfg, err 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. // 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 { 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) status := getStatus(b.Status)
desc := getDesc(b.Status) desc := getDesc(b.Status)
_, err := client.CreateStatus( _, _, err = client.CreateStatus(
r.Owner, r.Owner,
r.Name, r.Name,
b.Commit, b.Commit,
@ -306,24 +327,31 @@ func (c *client) Activate(u *model.User, r *model.Repo, link string) error {
Active: true, Active: true,
} }
client := c.newClientToken(u.Token) client, err := c.newClientToken(u.Token)
_, err := client.CreateRepoHook(r.Owner, r.Name, hook) if err != nil {
return err
}
_, _, err = client.CreateRepoHook(r.Owner, r.Name, hook)
return err return err
} }
// Deactivate deactives the repository be removing repository push hooks from // Deactivate deactives the repository be removing repository push hooks from
// the Gitea repository. // the Gitea repository.
func (c *client) Deactivate(u *model.User, r *model.Repo, link string) error { 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 { if err != nil {
return err return err
} }
hook := matchingHooks(hooks, link) hook := matchingHooks(hooks, link)
if hook != nil { 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 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 // helper function to return the Gitea client
func (c *client) newClient() *gitea.Client { func (c *client) newClientToken(token string) (*gitea.Client, error) {
return c.newClientToken("")
}
// helper function to return the Gitea client
func (c *client) newClientToken(token string) *gitea.Client {
client := gitea.NewClient(c.URL, token)
if c.SkipVerify {
httpClient := &http.Client{} httpClient := &http.Client{}
if c.SkipVerify {
httpClient.Transport = &http.Transport{ httpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, 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. // helper function to return matching hooks.