diff --git a/server/forge/bitbucket/bitbucket.go b/server/forge/bitbucket/bitbucket.go index 62a76cb1e..8f7dacf43 100644 --- a/server/forge/bitbucket/bitbucket.go +++ b/server/forge/bitbucket/bitbucket.go @@ -302,8 +302,21 @@ func (c *config) BranchHead(_ context.Context, _ *model.User, _ *model.Repo, _ s return "", forge_types.ErrNotImplemented } -func (c *config) PullRequests(_ context.Context, _ *model.User, _ *model.Repo, _ *model.ListOptions) ([]*model.PullRequest, error) { - return nil, forge_types.ErrNotImplemented +// PullRequests returns the pull requests of the named repository. +func (c *config) PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]*model.PullRequest, error) { + opts := internal.ListOpts{Page: p.Page, PageLen: p.Page} + pullRequests, err := c.newClient(ctx, u).ListPullRequests(r.Owner, r.Name, &opts) + if err != nil { + return nil, err + } + result := []*model.PullRequest{} + for _, pullRequest := range pullRequests { + result = append(result, &model.PullRequest{ + Index: int64(pullRequest.ID), + Title: pullRequest.Title, + }) + } + return result, nil } // Hook parses the incoming Bitbucket hook and returns the Repository and diff --git a/server/forge/bitbucket/internal/client.go b/server/forge/bitbucket/internal/client.go index 05df7ce9d..aca051805 100644 --- a/server/forge/bitbucket/internal/client.go +++ b/server/forge/bitbucket/internal/client.go @@ -34,18 +34,19 @@ const ( ) const ( - pathUser = "%s/2.0/user/" - pathEmails = "%s/2.0/user/emails" - pathPermissions = "%s/2.0/user/permissions/repositories?q=repository.full_name=%q" - pathWorkspace = "%s/2.0/workspaces/?%s" - pathRepo = "%s/2.0/repositories/%s/%s" - 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/2.0/repositories/%s/%s/src/%s/%s" - pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build" - pathBranches = "%s/2.0/repositories/%s/%s/refs/branches" - pathOrgPerms = "%s/2.0/workspaces/%s/permissions?%s" + pathUser = "%s/2.0/user/" + pathEmails = "%s/2.0/user/emails" + pathPermissions = "%s/2.0/user/permissions/repositories?q=repository.full_name=%q" + pathWorkspace = "%s/2.0/workspaces/?%s" + pathRepo = "%s/2.0/repositories/%s/%s" + 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/2.0/repositories/%s/%s/src/%s/%s" + pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build" + pathBranches = "%s/2.0/repositories/%s/%s/refs/branches" + pathOrgPerms = "%s/2.0/workspaces/%s/permissions?%s" + pathPullRequests = "%s/2.0/repositories/%s/%s/pullrequests" ) type Client struct { @@ -204,6 +205,13 @@ func (c *Client) GetUserWorkspaceMembership(workspace, user string) (string, err return "", nil } +func (c *Client) ListPullRequests(owner, name string, opts *ListOpts) ([]*PullRequest, error) { + out := new(PullRequestResp) + uri := fmt.Sprintf(pathPullRequests, c.base, owner, name) + _, err := c.do(uri, get, opts.Encode(), out) + return out.Values, err +} + func (c *Client) do(rawurl, method string, in, out interface{}) (*string, error) { uri, err := url.Parse(rawurl) if err != nil { diff --git a/server/forge/bitbucket/internal/types.go b/server/forge/bitbucket/internal/types.go index b3e6cc4fa..4c4889d7a 100644 --- a/server/forge/bitbucket/internal/types.go +++ b/server/forge/bitbucket/internal/types.go @@ -259,3 +259,15 @@ type BranchResp struct { type Branch struct { Name string `json:"name"` } + +type PullRequestResp struct { + Page uint `json:"page"` + PageLen uint `json:"pagelen"` + Size uint `json:"size"` + Values []*PullRequest `json:"values"` +} + +type PullRequest struct { + ID uint `json:"id"` + Title string `json:"title"` +}