mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-02-23 06:36:17 +00:00
pull .drone.yml and .drone.sec from bitbucket
This commit is contained in:
parent
75cca2807d
commit
1c87bd9c3b
3 changed files with 68 additions and 19 deletions
|
@ -98,7 +98,7 @@ func (bb *Bitbucket) Login(res http.ResponseWriter, req *http.Request) (*model.U
|
||||||
// of organizations, get the orgs and verify the
|
// of organizations, get the orgs and verify the
|
||||||
// user is a member.
|
// user is a member.
|
||||||
if len(bb.Orgs) != 0 {
|
if len(bb.Orgs) != 0 {
|
||||||
resp, err := client.ListTeams(nil)
|
resp, err := client.ListTeams(&ListOpts{Page: 1, PageLen: 100})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
@ -182,7 +182,7 @@ func (bb *Bitbucket) Repos(u *model.User) ([]*model.RepoLite, error) {
|
||||||
|
|
||||||
// var accounts = []string{u.Login}
|
// var accounts = []string{u.Login}
|
||||||
var repos []*model.RepoLite
|
var repos []*model.RepoLite
|
||||||
var page = 1
|
|
||||||
// for {
|
// for {
|
||||||
// resp, err := client.ListTeams(&ListOpts{Page: page})
|
// resp, err := client.ListTeams(&ListOpts{Page: page})
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
|
@ -198,10 +198,11 @@ func (bb *Bitbucket) Repos(u *model.User) ([]*model.RepoLite, error) {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
page = 1
|
var page = 1
|
||||||
for {
|
for {
|
||||||
resp, err := client.ListRepos(u.Login, &ListOpts{Page: page})
|
resp, err := client.ListRepos(u.Login, &ListOpts{Page: page, PageLen: 100})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
println(err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,9 +210,12 @@ func (bb *Bitbucket) Repos(u *model.User) ([]*model.RepoLite, error) {
|
||||||
repos = append(repos, convertRepoLite(&repo))
|
repos = append(repos, convertRepoLite(&repo))
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Page == resp.Pages {
|
if len(resp.Next) == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
page = resp.Page + 1
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
return repos, nil
|
return repos, nil
|
||||||
|
@ -247,7 +251,28 @@ func (bb *Bitbucket) Perm(u *model.User, owner, name string) (*model.Perm, error
|
||||||
// Script fetches the build script (.drone.yml) from the remote
|
// Script fetches the build script (.drone.yml) from the remote
|
||||||
// repository and returns in string format.
|
// repository and returns in string format.
|
||||||
func (bb *Bitbucket) Script(u *model.User, r *model.Repo, b *model.Build) ([]byte, []byte, error) {
|
func (bb *Bitbucket) Script(u *model.User, r *model.Repo, b *model.Build) ([]byte, []byte, error) {
|
||||||
return nil, nil, nil
|
client := NewClientToken(
|
||||||
|
bb.Client,
|
||||||
|
bb.Secret,
|
||||||
|
&oauth2.Token{
|
||||||
|
AccessToken: u.Token,
|
||||||
|
RefreshToken: u.Secret,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// fetches the .drone.yml for the specified revision. This file
|
||||||
|
// is required, and will error if not found
|
||||||
|
config, err := client.FindSource(r.Owner, r.Name, b.Commit, ".drone.yml")
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetches the .drone.sec for the specified revision. This file
|
||||||
|
// is completely optional, therefore we will not return a not
|
||||||
|
// found error
|
||||||
|
sec, _ := client.FindSource(r.Owner, r.Name, b.Commit, ".drone.sec")
|
||||||
|
|
||||||
|
return []byte(config.Data), []byte(sec.Data), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status sends the commit status to the remote system.
|
// Status sends the commit status to the remote system.
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/bitbucket"
|
"golang.org/x/oauth2/bitbucket"
|
||||||
|
@ -54,10 +55,7 @@ func (c *Client) ListEmail() (*EmailResp, error) {
|
||||||
|
|
||||||
func (c *Client) ListTeams(opts *ListOpts) (*AccountResp, error) {
|
func (c *Client) ListTeams(opts *ListOpts) (*AccountResp, error) {
|
||||||
var out = new(AccountResp)
|
var out = new(AccountResp)
|
||||||
var uri = fmt.Sprintf("%s/2.0/teams/?role=member", api)
|
var uri = fmt.Sprintf("%s/2.0/teams/?role=member&%s", api, encodeListOpts(opts))
|
||||||
if opts != nil && opts.Page > 0 {
|
|
||||||
uri = fmt.Sprintf("%s&page=%d", uri, opts.Page)
|
|
||||||
}
|
|
||||||
var err = c.do(uri, get, nil, out)
|
var err = c.do(uri, get, nil, out)
|
||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
@ -71,10 +69,7 @@ func (c *Client) FindRepo(owner, name string) (*Repo, error) {
|
||||||
|
|
||||||
func (c *Client) ListRepos(account string, opts *ListOpts) (*RepoResp, error) {
|
func (c *Client) ListRepos(account string, opts *ListOpts) (*RepoResp, error) {
|
||||||
var out = new(RepoResp)
|
var out = new(RepoResp)
|
||||||
var uri = fmt.Sprintf("%s/2.0/repositories/%s", api)
|
var uri = fmt.Sprintf("%s/2.0/repositories/%s?%s", api, account, encodeListOpts(opts))
|
||||||
if opts != nil && opts.Page > 0 {
|
|
||||||
uri = fmt.Sprintf("%s?page=%d", uri, opts.Page)
|
|
||||||
}
|
|
||||||
var err = c.do(uri, get, nil, out)
|
var err = c.do(uri, get, nil, out)
|
||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
@ -88,10 +83,7 @@ func (c *Client) FindHook(owner, name, id string) (*Hook, error) {
|
||||||
|
|
||||||
func (c *Client) ListHooks(owner, name string, opts *ListOpts) (*HookResp, error) {
|
func (c *Client) ListHooks(owner, name string, opts *ListOpts) (*HookResp, error) {
|
||||||
var out = new(HookResp)
|
var out = new(HookResp)
|
||||||
var uri = fmt.Sprintf("%s/2.0/repositories/%s/%s/hooks", api, owner, name)
|
var uri = fmt.Sprintf("%s/2.0/repositories/%s/%s/hooks?%s", api, owner, name, encodeListOpts(opts))
|
||||||
if opts != nil && opts.Page > 0 {
|
|
||||||
uri = fmt.Sprintf("%s?page=%d", uri, opts.Page)
|
|
||||||
}
|
|
||||||
var err = c.do(uri, get, nil, out)
|
var err = c.do(uri, get, nil, out)
|
||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
@ -106,13 +98,20 @@ func (c *Client) DeleteHook(owner, name, id string) error {
|
||||||
return c.do(uri, del, nil, nil)
|
return c.do(uri, del, nil, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) FindSource(owner, name, revision, path string) (*Source, error) {
|
||||||
|
var out = new(Source)
|
||||||
|
var uri = fmt.Sprintf("%s/1.0/repositories/%s/%s/src/%s/%s", api, owner, name, revision, path)
|
||||||
|
var err = c.do(uri, get, nil, out)
|
||||||
|
return out, err
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) do(rawurl, method string, in, out interface{}) error {
|
func (c *Client) do(rawurl, method string, in, out interface{}) error {
|
||||||
|
|
||||||
uri, err := url.Parse(rawurl)
|
uri, err := url.Parse(rawurl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
println(uri.String())
|
|
||||||
// if we are posting or putting data, we need to
|
// if we are posting or putting data, we need to
|
||||||
// write it to the body of the request.
|
// write it to the body of the request.
|
||||||
var buf io.ReadWriter
|
var buf io.ReadWriter
|
||||||
|
@ -153,3 +152,17 @@ func (c *Client) do(rawurl, method string, in, out interface{}) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func encodeListOpts(opts *ListOpts) string {
|
||||||
|
var params = new(url.Values)
|
||||||
|
if opts == nil {
|
||||||
|
return params.Encode()
|
||||||
|
}
|
||||||
|
if opts.Page != 0 {
|
||||||
|
params.Set("page", strconv.Itoa(opts.Page))
|
||||||
|
}
|
||||||
|
if opts.PageLen != 0 {
|
||||||
|
params.Set("pagelen", strconv.Itoa(opts.PageLen))
|
||||||
|
}
|
||||||
|
return params.Encode()
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ type AccountResp struct {
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
Pages int `json:"pagelen"`
|
Pages int `json:"pagelen"`
|
||||||
Size int `json:"size"`
|
Size int `json:"size"`
|
||||||
|
Next string `json:"next"`
|
||||||
Values []Account `json:"values"`
|
Values []Account `json:"values"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +25,7 @@ type EmailResp struct {
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
Pages int `json:"pagelen"`
|
Pages int `json:"pagelen"`
|
||||||
Size int `json:"size"`
|
Size int `json:"size"`
|
||||||
|
Next string `json:"next"`
|
||||||
Values []Email `json:"values"`
|
Values []Email `json:"values"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +41,7 @@ type HookResp struct {
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
Pages int `json:"pagelen"`
|
Pages int `json:"pagelen"`
|
||||||
Size int `json:"size"`
|
Size int `json:"size"`
|
||||||
|
Next string `json:"next"`
|
||||||
Values []Hook `json:"values"`
|
Values []Hook `json:"values"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,9 +75,17 @@ type RepoResp struct {
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
Pages int `json:"pagelen"`
|
Pages int `json:"pagelen"`
|
||||||
Size int `json:"size"`
|
Size int `json:"size"`
|
||||||
|
Next string `json:"next"`
|
||||||
Values []Repo `json:"values"`
|
Values []Repo `json:"values"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Source struct {
|
||||||
|
Node string `json:"node"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Data string `json:"data"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
}
|
||||||
|
|
||||||
type ListOpts struct {
|
type ListOpts struct {
|
||||||
Page int
|
Page int
|
||||||
PageLen int
|
PageLen int
|
||||||
|
|
Loading…
Reference in a new issue