internal/control/deps.go
206 lines · 6987 bytes
1package control
2
3import (
4 "errors"
5 "fmt"
6 "io"
7 "strconv"
8
9 "gitbay.org/gitbay/internal/policy"
10 "gitbay.org/gitbay/internal/protocol"
11 "gitbay.org/gitbay/internal/store"
12)
13
14func init() {
15 // Dependency update and advisory checks. Opt-in per repository: the
16 // check tells a public registry and OSV what the repository depends on,
17 // which is the owner's disclosure to make, not the instance's.
18 register(Command{Path: []string{"repo", "deps", "enable"},
19 Summary: "check dependencies for updates and advisories",
20 Usage: "repo deps enable <owner/name>",
21 Examples: []string{"repo deps enable krz/gitbay"},
22 Run: runDepsEnable})
23 register(Command{Path: []string{"repo", "deps", "disable"},
24 Summary: "stop checking dependencies",
25 Usage: "repo deps disable <owner/name>",
26 Examples: []string{"repo deps disable krz/gitbay"},
27 Run: runDepsDisable})
28 register(Command{Path: []string{"repo", "deps", "status"},
29 Summary: "show dependency check state",
30 Usage: "repo deps status <owner/name>",
31 Examples: []string{"repo deps status krz/gitbay"},
32 ReadOnly: true, Run: runDepsStatus})
33}
34
35// DepsOut is what `repo deps status` emits: the check's state and what
36// the last run found behind or matching an advisory. Named so the web
37// decodes the same struct the command writes.
38type DepsOut struct {
39 Enabled bool `json:"enabled"`
40 LastCheck string `json:"last_check,omitempty"`
41 LastError string `json:"last_error,omitempty"`
42 IssueNumber int64 `json:"issue_number,omitempty"`
43 AdvisoryIssueNumber int64 `json:"advisory_issue_number,omitempty"`
44 Behind []DepBehind `json:"behind"`
45 Advisories []DepAdvisory `json:"advisories"`
46}
47
48// DepBehind is one dependency with a newer release than the manifest pins.
49type DepBehind struct {
50 Ecosystem string `json:"ecosystem"`
51 Name string `json:"name"`
52 Current string `json:"current"`
53 Latest string `json:"latest"`
54}
55
56// DepAdvisory is one published advisory matching a resolved dependency.
57type DepAdvisory struct {
58 Ecosystem string `json:"ecosystem"`
59 Name string `json:"name"`
60 Version string `json:"version"`
61 ID string `json:"id"`
62 Summary string `json:"summary"`
63 Severity string `json:"severity,omitempty"`
64 Fixed string `json:"fixed,omitempty"`
65}
66
67func runDepsEnable(c *Ctx, args []string) int {
68 if len(args) != 1 {
69 return c.usage()
70 }
71 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
72 if code >= 0 {
73 return code
74 }
75 if err := c.Store.EnableDepCheck(repo.ID); err != nil {
76 return c.fail(protocol.ExitFailure, "%v", err)
77 }
78 return c.emit(map[string]any{"enabled": true}, func(w io.Writer) {
79 fmt.Fprintf(w, "dependency checks enabled on %s\n", repo.Path())
80 })
81}
82
83func runDepsDisable(c *Ctx, args []string) int {
84 if len(args) != 1 {
85 return c.usage()
86 }
87 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
88 if code >= 0 {
89 return code
90 }
91 if err := c.Store.DisableDepCheck(repo.ID); err != nil {
92 return c.fail(protocol.ExitFailure, "%v", err)
93 }
94 return c.emit(map[string]any{"enabled": false}, func(w io.Writer) {
95 fmt.Fprintf(w, "dependency checks disabled on %s\n", repo.Path())
96 })
97}
98
99func runDepsStatus(c *Ctx, args []string) int {
100 if len(args) != 1 {
101 return c.usage()
102 }
103 repo, code := resolveRepo(c, args[0], policy.CanRead)
104 if code >= 0 {
105 return code
106 }
107 check, err := c.Store.DepCheckFor(repo.ID)
108 if errors.Is(err, store.ErrNotFound) {
109 return c.emitView(map[string]any{"enabled": false}, func(w io.Writer) {
110 v := c.view(w)
111 v.fields("checks", fmt.Sprintf("off (repo deps enable %s)", repo.Path()))
112 }, func() screen {
113 return screen{
114 fields: []field{{"Checks", []cell{cText("off")}}},
115 actions: []action{{"Deps", []string{"repo", "deps", "enable", repo.Path()}}},
116 }
117 })
118 }
119 if err != nil {
120 return c.fail(protocol.ExitFailure, "%v", err)
121 }
122 reports, err := c.Store.ReportedDeps(repo.ID)
123 if err != nil {
124 return c.fail(protocol.ExitFailure, "%v", err)
125 }
126 advisories, err := c.Store.ReportedAdvisories(repo.ID)
127 if err != nil {
128 return c.fail(protocol.ExitFailure, "%v", err)
129 }
130 out := DepsOut{Enabled: true, LastCheck: check.LastCheck, LastError: check.LastError,
131 IssueNumber: check.IssueNumber, AdvisoryIssueNumber: check.AdvisoryIssueNumber,
132 Behind: []DepBehind{}, Advisories: []DepAdvisory{}}
133 for _, r := range reports {
134 out.Behind = append(out.Behind, DepBehind{r.Ecosystem, r.Name, r.Current, r.Latest})
135 }
136 for _, a := range advisories {
137 out.Advisories = append(out.Advisories,
138 DepAdvisory{a.Ecosystem, a.Name, a.Version, a.ID, a.Summary, a.Severity, a.Fixed})
139 }
140 return c.emitView(out, func(w io.Writer) {
141 tracked, advised := "", ""
142 if check.IssueNumber != 0 {
143 tracked = fmt.Sprintf("#%d", check.IssueNumber)
144 }
145 if check.AdvisoryIssueNumber != 0 {
146 advised = fmt.Sprintf("#%d", check.AdvisoryIssueNumber)
147 }
148 v := c.view(w)
149 v.fields(
150 "checks", "on",
151 "last check", c.when(check.LastCheck),
152 "last error", check.LastError,
153 "tracked in", tracked,
154 "advisories in", advised,
155 )
156 if len(out.Behind) > 0 {
157 v.section("behind")
158 tb := c.table(w, "ECOSYSTEM", "NAME", "CURRENT", "LATEST")
159 for _, b := range out.Behind {
160 tb.row(cText(b.Ecosystem), cRef(b.Name), cText(b.Current), cText(b.Latest))
161 }
162 tb.flush()
163 }
164 if len(out.Advisories) > 0 {
165 v.section("advisories")
166 tb := c.table(w, "ECOSYSTEM", "NAME", "VERSION", "ADVISORY", "FIXED")
167 for _, a := range out.Advisories {
168 tb.row(cText(a.Ecosystem), cRef(a.Name), cText(a.Version), cText(a.ID), cText(a.Fixed))
169 }
170 tb.flush()
171 }
172 }, func() screen {
173 state := []cell{cGlyph("ok"), cText("on")}
174 if check.LastError != "" {
175 state = []cell{cGlyph("failed"), cText("on")}
176 }
177 s := screen{fields: []field{
178 {"Checks", state},
179 {"Last check", []cell{cAge(check.LastCheck)}},
180 {"Error", []cell{cMark(check.LastError, sgrRed)}},
181 }}
182 if check.IssueNumber != 0 {
183 n := strconv.FormatInt(check.IssueNumber, 10)
184 s.fields = append(s.fields, field{"Tracked in", []cell{cLink("#"+n, c.siteURL(repo.Path(), "issues", n))}})
185 }
186 if check.AdvisoryIssueNumber != 0 {
187 n := strconv.FormatInt(check.AdvisoryIssueNumber, 10)
188 s.fields = append(s.fields, field{"Advisories in", []cell{cLink("#"+n, c.siteURL(repo.Path(), "issues", n))}})
189 }
190 behind := section{title: "Behind", n: len(out.Behind)}
191 for _, b := range out.Behind {
192 behind.rows = append(behind.rows, rowOf(cRef(b.Name), cText(b.Ecosystem), cMeta(b.Current+" → "+b.Latest)))
193 }
194 advised := section{title: "Advisories", n: len(out.Advisories)}
195 for _, a := range out.Advisories {
196 fix := a.Version
197 if a.Fixed != "" {
198 fix += " → " + a.Fixed
199 }
200 advised.rows = append(advised.rows, rowOf(cRef(a.Name), cMark(a.ID, sgrRed), cMeta(fix)))
201 }
202 s.sections = []section{behind, advised}
203 s.actions = []action{{"Deps", []string{"repo", "deps", "disable", repo.Path()}}}
204 return s
205 })
206}