Skip to content

Commit 98365ed

Browse files
authored
Merge pull request #1813 from sergiodj/bump-ignore-bot-comments
bump.go: Ignore bot-generated CVE/GHSA comments
2 parents d32a5cd + abbc201 commit 98365ed

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

pkg/cli/bump.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,13 @@ func bumpEpoch(ctx context.Context, opts bumpOptions, path string) error {
118118
old := fmt.Sprintf(epochPattern, cfg.Package.Epoch)
119119
for scanner.Scan() {
120120
line := scanner.Text()
121-
nocomment, _, _ := strings.Cut(line, "#")
121+
nocomment, comment, _ := strings.Cut(line, "#")
122122
if strings.TrimSpace(nocomment) == old {
123123
found = true
124+
comment = strings.TrimSpace(comment)
125+
if strings.HasPrefix(comment, "CVE-") || strings.HasPrefix(comment, "GHSA-") {
126+
line = strings.TrimRight(nocomment, " ")
127+
}
124128
newFile = append(
125129
newFile, strings.ReplaceAll(line, old, fmt.Sprintf(epochPattern, cfg.Package.Epoch+1)),
126130
)

pkg/cli/bump_test.go

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,56 @@ package cli
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67
"testing"
78

89
"github.com/google/go-cmp/cmp"
910
)
1011

11-
func TestBumpWithComment(t *testing.T) {
12-
before := []byte(`
12+
func testPkgDefinition(epoch string) []byte {
13+
pkgTemplate := `
1314
package:
1415
name: awesome-tool
1516
version: 0.61.0
16-
epoch: 1 # a comment!
17-
`)
18-
19-
want := []byte(`
20-
package:
21-
name: awesome-tool
22-
version: 0.61.0
23-
epoch: 2 # a comment!
24-
`)
25-
26-
name := filepath.Join(t.TempDir(), "awesome-tool.yaml")
27-
28-
if err := os.WriteFile(name, before, 0o644); err != nil {
29-
t.Fatal(err)
30-
}
31-
32-
if err := bumpEpoch(t.Context(), bumpOptions{}, name); err != nil {
33-
t.Fatal(err)
34-
}
17+
epoch: EPOCH_HERE
18+
`
19+
return []byte(strings.ReplaceAll(pkgTemplate, "EPOCH_HERE", epoch))
20+
}
3521

36-
got, err := os.ReadFile(name)
37-
if err != nil {
38-
t.Fatal(err)
39-
}
40-
if diff := cmp.Diff(got, want); diff != "" {
41-
t.Errorf("bumpEpoch() mismatch (-want +got):\n%s", diff)
22+
func TestBumpWithComment(t *testing.T) {
23+
for i, td := range []struct {
24+
before []byte
25+
want []byte
26+
}{
27+
{
28+
testPkgDefinition("1 # a comment!"),
29+
testPkgDefinition("2 # a comment!"),
30+
},
31+
{
32+
testPkgDefinition("1 # CVE-111-222"),
33+
testPkgDefinition("2"),
34+
},
35+
{
36+
testPkgDefinition("1 # GHSA-a1b2-c1c2"),
37+
testPkgDefinition("2"),
38+
},
39+
} {
40+
name := filepath.Join(t.TempDir(), "awesome-tool.yaml")
41+
42+
if err := os.WriteFile(name, td.before, 0o644); err != nil {
43+
t.Fatal(err)
44+
}
45+
46+
if err := bumpEpoch(t.Context(), bumpOptions{}, name); err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
got, err := os.ReadFile(name)
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
if diff := cmp.Diff(got, td.want); diff != "" {
55+
t.Errorf("%d - bumpEpoch() mismatch (-want +got):\n%s", i, diff)
56+
}
4257
}
4358
}

0 commit comments

Comments
 (0)