internal/deps/report.go

153 lines · 4505 bytes

  1package deps
  2
  3import (
  4	"fmt"
  5	"sort"
  6	"strings"
  7
  8	"gitbay.org/gitbay/internal/store"
  9)
 10
 11// IssueTitle is fixed so the issue this worker maintains is recognizable
 12// across sweeps, in listings, and to the maintainer.
 13const IssueTitle = "Dependency updates available"
 14
 15// AdvisoryTitle is the advisories issue's, fixed for the same reason.
 16const AdvisoryTitle = "Known vulnerabilities in dependencies"
 17
 18// ecosystemNames are how the four appear in the issue body.
 19var ecosystemNames = map[string]string{
 20	EcoGo:    "Go",
 21	EcoNPM:   "npm",
 22	EcoCargo: "Cargo",
 23	EcoPyPI:  "PyPI",
 24	EcoSwift: "Swift",
 25}
 26
 27// order is the sequence sections appear in.
 28var order = []string{EcoGo, EcoNPM, EcoCargo, EcoPyPI, EcoSwift}
 29
 30// Body renders the issue: one table per ecosystem, sorted, so a diff
 31// between sweeps reads as a diff of what is behind.
 32func Body(branch string, reports []store.DepReport) string {
 33	var b strings.Builder
 34	fmt.Fprintf(&b, "Dependencies declared on `%s` are behind their latest release. "+
 35		"This issue is kept up to date as that changes, and closed once nothing is behind.\n", branch)
 36	byEco := map[string][]store.DepReport{}
 37	for _, r := range reports {
 38		byEco[r.Ecosystem] = append(byEco[r.Ecosystem], r)
 39	}
 40	for _, eco := range order {
 41		rows := byEco[eco]
 42		if len(rows) == 0 {
 43			continue
 44		}
 45		sort.Slice(rows, func(i, j int) bool { return rows[i].Name < rows[j].Name })
 46		fmt.Fprintf(&b, "\n### %s\n\n", ecosystemNames[eco])
 47		cells := [][]string{{"Package", "Current", "Latest"}}
 48		for _, r := range rows {
 49			cells = append(cells, []string{"`" + r.Name + "`", r.Current, r.Latest})
 50		}
 51		b.WriteString(table(cells))
 52	}
 53	return b.String()
 54}
 55
 56// AdvisoryBody renders the advisories issue: a table per ecosystem, then
 57// each advisory's summary and link, which would make the table too wide to
 58// read as plain text.
 59func AdvisoryBody(branch string, matched []store.DepAdvisory) string {
 60	var b strings.Builder
 61	fmt.Fprintf(&b, "Dependencies resolved on `%s` match published advisories. "+
 62		"This issue is kept up to date as that changes, and closed once none match.\n", branch)
 63	byEco := map[string][]store.DepAdvisory{}
 64	for _, a := range matched {
 65		byEco[a.Ecosystem] = append(byEco[a.Ecosystem], a)
 66	}
 67	for _, eco := range order {
 68		rows := byEco[eco]
 69		if len(rows) == 0 {
 70			continue
 71		}
 72		fmt.Fprintf(&b, "\n### %s\n\n", ecosystemNames[eco])
 73		cells := [][]string{{"Package", "Version", "Advisory", "Severity", "Fixed in"}}
 74		for _, a := range rows {
 75			cells = append(cells, []string{"`" + a.Name + "`", a.Version, a.ID, a.Severity, a.Fixed})
 76		}
 77		b.WriteString(table(cells))
 78	}
 79	b.WriteString("\n### Advisories\n\n")
 80	seen := map[string]bool{}
 81	for _, a := range matched {
 82		if seen[a.ID] {
 83			continue
 84		}
 85		seen[a.ID] = true
 86		fmt.Fprintf(&b, "- %s: %s https://osv.dev/vulnerability/%s\n", a.ID, a.Summary, a.ID)
 87	}
 88	return b.String()
 89}
 90
 91// table pads every cell to its column, because the body is read twice: as
 92// a rendered table on the web, where the padding is ignored, and as the
 93// plain text of the notification mail, where it is the only thing keeping
 94// the columns readable. Row 0 is the header.
 95func table(cells [][]string) string {
 96	w := make([]int, len(cells[0]))
 97	for _, c := range cells {
 98		for i := range w {
 99			if n := len(c[i]); n > w[i] {
100				w[i] = n
101			}
102		}
103	}
104	var b strings.Builder
105	row := func(c []string) {
106		b.WriteString("|")
107		for i := range w {
108			fmt.Fprintf(&b, " %-*s |", w[i], c[i])
109		}
110		b.WriteString("\n")
111	}
112	row(cells[0])
113	rule := make([]string, len(w))
114	for i := range w {
115		rule[i] = strings.Repeat("-", w[i])
116	}
117	row(rule)
118	for _, c := range cells[1:] {
119		row(c)
120	}
121	return b.String()
122}
123
124// same reports whether two outdated sets are equal, which is what decides
125// between leaving the issue alone and rewriting it. Both sides arrive
126// sorted by (ecosystem, name).
127func same(a, b []store.DepReport) bool {
128	if len(a) != len(b) {
129		return false
130	}
131	for i := range a {
132		if a[i] != b[i] {
133			return false
134		}
135	}
136	return true
137}
138
139// sameAdvisories reports whether two matching sets name the same
140// advisories against the same versions. A summary or severity edited
141// upstream does not count as a change worth a notification.
142func sameAdvisories(a, b []store.DepAdvisory) bool {
143	if len(a) != len(b) {
144		return false
145	}
146	for i := range a {
147		if a[i].Ecosystem != b[i].Ecosystem || a[i].Name != b[i].Name ||
148			a[i].Version != b[i].Version || a[i].ID != b[i].ID || a[i].Fixed != b[i].Fixed {
149			return false
150		}
151	}
152	return true
153}