]> git.madduck.net Git - etc/vim.git/blob - ale_linters/yaml/actionlint.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / ale_linters / yaml / actionlint.vim
1 " Author: Peter Benjamin <petermbenjamin@gmail.com>
2 " Description: Linter for GitHub Workflows
3
4 call ale#Set('yaml_actionlint_executable', 'actionlint')
5 call ale#Set('yaml_actionlint_options', '')
6
7 function! ale_linters#yaml#actionlint#GetCommand(buffer) abort
8     " Only execute actionlint on YAML files in /.github/ paths.
9     if expand('#' . a:buffer . ':p') !~# '\v[/\\]\.github[/\\]'
10         return ''
11     endif
12
13     let l:options = ale#Var(a:buffer, 'yaml_actionlint_options')
14
15     if l:options !~# '-no-color'
16         let l:options .= ale#Pad('-no-color')
17     endif
18
19     if l:options !~# '-oneline'
20         let l:options .= ale#Pad('-oneline')
21     endif
22
23     let l:configfile = ale_linters#yaml#actionlint#GitRepoHasConfig(a:buffer)
24
25     if !empty(l:configfile)
26         let l:options .= ale#Pad('-config-file ' . l:configfile)
27     endif
28
29     return '%e' . ale#Pad(l:options) . ' - '
30 endfunction
31
32 " If we have a actionlint.yml or actionlint.yaml in our github directory
33 " use that as our config file.
34 function! ale_linters#yaml#actionlint#GitRepoHasConfig(buffer) abort
35     let l:filename = expand('#' . a:buffer . ':p')
36     let l:configfilebase = substitute(l:filename, '\.github/.*', '.github/actionlint.','')
37
38     for l:ext in ['yml', 'yaml']
39         let l:configfile = l:configfilebase . l:ext
40
41         if filereadable(l:configfile)
42             return l:configfile
43         endif
44     endfor
45
46     return ''
47 endfunction
48
49 function! ale_linters#yaml#actionlint#Handle(buffer, lines) abort
50     " Matches patterns line the following:
51     ".github/workflows/main.yml:19:0: could not parse as YAML: yaml: line 19: mapping values are not allowed in this context [yaml-syntax]
52     let l:pattern = '\v^.{-}:(\d+):(\d+): (.+) \[(.+)\]$'
53     let l:output = []
54
55
56     for l:match in ale#util#GetMatches(a:lines, l:pattern)
57         let l:code = l:match[4]
58         let l:text = l:match[3]
59
60         " Handle sub-linter errors like the following:
61         "validate.yml:19:9: shellcheck reported issue in this script: SC2086:info:1:15: Double quote to prevent globbing and word splitting [shellcheck]
62         if l:code is# 'shellcheck'
63             let l:shellcheck_match = matchlist(l:text, '\v^.+: (SC\d{4}):.+:\d+:\d+: (.+)$')
64             let l:text = l:shellcheck_match[2]
65             let l:code = 'shellcheck ' . l:shellcheck_match[1]
66         endif
67
68         let l:item = {
69         \   'lnum': l:match[1] + 0,
70         \   'col': l:match[2] + 0,
71         \   'text': l:text,
72         \   'code': l:code,
73         \   'type': 'E',
74         \}
75
76         call add(l:output, l:item)
77     endfor
78
79     return l:output
80 endfunction
81
82 call ale#linter#Define('yaml', {
83 \   'name': 'actionlint',
84 \   'executable': {b -> ale#Var(b, 'yaml_actionlint_executable')},
85 \   'command': function('ale_linters#yaml#actionlint#GetCommand'),
86 \   'callback': 'ale_linters#yaml#actionlint#Handle',
87 \})