-
Notifications
You must be signed in to change notification settings - Fork 2
/
yamllint-action.go
224 lines (183 loc) · 4.87 KB
/
yamllint-action.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
/*
Copyright 2021, Staffbase GmbH and contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bufio"
"context"
"fmt"
"io"
"log"
"os"
"regexp"
"sort"
"strconv"
"strings"
"github.com/google/go-github/v62/github"
"github.com/ldez/ghactions"
)
type Report struct {
NumFailedLines int
Success bool
ErrorHasOccured bool
LinterResults []*LinterResult
}
type LinterResult struct {
AssertionResults []*AssertionResult
FilePath string
}
type AssertionResult struct {
Message string
Status string
Column int
Line int
Severity string
}
func mapSeverity(severity string) string {
if severity == "warning" {
return "warning"
}
return "failure"
}
func getSortedKeySlice(items map[string]*LinterResult) []string {
keys := make([]string, len(items))
i := 0
for k := range items {
keys[i] = k
i++
}
sort.Strings(keys)
return keys
}
func parseInput(r io.Reader) Report {
scanner := bufio.NewScanner(r)
files := make(map[string]*LinterResult)
failedLines := 0
ErrorHasOccured := false
re := regexp.MustCompile(` \[(.*)]`)
for scanner.Scan() {
cols := strings.Split(scanner.Text(), ":")
if len(cols) < 4 {
log.Println(scanner.Text())
break
}
codeLine, _ := strconv.Atoi(cols[1])
codeCol, _ := strconv.Atoi(cols[2])
fileName := cols[0]
message := strings.Split(cols[3], "] ")[1]
if len(cols) == 5 {
message += ":" + cols[4]
}
severity := mapSeverity(re.FindStringSubmatch(cols[3])[1])
if severity == "failure" {
ErrorHasOccured = true
}
assertionResult := AssertionResult{
Message: message,
Line: codeLine,
Column: codeCol,
Severity: severity,
}
if _, exist := files[fileName]; exist == false {
files[fileName] = &LinterResult{FilePath: fileName}
}
files[fileName].AssertionResults = append(files[fileName].AssertionResults, &assertionResult)
failedLines++
}
report := Report{
NumFailedLines: failedLines,
Success: failedLines == 0,
ErrorHasOccured: ErrorHasOccured,
}
keys := getSortedKeySlice(files)
for _, key := range keys {
report.LinterResults = append(report.LinterResults, files[key])
}
return report
}
func main() {
report := parseInput(os.Stdin)
ctx := context.Background()
action := ghactions.NewAction(ctx)
action.OnPush(func(client *github.Client, event *github.PushEvent) error {
return handlePush(ctx, client, report)
})
if err := action.Run(); err != nil {
log.Fatal(err)
}
}
func handlePush(ctx context.Context, client *github.Client, report Report) error {
if report.Success {
return nil
}
head := os.Getenv(ghactions.GithubSha)
owner, repoName := ghactions.GetRepoInfo()
// find the action's checkrun
checkName := os.Getenv("ACTION_NAME")
result, _, err := client.Checks.ListCheckRunsForRef(ctx, owner, repoName, head, &github.ListCheckRunsOptions{
CheckName: github.String(checkName),
Status: github.String("in_progress"),
})
if err != nil {
return err
}
if len(result.CheckRuns) == 0 {
return fmt.Errorf("Unable to find check run for action: %s", checkName)
}
checkRun := result.CheckRuns[0]
// add annotations for test failures
workspacePath := os.Getenv(ghactions.GithubWorkspace) + "/"
var annotations []*github.CheckRunAnnotation
for _, t := range report.LinterResults {
path := strings.TrimPrefix(t.FilePath, workspacePath)
if len(t.AssertionResults) > 0 {
for _, a := range t.AssertionResults {
annotations = append(annotations, &github.CheckRunAnnotation{
Path: github.String(path),
StartLine: github.Int(a.Line),
EndLine: github.Int(a.Line),
AnnotationLevel: github.String(a.Severity),
Title: github.String(""),
Message: github.String(a.Message),
})
}
}
}
summary := fmt.Sprintf(
"Tested lines: %d failed\n",
report.NumFailedLines,
)
// add annotations in #50 chunks
for i := 0; i < len(annotations); i += 50 {
end := i + 50
if end > len(annotations) {
end = len(annotations)
}
output := &github.CheckRunOutput{
Title: github.String("Result"),
Summary: github.String(summary),
Annotations: annotations[i:end],
}
_, _, err = client.Checks.UpdateCheckRun(ctx, owner, repoName, checkRun.GetID(), github.UpdateCheckRunOptions{
Name: checkName,
Output: output,
})
if err != nil {
return err
}
}
if report.ErrorHasOccured {
return fmt.Errorf(summary)
} else {
return nil
}
}