Skip to content

Commit 1a252d2

Browse files
committed
cmd/vulnreport: move main.go code to separate files
The main.go file was becoming somewhat hard to navigate, so this change moves most of its code to separate files, organized by the subcommand the code is primarily associated with. This change only moves code and does not modify its behavior. Change-Id: I684cda7e2e65eb8a043e8c04fdeda187420a95f0 Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/559596 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent c4fad53 commit 1a252d2

11 files changed

+1058
-914
lines changed

cmd/vulnreport/commit.go

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"context"
9+
"flag"
10+
"fmt"
11+
"strings"
12+
13+
"golang.org/x/exp/slices"
14+
"golang.org/x/vulndb/internal/derrors"
15+
"golang.org/x/vulndb/internal/ghsa"
16+
"golang.org/x/vulndb/internal/proxy"
17+
"golang.org/x/vulndb/internal/report"
18+
)
19+
20+
var (
21+
updateIssue = flag.Bool("up", false, "for commit, create a CL that updates (doesn't fix) the tracking bug")
22+
)
23+
24+
func commit(ctx context.Context, filename string, ghsaClient *ghsa.Client, pc *proxy.Client, force bool) (err error) {
25+
defer derrors.Wrap(&err, "commit(%q)", filename)
26+
27+
// Clean up the report file and lint the result.
28+
// Stop if there any problems.
29+
if err := fix(ctx, filename, ghsaClient, pc, force); err != nil {
30+
return err
31+
}
32+
r, err := report.ReadAndLint(filename, pc)
33+
if err != nil {
34+
return err
35+
}
36+
if hasUnaddressedTodos(r) {
37+
// Check after fix() as it can add new TODOs.
38+
return fmt.Errorf("file %q has unaddressed %q fields", filename, "TODO:")
39+
}
40+
41+
// Find all derived files (OSV and CVE).
42+
files := []string{filename}
43+
if r.Excluded == "" {
44+
files = append(files, r.OSVFilename())
45+
}
46+
if r.CVEMetadata != nil {
47+
files = append(files, r.CVEFilename())
48+
}
49+
50+
// Add the files to git.
51+
if err := gitAdd(files...); err != nil {
52+
return err
53+
}
54+
55+
// Commit the files, allowing the user to edit the default commit message.
56+
msg, err := newCommitMsg(r)
57+
if err != nil {
58+
return err
59+
}
60+
return gitCommit(msg, files...)
61+
}
62+
63+
func newCommitMsg(r *report.Report) (string, error) {
64+
f, err := r.YAMLFilename()
65+
if err != nil {
66+
return "", err
67+
}
68+
69+
folder, filename, issueID, err := report.ParseFilepath(f)
70+
if err != nil {
71+
return "", err
72+
}
73+
74+
issueAction := "Fixes"
75+
fileAction := "add"
76+
if *updateIssue {
77+
fileAction = "update"
78+
issueAction = "Updates"
79+
}
80+
// For now, we need to manually publish the CVE record so the issue
81+
// should not be auto-closed on add.
82+
if r.CVEMetadata != nil {
83+
issueAction = "Updates"
84+
}
85+
86+
return fmt.Sprintf(
87+
"%s: %s %s\n\nAliases: %s\n\n%s golang/vulndb#%d",
88+
folder, fileAction, filename, strings.Join(r.Aliases(), ", "),
89+
issueAction, issueID), nil
90+
}
91+
92+
// hasUnaddressedTodos returns true if report has any unaddressed todos in the
93+
// report, i.e. starts with "TODO:".
94+
func hasUnaddressedTodos(r *report.Report) bool {
95+
is := func(s string) bool { return strings.HasPrefix(s, "TODO:") }
96+
any := func(ss []string) bool { return slices.IndexFunc(ss, is) >= 0 }
97+
98+
if is(string(r.Excluded)) {
99+
return true
100+
}
101+
for _, m := range r.Modules {
102+
if is(m.Module) {
103+
return true
104+
}
105+
for _, v := range m.Versions {
106+
if is(string(v.Introduced)) {
107+
return true
108+
}
109+
if is(string(v.Fixed)) {
110+
return true
111+
}
112+
}
113+
if is(string(m.VulnerableAt)) {
114+
return true
115+
}
116+
for _, p := range m.Packages {
117+
if is(p.Package) || is(p.SkipFix) || any(p.Symbols) || any(p.DerivedSymbols) {
118+
return true
119+
}
120+
}
121+
}
122+
for _, ref := range r.References {
123+
if is(ref.URL) {
124+
return true
125+
}
126+
}
127+
if any(r.CVEs) || any(r.GHSAs) {
128+
return true
129+
}
130+
return is(r.Summary.String()) || is(r.Description.String()) || any(r.Credits)
131+
}

0 commit comments

Comments
 (0)