-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- create and check out new version branch Fixes #140
- Loading branch information
Showing
7 changed files
with
293 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.