From 5869b62d3d45e0ae2b48f3ca774845bc27cf009a Mon Sep 17 00:00:00 2001 From: Laszlo Fogas Date: Thu, 13 Jun 2019 16:33:51 +0200 Subject: [PATCH] Moving to 2.0 repositories endpoint, it returns plain text --- remote/bitbucket/bitbucket.go | 2 +- remote/bitbucket/fixtures/handler.go | 8 +--- remote/bitbucket/internal/client.go | 56 ++++++++++++++++------------ remote/bitbucket/internal/types.go | 7 ---- 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/remote/bitbucket/bitbucket.go b/remote/bitbucket/bitbucket.go index ebef2f011..9d4f3f793 100644 --- a/remote/bitbucket/bitbucket.go +++ b/remote/bitbucket/bitbucket.go @@ -211,7 +211,7 @@ func (c *config) FileRef(u *model.User, r *model.Repo, ref, f string) ([]byte, e if err != nil { return nil, err } - return []byte(config.Data), err + return []byte(*config), err } // Status creates a build status for the Bitbucket commit. diff --git a/remote/bitbucket/fixtures/handler.go b/remote/bitbucket/fixtures/handler.go index f0565995b..9d4fde58e 100644 --- a/remote/bitbucket/fixtures/handler.go +++ b/remote/bitbucket/fixtures/handler.go @@ -30,7 +30,7 @@ func Handler() http.Handler { e.POST("/site/oauth2/access_token", getOauth) e.GET("/2.0/repositories/:owner/:name", getRepo) e.GET("/2.0/repositories/:owner/:name/hooks", getRepoHooks) - e.GET("/1.0/repositories/:owner/:name/src/:commit/:file", getRepoFile) + e.GET("/2.0/repositories/:owner/:name/src/:commit/:file", getRepoFile) e.DELETE("/2.0/repositories/:owner/:name/hooks/:hook", deleteRepoHook) e.POST("/2.0/repositories/:owner/:name/hooks", createRepoHook) e.POST("/2.0/repositories/:owner/:name/commit/:commit/statuses/build", createRepoStatus) @@ -214,11 +214,7 @@ const repoHookPayload = ` } ` -const repoFilePayload = ` -{ - "data": "{ platform: linux/amd64 }" -} -` +const repoFilePayload = "dummy payload" const userPayload = ` { diff --git a/remote/bitbucket/internal/client.go b/remote/bitbucket/internal/client.go index 71ea12a7a..59cf916e1 100644 --- a/remote/bitbucket/internal/client.go +++ b/remote/bitbucket/internal/client.go @@ -19,6 +19,7 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" "net/http" "net/url" @@ -42,7 +43,7 @@ const ( pathRepos = "%s/2.0/repositories/%s?%s" pathHook = "%s/2.0/repositories/%s/%s/hooks/%s" pathHooks = "%s/2.0/repositories/%s/%s/hooks?%s" - pathSource = "%s/1.0/repositories/%s/%s/src/%s/%s" + pathSource = "%s/2.0/repositories/%s/%s/src/%s/%s" pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build" ) @@ -67,35 +68,35 @@ func NewClientToken(url, client, secret string, token *oauth2.Token) *Client { func (c *Client) FindCurrent() (*Account, error) { out := new(Account) uri := fmt.Sprintf(pathUser, c.base) - err := c.do(uri, get, nil, out) + _, err := c.do(uri, get, nil, out) return out, err } func (c *Client) ListEmail() (*EmailResp, error) { out := new(EmailResp) uri := fmt.Sprintf(pathEmails, c.base) - err := c.do(uri, get, nil, out) + _, err := c.do(uri, get, nil, out) return out, err } func (c *Client) ListTeams(opts *ListTeamOpts) (*AccountResp, error) { out := new(AccountResp) uri := fmt.Sprintf(pathTeams, c.base, opts.Encode()) - err := c.do(uri, get, nil, out) + _, err := c.do(uri, get, nil, out) return out, err } func (c *Client) FindRepo(owner, name string) (*Repo, error) { out := new(Repo) uri := fmt.Sprintf(pathRepo, c.base, owner, name) - err := c.do(uri, get, nil, out) + _, err := c.do(uri, get, nil, out) return out, err } func (c *Client) ListRepos(account string, opts *ListOpts) (*RepoResp, error) { out := new(RepoResp) uri := fmt.Sprintf(pathRepos, c.base, account, opts.Encode()) - err := c.do(uri, get, nil, out) + _, err := c.do(uri, get, nil, out) return out, err } @@ -120,43 +121,44 @@ func (c *Client) ListReposAll(account string) ([]*Repo, error) { func (c *Client) FindHook(owner, name, id string) (*Hook, error) { out := new(Hook) uri := fmt.Sprintf(pathHook, c.base, owner, name, id) - err := c.do(uri, get, nil, out) + _, err := c.do(uri, get, nil, out) return out, err } func (c *Client) ListHooks(owner, name string, opts *ListOpts) (*HookResp, error) { out := new(HookResp) uri := fmt.Sprintf(pathHooks, c.base, owner, name, opts.Encode()) - err := c.do(uri, get, nil, out) + _, err := c.do(uri, get, nil, out) return out, err } func (c *Client) CreateHook(owner, name string, hook *Hook) error { uri := fmt.Sprintf(pathHooks, c.base, owner, name, "") - return c.do(uri, post, hook, nil) + _, err := c.do(uri, post, hook, nil) + return err } func (c *Client) DeleteHook(owner, name, id string) error { uri := fmt.Sprintf(pathHook, c.base, owner, name, id) - return c.do(uri, del, nil, nil) + _, err := c.do(uri, del, nil, nil) + return err } -func (c *Client) FindSource(owner, name, revision, path string) (*Source, error) { - out := new(Source) +func (c *Client) FindSource(owner, name, revision, path string) (*string, error) { uri := fmt.Sprintf(pathSource, c.base, owner, name, revision, path) - err := c.do(uri, get, nil, out) - return out, err + return c.do(uri, get, nil, nil) } func (c *Client) CreateStatus(owner, name, revision string, status *BuildStatus) error { uri := fmt.Sprintf(pathStatus, c.base, owner, name, revision) - return c.do(uri, post, status, nil) + _, err := c.do(uri, post, status, nil) + return err } func (c *Client) GetPermission(fullName string) (*RepoPerm, error) { out := new(RepoPermResp) uri := fmt.Sprintf(pathPermissions, c.base, fullName) - err := c.do(uri, get, nil, out) + _, err := c.do(uri, get, nil, out) if err != nil { return nil, err @@ -169,11 +171,11 @@ func (c *Client) GetPermission(fullName string) (*RepoPerm, error) { } } -func (c *Client) do(rawurl, method string, in, out interface{}) error { +func (c *Client) do(rawurl, method string, in, out interface{}) (*string, error) { uri, err := url.Parse(rawurl) if err != nil { - return err + return nil, err } // if we are posting or putting data, we need to @@ -183,14 +185,14 @@ func (c *Client) do(rawurl, method string, in, out interface{}) error { buf = new(bytes.Buffer) err := json.NewEncoder(buf).Encode(in) if err != nil { - return err + return nil, err } } // creates a new http request to bitbucket. req, err := http.NewRequest(method, uri.String(), buf) if err != nil { - return err + return nil, err } if in != nil { req.Header.Set("Content-Type", "application/json") @@ -198,7 +200,7 @@ func (c *Client) do(rawurl, method string, in, out interface{}) error { resp, err := c.Do(req) if err != nil { - return err + return nil, err } defer resp.Body.Close() @@ -208,14 +210,20 @@ func (c *Client) do(rawurl, method string, in, out interface{}) error { err := Error{} json.NewDecoder(resp.Body).Decode(&err) err.Status = resp.StatusCode - return err + return nil, err } // if a json response is expected, parse and return // the json response. if out != nil { - return json.NewDecoder(resp.Body).Decode(out) + return nil, json.NewDecoder(resp.Body).Decode(out) } - return nil + bodyBytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + bodyString := string(bodyBytes) + + return &bodyString, nil } diff --git a/remote/bitbucket/internal/types.go b/remote/bitbucket/internal/types.go index 652b037cc..55e4105de 100644 --- a/remote/bitbucket/internal/types.go +++ b/remote/bitbucket/internal/types.go @@ -107,13 +107,6 @@ type RepoResp struct { Values []*Repo `json:"values"` } -type Source struct { - Node string `json:"node"` - Path string `json:"path"` - Data string `json:"data"` - Size int64 `json:"size"` -} - type Change struct { New struct { Type string `json:"type"`