Skip to content

Commit

Permalink
fix: rebase and fix failing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Zipitria <[email protected]>
  • Loading branch information
fzipi committed Feb 27, 2024
1 parent f2418cf commit 9eaefbd
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 3 deletions.
6 changes: 6 additions & 0 deletions chore/update_copyright.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/chore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions cmd/chore_update_copyright.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/Masterminds/semver/v3"
"github.com/spf13/cobra"

"github.com/coreruleset/crs-toolchain/chore"
Expand All @@ -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",
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/chore_update_copyright_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions regex/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9eaefbd

Please sign in to comment.