Skip to content

Commit

Permalink
Add a function to get the latest commit id
Browse files Browse the repository at this point in the history
  • Loading branch information
ttetzlaff committed Oct 22, 2021
1 parent 40ca96b commit e6a2870
Show file tree
Hide file tree
Showing 15 changed files with 1,947 additions and 110 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Currently supported providers are: [GitHub](#github), [Bitbucket Server](#bitbuc
- [Delete Webhook](#delete-webhook)
- [Set Commit Status](#set-commit-status)
- [Create Pull Request](#create-pull-request)
- [Latest Commit Hash](#get-latest-commit-hash)
- [Webhook Parser](#webhook-parser)

### VCS Clients
Expand Down Expand Up @@ -244,6 +245,22 @@ description := "Pull request description"
err := client.CreatePullRequest(ctx, owner, repository, sourceBranch, targetBranch, title, description string)
```

#### Get Latest Commit Hash

```go
// Go context
ctx := context.Background()
// Organization or username
owner := "jfrog"
// VCS repository
repository := "jfrog-cli"
// VCS branch
branch := "dev"

// SHA-1 hash of the latest commit
commitHash, err := client.GetLatestCommitHash(ctx, owner, repository, branch)
```

### Webhook Parser

```go
Expand Down
112 changes: 84 additions & 28 deletions vcsclient/bitbucketcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (
)

type BitbucketCloudClient struct {
bitbucketClient *bitbucket.Client
username string
token string
vcsInfo *VcsInfo
}

func NewBitbucketCloudClient(vcsInfo *VcsInfo) (*BitbucketCloudClient, error) {
Expand All @@ -25,26 +23,30 @@ func NewBitbucketCloudClient(vcsInfo *VcsInfo) (*BitbucketCloudClient, error) {
return nil, err
}
bitbucketClient := &BitbucketCloudClient{
bitbucketClient: bitbucket.NewBasicAuth(vcsInfo.Username, vcsInfo.Token),
username: vcsInfo.Username,
token: vcsInfo.Token,
vcsInfo: vcsInfo,
}
return bitbucketClient, nil
}

func (client *BitbucketCloudClient) TestConnection(_ context.Context) error {
_, err := client.bitbucketClient.User.Profile()
func (client *BitbucketCloudClient) buildBitbucketCloudClient(_ context.Context) *bitbucket.Client {
return bitbucket.NewBasicAuth(client.vcsInfo.Username, client.vcsInfo.Token)
}

func (client *BitbucketCloudClient) TestConnection(ctx context.Context) error {
bitbucketClient := client.buildBitbucketCloudClient(ctx)
_, err := bitbucketClient.User.Profile()
return err
}

func (client *BitbucketCloudClient) ListRepositories(_ context.Context) (map[string][]string, error) {
func (client *BitbucketCloudClient) ListRepositories(ctx context.Context) (map[string][]string, error) {
bitbucketClient := client.buildBitbucketCloudClient(ctx)
results := make(map[string][]string)
workspaces, err := client.bitbucketClient.Workspaces.List()
workspaces, err := bitbucketClient.Workspaces.List()
if err != nil {
return nil, err
}
for _, workspace := range workspaces.Workspaces {
repositoriesRes, err := client.bitbucketClient.Repositories.ListForAccount(&bitbucket.RepositoriesOptions{Owner: workspace.Slug})
repositoriesRes, err := bitbucketClient.Repositories.ListForAccount(&bitbucket.RepositoriesOptions{Owner: workspace.Slug})
if err != nil {
return nil, err
}
Expand All @@ -55,8 +57,9 @@ func (client *BitbucketCloudClient) ListRepositories(_ context.Context) (map[str
return results, nil
}

func (client *BitbucketCloudClient) ListBranches(_ context.Context, owner, repository string) ([]string, error) {
branches, err := client.bitbucketClient.Repositories.Repository.ListBranches(&bitbucket.RepositoryBranchOptions{Owner: owner, RepoSlug: repository})
func (client *BitbucketCloudClient) ListBranches(ctx context.Context, owner, repository string) ([]string, error) {
bitbucketClient := client.buildBitbucketCloudClient(ctx)
branches, err := bitbucketClient.Repositories.Repository.ListBranches(&bitbucket.RepositoryBranchOptions{Owner: owner, RepoSlug: repository})
if err != nil {
return nil, err
}
Expand All @@ -68,8 +71,9 @@ func (client *BitbucketCloudClient) ListBranches(_ context.Context, owner, repos
return results, nil
}

func (client *BitbucketCloudClient) CreateWebhook(_ context.Context, owner, repository, _, payloadUrl string,
func (client *BitbucketCloudClient) CreateWebhook(ctx context.Context, owner, repository, _, payloadUrl string,
webhookEvents ...vcsutils.WebhookEvent) (string, string, error) {
bitbucketClient := client.buildBitbucketCloudClient(ctx)
token := vcsutils.CreateToken()
options := &bitbucket.WebhooksOptions{
Active: true,
Expand All @@ -78,7 +82,7 @@ func (client *BitbucketCloudClient) CreateWebhook(_ context.Context, owner, repo
Url: payloadUrl + "?token=" + url.QueryEscape(token),
Events: getBitbucketCloudWebhookEvents(webhookEvents...),
}
response, err := client.bitbucketClient.Repositories.Webhooks.Create(options)
response, err := bitbucketClient.Repositories.Webhooks.Create(options)
if err != nil {
return "", "", err
}
Expand All @@ -89,8 +93,9 @@ func (client *BitbucketCloudClient) CreateWebhook(_ context.Context, owner, repo
return id, token, err
}

func (client *BitbucketCloudClient) UpdateWebhook(_ context.Context, owner, repository, _, payloadUrl, token,
func (client *BitbucketCloudClient) UpdateWebhook(ctx context.Context, owner, repository, _, payloadUrl, token,
webhookId string, webhookEvents ...vcsutils.WebhookEvent) error {
bitbucketClient := client.buildBitbucketCloudClient(ctx)
options := &bitbucket.WebhooksOptions{
Active: true,
Uuid: webhookId,
Expand All @@ -99,22 +104,24 @@ func (client *BitbucketCloudClient) UpdateWebhook(_ context.Context, owner, repo
Url: payloadUrl + "?token=" + url.QueryEscape(token),
Events: getBitbucketCloudWebhookEvents(webhookEvents...),
}
_, err := client.bitbucketClient.Repositories.Webhooks.Update(options)
_, err := bitbucketClient.Repositories.Webhooks.Update(options)
return err
}

func (client *BitbucketCloudClient) DeleteWebhook(_ context.Context, owner, repository, webhookId string) error {
func (client *BitbucketCloudClient) DeleteWebhook(ctx context.Context, owner, repository, webhookId string) error {
bitbucketClient := client.buildBitbucketCloudClient(ctx)
options := &bitbucket.WebhooksOptions{
Uuid: webhookId,
Owner: owner,
RepoSlug: repository,
}
_, err := client.bitbucketClient.Repositories.Webhooks.Delete(options)
_, err := bitbucketClient.Repositories.Webhooks.Delete(options)
return err
}

func (client *BitbucketCloudClient) SetCommitStatus(_ context.Context, commitStatus CommitStatus, owner, repository,
func (client *BitbucketCloudClient) SetCommitStatus(ctx context.Context, commitStatus CommitStatus, owner, repository,
ref, title, description, detailsUrl string) error {
bitbucketClient := client.buildBitbucketCloudClient(ctx)
commitOptions := &bitbucket.CommitsOptions{
Owner: owner,
RepoSlug: repository,
Expand All @@ -126,13 +133,14 @@ func (client *BitbucketCloudClient) SetCommitStatus(_ context.Context, commitSta
Description: description,
Url: detailsUrl,
}
_, err := client.bitbucketClient.Repositories.Commits.CreateCommitStatus(commitOptions, commitStatusOptions)
_, err := bitbucketClient.Repositories.Commits.CreateCommitStatus(commitOptions, commitStatusOptions)
return err
}

func (client *BitbucketCloudClient) DownloadRepository(_ context.Context, owner, repository, branch,
func (client *BitbucketCloudClient) DownloadRepository(ctx context.Context, owner, repository, branch,
localPath string) error {
repo, err := client.bitbucketClient.Repositories.Repository.Get(&bitbucket.RepositoryOptions{
bitbucketClient := client.buildBitbucketCloudClient(ctx)
repo, err := bitbucketClient.Repositories.Repository.Get(&bitbucket.RepositoryOptions{
Owner: owner,
RepoSlug: repository,
})
Expand All @@ -149,19 +157,20 @@ func (client *BitbucketCloudClient) DownloadRepository(_ context.Context, owner,
if err != nil {
return err
}
if len(client.username) > 0 || len(client.token) > 0 {
getRequest.SetBasicAuth(client.username, client.token)
if len(client.vcsInfo.Username) > 0 || len(client.vcsInfo.Token) > 0 {
getRequest.SetBasicAuth(client.vcsInfo.Username, client.vcsInfo.Token)
}

response, err := client.bitbucketClient.HttpClient.Do(getRequest)
response, err := bitbucketClient.HttpClient.Do(getRequest)
if err != nil {
return err
}
return vcsutils.Untar(localPath, response.Body, true)
}

func (client *BitbucketCloudClient) CreatePullRequest(_ context.Context, owner, repository, sourceBranch,
func (client *BitbucketCloudClient) CreatePullRequest(ctx context.Context, owner, repository, sourceBranch,
targetBranch, title, description string) error {
bitbucketClient := client.buildBitbucketCloudClient(ctx)
options := &bitbucket.PullRequestsOptions{
Owner: owner,
SourceRepository: owner + "/" + repository,
Expand All @@ -171,10 +180,57 @@ func (client *BitbucketCloudClient) CreatePullRequest(_ context.Context, owner,
Title: title,
Description: description,
}
_, err := client.bitbucketClient.Repositories.PullRequests.Create(options)
_, err := bitbucketClient.Repositories.PullRequests.Create(options)
return err
}

func (client *BitbucketCloudClient) GetLatestCommitHash(ctx context.Context, owner, repository, branch string) (string, error) {
bitbucketClient := client.buildBitbucketCloudClient(ctx)
bitbucketClient.Pagelen = 1
options := &bitbucket.CommitsOptions{
Owner: owner,
RepoSlug: repository,
}
if branch != "" {
options.Branchortag = branch
}
commits, err := bitbucketClient.Repositories.Commits.GetCommits(options)
if err != nil {
return "", err
}
id, err := extractCommitHashFromResponse(commits)
if err != nil {
return "", err
}
return id, nil
}

func extractCommitHashFromResponse(commits interface{}) (string, error) {
var res commitResponse
b, err := json.Marshal(commits)
if err != nil {
return "", err
}
err = json.Unmarshal(b, &res)
if err != nil {
return "", err
}
if len(res.Values) > 0 {
h := res.Values[0]
return h.Hash, nil
}
return "", nil

}

type commitResponse struct {
Values []commitHash `json:"values"`
}

type commitHash struct {
Hash string `json:"hash,omitempty"`
}

// Extract the webhook id from the webhook create response
func getBitbucketCloudWebhookId(r interface{}) (string, error) {
webhook := &bitbucket.WebhooksOptions{}
Expand Down
Loading

0 comments on commit e6a2870

Please sign in to comment.