package control import ( "errors" "fmt" "io" "strconv" "gitbay.org/gitbay/internal/policy" "gitbay.org/gitbay/internal/protocol" "gitbay.org/gitbay/internal/store" ) func init() { // Dependency update and advisory checks. Opt-in per repository: the // check tells a public registry and OSV what the repository depends on, // which is the owner's disclosure to make, not the instance's. register(Command{Path: []string{"repo", "deps", "enable"}, Summary: "check dependencies for updates and advisories", Usage: "repo deps enable ", Examples: []string{"repo deps enable krz/gitbay"}, Run: runDepsEnable}) register(Command{Path: []string{"repo", "deps", "disable"}, Summary: "stop checking dependencies", Usage: "repo deps disable ", Examples: []string{"repo deps disable krz/gitbay"}, Run: runDepsDisable}) register(Command{Path: []string{"repo", "deps", "status"}, Summary: "show dependency check state", Usage: "repo deps status ", Examples: []string{"repo deps status krz/gitbay"}, ReadOnly: true, Run: runDepsStatus}) } // DepsOut is what `repo deps status` emits: the check's state and what // the last run found behind or matching an advisory. Named so the web // decodes the same struct the command writes. type DepsOut struct { Enabled bool `json:"enabled"` LastCheck string `json:"last_check,omitempty"` LastError string `json:"last_error,omitempty"` IssueNumber int64 `json:"issue_number,omitempty"` AdvisoryIssueNumber int64 `json:"advisory_issue_number,omitempty"` Behind []DepBehind `json:"behind"` Advisories []DepAdvisory `json:"advisories"` } // DepBehind is one dependency with a newer release than the manifest pins. type DepBehind struct { Ecosystem string `json:"ecosystem"` Name string `json:"name"` Current string `json:"current"` Latest string `json:"latest"` } // DepAdvisory is one published advisory matching a resolved dependency. type DepAdvisory struct { Ecosystem string `json:"ecosystem"` Name string `json:"name"` Version string `json:"version"` ID string `json:"id"` Summary string `json:"summary"` Severity string `json:"severity,omitempty"` Fixed string `json:"fixed,omitempty"` } func runDepsEnable(c *Ctx, args []string) int { if len(args) != 1 { return c.usage() } repo, code := resolveRepo(c, args[0], policy.CanAdmin) if code >= 0 { return code } if err := c.Store.EnableDepCheck(repo.ID); err != nil { return c.fail(protocol.ExitFailure, "%v", err) } return c.emit(map[string]any{"enabled": true}, func(w io.Writer) { fmt.Fprintf(w, "dependency checks enabled on %s\n", repo.Path()) }) } func runDepsDisable(c *Ctx, args []string) int { if len(args) != 1 { return c.usage() } repo, code := resolveRepo(c, args[0], policy.CanAdmin) if code >= 0 { return code } if err := c.Store.DisableDepCheck(repo.ID); err != nil { return c.fail(protocol.ExitFailure, "%v", err) } return c.emit(map[string]any{"enabled": false}, func(w io.Writer) { fmt.Fprintf(w, "dependency checks disabled on %s\n", repo.Path()) }) } func runDepsStatus(c *Ctx, args []string) int { if len(args) != 1 { return c.usage() } repo, code := resolveRepo(c, args[0], policy.CanRead) if code >= 0 { return code } check, err := c.Store.DepCheckFor(repo.ID) if errors.Is(err, store.ErrNotFound) { return c.emitView(map[string]any{"enabled": false}, func(w io.Writer) { v := c.view(w) v.fields("checks", fmt.Sprintf("off (repo deps enable %s)", repo.Path())) }, func() screen { return screen{ fields: []field{{"Checks", []cell{cText("off")}}}, actions: []action{{"Deps", []string{"repo", "deps", "enable", repo.Path()}}}, } }) } if err != nil { return c.fail(protocol.ExitFailure, "%v", err) } reports, err := c.Store.ReportedDeps(repo.ID) if err != nil { return c.fail(protocol.ExitFailure, "%v", err) } advisories, err := c.Store.ReportedAdvisories(repo.ID) if err != nil { return c.fail(protocol.ExitFailure, "%v", err) } out := DepsOut{Enabled: true, LastCheck: check.LastCheck, LastError: check.LastError, IssueNumber: check.IssueNumber, AdvisoryIssueNumber: check.AdvisoryIssueNumber, Behind: []DepBehind{}, Advisories: []DepAdvisory{}} for _, r := range reports { out.Behind = append(out.Behind, DepBehind{r.Ecosystem, r.Name, r.Current, r.Latest}) } for _, a := range advisories { out.Advisories = append(out.Advisories, DepAdvisory{a.Ecosystem, a.Name, a.Version, a.ID, a.Summary, a.Severity, a.Fixed}) } return c.emitView(out, func(w io.Writer) { tracked, advised := "", "" if check.IssueNumber != 0 { tracked = fmt.Sprintf("#%d", check.IssueNumber) } if check.AdvisoryIssueNumber != 0 { advised = fmt.Sprintf("#%d", check.AdvisoryIssueNumber) } v := c.view(w) v.fields( "checks", "on", "last check", c.when(check.LastCheck), "last error", check.LastError, "tracked in", tracked, "advisories in", advised, ) if len(out.Behind) > 0 { v.section("behind") tb := c.table(w, "ECOSYSTEM", "NAME", "CURRENT", "LATEST") for _, b := range out.Behind { tb.row(cText(b.Ecosystem), cRef(b.Name), cText(b.Current), cText(b.Latest)) } tb.flush() } if len(out.Advisories) > 0 { v.section("advisories") tb := c.table(w, "ECOSYSTEM", "NAME", "VERSION", "ADVISORY", "FIXED") for _, a := range out.Advisories { tb.row(cText(a.Ecosystem), cRef(a.Name), cText(a.Version), cText(a.ID), cText(a.Fixed)) } tb.flush() } }, func() screen { state := []cell{cGlyph("ok"), cText("on")} if check.LastError != "" { state = []cell{cGlyph("failed"), cText("on")} } s := screen{fields: []field{ {"Checks", state}, {"Last check", []cell{cAge(check.LastCheck)}}, {"Error", []cell{cMark(check.LastError, sgrRed)}}, }} if check.IssueNumber != 0 { n := strconv.FormatInt(check.IssueNumber, 10) s.fields = append(s.fields, field{"Tracked in", []cell{cLink("#"+n, c.siteURL(repo.Path(), "issues", n))}}) } if check.AdvisoryIssueNumber != 0 { n := strconv.FormatInt(check.AdvisoryIssueNumber, 10) s.fields = append(s.fields, field{"Advisories in", []cell{cLink("#"+n, c.siteURL(repo.Path(), "issues", n))}}) } behind := section{title: "Behind", n: len(out.Behind)} for _, b := range out.Behind { behind.rows = append(behind.rows, rowOf(cRef(b.Name), cText(b.Ecosystem), cMeta(b.Current+" → "+b.Latest))) } advised := section{title: "Advisories", n: len(out.Advisories)} for _, a := range out.Advisories { fix := a.Version if a.Fixed != "" { fix += " → " + a.Fixed } advised.rows = append(advised.rows, rowOf(cRef(a.Name), cMark(a.ID, sgrRed), cMeta(fix))) } s.sections = []section{behind, advised} s.actions = []action{{"Deps", []string{"repo", "deps", "disable", repo.Path()}}} return s }) }