-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add get commit status #75
Changes from 18 commits
331c083
62324e3
8c3cf38
5865687
9535a53
9746144
1b00d2d
dd6c0e0
b56fdd6
76f2453
fc8f240
860fcbb
fbcc34f
6bdd81b
bdbf594
6a91300
aaf86dc
a58b9ea
8c47740
8d685f2
80110c5
495ecd3
9c2e81b
390acdc
edad2af
095fcd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -352,10 +352,12 @@ func TestAzureReposClient_DeleteWebhook(t *testing.T) { | |
|
||
func TestAzureReposClient_SetCommitStatus(t *testing.T) { | ||
ctx := context.Background() | ||
client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, "", "unsupportedTest", createAzureReposHandler) | ||
commitHash := "86d6919952702f9ab03bc95b45687f145a663de0" | ||
expectedUri := "/_apis/ResourceAreas/commitStatus" | ||
client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, nil, expectedUri, createAzureReposHandler) | ||
defer cleanUp() | ||
err := client.SetCommitStatus(ctx, 1, owner, repo1, "", "", "", "") | ||
assert.Error(t, err) | ||
err := client.SetCommitStatus(ctx, 1, owner, repo1, commitHash, "", "", "") | ||
assert.NoError(t, err) | ||
yahavi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func TestAzureReposClient_GetLabel(t *testing.T) { | ||
|
@@ -484,3 +486,30 @@ func createBadAzureReposClient(t *testing.T, response []byte) (VcsClient, func() | |
createAzureReposHandler) | ||
return client, cleanUp | ||
} | ||
|
||
func TestAzureReposClient_GetCommitStatus(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's put this method inline with the other tests (above the private methods) |
||
ctx := context.Background() | ||
|
||
t.Run("full response", func(t *testing.T) { | ||
commitHash := "86d6919952702f9ab03bc95b45687f145a663de0" | ||
expectedUri := "/_apis/ResourceAreas/commitStatus" | ||
response, err := os.ReadFile(filepath.Join("testdata", "azurerepos", "commits_statuses.json")) | ||
assert.NoError(t, err) | ||
client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, response, expectedUri, createAzureReposHandler) | ||
defer cleanUp() | ||
commitStatuses, err := client.GetCommitStatus(ctx, owner, repo1, commitHash) | ||
assert.NoError(t, err) | ||
assert.True(t, len(commitStatuses) == 3) | ||
assert.True(t, commitStatuses[0].State == CommitStatusStateSuccess) | ||
assert.True(t, commitStatuses[1].State == CommitStatusStatePending) | ||
assert.True(t, commitStatuses[2].State == CommitStatusStateFailure) | ||
}) | ||
t.Run("empty response", func(t *testing.T) { | ||
commitHash := "86d6919952702f9ab03bc95b45687f145a663de0" | ||
expectedUri := "/_apis/ResourceAreas/commitStatus" | ||
client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, nil, expectedUri, createAzureReposHandler) | ||
defer cleanUp() | ||
_, err := client.GetCommitStatus(ctx, owner, repo1, commitHash) | ||
assert.NoError(t, err) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a negative test: t.Run("erroneous client", func(t *testing.T) {
badClient, badClientCleanup := createBadAzureReposClient(t, []byte{})
defer badClientCleanup()
_, err := badClient.GetCommitStatus(ctx, owner, repo1, "")
assert.Error(t, err)
}) |
||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,6 +25,43 @@ type BitbucketCloudClient struct { | |||||
logger Log | ||||||
} | ||||||
|
||||||
// GetCommitStatus for a specific branch, NOTE: currently does not support pagination ! | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not only a branch but also a tag, commit hash, etc...
Suggested change
About the pagination comment - it looks like it is not supported also on GitHub, GitLab, and Bitbucket cloud (maybe in Azure too). Can we add this comment in the vcsclient.go and in the README? |
||||||
func (client *BitbucketCloudClient) GetCommitStatus(ctx context.Context, owner, repository, ref string) (status []CommitStatus, err error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's put this method below SetCommitStatus |
||||||
bitbucketClient := client.buildBitbucketCloudClient(ctx) | ||||||
commitOptions := &bitbucket.CommitsOptions{ | ||||||
Owner: owner, | ||||||
RepoSlug: repository, | ||||||
Revision: ref, | ||||||
} | ||||||
results := make([]CommitStatus, 0) | ||||||
rawStatuses, err := bitbucketClient.Repositories.Commits.GetCommitStatuses(commitOptions) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
statuses := struct { | ||||||
Statuses []struct { | ||||||
Title string `mapstructure:"key"` | ||||||
Url string `mapstructure:"url"` | ||||||
State string `mapstructure:"state"` | ||||||
Description string `mapstructure:"description"` | ||||||
Creator string `mapstructure:"name"` | ||||||
} `mapstructure:"values"` | ||||||
}{} | ||||||
err = mapstructure.Decode(rawStatuses, &statuses) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional, you like - if err = mapstructure.Decode(rawStatuses, &statuses); err != nil {
return nil, err
} |
||||||
for _, commitStatus := range statuses.Statuses { | ||||||
results = append(results, CommitStatus{ | ||||||
State: CommitStatusAsStringToStatus(commitStatus.State), | ||||||
Description: commitStatus.Description, | ||||||
DetailsUrl: commitStatus.Url, | ||||||
Creator: commitStatus.Creator, | ||||||
}) | ||||||
} | ||||||
return results, err | ||||||
} | ||||||
|
||||||
// NewBitbucketCloudClient create a new BitbucketCloudClient | ||||||
func NewBitbucketCloudClient(vcsInfo VcsInfo, logger Log) (*BitbucketCloudClient, error) { | ||||||
bitbucketClient := &BitbucketCloudClient{ | ||||||
|
@@ -197,7 +234,7 @@ func (client *BitbucketCloudClient) DeleteWebhook(ctx context.Context, owner, re | |||||
} | ||||||
|
||||||
// SetCommitStatus on Bitbucket cloud | ||||||
func (client *BitbucketCloudClient) SetCommitStatus(ctx context.Context, commitStatus CommitStatus, owner, repository, | ||||||
func (client *BitbucketCloudClient) SetCommitStatus(ctx context.Context, commitStatus CommitStatusState, owner, repository, | ||||||
ref, title, description, detailsURL string) error { | ||||||
bitbucketClient := client.buildBitbucketCloudClient(ctx) | ||||||
commitOptions := &bitbucket.CommitsOptions{ | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put this method below SetCommitStatus