Add bitbucket forge BranchHead implementation (#2011)

In order to test this functionality, we had to expose the `BranchHead()`
through an API endpoint
```
GET /repos/{repo_id}/branches/{branch}/head
```

The response is a string that contains the latest commit hash of the
requested branch.
This commit is contained in:
Michalis Zampetakis 2023-07-17 22:30:06 +03:00 committed by GitHub
parent c805c87e90
commit dcba48f916
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 16 deletions

View file

@ -297,9 +297,8 @@ func (c *config) Branches(ctx context.Context, u *model.User, r *model.Repo, _ *
} }
// BranchHead returns the sha of the head (latest commit) of the specified branch // BranchHead returns the sha of the head (latest commit) of the specified branch
func (c *config) BranchHead(_ context.Context, _ *model.User, _ *model.Repo, _ string) (string, error) { func (c *config) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
// TODO(1138): missing implementation return c.newClient(ctx, u).GetBranchHead(r.Owner, r.Name, branch)
return "", forge_types.ErrNotImplemented
} }
// PullRequests returns the pull requests of the named repository. // PullRequests returns the pull requests of the named repository.

View file

@ -34,19 +34,20 @@ const (
) )
const ( const (
pathUser = "%s/2.0/user/" pathUser = "%s/2.0/user/"
pathEmails = "%s/2.0/user/emails" pathEmails = "%s/2.0/user/emails"
pathPermissions = "%s/2.0/user/permissions/repositories?q=repository.full_name=%q" pathPermissions = "%s/2.0/user/permissions/repositories?q=repository.full_name=%q"
pathWorkspace = "%s/2.0/workspaces/?%s" pathWorkspace = "%s/2.0/workspaces/?%s"
pathRepo = "%s/2.0/repositories/%s/%s" pathRepo = "%s/2.0/repositories/%s/%s"
pathRepos = "%s/2.0/repositories/%s?%s" pathRepos = "%s/2.0/repositories/%s?%s"
pathHook = "%s/2.0/repositories/%s/%s/hooks/%s" pathHook = "%s/2.0/repositories/%s/%s/hooks/%s"
pathHooks = "%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" pathSource = "%s/2.0/repositories/%s/%s/src/%s/%s"
pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build" pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build"
pathBranches = "%s/2.0/repositories/%s/%s/refs/branches" pathBranches = "%s/2.0/repositories/%s/%s/refs/branches"
pathOrgPerms = "%s/2.0/workspaces/%s/permissions?%s" pathOrgPerms = "%s/2.0/workspaces/%s/permissions?%s"
pathPullRequests = "%s/2.0/repositories/%s/%s/pullrequests" pathPullRequests = "%s/2.0/repositories/%s/%s/pullrequests"
pathBranchCommits = "%s/2.0/repositories/%s/%s/commits/%s"
) )
type Client struct { type Client struct {
@ -183,6 +184,19 @@ func (c *Client) ListBranches(owner, name string) ([]*Branch, error) {
return out.Values, err return out.Values, err
} }
func (c *Client) GetBranchHead(owner, name, branch string) (string, error) {
out := new(CommitsResp)
uri := fmt.Sprintf(pathBranchCommits, c.base, owner, name, branch)
_, err := c.do(uri, get, nil, out)
if err != nil {
return "", err
}
if len(out.Values) == 0 {
return "", fmt.Errorf("no commits in branch %s", branch)
}
return out.Values[0].Hash, nil
}
func (c *Client) GetUserWorkspaceMembership(workspace, user string) (string, error) { func (c *Client) GetUserWorkspaceMembership(workspace, user string) (string, error) {
out := new(WorkspaceMembershipResp) out := new(WorkspaceMembershipResp)
opts := &ListOpts{Page: 1, PageLen: 100} opts := &ListOpts{Page: 1, PageLen: 100}

View file

@ -271,3 +271,11 @@ type PullRequest struct {
ID uint `json:"id"` ID uint `json:"id"`
Title string `json:"title"` Title string `json:"title"`
} }
type CommitsResp struct {
Values []*Commit `json:"values"`
}
type Commit struct {
Hash string `json:"hash"`
}