-
Notifications
You must be signed in to change notification settings - Fork 11
Description
Hey, first of kudos for the nice work. I really like the functionality that you have put together with this branch.
I would like to know if you would be open to receive a PR with some added behavior for the JIRA template.
Let me know what you think of the suggestion and the approach and if you prefer I can open a PR with a draft and we discuss there.
Context
Internally we use a specific naming convention for branches. Would you consider to expand the current functionality to extract the JIRA_TASK based on a regex?
E.g., a branch with name feature/PRJ-0001 would become PRJ-0001 if I would pass the regex that would group the ID that I want.
Possible approach
With my approach I expect a valid regex to be defined in the config yaml (if the regex is not valid it will fail running CMF) and I also expect a group named JIRA_TASK to be available inside the regex (if not provided it returns the original branch name).
Rough sample (The Go Playground):
package main
import (
"fmt"
"regexp"
)
func findKey(names []string) int {
key := "JIRA_TASK"
for i, v := range names {
if v == key {
return i
}
}
return 0
}
func getGitBranchName() string {
// Current behaviour to fetch the branch name
return "feature/PRJ-0001"
}
func getBranchName(regex string) (string, error) {
branchName := getGitBranchName()
r, err := regexp.Compile(regex)
if err == nil {
// Position of the group we expect (defaults to 0 to be the entire branch name)
pos := findKey(r.SubexpNames())
// Extracts the branch name
branchName = r.FindStringSubmatch(branchName)[pos]
}
return branchName, err
}
func main() {
jiraTaskRegex := "feature/(?P<JIRA_TASK>PRJ-[0-9]+)(-.*)?"
branchName, err := getBranchName(jiraTaskRegex)
if err == nil {
fmt.Println("Branch name: ", branchName)
} else {
fmt.Println("Error: ", err)
}
}