Skip to content

Commit

Permalink
chore: add missing functionality from shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
theseion committed Jan 3, 2025
1 parent b0bf182 commit 220a758
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
63 changes: 40 additions & 23 deletions chore/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ import (
var logger = log.With().Str("component", "release").Logger()

func Release(context *context.Context, repositoryPath string, version *semver.Version, sourceRef string) {
remoteName := findRemoteName()
if remoteName == "" {
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())
createAndCheckOutBranch(context, branchName, sourceRef)
copyright.UpdateCopyright(context, version, uint16(time.Now().Year()))
copyright.UpdateCopyright(context, version, uint16(time.Now().Year()), []string{"util/crs-rules-check/examples"})
createCommit(context, branchName)
pushBranch(branchName)
pushBranch(remoteName, branchName)
createPullRequest(version, branchName, sourceRef)
}

Expand All @@ -43,7 +48,7 @@ func createAndCheckOutBranch(context *context.Context, branchName string, source
}

func createCommit(context *context.Context, branchName string) {
out, err := runGit(context.RootDir(), "commit", "-am", "Release "+branchName)
out, err := runGit(context.RootDir(), "commit", "-am", "chore: release "+branchName)
if err != nil {
logger.Fatal().Err(err).Bytes("command-output", out).Msg("failed to create commit for release")
}
Expand Down Expand Up @@ -83,14 +88,18 @@ func createPullRequest(version *semver.Version, branchName string, targetBranchN
}

type prBody struct {
Title string `json:"title"`
Head string `json:"head"`
Base string `json:"base"`
Title string `json:"title"`
Head string `json:"head"`
Base string `json:"base"`
Label string `json:"label"`
Reviewer string `json:"reviewer"`
}
bodyJson, err := json.Marshal(&prBody{
Title: fmt.Sprintf("Release v%d.%d%d", version.Major(), version.Minor(), version.Patch()),
Head: "coreruleset:" + branchName,
Base: targetBranchName,
Title: fmt.Sprintf("Release v%d.%d%d", version.Major(), version.Minor(), version.Patch()),
Head: "coreruleset:" + branchName,
Base: targetBranchName,
Label: "release:ignore",
Reviewer: "coreruleset/core-developers",
})
if err != nil {
log.Fatal().Err(err).Msg("failed to serialize body of GH REST request")
Expand All @@ -103,24 +112,15 @@ func createPullRequest(version *semver.Version, branchName string, targetBranchN
defer response.Body.Close()
}

func pushBranch(branchName string) {
out, err := runGit("remote", "-v")
func pushBranch(remoteName string, branchName string) {
out, err := runGit("push", remoteName, branchName)
if err != nil {
logger.Fatal().Err(err).Bytes("command-output", out)
}
var remoteName string
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "coreruleset/coreruleset") {
remoteName = strings.Split(line, " ")[0]
}
}
if remoteName == "" {
logger.Fatal().Msg("failed to find remote to push release branch to")
}
}

out, err = runGit("push", remoteName, branchName)
func fetchSourceRef(remoteName string, sourceRef string) {
out, err := runGit("fetch", remoteName, sourceRef)
if err != nil {
logger.Fatal().Err(err).Bytes("command-output", out)
}
Expand All @@ -131,3 +131,20 @@ func runGit(repositoryPath string, args ...string) ([]byte, error) {
cmd.Dir = repositoryPath
return cmd.CombinedOutput()
}

func findRemoteName() string {
out, err := runGit("remote", "-v")
if err != nil {
logger.Fatal().Err(err).Bytes("command-output", out)
}
var remoteName string
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "coreruleset/coreruleset") {
remoteName = strings.Split(line, " ")[0]
}
}

return remoteName
}
2 changes: 1 addition & 1 deletion chore/release/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *choreReleaseTestSuite) TestCreateCommit() {
s.Require().NoError(err)
commit, err := repo.CommitObject(*revision)
s.Require().NoError(err)
s.Equal(fmt.Sprintf("Release %s\n", branchName), commit.Message)
s.Equal(fmt.Sprintf("chore: release %s\n", branchName), commit.Message)

// parent of HEAD is main
parent, err := commit.Parent(0)
Expand Down
9 changes: 8 additions & 1 deletion chore/update_copyright/update_copyright.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
var logger = log.With().Str("component", "update-copyright").Logger()

// UpdateCopyright updates the copyright portion of the rules files to the provided year and version.
func UpdateCopyright(ctxt *context.Context, version *semver.Version, year uint16) {
func UpdateCopyright(ctxt *context.Context, version *semver.Version, year uint16, ignoredPaths []string) {
err := filepath.WalkDir(ctxt.RootDir(), func(path string, d fs.DirEntry, err error) error {
if err != nil {
// abort
Expand All @@ -30,6 +30,13 @@ func UpdateCopyright(ctxt *context.Context, version *semver.Version, year uint16
// continue
return nil
}
for _, ignoredPath := range ignoredPaths {
if strings.HasPrefix(path, ignoredPath) {
// continue
return nil
}
}

if strings.HasSuffix(d.Name(), ".conf") || strings.HasSuffix(d.Name(), ".example") {
if err := processFile(path, version, year); err != nil {
// abort
Expand Down
8 changes: 5 additions & 3 deletions cmd/chore_update_copyright.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (

var choreUpdateCopyrightCmd = createChoreUpdateCopyrightCommand()
var copyrightVariables struct {
Version string
Year uint16
Version string
Year uint16
IgnoredPaths []string
}
var copyrightParsedVariables struct {
version *semver.Version
Expand Down Expand Up @@ -48,7 +49,7 @@ func createChoreUpdateCopyrightCommand() *cobra.Command {
},
Run: func(cmd *cobra.Command, args []string) {
rootContext := context.New(rootValues.workingDirectory.String(), rootValues.configurationFileName.String())
copyright.UpdateCopyright(rootContext, copyrightParsedVariables.version, copyrightVariables.Year)
copyright.UpdateCopyright(rootContext, copyrightParsedVariables.version, copyrightVariables.Year, copyrightVariables.IgnoredPaths)
},
}
}
Expand All @@ -57,6 +58,7 @@ func buildChoreUpdateCopyrightCommand() {
choreCmd.AddCommand(choreUpdateCopyrightCmd)
choreUpdateCopyrightCmd.Flags().Uint16VarP(&copyrightVariables.Year, "year", "y", uint16(time.Now().Year()), "Four digit year")
choreUpdateCopyrightCmd.Flags().StringVarP(&copyrightVariables.Version, "version", "v", "", "Add this text as the version to the file.")
choreUpdateCopyrightCmd.Flags().StringArrayVarP(&copyrightVariables.IgnoredPaths, "ignore", "i", []string{}, "Comma separated list of paths to ignore")
}

func rebuildChoreUpdateCopyrightCommand() {
Expand Down

0 comments on commit 220a758

Please sign in to comment.