Skip to content

Commit

Permalink
fixup! Release v1.2.3
Browse files Browse the repository at this point in the history
use CLI instead of go-git for write operations
  • Loading branch information
theseion committed Jan 1, 2025
1 parent 47e833d commit 1ff9c85
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
Binary file removed chore/__debug_bin2080182539
Binary file not shown.
26 changes: 15 additions & 11 deletions chore/release.go → chore/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"time"

"github.com/Masterminds/semver/v3"
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"
)

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

func Release(context *context.Context, repositoryPath string, version *semver.Version, sourceRef string) {
createAndCheckOutBranch(context, fmt.Sprintf("v%d.%d.%d", version.Major(), version.Minor(), version.Patch()), sourceRef)
UpdateCopyright(context, version, uint16(time.Now().Year()))
copyright.UpdateCopyright(context, version, uint16(time.Now().Year()))
createCommit(context, version)
}

Expand All @@ -23,22 +27,16 @@ func createAndCheckOutBranch(context *context.Context, branchName string, source
panic(err)
}

cmd := exec.Command("git", "switch", "-c", branchName, sourceRef)
cmd.Dir = context.RootDir()
out, err := cmd.CombinedOutput()
print(out)
out, err := runGit(context.RootDir(), "switch", "-c", branchName, sourceRef)
if err != nil {
//FIXME
panic(err)
logger.Fatal().Err(err).Bytes("command-output", out).Msg("failed to create commit for release")
}
}

func createCommit(context *context.Context, version *semver.Version) {
cmd := exec.Command("git", "commit", "-am", "Release "+fmt.Sprintf("v%d.%d.%d", version.Major(), version.Minor(), version.Patch()))
err := cmd.Run()
out, err := runGit(context.RootDir(), "commit", "-am", "Release "+fmt.Sprintf("v%d.%d.%d", version.Major(), version.Minor(), version.Patch()))
if err != nil {
//FIXME
panic(err)
logger.Fatal().Err(err).Bytes("command-output", out).Msg("failed to create commit for release")
}
}

Expand All @@ -65,3 +63,9 @@ func checkForCleanWorkTree(context *context.Context) error {
}
return nil
}

func runGit(repositoryPath string, args ...string) ([]byte, error) {
cmd := exec.Command("git", args...)
cmd.Dir = repositoryPath
return cmd.CombinedOutput()
}
13 changes: 9 additions & 4 deletions chore/release_test.go → chore/release/release_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package chore

import (
"fmt"
"os"
"os/exec"
"path"
"testing"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -69,8 +71,9 @@ func (s *choreReleaseTestSuite) TestCreateCommit() {
createAndCheckOutBranch(ctxt, branchName, "main")

// Add something to commit, as `createCommit` doesn't allow empty commits
os.WriteFile(s.repoDir+"file", []byte("content"), os.ModePerm)
os.WriteFile(path.Join(s.repoDir, "file"), []byte("content"), os.ModePerm)

Check failure on line 74 in chore/release/release_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.WriteFile` is not checked (errcheck)

Check failure on line 74 in chore/release/release_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.WriteFile` is not checked (errcheck)
cmd := exec.Command("git", "add", ".")
cmd.Dir = s.repoDir
err = cmd.Run()
s.Require().NoError(err)

Expand All @@ -84,15 +87,17 @@ func (s *choreReleaseTestSuite) TestCreateCommit() {
s.Require().NoError(err)
s.True(status.IsClean())

// HEAD has the new commit message
revision, err := repo.ResolveRevision("HEAD")
s.Require().NoError(err)
commit, err := repo.CommitObject(*revision)
s.Require().NoError(err)
s.Equal("Release "+branchName, commit.Message)
s.Equal(fmt.Sprintf("Release %s\n", branchName), commit.Message)

// parent of HEAD is main
parent, err := commit.Parent(0)
s.Require().NoError(err)
branch, err := repo.ResolveRevision(plumbing.Revision(branchName))
branchHash, err := repo.ResolveRevision(plumbing.Revision("main"))
s.Require().NoError(err)
s.Equal(*branch, parent.Hash)
s.Equal(*branchHash, parent.Hash)
}
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions cmd/chore_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"

"github.com/Masterminds/semver/v3"
"github.com/coreruleset/crs-toolchain/v2/chore"
release "github.com/coreruleset/crs-toolchain/v2/chore/release"
"github.com/coreruleset/crs-toolchain/v2/context"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -52,7 +52,7 @@ func createChoreReleaseCommand() *cobra.Command {
},
Run: func(cmd *cobra.Command, args []string) {
rootContext := context.New(rootValues.workingDirectory.String(), rootValues.configurationFileName.String())
chore.Release(rootContext, releaseParsedArgs.repositoryPath, releaseParsedArgs.version, releaseVariables.sourceRef)
release.Release(rootContext, releaseParsedArgs.repositoryPath, releaseParsedArgs.version, releaseVariables.sourceRef)
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/chore_update_copyright.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/spf13/cobra"

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

Expand Down Expand Up @@ -48,7 +48,7 @@ func createChoreUpdateCopyrightCommand() *cobra.Command {
},
Run: func(cmd *cobra.Command, args []string) {
rootContext := context.New(rootValues.workingDirectory.String(), rootValues.configurationFileName.String())
chore.UpdateCopyright(rootContext, copyrightParsedVariables.version, copyrightVariables.Year)
copyright.UpdateCopyright(rootContext, copyrightParsedVariables.version, copyrightVariables.Year)
},
}
}
Expand Down

0 comments on commit 1ff9c85

Please sign in to comment.