Skip to content

Commit a1c54ee

Browse files
committed
fix: rebase and fix failing tests
Signed-off-by: Felipe Zipitria <felipe.zipitria@owasp.org>
1 parent 717d1b6 commit a1c54ee

File tree

6 files changed

+25
-3
lines changed

6 files changed

+25
-3
lines changed

chore/update_copyright.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/fs"
88
"os"
99
"path/filepath"
10+
"regexp"
1011
"strings"
1112

1213
"github.com/rs/zerolog/log"
@@ -68,12 +69,17 @@ func updateRules(version string, year string, contents []byte) ([]byte, error) {
6869
output := new(bytes.Buffer)
6970
writer := bufio.NewWriter(output)
7071
replaceVersion := fmt.Sprintf("${1}%s", version)
72+
// only keep numbers from the version
73+
semanticVersion := regexp.MustCompile(`(\d)\.(\d)\.(\d)*`)
74+
shortVersion := semanticVersion.FindString(version)
75+
replaceShortVersion := fmt.Sprintf("${1}%s", shortVersion)
7176
replaceYear := fmt.Sprintf("${1}%s${3}", year)
7277
replaceSecRuleVersion := fmt.Sprintf("${1}%s", version)
7378
replaceSecComponentSignature := fmt.Sprintf("${1}%s", version)
7479
for scanner.Scan() {
7580
line := scanner.Text()
7681
line = regex.CRSVersionRegex.ReplaceAllString(line, replaceVersion)
82+
line = regex.ShortCRSVersionRegex.ReplaceAllString(line, replaceShortVersion)
7783
line = regex.CRSCopyrightYearRegex.ReplaceAllString(line, replaceYear)
7884
line = regex.CRSYearSecRuleVerRegex.ReplaceAllString(line, replaceSecRuleVersion)
7985
line = regex.CRSVersionComponentSignatureRegex.ReplaceAllString(line, replaceSecComponentSignature)

cmd/chore_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (s *choreTestSuite) TestChore_RulesFile() {
5858
5959
#
6060
# This file REQUEST-901-INITIALIZATION.conf initializes the Core Rules`)
61-
rootCmd.SetArgs([]string{"-d", s.tempDir, "chore", "update-copyright", "-v", "NEW_VERSION", "-y", "1234"})
61+
rootCmd.SetArgs([]string{"-d", s.tempDir, "chore", "update-copyright", "-v", "1.2.3", "-y", "1234"})
6262
_, err := rootCmd.ExecuteC()
6363

6464
s.Require().NoError(err, "failed to execute rootCmd")

cmd/chore_update_copyright.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88
"time"
99

10+
"github.com/Masterminds/semver/v3"
1011
"github.com/spf13/cobra"
1112

1213
"github.com/coreruleset/crs-toolchain/chore"
@@ -23,6 +24,14 @@ func init() {
2324
buildChoreUpdateCopyrightCommand()
2425
}
2526

27+
func validateSemver(version string) error {
28+
_, err := semver.NewVersion(version)
29+
if err != nil {
30+
return err
31+
}
32+
return nil
33+
}
34+
2635
func createChoreUpdateCopyrightCommand() *cobra.Command {
2736
return &cobra.Command{
2837
Use: "update-copyright",
@@ -31,6 +40,9 @@ func createChoreUpdateCopyrightCommand() *cobra.Command {
3140
if copyrightVariables.Version == "" {
3241
return ErrUpdateCopyrightWithoutVersion
3342
}
43+
if err := validateSemver(copyrightVariables.Version); err != nil {
44+
return err
45+
}
3446
return nil
3547
},
3648
Run: func(cmd *cobra.Command, args []string) {

cmd/chore_update_copyright_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (s *choreUpdateCopyrightTestSuite) TestUpdateCopyright_Version512() {
9191
}
9292

9393
func (s *choreUpdateCopyrightTestSuite) TestUpdateCopyright_Year2100() {
94-
rootCmd.SetArgs([]string{"-d", s.tempDir, "chore", "update-copyright", "-y", "2100", "-v", "experimental"})
94+
rootCmd.SetArgs([]string{"-d", s.tempDir, "chore", "update-copyright", "-y", "2100", "-v", "7.1.22"})
9595
cmd, _ := rootCmd.ExecuteC()
9696

9797
s.Equal("update-copyright", cmd.Name())

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
)
1010

1111
require (
12+
github.com/Masterminds/semver/v3 v3.2.1
1213
github.com/creativeprojects/go-selfupdate v1.1.3
1314
github.com/google/uuid v1.6.0
1415
github.com/itchyny/rassemble-go v0.1.0
@@ -17,7 +18,6 @@ require (
1718

1819
require (
1920
code.gitea.io/sdk/gitea v0.17.1 // indirect
20-
github.com/Masterminds/semver/v3 v3.2.1 // indirect
2121
github.com/davecgh/go-spew v1.1.1 // indirect
2222
github.com/davidmz/go-pageant v1.0.2 // indirect
2323
github.com/go-fed/httpsig v1.1.0 // indirect

regex/definitions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ var DefinitionReferenceRegex = regexp.MustCompile(`{{([a-zA-Z0-9-_]+)}}`)
8282
// The version declared on the file is captured in group 1.
8383
var CRSVersionRegex = regexp.MustCompile(`^(# OWASP (ModSecurity Core Rule Set|CRS) ver\.)(.+)$`)
8484

85+
// ShortCRSVersionRegex matches the version contained on every rules file.
86+
// The version declared on the file is captured in group 1.
87+
var ShortCRSVersionRegex = regexp.MustCompile(`setvar:tx.crs_setup_version=(\d{3-4})`)
88+
8589
// CRSCopyrightYearRegex matches the version and year range of the copyright text in setup,
8690
// setup example, and rule files.
8791
// The matched end year of the copyright year range will be captured in group 1.

0 commit comments

Comments
 (0)