Skip to content

Commit

Permalink
fix: support abs path for related location
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Jun 14, 2024
1 parent 34a54e5 commit 450ce0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
12 changes: 9 additions & 3 deletions service/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions service/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -479,7 +480,7 @@ func TestGitHubPullRequest_Post_Flush_review_api(t *testing.T) {
"<hr>",
"",
"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")),
},
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 450ce0f

Please sign in to comment.