This repository was archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgithub_reviewer.go
169 lines (140 loc) · 4.31 KB
/
github_reviewer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package reporters
import (
"context"
"fmt"
"os"
"strings"
"github.com/golangci/golangci-api/pkg/worker/lib/experiments"
"github.com/golangci/golangci-api/pkg/goenvbuild/config"
envbuildresult "github.com/golangci/golangci-api/pkg/goenvbuild/result"
"github.com/golangci/golangci-api/pkg/worker/analyze/linters/result"
"github.com/golangci/golangci-api/pkg/worker/lib/github"
gh "github.com/google/go-github/github"
"github.com/pkg/errors"
)
type GithubReviewer struct {
*github.Context
client github.Client
ec *experiments.Checker
}
func NewGithubReviewer(c *github.Context, client github.Client, ec *experiments.Checker) *GithubReviewer {
accessToken := os.Getenv("GITHUB_REVIEWER_ACCESS_TOKEN")
if accessToken != "" { // review as special user
cCopy := *c
cCopy.GithubAccessToken = accessToken
c = &cCopy
}
ret := &GithubReviewer{
Context: c,
client: client,
ec: ec,
}
return ret
}
type existingComment struct {
file string
line int
}
type existingComments []existingComment
func (ecs existingComments) contains(i *result.Issue) bool {
for _, c := range ecs {
if c.file == i.File && c.line == i.HunkPos {
return true
}
}
return false
}
func (gr GithubReviewer) fetchExistingComments(ctx context.Context) (existingComments, error) {
comments, err := gr.client.GetPullRequestComments(ctx, gr.Context)
if err != nil {
return nil, err
}
var ret existingComments
for _, c := range comments {
if c.Position == nil { // comment on outdated code, skip it
continue
}
ret = append(ret, existingComment{
file: c.GetPath(),
line: c.GetPosition(),
})
}
return ret, nil
}
func (gr GithubReviewer) makeSimpleIssueCommentBody(i *result.Issue) string {
text := i.Text
if i.FromLinter != "" {
text += fmt.Sprintf(" (from `%s`)", i.FromLinter)
}
return text
}
func (gr GithubReviewer) makeIssueCommentBody(i *result.Issue, buildConfig *config.Service) string {
if buildConfig.SuggestedChanges.Disabled {
return gr.makeSimpleIssueCommentBody(i)
}
if i.Replacement == nil {
return gr.makeSimpleIssueCommentBody(i)
}
if i.LineRange != nil && i.LineRange.From != i.LineRange.To {
// github api doesn't support multi-line suggestion
return gr.makeSimpleIssueCommentBody(i)
}
if !gr.ec.IsActiveForRepo("SUGGESTED_CHANGES", gr.Repo.Owner, gr.Repo.Name) {
return gr.makeSimpleIssueCommentBody(i)
}
var suggestionBody string
if !i.Replacement.NeedOnlyDelete {
suggestionBody = strings.Join(i.Replacement.NewLines, "\n")
}
return fmt.Sprintf("%s\n```suggestion\n%s\n```", gr.makeSimpleIssueCommentBody(i), suggestionBody)
}
func (gr GithubReviewer) makeComments(issues []result.Issue, ec existingComments,
buildConfig *config.Service) []*gh.DraftReviewComment {
comments := []*gh.DraftReviewComment{}
for _, i := range issues {
if ec.contains(&i) {
continue // don't be annoying: don't comment on the same line twice
}
comment := &gh.DraftReviewComment{
Path: gh.String(i.File),
Position: gh.Int(i.HunkPos),
Body: gh.String(gr.makeIssueCommentBody(&i, buildConfig)),
}
comments = append(comments, comment)
}
return comments
}
func (gr GithubReviewer) Report(ctx context.Context, buildConfig *config.Service, buildLog *envbuildresult.Log,
ref string, issues []result.Issue) error {
return buildLog.RunNewGroup("post review", func(sg *envbuildresult.StepGroup) error {
step := sg.AddStep("check issues")
if len(issues) == 0 {
step.AddOutputLine("Nothing to report: no issues found")
return nil
}
step.AddOutputLine("Have %d issues", len(issues))
sg.AddStep("fetch existing comments")
existingComments, err := gr.fetchExistingComments(ctx)
if err != nil {
return err
}
step = sg.AddStep("build new review comments")
comments := gr.makeComments(issues, existingComments, buildConfig)
if len(comments) == 0 {
step.AddOutputLine("No new comments were built")
return nil // all comments are already exist
}
step.AddOutputLine("Send %d comments about new issues", len(comments))
sg.AddStep("create GitHub review")
review := &gh.PullRequestReviewRequest{
CommitID: gh.String(ref),
Event: gh.String("COMMENT"),
Body: gh.String(""),
Comments: comments,
}
if err := gr.client.CreateReview(ctx, gr.Context, review); err != nil {
return errors.Wrapf(err, "can't create review %v", review)
}
return nil
})
}