diff --git a/pkg/branch/branch.go b/pkg/branch/branch.go index 05f9842..e4b47bf 100644 --- a/pkg/branch/branch.go +++ b/pkg/branch/branch.go @@ -10,7 +10,7 @@ import ( const _replacementCharacter = "-" -var _regexp = regexp.MustCompile("[^A-Za-z0-9.]+") +var _regexp = regexp.MustCompile("[^A-Za-z0-9]+") // GenerateName generates a new branch name off of the identifier and title specified. // Both the identifier and title must not be blank. @@ -22,7 +22,7 @@ func GenerateName(id string, title string, maxLen int) (string, error) { if title == "" || strings.TrimSpace(title) == "" { return "", errors.New("title must not be blank") } - titleWithoutDotSuffix := strings.TrimSuffix(title, ".") + titleWithoutDotSuffix := strings.TrimRight(title, ".") if titleWithoutDotSuffix == "" { return "", fmt.Errorf("invalid title: %s", title) } @@ -38,5 +38,5 @@ func GenerateName(id string, title string, maxLen int) (string, error) { if utf8.RuneCountInString(generatedName) < maxLen { return generatedName, nil } - return string([]rune(generatedName)[:maxLen]), nil + return strings.TrimRight(string([]rune(generatedName)[:maxLen]), _replacementCharacter), nil } diff --git a/pkg/branch/branch_test.go b/pkg/branch/branch_test.go index 0325af2..5318ec1 100644 --- a/pkg/branch/branch_test.go +++ b/pkg/branch/branch_test.go @@ -127,6 +127,33 @@ func TestGenerateName(t *testing.T) { want: "2345-this-is-an-improvement-suggestion", wantErr: false, }, + { + name: "multiple trailing dots should be stripped", + args: args{ + id: "6993", + title: "Collect metrics for issues, pull requests, ...", + }, + want: "6993-collect-metrics-for-issues-pull-requests", + wantErr: false, + }, + { + name: "dots within title should be replaced", + args: args{ + id: "42", + title: "Fix v2.0 regression", + }, + want: "42-fix-v2-0-regression", + wantErr: false, + }, + { + name: "only dots title", + args: args{ + id: "33", + title: "...", + }, + want: "", + wantErr: true, + }, { name: "branch name should not exceed maxLen - negative max length", args: args{ @@ -157,6 +184,16 @@ func TestGenerateName(t *testing.T) { want: "2345-this-is-an-improvement", wantErr: false, }, + { + name: "branch name should not end with hyphen after truncation", + args: args{ + id: "2345", + title: "THIS IS AN IMPROVEMENT SUGGESTION :-) !!!", + maxLen: 10, + }, + want: "2345-this", + wantErr: false, + }, } { t.Run(tt.name, func(t *testing.T) { got, err := GenerateName(tt.args.id, tt.args.title, tt.args.maxLen)