commit-message-formatter icon indicating copy to clipboard operation
commit-message-formatter copied to clipboard

Extract JIRA task from branch name

Open ealves-pt opened this issue 6 years ago • 2 comments

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)
	}
}

ealves-pt avatar Jul 18 '19 16:07 ealves-pt

Thank you for open this issue, sorry for the late answer, I am refreshing this repo and definitely I will use your approach on this new version =)

Rodrigonavarro23 avatar Jun 14 '21 21:06 Rodrigonavarro23

And we are super open to PR's if you are still interested in this.

Rodrigonavarro23 avatar Jun 14 '21 21:06 Rodrigonavarro23