Skip to content

Commit

Permalink
chore: apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
theseion committed Jan 4, 2025
1 parent 1c18ff1 commit 9b4d78f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
29 changes: 17 additions & 12 deletions chore/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/cli/go-gh/v2/pkg/api"
copyright "github.com/coreruleset/crs-toolchain/v2/chore/update_copyright"
"github.com/coreruleset/crs-toolchain/v2/context"
"github.com/go-git/go-git/v5"
"github.com/rs/zerolog/log"

copyright "github.com/coreruleset/crs-toolchain/v2/chore/update_copyright"
"github.com/coreruleset/crs-toolchain/v2/context"
)

const examplesPath = "util/crs-rules-check/examples"
const branchNameTemplate = "release/v%d.%d.%d"
const prTitleTemplate = "chore: release v%d.%d.%d"

var logger = log.With().Str("component", "release").Logger()

func Release(context *context.Context, repositoryPath string, version *semver.Version, sourceRef string) {
Expand All @@ -27,9 +32,9 @@ func Release(context *context.Context, repositoryPath string, version *semver.Ve
logger.Fatal().Msg("failed to find remote for coreruleset/coreruleset")
}
fetchSourceRef(remoteName, sourceRef)
branchName := fmt.Sprintf("v%d.%d.%d", version.Major(), version.Minor(), version.Patch())
branchName := fmt.Sprintf(branchNameTemplate, version.Major(), version.Minor(), version.Patch())
createAndCheckOutBranch(context, branchName, sourceRef)
copyright.UpdateCopyright(context, version, uint16(time.Now().Year()), []string{"util/crs-rules-check/examples"})
copyright.UpdateCopyright(context, version, uint16(time.Now().Year()), []string{examplesPath})
createCommit(context, branchName)
pushBranch(remoteName, branchName)
createPullRequest(version, branchName, sourceRef)
Expand Down Expand Up @@ -88,17 +93,17 @@ func createPullRequest(version *semver.Version, branchName string, targetBranchN
}

type prBody struct {
Title string `json:"title"`
Head string `json:"head"`
Base string `json:"base"`
Label string `json:"label"`
Reviewer string `json:"reviewer"`
Title string `json:"title"`
Head string `json:"head"`
Base string `json:"base"`
Labels []string `json:"labels"`
Reviewer string `json:"reviewer"`
}
bodyJson, err := json.Marshal(&prBody{
Title: fmt.Sprintf("Release v%d.%d%d", version.Major(), version.Minor(), version.Patch()),
Title: fmt.Sprintf(prTitleTemplate, version.Major(), version.Minor(), version.Patch()),
Head: "coreruleset:" + branchName,
Base: targetBranchName,
Label: "release:ignore",
Labels: []string{"release", "release:ignore"},
Reviewer: "coreruleset/core-developers",
})
if err != nil {
Expand All @@ -107,7 +112,7 @@ func createPullRequest(version *semver.Version, branchName string, targetBranchN

response, err := client.Request(http.MethodPost, "repos/coreruleset/coreruleset/pulls", bytes.NewReader(bodyJson))
if err != nil {
log.Fatal().Err(err).Msg("Creating PR failed")
log.Fatal().Err(err).Msg("creating PR failed")
}
defer response.Body.Close()
}
Expand Down
19 changes: 11 additions & 8 deletions chore/release/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"path"
"testing"

"github.com/coreruleset/crs-toolchain/v2/context"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/stretchr/testify/suite"

"github.com/coreruleset/crs-toolchain/v2/context"
)

type choreReleaseTestSuite struct {
Expand All @@ -22,13 +23,13 @@ func (s *choreReleaseTestSuite) SetupTest() {
s.repoDir = s.T().TempDir()
cmd := exec.Command("git", "init", "-b", "main")
cmd.Dir = s.repoDir
err := cmd.Run()
s.Require().NoError(err)
out, err := cmd.CombinedOutput()
s.Require().NoError(err, out)

cmd = exec.Command("git", "commit", "--allow-empty", "-m", "dummy")
cmd.Dir = s.repoDir
err = cmd.Run()
s.Require().NoError(err)
out, err = cmd.CombinedOutput()
s.Require().NoError(err, out)
}

func TestRunChoreReleaseTestSuite(t *testing.T) {
Expand All @@ -47,13 +48,14 @@ func (s *choreReleaseTestSuite) TestCreateAndcheckoutBranch() {

found := false
var branchRef *plumbing.Reference
branches.ForEach(func(r *plumbing.Reference) error {
err = branches.ForEach(func(r *plumbing.Reference) error {
if r.Name().Short() == branchName {
found = true
branchRef = r
}
return nil
})
s.Require().NoError(err)
s.True(found)

headRef, err := repo.Head()
Expand All @@ -68,10 +70,11 @@ func (s *choreReleaseTestSuite) TestCreateCommit() {
createAndCheckOutBranch(ctxt, branchName, "main")

// Add something to commit, as `createCommit` doesn't allow empty commits
os.WriteFile(path.Join(s.repoDir, "file"), []byte("content"), os.ModePerm)
err := os.WriteFile(path.Join(s.repoDir, "file"), []byte("content"), os.ModePerm)
s.Require().NoError(err)
cmd := exec.Command("git", "add", ".")
cmd.Dir = s.repoDir
err := cmd.Run()
err = cmd.Run()
s.Require().NoError(err)

createCommit(ctxt, branchName)
Expand Down
3 changes: 2 additions & 1 deletion cmd/chore_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"os"

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

release "github.com/coreruleset/crs-toolchain/v2/chore/release"
"github.com/coreruleset/crs-toolchain/v2/context"
"github.com/spf13/cobra"
)

var choreReleaseCmd = createChoreReleaseCommand()
Expand Down

0 comments on commit 9b4d78f

Please sign in to comment.