mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-23 17:00:30 +00:00
Update Gitea SDK to v0.13.0 and refactor
This commit is contained in:
parent
d7a61cebf4
commit
02c957bcdd
3 changed files with 70 additions and 49 deletions
2
go.mod
2
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.0
|
||||
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
|
||||
|
|
4
go.sum
4
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.0 h1:iHognp8ZMhMFLooUUNZFpm8IHaC9qoHJDvAE5vTm5aw=
|
||||
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/go.mod h1:7tiAn5a0LFmjbPDbyTPOaTTOuG1ZRNXdPA6RvKY+fpY=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue