forked from morphy2k/revive-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
272 lines (219 loc) · 5.59 KB
/
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"context"
"encoding/json"
"fmt"
"go/token"
"os"
"strings"
"sync"
"time"
"github.com/google/go-github/v33/github"
"golang.org/x/oauth2"
)
const (
envName = "CHECK_NAME"
envRepo = "GITHUB_REPOSITORY"
envSHA = "GITHUB_SHA"
envToken = "GITHUB_TOKEN"
chunkLimit = 50
)
var (
name string
ghToken string
repoOwner string
repoName string
headSHA string
)
var client *github.Client
func init() {
if env := os.Getenv(envName); len(env) > 0 {
name = env
} else {
name = "revive-action"
}
if env := os.Getenv(envToken); len(env) > 0 {
ghToken = env
} else {
fmt.Fprintln(os.Stderr, "Missing environment variable:", envToken)
os.Exit(2)
}
if env := os.Getenv(envRepo); len(env) > 0 {
s := strings.SplitN(env, "/", 2)
repoOwner, repoName = s[0], s[1]
} else {
fmt.Fprintln(os.Stderr, "Missing environment variable:", envRepo)
os.Exit(2)
}
if env := os.Getenv(envSHA); len(env) > 0 {
headSHA = env
} else {
fmt.Fprintln(os.Stderr, "Missing environment variable:", envSHA)
os.Exit(2)
}
tc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken},
))
client = github.NewClient(tc)
}
func createCheck() *github.CheckRun {
opts := github.CreateCheckRunOptions{
Name: name,
HeadSHA: headSHA,
Status: github.String("in_progress"),
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
fmt.Println(repoOwner, repoName)
check, _, err := client.Checks.CreateCheckRun(ctx, repoOwner, repoName, opts)
if err != nil {
fmt.Fprintln(os.Stderr, "Error while creating check-run:", err)
os.Exit(1)
}
return check
}
type conclusion int
const (
conclSuccess conclusion = iota
conclFailure
)
func (c conclusion) String() string {
return [...]string{"success", "failure"}[c]
}
func completeCheck(check *github.CheckRun, concl conclusion, stats *failureStats) {
opts := github.UpdateCheckRunOptions{
Name: name,
Conclusion: github.String(concl.String()),
Output: &github.CheckRunOutput{
Title: github.String("Result"),
Summary: github.String(stats.String()),
},
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if _, _, err := client.Checks.UpdateCheckRun(
ctx, repoOwner, repoName, check.GetID(), opts); err != nil {
fmt.Fprintln(os.Stderr, "Error while completing check-run:", err)
os.Exit(1)
}
}
type failure struct {
Failure string
RuleName string
Category string
Position failurePosition
Confidence float64
Severity string
}
type failurePosition struct {
Start token.Position
End token.Position
}
type failureStats struct {
Total, Warnings, Errors int
}
func (f failureStats) String() string {
return fmt.Sprintf("%d failures (%d warnings, %d errors)",
f.Total, f.Warnings, f.Errors)
}
func getFailures(ch chan *failure) {
dec := json.NewDecoder(os.Stdin)
for dec.More() {
f := &failure{}
if err := dec.Decode(f); err != nil {
fmt.Fprintln(os.Stderr, "Error while decoding stdin:", err)
os.Exit(1)
}
ch <- f
}
close(ch)
}
func createAnnotations(failures []*failure) []*github.CheckRunAnnotation {
annotations := make([]*github.CheckRunAnnotation, len(failures))
for i, f := range failures {
var level string
switch f.Severity {
case "warning":
level = "warning"
case "error":
level = "failure"
}
fmt.Println(f.Position.Start.Filename, f.Position.Start.Line)
a := &github.CheckRunAnnotation{
Path: github.String(f.Position.Start.Filename),
StartLine: github.Int(f.Position.Start.Line),
EndLine: github.Int(f.Position.End.Line),
AnnotationLevel: github.String(level),
Title: github.String(
fmt.Sprintf("%s (%s)", strings.Title(f.Category), f.RuleName),
),
Message: github.String(f.Failure),
}
if f.Position.Start.Line == f.Position.End.Line {
a.StartColumn = github.Int(f.Position.Start.Column)
a.EndColumn = github.Int(f.Position.End.Column)
}
annotations[i] = a
}
return annotations
}
func pushFailures(check *github.CheckRun, failures []*failure, stats *failureStats, wg *sync.WaitGroup) {
opts := github.UpdateCheckRunOptions{
Name: name,
Output: &github.CheckRunOutput{
Title: github.String("Result"),
Summary: github.String(stats.String()),
Annotations: createAnnotations(failures),
},
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, _, err := client.Checks.UpdateCheckRun(
ctx, repoOwner, repoName, check.GetID(), opts)
if err != nil {
fmt.Fprintln(os.Stderr, "Error while updating check-run:", err)
os.Exit(1)
}
for _, annotation := range opts.Output.Annotations {
fmt.Println(annotation.Path, annotation.StartLine, annotation.Message)
}
wg.Done()
}
func main() {
var concl conclusion
check := createCheck()
failures := make([]*failure, 0)
stats := &failureStats{}
ch := make(chan *failure)
go getFailures(ch)
wg := &sync.WaitGroup{}
chunks := 1
for f := range ch {
failures = append(failures, f)
stats.Total++
switch f.Severity {
case "warning":
stats.Warnings++
case "error":
stats.Errors++
}
if c := chunks * chunkLimit; stats.Total > c {
wg.Add(1)
go pushFailures(check, failures[c-chunkLimit:c], stats, wg)
chunks++
}
}
if stats.Total > 0 {
wg.Add(1)
if chunks == 1 {
go pushFailures(check, failures, stats, wg)
} else {
c := chunks * chunkLimit
go pushFailures(check, failures[c-chunkLimit:], stats, wg)
}
wg.Wait()
concl = conclFailure
}
completeCheck(check, concl, stats)
fmt.Println("Successful run with", stats.String())
}