Skip to content

Commit 11c4073

Browse files
committed
Modify Config struct to include Protect field and add branch protection check in commit process
1 parent 1ac5470 commit 11c4073

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

packages/commithelper-go/main.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
)
1212

1313
type Config struct {
14-
Rules map[string]*string `json:"rules"`
14+
Rules map[string]*string `json:"rules"`
15+
Protect []string `json:"protect"`
1516
}
1617

1718
func main() {
@@ -56,6 +57,12 @@ func main() {
5657
branchName := getCurrentBranchName()
5758
config := loadConfig()
5859

60+
// Check if current branch is protected
61+
if isProtectedBranch(branchName, config.Protect) {
62+
fmt.Printf("Error: Cannot commit to protected branch '%s'\n", branchName)
63+
os.Exit(1)
64+
}
65+
5966
prefix := generatePrefix(branchName, config)
6067
if prefix != "" {
6168
commitMessage = fmt.Sprintf("[%s] %s", prefix, commitMessage)
@@ -96,7 +103,7 @@ func loadConfig() Config {
96103
if err != nil {
97104
if os.IsNotExist(err) {
98105
// Return an empty config if the file does not exist
99-
return Config{Rules: map[string]*string{}}
106+
return Config{Rules: map[string]*string{}, Protect: []string{}}
100107
}
101108
fmt.Printf("Error reading config file at %s: %v\n", configPath, err)
102109
os.Exit(1)
@@ -140,6 +147,15 @@ func generatePrefix(branchName string, config Config) string {
140147
return fmt.Sprintf("%s#%s", *repo, issueNumber)
141148
}
142149

150+
func isProtectedBranch(branchName string, protectedBranches []string) bool {
151+
for _, protected := range protectedBranches {
152+
if branchName == protected {
153+
return true
154+
}
155+
}
156+
return false
157+
}
158+
143159
func isAlreadyTagged(commitMessage string) bool {
144160
// Check if commit message already contains issue tag like [#123] or [org/repo#123]
145161
// This pattern matches:

0 commit comments

Comments
 (0)