Add pull-request implementation for bitbucket forge (#1889)

This commit is contained in:
Michalis Zampetakis 2023-06-28 00:40:28 +03:00 committed by GitHub
parent 3033abc3b4
commit 456725dde7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 14 deletions

View file

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

View file

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

View file

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