Skip to content
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 list pull request labels API #36

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Currently supported providers are: [GitHub](#github), [Bitbucket Server](#bitbuc
- [Get Repository Info](#get-repository-info)
- [Create a Label](#create-a-label)
- [Get a Label](#get-a-label)
- [List Pull Request Labels](#list-pull-request-labels)
- [Unlabel Pull Request](#unlabel-pull-request)
- [Webhook Parser](#webhook-parser)

Expand Down Expand Up @@ -364,6 +365,24 @@ labelName := "label-name"
labelInfo, err := client.GetLabel(ctx, owner, repository, labelName)
```

#### List Pull Request Labels

Notice - Labels are not supported in Bitbucket

```go
// Go context
ctx := context.Background()
// Organization or username
owner := "jfrog"
// VCS repository
repository := "jfrog-cli"
// Pull Request ID
pullRequestID := 5

// List all labels assigned to pull request 5
pullRequestLabels, err := client.ListPullRequestLabels(ctx, owner, repository, pullRequestID)
```

#### Unlabel Pull Request

Notice - Labels are not supported in Bitbucket
Expand Down
5 changes: 5 additions & 0 deletions vcsclient/bitbucketcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ func (client *BitbucketCloudClient) GetLabel(ctx context.Context, owner, reposit
return nil, errLabelsNotSupported
}

// ListPullRequestLabels on Bitbucket cloud
func (client *BitbucketCloudClient) ListPullRequestLabels(ctx context.Context, owner, repository string, pullRequestID int) ([]string, error) {
return nil, errLabelsNotSupported
}

// UnlabelPullRequest on Bitbucket cloud
func (client *BitbucketCloudClient) UnlabelPullRequest(ctx context.Context, owner, repository, name string, pullRequestID int) error {
return errLabelsNotSupported
Expand Down
9 changes: 9 additions & 0 deletions vcsclient/bitbucketcloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ func TestBitbucketCloud_GetLabel(t *testing.T) {
assert.ErrorIs(t, err, errLabelsNotSupported)
}

func TestBitbucketCloud_ListPullRequestLabels(t *testing.T) {
ctx := context.Background()
client, err := NewClientBuilder(vcsutils.BitbucketCloud).Build()
assert.NoError(t, err)

_, err = client.ListPullRequestLabels(ctx, owner, repo1, 1)
assert.ErrorIs(t, err, errLabelsNotSupported)
}

func TestBitbucketCloud_UnlabelPullRequest(t *testing.T) {
ctx := context.Background()
client, err := NewClientBuilder(vcsutils.BitbucketCloud).Build()
Expand Down
5 changes: 5 additions & 0 deletions vcsclient/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ func (client *BitbucketServerClient) GetLabel(ctx context.Context, owner, reposi
return nil, errLabelsNotSupported
}

// ListPullRequestLabels on Bitbucket server
func (client *BitbucketServerClient) ListPullRequestLabels(ctx context.Context, owner, repository string, pullRequestID int) ([]string, error) {
return nil, errLabelsNotSupported
}

// UnlabelPullRequest on Bitbucket server
func (client *BitbucketServerClient) UnlabelPullRequest(ctx context.Context, owner, repository, name string, pullRequestID int) error {
return errLabelsNotSupported
Expand Down
9 changes: 9 additions & 0 deletions vcsclient/bitbucketserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ func TestBitbucketServer_GetLabel(t *testing.T) {
assert.ErrorIs(t, err, errLabelsNotSupported)
}

func TestBitbucketServer_ListPullRequestLabels(t *testing.T) {
ctx := context.Background()
client, err := NewClientBuilder(vcsutils.BitbucketServer).Build()
assert.NoError(t, err)

_, err = client.ListPullRequestLabels(ctx, owner, repo1, 1)
assert.ErrorIs(t, err, errLabelsNotSupported)
}

func TestBitbucketServer_UnlabelPullRequest(t *testing.T) {
ctx := context.Background()
client, err := NewClientBuilder(vcsutils.BitbucketServer).Build()
Expand Down
6 changes: 6 additions & 0 deletions vcsclient/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,9 @@ func getAllProviders() []vcsutils.VcsProvider {
vcsutils.GitHub, vcsutils.GitLab, vcsutils.BitbucketServer, vcsutils.BitbucketCloud,
}
}

func getNonBitbucketProviders() []vcsutils.VcsProvider {
return []vcsutils.VcsProvider{
vcsutils.GitHub, vcsutils.GitLab,
}
}
32 changes: 32 additions & 0 deletions vcsclient/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,40 @@ func (client *GitHubClient) GetLabel(ctx context.Context, owner, repository, nam
}, err
}

// ListPullRequestLabels on GitHub
func (client *GitHubClient) ListPullRequestLabels(ctx context.Context, owner, repository string, pullRequestID int) ([]string, error) {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return nil, err
}
ghClient, err := client.buildGithubClient(ctx)
if err != nil {
return []string{}, err
}

results := []string{}
for nextPage := 0; ; nextPage++ {
options := &github.ListOptions{Page: nextPage}
labels, response, err := ghClient.Issues.ListLabelsByIssue(ctx, owner, repository, pullRequestID, options)
if err != nil {
return []string{}, err
}
for _, label := range labels {
results = append(results, *label.Name)
}
if nextPage+1 >= response.LastPage {
break
}
}
return results, nil
}

// UnlabelPullRequest on GitHub
func (client *GitHubClient) UnlabelPullRequest(ctx context.Context, owner, repository, name string, pullRequestID int) error {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return err
}
ghClient, err := client.buildGithubClient(ctx)
if err != nil {
return err
Expand Down
16 changes: 15 additions & 1 deletion vcsclient/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,23 @@ func TestGitGubClient_GetLabelNotExisted(t *testing.T) {
assert.Nil(t, actualLabel)
}

func TestGitHubClient_ListPullRequestLabels(t *testing.T) {
ctx := context.Background()
client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, []*github.Label{{Name: &labelName}}, "/repos/jfrog/repo-1/issues/1/labels", createGitHubHandler)
defer cleanUp()

labels, err := client.ListPullRequestLabels(ctx, owner, repo1, 1)
assert.NoError(t, err)
assert.Len(t, labels, 1)
assert.Equal(t, labelName, labels[0])

_, err = createBadGitHubClient(t).ListPullRequestLabels(ctx, owner, repo1, 1)
assert.Error(t, err)
}

func TestGitHubClient_UnlabelPullRequest(t *testing.T) {
ctx := context.Background()
client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, github.PullRequest{}, fmt.Sprintf("/repos/jfrog/repo-1/issues/1/labels/%s", url.PathEscape(labelName)), createGitHubHandler)
client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, &github.PullRequest{}, fmt.Sprintf("/repos/jfrog/repo-1/issues/1/labels/%s", url.PathEscape(labelName)), createGitHubHandler)
defer cleanUp()

err := client.UnlabelPullRequest(ctx, owner, repo1, labelName, 1)
Expand Down
21 changes: 20 additions & 1 deletion vcsclient/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,28 @@ func (client *GitLabClient) GetLabel(ctx context.Context, owner, repository, nam
return nil, nil
}

// ListPullRequestLabels on GitLab
func (client *GitLabClient) ListPullRequestLabels(ctx context.Context, owner, repository string, pullRequestID int) ([]string, error) {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return []string{}, err
}
mergeRequest, _, err := client.glClient.MergeRequests.GetMergeRequest(getProjectID(owner, repository), pullRequestID,
&gitlab.GetMergeRequestsOptions{}, gitlab.WithContext(ctx))
if err != nil {
return []string{}, err
}

return mergeRequest.Labels, nil
}

// UnlabelPullRequest on GitLab
func (client *GitLabClient) UnlabelPullRequest(ctx context.Context, owner, repository, name string, pullRequestID int) error {
_, _, err := client.glClient.MergeRequests.UpdateMergeRequest(getProjectID(owner, repository), pullRequestID, &gitlab.UpdateMergeRequestOptions{
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return err
}
_, _, err = client.glClient.MergeRequests.UpdateMergeRequest(getProjectID(owner, repository), pullRequestID, &gitlab.UpdateMergeRequestOptions{
RemoveLabels: gitlab.Labels{name},
}, gitlab.WithContext(ctx))
return err
Expand Down
12 changes: 12 additions & 0 deletions vcsclient/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,18 @@ func TestGitlabClient_GetLabel(t *testing.T) {
assert.Nil(t, labelInfo)
}

func TestGitlabClient_ListPullRequestLabels(t *testing.T) {
ctx := context.Background()
client, cleanUp := createServerAndClient(t, vcsutils.GitLab, false, &gitlab.MergeRequest{Labels: gitlab.Labels{labelName}},
fmt.Sprintf("/api/v4/projects/%s/merge_requests/1", url.PathEscape(owner+"/"+repo1)), createGitLabHandler)
defer cleanUp()

labels, err := client.ListPullRequestLabels(ctx, owner, repo1, 1)
assert.NoError(t, err)
assert.Len(t, labels, 1)
assert.Equal(t, labelName, labels[0])
}

func TestGitlabClient_UnlabelPullRequest(t *testing.T) {
ctx := context.Background()
client, cleanUp := createServerAndClient(t, vcsutils.GitLab, false, nil,
Expand Down
Loading