internal/deps/report.go

dca7370cd168dc7cf1f262252ab9548f66cc7dcc
gitbay/internal/deps/report.go history · blame · raw

63 lines · 1802 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// ecosystemNames are how the four appear in the issue body.
16var ecosystemNames = map[string]string{
17	EcoGo:    "Go",
18	EcoNPM:   "npm",
19	EcoCargo: "Cargo",
20	EcoPyPI:  "PyPI",
21}
22
23// order is the sequence sections appear in.
24var order = []string{EcoGo, EcoNPM, EcoCargo, EcoPyPI}
25
26// Body renders the issue: one table per ecosystem, sorted, so a diff
27// between sweeps reads as a diff of what is behind.
28func Body(branch string, reports []store.DepReport) string {
29	var b strings.Builder
30	fmt.Fprintf(&b, "Dependencies declared on `%s` are behind their latest release. "+
31		"This issue is kept up to date as that changes, and closed once nothing is behind.\n", branch)
32	byEco := map[string][]store.DepReport{}
33	for _, r := range reports {
34		byEco[r.Ecosystem] = append(byEco[r.Ecosystem], r)
35	}
36	for _, eco := range order {
37		rows := byEco[eco]
38		if len(rows) == 0 {
39			continue
40		}
41		sort.Slice(rows, func(i, j int) bool { return rows[i].Name < rows[j].Name })
42		fmt.Fprintf(&b, "\n### %s\n\n| Package | Current | Latest |\n| --- | --- | --- |\n", ecosystemNames[eco])
43		for _, r := range rows {
44			fmt.Fprintf(&b, "| `%s` | %s | %s |\n", r.Name, r.Current, r.Latest)
45		}
46	}
47	return b.String()
48}
49
50// same reports whether two outdated sets are equal, which is what decides
51// between leaving the issue alone and rewriting it. Both sides arrive
52// sorted by (ecosystem, name).
53func same(a, b []store.DepReport) bool {
54	if len(a) != len(b) {
55		return false
56	}
57	for i := range a {
58		if a[i] != b[i] {
59			return false
60		}
61	}
62	return true
63}