Skip to content

Commit

Permalink
feat: automate releases
Browse files Browse the repository at this point in the history
- create and check out new version branch

Fixes #140
  • Loading branch information
theseion committed Jan 1, 2025
1 parent 95bda94 commit e3acb36
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 48 deletions.
65 changes: 65 additions & 0 deletions chore/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package chore

import (
"fmt"

"github.com/Masterminds/semver/v3"
"github.com/coreruleset/crs-toolchain/v2/context"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
)

func Release(context *context.Context, repositoryPath string, version *semver.Version) {
createAndCheckOutBranch(repositoryPath, fmt.Sprintf("v%d.%d.%d", version.Major(), version.Minor(), version.Patch()))
}

func createAndCheckOutBranch(repositoryPath string, branchName string) {
r, err := git.PlainOpen(repositoryPath)
if err != nil {
//FIXME
panic(err)
}
headRef, err := r.Head()
if err != nil {
//FIXME
panic(err)
}

// Create a new plumbing.HashReference object with the name of the branch
// and the hash from the HEAD. The reference name should be a full reference
// name and not an abbreviated one, as is used on the git cli.
refName := plumbing.ReferenceName("refs/heads/" + branchName)
ref := plumbing.NewHashReference(refName, headRef.Hash())

// The created reference is saved in the storage.
err = r.Storer.SetReference(ref)
if err != nil {
//FIXME
panic(err)
}
branch := &config.Branch{
Name: branchName,
Merge: refName,
}
err = r.CreateBranch(branch)
if err != nil {
//FIXME
panic(err)
}

branchCoOpts := git.CheckoutOptions{
Branch: refName,
Force: true,
}
w, err := r.Worktree()
if err != nil {
//FIXME
panic(err)
}

if err := w.Checkout(&branchCoOpts); err != nil {
//FIXME
panic(err)
}
}
57 changes: 57 additions & 0 deletions chore/release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package chore

import (
"testing"
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/suite"
)

type choreReleaseTestSuite struct {
suite.Suite
repoDir string
}

func (s *choreReleaseTestSuite) SetupTest() {
s.repoDir = s.T().TempDir()
repo, err := git.PlainInit(s.repoDir, false)
s.Require().NoError(err)

w, err := repo.Worktree()
s.Require().NoError(err)
commit, err := w.Commit("dummy", &git.CommitOptions{
Author: &object.Signature{
Name: "Homer Simpson",
Email: "[email protected]",
When: time.Now(),
},
AllowEmptyCommits: true,
})
s.Require().NoError(err)
_, err = repo.CommitObject(commit)
s.Require().NoError(err)
}

func TestRunChoreReleaseTestSuite(t *testing.T) {
suite.Run(t, new(choreReleaseTestSuite))
}

func (s *choreReleaseTestSuite) TestCreateBranch() {
branchName := "v1.2.3"
createAndCheckOutBranch(s.repoDir, branchName)
repo, err := git.PlainOpen(s.repoDir)
s.Require().NoError(err)

branch, err := repo.Branch(branchName)
s.Require().NoError(err, "Branch should have been created")

headRef, err := repo.Head()
s.Require().NoError(err)

branchHash, err := repo.ResolveRevision(plumbing.Revision(branch.Name))
s.Require().NoError(err)
s.Equal(*branchHash, headRef.Hash(), "New branch should have been checked out")
}
3 changes: 0 additions & 3 deletions chore/update_copyright_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ type copyrightUpdateTestsTestSuite struct {
suite.Suite
}

func (s *copyrightUpdateTestsTestSuite) SetupTest() {
}

func TestRunUpdateCopyrightTestsTestSuite(t *testing.T) {
suite.Run(t, new(copyrightUpdateTestsTestSuite))
}
Expand Down
64 changes: 64 additions & 0 deletions cmd/chore_release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 OWASP Core Rule Set Project
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"os"

"github.com/Masterminds/semver/v3"
"github.com/coreruleset/crs-toolchain/v2/chore"
"github.com/coreruleset/crs-toolchain/v2/context"
"github.com/spf13/cobra"
)

var choreReleaseCmd = createChoreReleaseCommand()

func init() {
buildChoreReleaseCommand()
}

func createChoreReleaseCommand() *cobra.Command {
return &cobra.Command{
Use: "release",
// FIXME
Short: "tbd",
Args: cobra.MatchAll(cobra.ExactArgs(2), cobra.OnlyValidArgs),
ValidArgs: []string{
"version",
},
PreRunE: func(cmd *cobra.Command, args []string) error {
repositoryPath := args[0]
version := args[1]

if _, err := os.Stat(repositoryPath); err != nil {
return err
}

_, err := semver.NewVersion(version)
return err
},
Run: func(cmd *cobra.Command, args []string) {
rootContext := context.New(rootValues.workingDirectory.String(), rootValues.configurationFileName.String())
version, err := semver.NewVersion(args[1])
if err != nil {
//FIXME
panic(err)
}
chore.Release(rootContext, args[0], version)
},
}
}

func buildChoreReleaseCommand() {
choreCmd.AddCommand(choreReleaseCmd)
}

func rebuildChoreReleaseCommand() {
if choreReleaseCmd != nil {
choreReleaseCmd.Parent().RemoveCommand(choreReleaseCmd)
}

choreReleaseCmd = createChoreReleaseCommand()
buildChoreReleaseCommand()
}
10 changes: 1 addition & 9 deletions cmd/chore_update_copyright.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ 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 @@ -40,7 +32,7 @@ func createChoreUpdateCopyrightCommand() *cobra.Command {
if copyrightVariables.Version == "" {
return ErrUpdateCopyrightWithoutVersion
}
if err := validateSemver(copyrightVariables.Version); err != nil {
if _, err := semver.NewVersion(copyrightVariables.Version); err != nil {
return err
}
return nil
Expand Down
22 changes: 20 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,50 @@ require (
require (
github.com/Masterminds/semver/v3 v3.3.1
github.com/creativeprojects/go-selfupdate v1.4.0
github.com/go-git/go-git/v5 v5.13.0
github.com/google/uuid v1.6.0
github.com/itchyny/rassemble-go v0.1.2
gopkg.in/yaml.v3 v3.0.1
)

require (
code.gitea.io/sdk/gitea v0.19.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.1.3 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidmz/go-pageant v1.0.2 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-fed/httpsig v1.1.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.6.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-github/v30 v30.1.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.3.0 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/xanzy/go-gitlab v0.112.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/time v0.7.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

require (
Expand Down
Loading

0 comments on commit e3acb36

Please sign in to comment.