Skip to content

Commit

Permalink
Code Review Comments API (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi authored Sep 12, 2023
1 parent b0a9d5c commit 5004344
Show file tree
Hide file tree
Showing 20 changed files with 1,189 additions and 41 deletions.
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ Currently supported providers are: [GitHub](#github), [Bitbucket Server](#bitbuc
- [List Open Pull Requests](#list-open-pull-requests)
- [List Open Pull Requests With Body](#list-open-pull-requests-with-body)
- [Add Pull Request Comment](#add-pull-request-comment)
- [Add Pull Request Review Comments](#add-pull-request-review-comments)
- [List Pull Request Comments](#list-pull-request-comments)
- [List Pull Request Review Comments](#list-pull-request-review-comments)
- [Delete Pull Request Comment](#delete-pull-request-comment)
- [Delete Pull Request Review Comments](#delete-pull-request-review-comments)
- [Get Commits](#get-commits)
- [Get Latest Commit](#get-latest-commit)
- [Get Commit By SHA](#get-commit-by-sha)
Expand Down Expand Up @@ -417,6 +421,42 @@ pullRequestID := 5
err := client.AddPullRequestComment(ctx, owner, repository, content, pullRequestID)
```

##### Add Pull Request Review Comments

```go
// Go context
ctx := context.Background()
// Organization or username
owner := "jfrog"
// VCS repository
repository := "jfrog-cli"
// Pull Request ID
pullRequestID := 5
// Pull Request Comment
comments := []PullRequestComment{
{
CommentInfo: CommentInfo{
Content: "content",
},
PullRequestDiff: PullRequestDiff{
OriginalFilePath: index.js
OriginalStartLine: 1
OriginalEndLine: 1
OriginalStartColumn: 1
OriginalEndColumn: 1
NewFilePath: index.js
NewStartLine: 1
NewEndLine: 1
NewStartColumn: 1
NewEndColumn: 1
},
}
}


err := client.AddPullRequestReviewComments(ctx, owner, repository, pullRequestID, comments...)
```

##### List Pull Request Comments

```go
Expand All @@ -432,6 +472,62 @@ pullRequestID := 5
pullRequestComments, err := client.ListPullRequestComment(ctx, owner, repository, pullRequestID)
```

##### List Pull Request Review Comments

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

pullRequestComments, err := client.ListPullRequestReviewComments(ctx, owner, repository, pullRequestID)
```

##### Delete Pull Request Comment

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

err := client.DeletePullRequestComment(ctx, owner, repository, pullRequestID, commentID)
```

##### Delete Pull Request Review Comments

```go
// Go context
ctx := context.Background()
// Organization or username
owner := "jfrog"
// VCS repository
repository := "jfrog-cli"
// Pull Request ID
pullRequestID := 5
// Comment ID
comments := []CommentInfo{
{
ID: 2
// For GitLab
ThreadID: 7
}
}

err := client.DeletePullRequestComment(ctx, owner, repository, pullRequestID, comments...)
```


#### Get Commits

```go
Expand Down
61 changes: 53 additions & 8 deletions vcsclient/azurerepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,58 @@ func (client *AzureReposClient) UpdatePullRequest(ctx context.Context, owner, re

// AddPullRequestComment on Azure Repos
func (client *AzureReposClient) AddPullRequestComment(ctx context.Context, _, repository, content string, pullRequestID int) error {
return client.addPullRequestComment(ctx, repository, pullRequestID, PullRequestComment{CommentInfo: CommentInfo{Content: content}})
}

// AddPullRequestReviewComments on Azure Repos
func (client *AzureReposClient) AddPullRequestReviewComments(ctx context.Context, _, repository string, pullRequestID int, comments ...PullRequestComment) error {
if len(comments) == 0 {
return errors.New(vcsutils.ErrNoCommentsProvided)
}
for _, comment := range comments {
if err := client.addPullRequestComment(ctx, repository, pullRequestID, comment); err != nil {
return err
}
}
return nil
}

func (client *AzureReposClient) addPullRequestComment(ctx context.Context, repository string, pullRequestID int, comment PullRequestComment) error {
azureReposGitClient, err := client.buildAzureReposClient(ctx)
if err != nil {
return err
}
// To add a new comment to the pull request, we need to open a new thread, and add a comment inside this thread.
_, err = azureReposGitClient.CreateThread(ctx, git.CreateThreadArgs{
threadArgs := getThreadArgs(repository, client.vcsInfo.Project, pullRequestID, comment)
_, err = azureReposGitClient.CreateThread(ctx, threadArgs)
return err
}

func getThreadArgs(repository, project string, prId int, comment PullRequestComment) git.CreateThreadArgs {
filePath := vcsutils.GetPullRequestFilePath(comment.NewFilePath)
return git.CreateThreadArgs{
CommentThread: &git.GitPullRequestCommentThread{
Comments: &[]git.Comment{{Content: &content}},
Comments: &[]git.Comment{{Content: &comment.Content}},
Status: &git.CommentThreadStatusValues.Active,
ThreadContext: &git.CommentThreadContext{
FilePath: &filePath,
LeftFileStart: &git.CommentPosition{Line: &comment.OriginalStartLine, Offset: &comment.OriginalStartColumn},
LeftFileEnd: &git.CommentPosition{Line: &comment.OriginalEndLine, Offset: &comment.OriginalEndColumn},
RightFileStart: &git.CommentPosition{Line: &comment.NewStartLine, Offset: &comment.NewStartColumn},
RightFileEnd: &git.CommentPosition{Line: &comment.NewEndLine, Offset: &comment.NewEndColumn},
},
},
RepositoryId: &repository,
PullRequestId: &pullRequestID,
Project: &client.vcsInfo.Project,
})
return err
PullRequestId: &prId,
Project: &project,
}
}

// ListPullRequestComments returns all the pull request threads with their comments.
// ListPullRequestReviewComments on Azure Repos
func (client *AzureReposClient) ListPullRequestReviewComments(ctx context.Context, owner, repository string, pullRequestID int) ([]CommentInfo, error) {
return client.ListPullRequestComments(ctx, owner, repository, pullRequestID)
}

// ListPullRequestComments on Azure Repos
func (client *AzureReposClient) ListPullRequestComments(ctx context.Context, _, repository string, pullRequestID int) ([]CommentInfo, error) {
azureReposGitClient, err := client.buildAzureReposClient(ctx)
if err != nil {
Expand Down Expand Up @@ -266,6 +300,16 @@ func (client *AzureReposClient) ListPullRequestComments(ctx context.Context, _,
return commentInfo, nil
}

// DeletePullRequestReviewComments on Azure Repos
func (client *AzureReposClient) DeletePullRequestReviewComments(ctx context.Context, owner, repository string, pullRequestID int, comments ...CommentInfo) error {
for _, comment := range comments {
if err := client.DeletePullRequestComment(ctx, owner, repository, pullRequestID, int(comment.ID)); err != nil {
return err
}
}
return nil
}

// DeletePullRequestComment on Azure Repos
func (client *AzureReposClient) DeletePullRequestComment(ctx context.Context, _, repository string, pullRequestID, commentID int) error {
azureReposGitClient, err := client.buildAzureReposClient(ctx)
Expand Down Expand Up @@ -314,6 +358,7 @@ func (client *AzureReposClient) getOpenPullRequests(ctx context.Context, owner,
return pullRequestsInfo, nil
}

// GetPullRequestById in Azure Repos
func (client *AzureReposClient) GetPullRequestByID(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) {
azureReposGitClient, err := client.buildAzureReposClient(ctx)
if err != nil {
Expand Down
76 changes: 76 additions & 0 deletions vcsclient/azurerepos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,67 @@ func TestAzureRepos_TestAddPullRequestComment(t *testing.T) {
assert.Error(t, err)
}

func TestAzureRepos_TestAddPullRequestReviewComments(t *testing.T) {
type AddPullRequestCommentResponse struct {
Value git.GitPullRequestCommentThread
Count int
}
id := 123
pom := "/pom.xml"
startLine := 1
endLine := 5
startColumn := 1
endColumn := 13
res := AddPullRequestCommentResponse{
Value: git.GitPullRequestCommentThread{Id: &id, ThreadContext: &git.CommentThreadContext{
FilePath: &pom,
LeftFileEnd: &git.CommentPosition{
Line: &endLine,
Offset: &endColumn,
},
LeftFileStart: &git.CommentPosition{
Line: &startLine,
Offset: &startColumn,
},
RightFileEnd: &git.CommentPosition{
Line: &endLine,
Offset: &endColumn,
},
RightFileStart: &git.CommentPosition{
Line: &startLine,
Offset: &startColumn,
},
}},
Count: 1,
}
jsonRes, err := json.Marshal(res)
assert.NoError(t, err)
ctx := context.Background()
client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, jsonRes, "pullRequestComments", createAzureReposHandler)
defer cleanUp()
err = client.AddPullRequestReviewComments(ctx, "", repo1, 2, PullRequestComment{
CommentInfo: CommentInfo{Content: "test"},
PullRequestDiff: PullRequestDiff{
OriginalFilePath: pom,
OriginalStartLine: startLine,
OriginalEndLine: endLine,
OriginalStartColumn: startColumn,
OriginalEndColumn: startColumn,
NewFilePath: pom,
NewStartLine: startLine,
NewEndLine: endLine,
NewStartColumn: startColumn,
NewEndColumn: endColumn,
},
})
assert.NoError(t, err)

badClient, cleanUp := createBadAzureReposClient(t, []byte{})
defer cleanUp()
err = badClient.AddPullRequestReviewComments(ctx, "", repo1, 2, PullRequestComment{CommentInfo: CommentInfo{Content: "test"}})
assert.Error(t, err)
}

func TestAzureRepos_TestListOpenPullRequests(t *testing.T) {
type ListOpenPullRequestsResponse struct {
Value []git.GitPullRequest
Expand Down Expand Up @@ -320,6 +381,10 @@ func TestAzureReposClient_GetPullRequest(t *testing.T) {
assert.Error(t, err)
}

func TestListPullRequestReviewComments(t *testing.T) {
TestListPullRequestComments(t)
}

func TestListPullRequestComments(t *testing.T) {
type ListPullRequestCommentsResponse struct {
Value []git.GitPullRequestCommentThread
Expand Down Expand Up @@ -635,6 +700,17 @@ func TestAzureReposClient_GetModifiedFiles(t *testing.T) {
})
}

func TestAzureReposClient_DeletePullRequestReviewComments(t *testing.T) {
client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, "", "deletePullRequestComments", createAzureReposHandler)
defer cleanUp()
err := client.DeletePullRequestReviewComments(context.Background(), "", repo1, 1, []CommentInfo{{ID: 1}, {ID: 2}}...)
assert.NoError(t, err)
badClient, badClientCleanup := createBadAzureReposClient(t, []byte{})
defer badClientCleanup()
err = badClient.DeletePullRequestReviewComments(context.Background(), "", repo1, 1, []CommentInfo{{ID: 1}, {ID: 2}}...)
assert.Error(t, err)
}

func TestAzureReposClient_DeletePullRequestComment(t *testing.T) {
client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, "", "deletePullRequestComments", createAzureReposHandler)
defer cleanUp()
Expand Down
17 changes: 16 additions & 1 deletion vcsclient/bitbucketcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ func (client *BitbucketCloudClient) AddPullRequestComment(ctx context.Context, o
return err
}

// AddPullRequestReviewComments on Bitbucket cloud
func (client *BitbucketCloudClient) AddPullRequestReviewComments(_ context.Context, _, _ string, _ int, _ ...PullRequestComment) error {
return errBitbucketAddPullRequestReviewCommentsNotSupported
}

// ListPullRequestReviewComments on Bitbucket cloud
func (client *BitbucketCloudClient) ListPullRequestReviewComments(_ context.Context, _, _ string, _ int) ([]CommentInfo, error) {
return nil, errBitbucketListPullRequestReviewCommentsNotSupported
}

// ListPullRequestComments on Bitbucket cloud
func (client *BitbucketCloudClient) ListPullRequestComments(ctx context.Context, owner, repository string, pullRequestID int) (res []CommentInfo, err error) {
err = validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
Expand All @@ -432,9 +442,14 @@ func (client *BitbucketCloudClient) ListPullRequestComments(ctx context.Context,
return mapBitbucketCloudCommentToCommentInfo(&parsedComments), nil
}

// DeletePullRequestReviewComments on Bitbucket cloud
func (client *BitbucketCloudClient) DeletePullRequestReviewComments(_ context.Context, _, _ string, _ int, _ ...CommentInfo) error {
return errBitbucketDeletePullRequestComment
}

// DeletePullRequestComment on Bitbucket cloud
func (client *BitbucketCloudClient) DeletePullRequestComment(_ context.Context, _, _ string, _, _ int) error {
return nil
return errBitbucketDeletePullRequestComment
}

// GetLatestCommit on Bitbucket cloud
Expand Down
36 changes: 36 additions & 0 deletions vcsclient/bitbucketcloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,42 @@ func TestBitbucketCloud_CreateLabel(t *testing.T) {
assert.ErrorIs(t, err, errLabelsNotSupported)
}

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

err = client.AddPullRequestReviewComments(ctx, owner, repo1, 1)
assert.ErrorIs(t, err, errBitbucketAddPullRequestReviewCommentsNotSupported)
}

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

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

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

err = client.DeletePullRequestComment(ctx, owner, repo1, 1, 1)
assert.ErrorIs(t, err, errBitbucketDeletePullRequestComment)
}

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

err = client.DeletePullRequestReviewComments(ctx, owner, repo1, 1, CommentInfo{})
assert.ErrorIs(t, err, errBitbucketDeletePullRequestComment)
}

func TestBitbucketCloudClient_DownloadFileFromRepo(t *testing.T) {
ctx := context.Background()
client, err := NewClientBuilder(vcsutils.BitbucketCloud).Build()
Expand Down
Loading

0 comments on commit 5004344

Please sign in to comment.