Skip to content

Commit

Permalink
scm/github: filter issues that belongs to PR diff
Browse files Browse the repository at this point in the history
  • Loading branch information
herlon214 committed Apr 13, 2022
1 parent f580f7d commit d0abd46
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/gorilla/mux v1.8.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sourcegraph/go-diff v0.6.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,15 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/sourcegraph/go-diff v0.6.1 h1:hmA1LzxW0n1c3Q4YbrFgg4P99GSnebYa3x8gr0HZqLQ=
github.com/sourcegraph/go-diff v0.6.1/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.2.1 h1:+KmjbUw1hriSNMF55oPrkZcb27aECyrj8V2ytv7kWDw=
Expand Down
81 changes: 59 additions & 22 deletions pkg/scm/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/google/go-github/v41/github"
"github.com/pkg/errors"
"github.com/sourcegraph/go-diff/diff"
"golang.org/x/oauth2"

"github.com/herlon214/sonarqube-pr-issues/pkg/sonarqube"
Expand Down Expand Up @@ -56,22 +57,72 @@ func (g *Github) PublishIssuesReviewFor(ctx context.Context, issues []sonarqube.
reviewEvent = REVIEW_EVENT_COMMENT
}

// Convert PR number into int
prNumber, err := strconv.Atoi(pr.Key)
if err != nil {
return errors.Wrap(err, "failed to convert PR number to int")
}

// Parse PR path
ghPath, err := parseGithubPath(pr.URL)
if err != nil {
return errors.Wrap(err, "failed to parse github path")
}

// Fetch PR diffs
ghDiff, _, err := g.client.PullRequests.GetRaw(ctx, ghPath.Owner, ghPath.Repo, prNumber, github.RawOptions{github.Diff})
if err != nil {
return errors.Wrap(err, "failed to get raw PR")
}

// Parse diffs
fileDiffs, err := diff.ParseMultiFileDiff([]byte(ghDiff))
if err != nil {
return errors.Wrap(err, "failed to parse diff")
}

diffMap := make(map[string][]*diff.Hunk)
for i := range fileDiffs {
fileName := fileDiffs[i].OrigName[2:]
diffMap[fileName] = fileDiffs[i].Hunks
}

comments := make([]*github.DraftReviewComment, 0)

// Create a comment for each issue
for _, issue := range issues {
side := "RIGHT"
message := issue.MarkdownMessage(g.sonar.Root)
filePath := issue.FilePath()
line := issue.Line
lineNumber := issue.Line

// Skip if current issue is not part of the PR diff
hunks, ok := diffMap[filePath]
if !ok {
continue
}

comment := &github.DraftReviewComment{
Path: &filePath,
Body: &message,
Side: &side,
Line: &line,
for _, hunk := range hunks {
if lineNumber < int(hunk.OrigStartLine) || lineNumber < int(hunk.NewStartLine) {
continue
}
if lineNumber > int(hunk.OrigStartLine+hunk.OrigLines) || lineNumber > int(hunk.NewStartLine+hunk.NewLines) {
continue
}

comment := &github.DraftReviewComment{
Path: &filePath,
Body: &message,
Side: &side,
Line: &lineNumber,
}
comments = append(comments, comment)
}
comments = append(comments, comment)

}

if len(comments) == 0 {
return errors.Wrap(err, "failed to find relevant issues")
}

body := fmt.Sprintf(`:wave: Hey, I added %d comments about your changes, please take a look :slightly_smiling_face:`, len(issues))
Expand All @@ -82,26 +133,12 @@ func (g *Github) PublishIssuesReviewFor(ctx context.Context, issues []sonarqube.
Comments: comments,
}

// Convert PR number into int
prNumber, err := strconv.Atoi(pr.Key)
if err != nil {
return errors.Wrap(err, "failed to convert PR number to int")
}

// Parse PR path
ghPath, err := parseGithubPath(pr.URL)
if err != nil {
return errors.Wrap(err, "failed to parse github path")
}

// Create the review
out, res, err := g.client.PullRequests.CreateReview(ctx, ghPath.Owner, ghPath.Repo, prNumber, reviewRequest)
_, _, err = g.client.PullRequests.CreateReview(ctx, ghPath.Owner, ghPath.Repo, prNumber, reviewRequest)
if err != nil {
return errors.Wrap(err, "failed to create review")
}

fmt.Println(out, res)

return nil
}

Expand Down

0 comments on commit d0abd46

Please sign in to comment.