Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/changeset-1.md
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 18 additions & 2 deletions packages/commithelper-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down