From 450ce0fccdf4bec7df990926962a3abae9311906 Mon Sep 17 00:00:00 2001 From: haya14busa Date: Sat, 15 Jun 2024 00:02:10 +0900 Subject: [PATCH] fix: support abs path for related location --- service/github/github.go | 12 +++++++++--- service/github/github_test.go | 5 +++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/service/github/github.go b/service/github/github.go index e8fef737..fe40c76c 100644 --- a/service/github/github.go +++ b/service/github/github.go @@ -16,6 +16,7 @@ import ( "github.com/reviewdog/reviewdog" "github.com/reviewdog/reviewdog/cienv" + "github.com/reviewdog/reviewdog/pathutil" "github.com/reviewdog/reviewdog/proto/rdf" "github.com/reviewdog/reviewdog/service/commentutil" "github.com/reviewdog/reviewdog/service/github/githubutils" @@ -131,6 +132,10 @@ func (g *PullRequest) postAsReviewComment(ctx context.Context) error { reviewComments := make([]*github.DraftReviewComment, 0, len(postComments)) remaining := make([]*reviewdog.Comment, 0) repoBaseHTMLURLForRelatedLoc := "" + rootPath, err := serviceutil.GetGitRoot() + if err != nil { + return err + } for _, c := range postComments { if !c.Result.InDiffContext { // GitHub Review API cannot report results outside diff. If it's running @@ -149,7 +154,7 @@ func (g *PullRequest) postAsReviewComment(ctx context.Context) error { } repoBaseHTMLURLForRelatedLoc = repo.GetHTMLURL() + "/blob/" + g.sha } - body := buildBody(c, repoBaseHTMLURLForRelatedLoc) + body := buildBody(c, repoBaseHTMLURLForRelatedLoc, rootPath) if g.postedcs.IsPosted(c, githubCommentLine(c), body) { // it's already posted. skip it. continue @@ -398,7 +403,7 @@ func listAllPullRequestsComments(ctx context.Context, cli *github.Client, return append(comments, restComments...), nil } -func buildBody(c *reviewdog.Comment, baseRelatedLocURL string) string { +func buildBody(c *reviewdog.Comment, baseRelatedLocURL string, gitRootPath string) string { cbody := commentutil.MarkdownComment(c) if suggestion := buildSuggestions(c); suggestion != "" { cbody += "\n" + suggestion @@ -408,7 +413,8 @@ func buildBody(c *reviewdog.Comment, baseRelatedLocURL string) string { if loc.GetPath() == "" || loc.GetRange().GetStart().GetLine() == 0 { continue } - relatedURL := fmt.Sprintf("%s/%s#L%d", baseRelatedLocURL, loc.GetPath(), loc.GetRange().GetStart().GetLine()) + relPath := pathutil.NormalizePath(loc.GetPath(), gitRootPath, "") + relatedURL := fmt.Sprintf("%s/%s#L%d", baseRelatedLocURL, relPath, loc.GetRange().GetStart().GetLine()) if endLine := loc.GetRange().GetEnd().GetLine(); endLine > 0 { relatedURL += fmt.Sprintf("-L%d", endLine) } diff --git a/service/github/github_test.go b/service/github/github_test.go index 133e193e..3be394cb 100644 --- a/service/github/github_test.go +++ b/service/github/github_test.go @@ -7,6 +7,7 @@ import ( "net/http/httptest" "net/url" "os" + "path/filepath" "strings" "testing" @@ -479,7 +480,7 @@ func TestGitHubPullRequest_Post_Flush_review_api(t *testing.T) { "
", "", "related loc test (2)", - "https://test/repo/path/blob/sha/reviewdog2.go#L14", + "https://test/repo/path/blob/sha/service/github/reviewdog2.go#L14", }, "\n")), }, } @@ -1205,7 +1206,7 @@ func TestGitHubPullRequest_Post_Flush_review_api(t *testing.T) { }, { Location: &rdf.Location{ - Path: "reviewdog2.go", + Path: filepath.Join(cwd, "reviewdog2.go"), Range: &rdf.Range{ Start: &rdf.Position{ Line: 14,