|
| 1 | +package formatter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "maps" |
| 10 | + "slices" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + |
| 14 | + "github.com/sethvargo/ratchet/linter" |
| 15 | +) |
| 16 | + |
| 17 | +type Violation = linter.Violation |
| 18 | + |
| 19 | +type Formatter interface { |
| 20 | + Format(io.Writer, []*Violation) error |
| 21 | +} |
| 22 | + |
| 23 | +var formatterFactory = map[string]Formatter{ |
| 24 | + "actions": FormatterFunc(formatActions), |
| 25 | + "human": FormatterFunc(formatHuman), |
| 26 | + "json": FormatterFunc(formatJSON), |
| 27 | + "lsp": FormatterFunc(formatLSP), |
| 28 | + "null": FormatterFunc(formatNull), |
| 29 | +} |
| 30 | + |
| 31 | +var formatters = sync.OnceValue(func() []string { |
| 32 | + return slices.Sorted(maps.Keys(formatterFactory)) |
| 33 | +}) |
| 34 | + |
| 35 | +// For returns the parser that corresponds to the given name. |
| 36 | +func For(ctx context.Context, name string) (Formatter, error) { |
| 37 | + typ := strings.ToLower(strings.TrimSpace(name)) |
| 38 | + if v, ok := formatterFactory[typ]; ok { |
| 39 | + return v, nil |
| 40 | + } |
| 41 | + return nil, fmt.Errorf("unknown formatter %q, valid formatters are %q", |
| 42 | + typ, List()) |
| 43 | +} |
| 44 | + |
| 45 | +// List returns the list of parsers. |
| 46 | +func List() []string { |
| 47 | + return formatters() |
| 48 | +} |
| 49 | + |
| 50 | +// FormatterFunc is a function that implements the [Formatter] interface. |
| 51 | +type FormatterFunc func(io.Writer, []*Violation) error |
| 52 | + |
| 53 | +// Format implements the [Formatter] interface. |
| 54 | +func (f FormatterFunc) Format(w io.Writer, v []*Violation) error { |
| 55 | + return f(w, v) |
| 56 | +} |
| 57 | + |
| 58 | +// formatActions formats in GitHub Actions error output, which will also be |
| 59 | +// annotated in the UI. |
| 60 | +func formatActions(w io.Writer, violations []*Violation) error { |
| 61 | + var merr error |
| 62 | + for _, v := range violations { |
| 63 | + message := fmt.Sprintf("The reference `%s` is unpinned. Either pin the reference to a SHA or mark the line with `ratchet:exclude`.", v.Contents) |
| 64 | + if _, err := fmt.Fprintf(w, "::error file=%s,line=%d,col=%d,title=Ratchet - Unpinned Reference::%s\n", |
| 65 | + v.Filename, v.Line, v.Column, |
| 66 | + message); err != nil { |
| 67 | + merr = errors.Join(merr, err) |
| 68 | + } |
| 69 | + } |
| 70 | + return merr |
| 71 | +} |
| 72 | + |
| 73 | +// formatHuman reports a human-friendly output format. |
| 74 | +// |
| 75 | +// <path>:<line>:<column>: <message> |
| 76 | +// |
| 77 | +// For example: |
| 78 | +// |
| 79 | +// .github/workflows/test.yml:37:8: Unpinned reference "actions/checkout@v4" |
| 80 | +func formatHuman(w io.Writer, violations []*Violation) error { |
| 81 | + var merr error |
| 82 | + |
| 83 | + for _, v := range violations { |
| 84 | + if _, err := fmt.Fprintf(w, "%s:%d:%d: Unpinned reference %q\n", |
| 85 | + v.Filename, v.Line, v.Column, v.Contents); err != nil { |
| 86 | + merr = errors.Join(merr, err) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if len(violations) > 0 { |
| 91 | + if _, err := fmt.Fprintf(w, "\n❌ found %d violation(s)\n", |
| 92 | + len(violations)); err != nil { |
| 93 | + merr = errors.Join(merr, err) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return merr |
| 98 | +} |
| 99 | + |
| 100 | +// formatNull produces no output. |
| 101 | +func formatNull(w io.Writer, violations []*Violation) error { |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// formatJSON formats in JSON output. |
| 106 | +func formatJSON(w io.Writer, violations []*Violation) error { |
| 107 | + type InternalJSON struct { |
| 108 | + Filename string `json:"filename,omitempty"` |
| 109 | + Contents string `json:"contents,omitempty"` |
| 110 | + Line int `json:"line,omitempty"` |
| 111 | + Column int `json:"column,omitempty"` |
| 112 | + } |
| 113 | + |
| 114 | + list := make([]*InternalJSON, 0, len(violations)) |
| 115 | + for _, v := range violations { |
| 116 | + list = append(list, &InternalJSON{ |
| 117 | + Filename: v.Filename, |
| 118 | + Contents: v.Contents, |
| 119 | + Line: v.Line, |
| 120 | + Column: v.Column, |
| 121 | + }) |
| 122 | + } |
| 123 | + |
| 124 | + return json.NewEncoder(w).Encode(list) |
| 125 | +} |
| 126 | + |
| 127 | +// formatLSP formats a JSON response that is compatible with the Language Server |
| 128 | +// Protocol. This is useful for surfacing findings in an IDE that uses an LSP. |
| 129 | +func formatLSP(w io.Writer, violations []*Violation) error { |
| 130 | + type Position struct { |
| 131 | + Line int `json:"line,omitempty"` |
| 132 | + Character int `json:"character,omitempty"` |
| 133 | + } |
| 134 | + |
| 135 | + type Range struct { |
| 136 | + Start *Position `json:"start,omitempty"` |
| 137 | + End *Position `json:"end,omitempty"` |
| 138 | + } |
| 139 | + |
| 140 | + type InternalJSON struct { |
| 141 | + Message string `json:"message,omitempty"` |
| 142 | + Code string `json:"code,omitempty"` |
| 143 | + Severity string `json:"severity,omitempty"` |
| 144 | + Range *Range `json:"range,omitempty"` |
| 145 | + } |
| 146 | + |
| 147 | + list := make([]*InternalJSON, 0, len(violations)) |
| 148 | + for _, v := range violations { |
| 149 | + list = append(list, &InternalJSON{ |
| 150 | + Message: "Reference is unpinned", |
| 151 | + Code: "unpinned", |
| 152 | + Severity: "Error", |
| 153 | + Range: &Range{ |
| 154 | + Start: &Position{ |
| 155 | + Line: v.Line, |
| 156 | + Character: v.Column, |
| 157 | + }, |
| 158 | + End: &Position{ |
| 159 | + Line: v.Line, |
| 160 | + Character: v.Column + len(v.Contents), |
| 161 | + }, |
| 162 | + }, |
| 163 | + }) |
| 164 | + } |
| 165 | + |
| 166 | + return json.NewEncoder(w).Encode(list) |
| 167 | +} |
0 commit comments