-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
scm/github: filter issues that belongs to PR diff #9
Merged
+284
−24
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
diff --git a/go.mod b/go.mod | ||
index 5d90f4b..9521466 100644 | ||
--- a/go.mod | ||
+++ b/go.mod | ||
@@ -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 | ||
diff --git a/go.sum b/go.sum | ||
index c7ec3b3..f13e00b 100644 | ||
--- a/go.sum | ||
+++ b/go.sum | ||
@@ -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= | ||
diff --git a/pkg/scm/github.go b/pkg/scm/github.go | ||
index 0150f40..8475900 100644 | ||
--- a/pkg/scm/github.go | ||
+++ b/pkg/scm/github.go | ||
@@ -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" | ||
@@ -56,6 +57,36 @@ 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 | ||
@@ -63,15 +94,35 @@ func (g *Github) PublishIssuesReviewFor(ctx context.Context, issues []sonarqube. | ||
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)) | ||
@@ -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 | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@herlon214 I forgot to update the
I added %d comments about your changes
to uselen(comments)
as now thelen(issues)
>=len(comments)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed on v0.5.1, thanks @adenisonafifi