From 9b4d78fac94367079ec9245b2ebd3dff9e8ecb2f Mon Sep 17 00:00:00 2001 From: Max Leske <250711+theseion@users.noreply.github.com> Date: Sat, 4 Jan 2025 10:23:58 +0100 Subject: [PATCH] chore: apply review suggestions --- chore/release/release.go | 29 +++++++++++++++++------------ chore/release/release_test.go | 19 +++++++++++-------- cmd/chore_release.go | 3 ++- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/chore/release/release.go b/chore/release/release.go index ff795dc..ef9b62a 100644 --- a/chore/release/release.go +++ b/chore/release/release.go @@ -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) { @@ -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) @@ -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 { @@ -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() } diff --git a/chore/release/release_test.go b/chore/release/release_test.go index 652bd9c..0911598 100644 --- a/chore/release/release_test.go +++ b/chore/release/release_test.go @@ -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 { @@ -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) { @@ -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() @@ -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) diff --git a/cmd/chore_release.go b/cmd/chore_release.go index cd66d97..82a2206 100644 --- a/cmd/chore_release.go +++ b/cmd/chore_release.go @@ -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()