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
6 changes: 3 additions & 3 deletions pkg/branch/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand All @@ -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
}
37 changes: 37 additions & 0 deletions pkg/branch/branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down
Loading