Merge pull request #35 from laszlocph/bitbucket-fix

Moving to Bitbucket 2.0 repositories endpoint, it returns plain text
This commit is contained in:
Laszlo Fogas 2019-07-09 11:21:26 +02:00 committed by GitHub
commit 0434b255ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 38 deletions

View file

@ -206,7 +206,7 @@ func (c *config) File(u *model.User, r *model.Repo, b *model.Build, f string) ([
if err != nil {
return nil, err
}
return []byte(config.Data), err
return []byte(*config), err
}
func (c *config) Dir(u *model.User, r *model.Repo, b *model.Build, f string) ([]*remote.FileMeta, error) {

View file

@ -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 = `
{

View file

@ -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
}

View file

@ -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"`