forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linter.go
623 lines (555 loc) · 16.5 KB
/
linter.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package actionlint
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
"time"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
)
// LogLevel is log level of logger used in Linter instance.
type LogLevel int
const (
// LogLevelNone does not output any log output.
LogLevelNone LogLevel = 0
// LogLevelVerbose shows verbose log output. This is equivalent to specifying -verbose option
// to actionlint command.
LogLevelVerbose = 1
// LogLevelDebug shows all log output including debug information.
LogLevelDebug = 2
)
// ColorOptionKind is kind of colorful output behavior.
type ColorOptionKind int
const (
// ColorOptionKindAuto is kind to determine to colorize errors output automatically. It is
// determined based on pty and $NO_COLOR environment variable. See document of fatih/color
// for more details.
ColorOptionKindAuto ColorOptionKind = iota
// ColorOptionKindAlways is kind to always colorize errors output.
ColorOptionKindAlways
// ColorOptionKindNever is kind never to colorize errors output.
ColorOptionKindNever
)
// LinterOptions is set of options for Linter instance. This struct is used for NewLinter factory
// function call. The zero value LinterOptions{} represents the default behavior.
type LinterOptions struct {
// Verbose is flag if verbose log output is enabled.
Verbose bool
// Debug is flag if debug log output is enabled.
Debug bool
// LogWriter is io.Writer object to use to print log outputs. Note that error outputs detected
// by the linter are not included in the log outputs.
LogWriter io.Writer
// Color is option for colorizing error outputs. See ColorOptionKind document for each enum values.
Color ColorOptionKind
// Oneline is flag if one line output is enabled. When enabling it, one error is output per one
// line. It is useful when reading outputs from programs.
Oneline bool
// Shellcheck is executable for running shellcheck external command. It can be command name like
// "shellcheck" or file path like "/path/to/shellcheck", "path/to/shellcheck". When this value
// is empty, shellcheck won't run to check scripts in workflow file.
Shellcheck string
// Pyflakes is executable for running pyflakes external command. It can be command name like "pyflakes"
// or file path like "/path/to/pyflakes", "path/to/pyflakes". When this value is empty, pyflakes
// won't run to check scripts in workflow file.
Pyflakes string
// IgnorePatterns is list of regular expression to filter errors. The pattern is applied to error
// messages. When an error is matched, the error is ignored.
IgnorePatterns []string
// ConfigFile is a path to config file. Empty string means no config file path is given. In
// the case, actionlint will try to read config from .github/actionlint.yaml.
ConfigFile string
// Format is a custom template to format error messages. It must follow Go Template format and
// contain at least one {{ }} placeholder. https://pkg.go.dev/text/template
Format string
// StdinFileName is a file name when reading input from stdin. When this value is empty, "<stdin>"
// is used as the default value.
StdinFileName string
// WorkingDir is a file path to the current working directory. When this value is empty, os.Getwd
// will be used to get a working directory.
WorkingDir string
// OnRulesCreated is a hook to add or remove the check rules. This function is called on checking
// every workflow files. Rules created by Linter instance are passed to the argument and the
// function should return the modified rules.
// Note that syntax errors may be reported even if this function returns nil or an empty slice.
OnRulesCreated func([]Rule) []Rule
// More options will come here
}
// Linter is struct to lint workflow files.
type Linter struct {
projects *Projects
out io.Writer
logOut io.Writer
logLevel LogLevel
oneline bool
shellcheck string
pyflakes string
ignorePats []*regexp.Regexp
defaultConfig *Config
errFmt *ErrorFormatter
cwd string
onRulesCreated func([]Rule) []Rule
}
// NewLinter creates a new Linter instance.
// The out parameter is used to output errors from Linter instance. Set io.Discard if you don't
// want the outputs.
// The opts parameter is LinterOptions instance which configures behavior of linting.
func NewLinter(out io.Writer, opts *LinterOptions) (*Linter, error) {
level := LogLevelNone
if opts.Verbose {
level = LogLevelVerbose
} else if opts.Debug {
level = LogLevelDebug
}
if opts.Color == ColorOptionKindNever {
color.NoColor = true
} else {
if opts.Color == ColorOptionKindAlways {
color.NoColor = false
}
// Allow colorful output on Windows
if f, ok := out.(*os.File); ok {
out = colorable.NewColorable(f)
}
}
var lout io.Writer = io.Discard
if opts.LogWriter != nil {
lout = opts.LogWriter
}
var cfg *Config
if opts.ConfigFile != "" {
c, err := ReadConfigFile(opts.ConfigFile)
if err != nil {
return nil, err
}
cfg = c
}
ignore := make([]*regexp.Regexp, 0, len(opts.IgnorePatterns))
for _, s := range opts.IgnorePatterns {
r, err := regexp.Compile(s)
if err != nil {
return nil, fmt.Errorf("invalid regular expression for ignore pattern %q: %s", s, err.Error())
}
ignore = append(ignore, r)
}
var formatter *ErrorFormatter
if opts.Format != "" {
f, err := NewErrorFormatter(opts.Format)
if err != nil {
return nil, err
}
formatter = f
}
cwd := opts.WorkingDir
if cwd == "" {
if d, err := os.Getwd(); err == nil {
cwd = d
}
}
return &Linter{
NewProjects(),
out,
lout,
level,
opts.Oneline,
opts.Shellcheck,
opts.Pyflakes,
ignore,
cfg,
formatter,
cwd,
opts.OnRulesCreated,
}, nil
}
func (l *Linter) log(args ...interface{}) {
if l.logLevel < LogLevelVerbose {
return
}
fmt.Fprint(l.logOut, "verbose: ")
fmt.Fprintln(l.logOut, args...)
}
func (l *Linter) debug(format string, args ...interface{}) {
if l.logLevel < LogLevelDebug {
return
}
format = fmt.Sprintf("[Linter] %s\n", format)
fmt.Fprintf(l.logOut, format, args...)
}
func (l *Linter) debugWriter() io.Writer {
if l.logLevel < LogLevelDebug {
return nil
}
return l.logOut
}
// GenerateDefaultConfig generates default config file at ".github/actionlint.yaml" in project
// which the given directory path belongs to.
func (l *Linter) GenerateDefaultConfig(dir string) error {
l.log("Generating default actionlint.yaml in repository:", dir)
p, err := l.projects.At(dir)
if err != nil {
return err
}
if p == nil {
return errors.New("project is not found. check current project is initialized as Git repository and \".github/workflows\" directory exists")
}
path := filepath.Join(p.RootDir(), ".github", "actionlint.yaml")
if _, err := os.Stat(path); err == nil {
return fmt.Errorf("config file already exists at %q", path)
}
if err := writeDefaultConfigFile(path); err != nil {
return err
}
fmt.Fprintf(l.out, "Config file was generated at %q\n", path)
return nil
}
// LintRepository lints YAML workflow files and outputs the errors to given writer. It finds the nearest
// `.github/workflows` directory based on `dir` and applies lint rules to all YAML workflow files
// under the directory.
func (l *Linter) LintRepository(dir string) ([]*Error, error) {
l.log("Linting all workflow files in repository:", dir)
p, err := l.projects.At(dir)
if err != nil {
return nil, err
}
if p == nil {
return nil, fmt.Errorf("no project was found in any parent directories of %q. check workflows directory is put correctly in your Git repository", dir)
}
l.log("Detected project:", p.RootDir())
wd := p.WorkflowsDir()
return l.LintDir(wd, p)
}
// LintDir lints all YAML workflow files in the given directory recursively.
func (l *Linter) LintDir(dir string, project *Project) ([]*Error, error) {
files := []string{}
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".yml") || strings.HasSuffix(path, ".yaml") {
files = append(files, path)
}
return nil
}); err != nil {
return nil, fmt.Errorf("could not read files in %q: %w", dir, err)
}
if len(files) == 0 {
return nil, fmt.Errorf("no YAML file was found in %q", dir)
}
l.log("Collected", len(files), "YAML files")
// To make output deterministic, sort order of file paths
sort.Strings(files)
return l.LintFiles(files, project)
}
// LintFiles lints YAML workflow files and outputs the errors to given writer. It applies lint
// rules to all given files. The project parameter can be nil. In the case, a project is detected
// from the file path.
func (l *Linter) LintFiles(filepaths []string, project *Project) ([]*Error, error) {
n := len(filepaths)
switch n {
case 0:
return []*Error{}, nil
case 1:
return l.LintFile(filepaths[0], project)
}
l.log("Linting", n, "files")
cwd := l.cwd
proc := newConcurrentProcess(runtime.NumCPU())
sema := semaphore.NewWeighted(int64(runtime.NumCPU()))
ctx := context.Background()
dbg := l.debugWriter()
acf := NewLocalActionsCacheFactory(dbg)
rwcf := NewLocalReusableWorkflowCacheFactory(cwd, dbg)
type workspace struct {
path string
errs []*Error
src []byte
}
ws := make([]workspace, 0, len(filepaths))
for _, p := range filepaths {
ws = append(ws, workspace{path: p})
}
eg := errgroup.Group{}
for i := range ws {
// Each element of ws is accessed by single goroutine so mutex is unnecessary
w := &ws[i]
proj := project
if proj == nil {
// This method modifies state of l.projects so it cannot be called in parallel.
// Before entering goroutine, resolve project instance.
p, err := l.projects.At(w.path)
if err != nil {
return nil, err
}
proj = p
}
ac := acf.GetCache(proj) // #173
rwc := rwcf.GetCache(proj)
eg.Go(func() error {
// Bound concurrency on reading files to avoid "too many files to open" error (issue #3)
sema.Acquire(ctx, 1)
src, err := os.ReadFile(w.path)
sema.Release(1)
if err != nil {
return fmt.Errorf("could not read %q: %w", w.path, err)
}
if cwd != "" {
if r, err := filepath.Rel(cwd, w.path); err == nil {
w.path = r // Use relative path if possible
}
}
errs, err := l.check(w.path, src, proj, proc, ac, rwc)
if err != nil {
return fmt.Errorf("fatal error while checking %s: %w", w.path, err)
}
w.src = src
w.errs = errs
return nil
})
}
proc.wait()
if err := eg.Wait(); err != nil {
return nil, err
}
total := 0
for i := range ws {
total += len(ws[i].errs)
}
all := make([]*Error, 0, total)
if l.errFmt != nil {
temp := make([]*ErrorTemplateFields, 0, total)
for i := range ws {
w := &ws[i]
for _, err := range w.errs {
temp = append(temp, err.GetTemplateFields(w.src))
}
all = append(all, w.errs...)
}
if err := l.errFmt.Print(l.out, temp); err != nil {
return nil, err
}
} else {
for i := range ws {
w := &ws[i]
l.printErrors(w.errs, w.src)
all = append(all, w.errs...)
}
}
l.log("Found", total, "errors in", n, "files")
return all, nil
}
// LintFile lints one YAML workflow file and outputs the errors to given writer. The project
// parameter can be nil. In the case, the project is detected from the given path.
func (l *Linter) LintFile(path string, project *Project) ([]*Error, error) {
if project == nil {
p, err := l.projects.At(path)
if err != nil {
return nil, err
}
project = p
}
src, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read %q: %w", path, err)
}
if l.cwd != "" {
if r, err := filepath.Rel(l.cwd, path); err == nil {
path = r
}
}
proc := newConcurrentProcess(runtime.NumCPU())
dbg := l.debugWriter()
localActions := NewLocalActionsCache(project, dbg)
localReusableWorkflows := NewLocalReusableWorkflowCache(project, l.cwd, dbg)
errs, err := l.check(path, src, project, proc, localActions, localReusableWorkflows)
proc.wait()
if err != nil {
return nil, err
}
if l.errFmt != nil {
l.errFmt.PrintErrors(l.out, errs, src)
} else {
l.printErrors(errs, src)
}
return errs, err
}
// Lint lints YAML workflow file content given as byte slice. The path parameter is used as file
// path where the content came from. Setting "<stdin>" to path parameter indicates the output came
// from STDIN.
// When nil is passed to the project parameter, it tries to find the project from the path parameter.
func (l *Linter) Lint(path string, content []byte, project *Project) ([]*Error, error) {
if project == nil && path != "<stdin>" {
if _, err := os.Stat(path); !errors.Is(err, fs.ErrNotExist) {
p, err := l.projects.At(path)
if err != nil {
return nil, err
}
project = p
}
}
proc := newConcurrentProcess(runtime.NumCPU())
dbg := l.debugWriter()
localActions := NewLocalActionsCache(project, dbg)
localReusableWorkflows := NewLocalReusableWorkflowCache(project, l.cwd, dbg)
errs, err := l.check(path, content, project, proc, localActions, localReusableWorkflows)
proc.wait()
if err != nil {
return nil, err
}
if l.errFmt != nil {
l.errFmt.PrintErrors(l.out, errs, content)
} else {
l.printErrors(errs, content)
}
return errs, nil
}
func (l *Linter) check(
path string,
content []byte,
project *Project,
proc *concurrentProcess,
localActions *LocalActionsCache,
localReusableWorkflows *LocalReusableWorkflowCache,
) ([]*Error, error) {
// Note: This method is called to check multiple files in parallel.
// It must be thread safe assuming fields of Linter are not modified while running.
var start time.Time
if l.logLevel >= LogLevelVerbose {
start = time.Now()
}
l.log("Linting", path)
if project != nil {
l.log("Using project at", project.RootDir())
}
var cfg *Config
if l.defaultConfig != nil {
// `-config-file` option has higher prioritiy than repository config file
cfg = l.defaultConfig
} else if project != nil {
cfg = project.Config()
}
if cfg != nil {
l.debug("Config: %#v", cfg)
} else {
l.debug("No config was found")
}
w, all := Parse(content)
if l.logLevel >= LogLevelVerbose {
elapsed := time.Since(start)
l.log("Found", len(all), "parse errors in", elapsed.Milliseconds(), "ms for", path)
}
if w != nil {
dbg := l.debugWriter()
rules := []Rule{
NewRuleMatrix(),
NewRuleCredentials(),
NewRuleShellName(),
NewRuleRunnerLabel(),
NewRuleEvents(),
NewRuleJobNeeds(),
NewRuleAction(localActions),
NewRuleEnvVar(),
NewRuleID(),
NewRuleGlob(),
NewRulePermissions(),
NewRuleWorkflowCall(path, localReusableWorkflows),
NewRuleExpression(localActions, localReusableWorkflows),
NewRuleDeprecatedCommands(),
NewRuleIfCond(),
}
if l.shellcheck != "" {
r, err := NewRuleShellcheck(l.shellcheck, proc)
if err == nil {
rules = append(rules, r)
} else {
l.log("Rule \"shellcheck\" was disabled:", err)
}
} else {
l.log("Rule \"shellcheck\" was disabled since shellcheck command name was empty")
}
if l.pyflakes != "" {
r, err := NewRulePyflakes(l.pyflakes, proc)
if err == nil {
rules = append(rules, r)
} else {
l.log("Rule \"pyflakes\" was disabled:", err)
}
} else {
l.log("Rule \"pyflakes\" was disabled since pyflakes command name was empty")
}
if l.onRulesCreated != nil {
rules = l.onRulesCreated(rules)
}
v := NewVisitor()
for _, rule := range rules {
v.AddPass(rule)
}
if dbg != nil {
v.EnableDebug(dbg)
for _, r := range rules {
r.EnableDebug(dbg)
}
}
if cfg != nil {
for _, r := range rules {
r.SetConfig(cfg)
}
}
if err := v.Visit(w); err != nil {
l.debug("error occurred while visiting workflow syntax tree: %v", err)
return nil, err
}
for _, rule := range rules {
errs := rule.Errs()
l.debug("%s found %d errors", rule.Name(), len(errs))
all = append(all, errs...)
}
if l.errFmt != nil {
for _, rule := range rules {
l.errFmt.RegisterRule(rule)
}
}
}
if len(l.ignorePats) > 0 {
filtered := make([]*Error, 0, len(all))
Loop:
for _, err := range all {
for _, pat := range l.ignorePats {
if pat.MatchString(err.Message) {
continue Loop
}
}
filtered = append(filtered, err)
}
all = filtered
}
for _, err := range all {
err.Filepath = path // Populate filename in the error
}
sort.Stable(ByErrorPosition(all))
if l.logLevel >= LogLevelVerbose {
elapsed := time.Since(start)
l.log("Found total", len(all), "errors in", elapsed.Milliseconds(), "ms for", path)
}
return all, nil
}
func (l *Linter) printErrors(errs []*Error, src []byte) {
if l.oneline {
src = nil
}
for _, err := range errs {
err.PrettyPrint(l.out, src)
}
}