diff --git a/.changeset/changeset-1.md b/.changeset/changeset-1.md new file mode 100644 index 0000000..43a2eb4 --- /dev/null +++ b/.changeset/changeset-1.md @@ -0,0 +1,7 @@ +--- +"@naverpay/commithelper-go": patch +--- + +Modify Config struct to include Protect field and add branch protection check + +PR: [Modify Config struct to include Protect field and add branch protection check](https://github.com/NaverPayDev/cli/pull/44) \ No newline at end of file diff --git a/packages/commithelper-go/main.go b/packages/commithelper-go/main.go index 1afd010..bb6df9c 100644 --- a/packages/commithelper-go/main.go +++ b/packages/commithelper-go/main.go @@ -11,7 +11,8 @@ import ( ) type Config struct { - Rules map[string]*string `json:"rules"` + Rules map[string]*string `json:"rules"` + Protect []string `json:"protect"` } func main() { @@ -56,6 +57,12 @@ func main() { branchName := getCurrentBranchName() config := loadConfig() + // Check if current branch is protected + if isProtectedBranch(branchName, config.Protect) { + fmt.Printf("Error: Cannot commit to protected branch '%s'\n", branchName) + os.Exit(1) + } + prefix := generatePrefix(branchName, config) if prefix != "" { commitMessage = fmt.Sprintf("[%s] %s", prefix, commitMessage) @@ -96,7 +103,7 @@ func loadConfig() Config { if err != nil { if os.IsNotExist(err) { // Return an empty config if the file does not exist - return Config{Rules: map[string]*string{}} + return Config{Rules: map[string]*string{}, Protect: []string{}} } fmt.Printf("Error reading config file at %s: %v\n", configPath, err) os.Exit(1) @@ -140,6 +147,15 @@ func generatePrefix(branchName string, config Config) string { return fmt.Sprintf("%s#%s", *repo, issueNumber) } +func isProtectedBranch(branchName string, protectedBranches []string) bool { + for _, protected := range protectedBranches { + if branchName == protected { + return true + } + } + return false +} + func isAlreadyTagged(commitMessage string) bool { // Check if commit message already contains issue tag like [#123] or [org/repo#123] // This pattern matches: