Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi committed Sep 12, 2023
1 parent 3e608eb commit fe9427a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 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
4 changes: 2 additions & 2 deletions vcsclient/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -443,7 +443,7 @@ func (client *GitHubClient) AddPullRequestReviewComments(ctx context.Context, ow
latestCommitSHA := commits[len(commits)-1].GetSHA()

for _, comment := range comments {
filePath := strings.TrimPrefix(comment.NewFilePath, string(os.PathSeparator))
filePath := filepath.Clean(comment.NewFilePath)
startLine := &comment.NewStartLine
// GitHub API won't accept 'start_line' if it equals the end line
if *startLine == comment.NewEndLine {
Expand Down
11 changes: 5 additions & 6 deletions vcsclient/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (client *GitLabClient) getMergeRequestChanges(ctx context.Context, projectI
func (client *GitLabClient) addPullRequestReviewComment(ctx context.Context, projectID string, pullRequestID int, comment PullRequestComment, versions []*gitlab.MergeRequestDiffVersion, mergeRequestChanges *gitlab.MergeRequest) error {
// Find the corresponding change in merge request
var newPath, oldPath string
var newLine, oldLine int
var newLine int
var diffFound bool

for _, diff := range mergeRequestChanges.Changes {
Expand All @@ -371,7 +371,6 @@ func (client *GitLabClient) addPullRequestReviewComment(ctx context.Context, pro
// New files don't have old data
if !diff.NewFile {
oldPath = diff.OldPath
oldLine = comment.OriginalStartLine
}
break
}
Expand All @@ -390,7 +389,7 @@ func (client *GitLabClient) addPullRequestReviewComment(ctx context.Context, pro
PositionType: "text",
NewLine: newLine,
NewPath: newPath,
OldLine: oldLine,
OldLine: newLine,
OldPath: oldPath,
}

Expand All @@ -402,16 +401,16 @@ func (client *GitLabClient) addPullRequestReviewComment(ctx context.Context, pro
// - When commenting on an existing file that hasn't changed in the diff, include 'old_path' and 'old_line' parameters.

client.logger.Debug(fmt.Sprintf("Create merge request discussion sent. newPath: %v newLine: %v oldPath: %v, oldLine: %v",
newPath, newLine, oldPath, oldLine))
newPath, newLine, oldPath, newLine))
// Attempt to create a merge request discussion thread
_, _, err := client.createMergeRequestDiscussion(ctx, projectID, comment.Content, pullRequestID, diffPosition)

// Retry without oldLine and oldPath if the GitLab API call fails
if err != nil {
client.logger.Debug(fmt.Sprintf("Create merge request discussion second attempt sent. newPath: %v newLine: %v oldPath: %v, oldLine: %v",
newPath, newLine, oldPath, oldLine))
diffPosition.OldLine = 0
diffPosition.OldPath = ""
client.logger.Debug(fmt.Sprintf("Create merge request discussion second attempt sent. newPath: %v newLine: %v oldPath: %v, oldLine: %v",
newPath, newLine, oldPath, newLine))
_, _, err = client.createMergeRequestDiscussion(ctx, projectID, comment.Content, pullRequestID, diffPosition)
}

Expand Down

0 comments on commit fe9427a

Please sign in to comment.