From fe9427a63b0a7f9a4fc342c65459a4fdf8d78968 Mon Sep 17 00:00:00 2001 From: Omer Zidkoni Date: Tue, 12 Sep 2023 10:23:24 +0300 Subject: [PATCH] fix tests --- README.md | 96 +++++++++++++++++++++++++++++++++++++++++++++ vcsclient/github.go | 4 +- vcsclient/gitlab.go | 11 +++--- 3 files changed, 103 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 25a7b54e..424d8e36 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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 diff --git a/vcsclient/github.go b/vcsclient/github.go index 6b52fc90..5e6bca03 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -14,7 +14,7 @@ import ( "io" "net/http" "net/url" - "os" + "path/filepath" "sort" "strconv" "strings" @@ -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 { diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index 166011b0..643f0006 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -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 { @@ -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 } @@ -390,7 +389,7 @@ func (client *GitLabClient) addPullRequestReviewComment(ctx context.Context, pro PositionType: "text", NewLine: newLine, NewPath: newPath, - OldLine: oldLine, + OldLine: newLine, OldPath: oldPath, } @@ -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) }