From 9ea579cf0df5c76cd777e1a0025ec4dbdddad256 Mon Sep 17 00:00:00 2001 From: Felipe Zipitria Date: Tue, 27 Feb 2024 17:31:45 -0300 Subject: [PATCH] fix: rebase and fix failing tests Signed-off-by: Felipe Zipitria --- chore/update_copyright.go | 6 ++++++ cmd/chore_test.go | 2 +- cmd/chore_update_copyright.go | 12 ++++++++++++ cmd/chore_update_copyright_test.go | 2 +- go.mod | 2 +- regex/definitions.go | 4 ++++ 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/chore/update_copyright.go b/chore/update_copyright.go index d750ff1..08aed0d 100644 --- a/chore/update_copyright.go +++ b/chore/update_copyright.go @@ -7,6 +7,7 @@ import ( "io/fs" "os" "path/filepath" + "regexp" "strings" "github.com/rs/zerolog/log" @@ -68,12 +69,17 @@ func updateRules(version string, year string, contents []byte) ([]byte, error) { output := new(bytes.Buffer) writer := bufio.NewWriter(output) replaceVersion := fmt.Sprintf("${1}%s", version) + // only keep numbers from the version + semanticVersion := regexp.MustCompile(`(\d)\.(\d)\.(\d)*`) + shortVersion := semanticVersion.FindString(version) + replaceShortVersion := fmt.Sprintf("${1}%s", shortVersion) replaceYear := fmt.Sprintf("${1}%s${3}", year) replaceSecRuleVersion := fmt.Sprintf("${1}%s", version) replaceSecComponentSignature := fmt.Sprintf("${1}%s", version) for scanner.Scan() { line := scanner.Text() line = regex.CRSVersionRegex.ReplaceAllString(line, replaceVersion) + line = regex.ShortCRSVersionRegex.ReplaceAllString(line, replaceShortVersion) line = regex.CRSCopyrightYearRegex.ReplaceAllString(line, replaceYear) line = regex.CRSYearSecRuleVerRegex.ReplaceAllString(line, replaceSecRuleVersion) line = regex.CRSVersionComponentSignatureRegex.ReplaceAllString(line, replaceSecComponentSignature) diff --git a/cmd/chore_test.go b/cmd/chore_test.go index 0a7bbee..29752a9 100644 --- a/cmd/chore_test.go +++ b/cmd/chore_test.go @@ -58,7 +58,7 @@ func (s *choreTestSuite) TestChore_RulesFile() { # # This file REQUEST-901-INITIALIZATION.conf initializes the Core Rules`) - rootCmd.SetArgs([]string{"-d", s.tempDir, "chore", "update-copyright", "-v", "NEW_VERSION", "-y", "1234"}) + rootCmd.SetArgs([]string{"-d", s.tempDir, "chore", "update-copyright", "-v", "1.2.3", "-y", "1234"}) _, err := rootCmd.ExecuteC() s.Require().NoError(err, "failed to execute rootCmd") diff --git a/cmd/chore_update_copyright.go b/cmd/chore_update_copyright.go index 5f2e675..9541019 100644 --- a/cmd/chore_update_copyright.go +++ b/cmd/chore_update_copyright.go @@ -7,6 +7,7 @@ import ( "strconv" "time" + "github.com/Masterminds/semver/v3" "github.com/spf13/cobra" "github.com/coreruleset/crs-toolchain/chore" @@ -23,6 +24,14 @@ func init() { buildChoreUpdateCopyrightCommand() } +func validateSemver(version string) error { + _, err := semver.NewVersion(version) + if err != nil { + return err + } + return nil +} + func createChoreUpdateCopyrightCommand() *cobra.Command { return &cobra.Command{ Use: "update-copyright", @@ -31,6 +40,9 @@ func createChoreUpdateCopyrightCommand() *cobra.Command { if copyrightVariables.Version == "" { return ErrUpdateCopyrightWithoutVersion } + if err := validateSemver(copyrightVariables.Version); err != nil { + return err + } return nil }, Run: func(cmd *cobra.Command, args []string) { diff --git a/cmd/chore_update_copyright_test.go b/cmd/chore_update_copyright_test.go index 9dd340c..5191abd 100644 --- a/cmd/chore_update_copyright_test.go +++ b/cmd/chore_update_copyright_test.go @@ -91,7 +91,7 @@ func (s *choreUpdateCopyrightTestSuite) TestUpdateCopyright_Version512() { } func (s *choreUpdateCopyrightTestSuite) TestUpdateCopyright_Year2100() { - rootCmd.SetArgs([]string{"-d", s.tempDir, "chore", "update-copyright", "-y", "2100", "-v", "experimental"}) + rootCmd.SetArgs([]string{"-d", s.tempDir, "chore", "update-copyright", "-y", "2100", "-v", "7.1.22"}) cmd, _ := rootCmd.ExecuteC() s.Equal("update-copyright", cmd.Name()) diff --git a/go.mod b/go.mod index 29041d0..7e0a8c1 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( ) require ( + github.com/Masterminds/semver/v3 v3.2.1 github.com/creativeprojects/go-selfupdate v1.1.3 github.com/google/uuid v1.6.0 github.com/itchyny/rassemble-go v0.1.0 @@ -17,7 +18,6 @@ require ( require ( code.gitea.io/sdk/gitea v0.17.1 // indirect - github.com/Masterminds/semver/v3 v3.2.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/davidmz/go-pageant v1.0.2 // indirect github.com/go-fed/httpsig v1.1.0 // indirect diff --git a/regex/definitions.go b/regex/definitions.go index 90a2155..6dca44c 100644 --- a/regex/definitions.go +++ b/regex/definitions.go @@ -81,6 +81,10 @@ var DefinitionReferenceRegex = regexp.MustCompile(`{{([a-zA-Z0-9-_]+)}}`) // The version declared on the file is captured in group 1. var CRSVersionRegex = regexp.MustCompile(`^(# OWASP (ModSecurity Core Rule Set|CRS) ver\.)(.+)$`) +// ShortCRSVersionRegex matches the version contained on every rules file. +// The version declared on the file is captured in group 1. +var ShortCRSVersionRegex = regexp.MustCompile(`setvar:tx.crs_setup_version=(\d{3-4})`) + // CRSCopyrightYearRegex matches the version and year range of the copyright text in setup, // setup example, and rule files. // The matched end year of the copyright year range will be captured in group 1.