Skip to content

Commit 220a758

Browse files
committed
chore: add missing functionality from shell script
1 parent b0bf182 commit 220a758

4 files changed

Lines changed: 54 additions & 28 deletions

File tree

‎chore/release/release.go‎

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ import (
2222
var logger = log.With().Str("component", "release").Logger()
2323

2424
func Release(context *context.Context, repositoryPath string, version *semver.Version, sourceRef string) {
25+
remoteName := findRemoteName()
26+
if remoteName == "" {
27+
logger.Fatal().Msg("failed to find remote for coreruleset/coreruleset")
28+
}
29+
fetchSourceRef(remoteName, sourceRef)
2530
branchName := fmt.Sprintf("v%d.%d.%d", version.Major(), version.Minor(), version.Patch())
2631
createAndCheckOutBranch(context, branchName, sourceRef)
27-
copyright.UpdateCopyright(context, version, uint16(time.Now().Year()))
32+
copyright.UpdateCopyright(context, version, uint16(time.Now().Year()), []string{"util/crs-rules-check/examples"})
2833
createCommit(context, branchName)
29-
pushBranch(branchName)
34+
pushBranch(remoteName, branchName)
3035
createPullRequest(version, branchName, sourceRef)
3136
}
3237

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

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

8590
type prBody struct {
86-
Title string `json:"title"`
87-
Head string `json:"head"`
88-
Base string `json:"base"`
91+
Title string `json:"title"`
92+
Head string `json:"head"`
93+
Base string `json:"base"`
94+
Label string `json:"label"`
95+
Reviewer string `json:"reviewer"`
8996
}
9097
bodyJson, err := json.Marshal(&prBody{
91-
Title: fmt.Sprintf("Release v%d.%d%d", version.Major(), version.Minor(), version.Patch()),
92-
Head: "coreruleset:" + branchName,
93-
Base: targetBranchName,
98+
Title: fmt.Sprintf("Release v%d.%d%d", version.Major(), version.Minor(), version.Patch()),
99+
Head: "coreruleset:" + branchName,
100+
Base: targetBranchName,
101+
Label: "release:ignore",
102+
Reviewer: "coreruleset/core-developers",
94103
})
95104
if err != nil {
96105
log.Fatal().Err(err).Msg("failed to serialize body of GH REST request")
@@ -103,24 +112,15 @@ func createPullRequest(version *semver.Version, branchName string, targetBranchN
103112
defer response.Body.Close()
104113
}
105114

106-
func pushBranch(branchName string) {
107-
out, err := runGit("remote", "-v")
115+
func pushBranch(remoteName string, branchName string) {
116+
out, err := runGit("push", remoteName, branchName)
108117
if err != nil {
109118
logger.Fatal().Err(err).Bytes("command-output", out)
110119
}
111-
var remoteName string
112-
scanner := bufio.NewScanner(bytes.NewReader(out))
113-
for scanner.Scan() {
114-
line := scanner.Text()
115-
if strings.Contains(line, "coreruleset/coreruleset") {
116-
remoteName = strings.Split(line, " ")[0]
117-
}
118-
}
119-
if remoteName == "" {
120-
logger.Fatal().Msg("failed to find remote to push release branch to")
121-
}
120+
}
122121

123-
out, err = runGit("push", remoteName, branchName)
122+
func fetchSourceRef(remoteName string, sourceRef string) {
123+
out, err := runGit("fetch", remoteName, sourceRef)
124124
if err != nil {
125125
logger.Fatal().Err(err).Bytes("command-output", out)
126126
}
@@ -131,3 +131,20 @@ func runGit(repositoryPath string, args ...string) ([]byte, error) {
131131
cmd.Dir = repositoryPath
132132
return cmd.CombinedOutput()
133133
}
134+
135+
func findRemoteName() string {
136+
out, err := runGit("remote", "-v")
137+
if err != nil {
138+
logger.Fatal().Err(err).Bytes("command-output", out)
139+
}
140+
var remoteName string
141+
scanner := bufio.NewScanner(bytes.NewReader(out))
142+
for scanner.Scan() {
143+
line := scanner.Text()
144+
if strings.Contains(line, "coreruleset/coreruleset") {
145+
remoteName = strings.Split(line, " ")[0]
146+
}
147+
}
148+
149+
return remoteName
150+
}

‎chore/release/release_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (s *choreReleaseTestSuite) TestCreateCommit() {
8989
s.Require().NoError(err)
9090
commit, err := repo.CommitObject(*revision)
9191
s.Require().NoError(err)
92-
s.Equal(fmt.Sprintf("Release %s\n", branchName), commit.Message)
92+
s.Equal(fmt.Sprintf("chore: release %s\n", branchName), commit.Message)
9393

9494
// parent of HEAD is main
9595
parent, err := commit.Parent(0)

‎chore/update_copyright/update_copyright.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
var logger = log.With().Str("component", "update-copyright").Logger()
2121

2222
// UpdateCopyright updates the copyright portion of the rules files to the provided year and version.
23-
func UpdateCopyright(ctxt *context.Context, version *semver.Version, year uint16) {
23+
func UpdateCopyright(ctxt *context.Context, version *semver.Version, year uint16, ignoredPaths []string) {
2424
err := filepath.WalkDir(ctxt.RootDir(), func(path string, d fs.DirEntry, err error) error {
2525
if err != nil {
2626
// abort
@@ -30,6 +30,13 @@ func UpdateCopyright(ctxt *context.Context, version *semver.Version, year uint16
3030
// continue
3131
return nil
3232
}
33+
for _, ignoredPath := range ignoredPaths {
34+
if strings.HasPrefix(path, ignoredPath) {
35+
// continue
36+
return nil
37+
}
38+
}
39+
3340
if strings.HasSuffix(d.Name(), ".conf") || strings.HasSuffix(d.Name(), ".example") {
3441
if err := processFile(path, version, year); err != nil {
3542
// abort

‎cmd/chore_update_copyright.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import (
1616

1717
var choreUpdateCopyrightCmd = createChoreUpdateCopyrightCommand()
1818
var copyrightVariables struct {
19-
Version string
20-
Year uint16
19+
Version string
20+
Year uint16
21+
IgnoredPaths []string
2122
}
2223
var copyrightParsedVariables struct {
2324
version *semver.Version
@@ -48,7 +49,7 @@ func createChoreUpdateCopyrightCommand() *cobra.Command {
4849
},
4950
Run: func(cmd *cobra.Command, args []string) {
5051
rootContext := context.New(rootValues.workingDirectory.String(), rootValues.configurationFileName.String())
51-
copyright.UpdateCopyright(rootContext, copyrightParsedVariables.version, copyrightVariables.Year)
52+
copyright.UpdateCopyright(rootContext, copyrightParsedVariables.version, copyrightVariables.Year, copyrightVariables.IgnoredPaths)
5253
},
5354
}
5455
}
@@ -57,6 +58,7 @@ func buildChoreUpdateCopyrightCommand() {
5758
choreCmd.AddCommand(choreUpdateCopyrightCmd)
5859
choreUpdateCopyrightCmd.Flags().Uint16VarP(&copyrightVariables.Year, "year", "y", uint16(time.Now().Year()), "Four digit year")
5960
choreUpdateCopyrightCmd.Flags().StringVarP(&copyrightVariables.Version, "version", "v", "", "Add this text as the version to the file.")
61+
choreUpdateCopyrightCmd.Flags().StringArrayVarP(&copyrightVariables.IgnoredPaths, "ignore", "i", []string{}, "Comma separated list of paths to ignore")
6062
}
6163

6264
func rebuildChoreUpdateCopyrightCommand() {

0 commit comments

Comments
 (0)