Skip to content

Commit 5d3e97b

Browse files
authored
Merge pull request #19 from rm3l/allow_specifying_a_max_length_for_generated_branch_names
2 parents d8933c3 + 5d872d4 commit 5d3e97b

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
func main() {
1414

1515
var repo string
16+
var maxLen int
1617

1718
flag.StringVar(&repo, "repo", "", "repository to use for finding the issue")
19+
flag.IntVar(&maxLen, "max-length", -1, "max length of generated branch name")
1820

1921
flag.Usage = func() {
2022
//goland:noinspection GoUnhandledErrorResult
@@ -44,7 +46,7 @@ func main() {
4446
log.Fatalln(err)
4547
}
4648

47-
branchName, err := branch.GenerateName(issueInfo.Id, issueInfo.Title)
49+
branchName, err := branch.GenerateName(issueInfo.Id, issueInfo.Title, maxLen)
4850
if err != nil {
4951
log.Fatalln(err)
5052
}

pkg/branch/branch.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"regexp"
77
"strings"
8+
"unicode/utf8"
89
)
910

1011
const _replacementCharacter = "-"
@@ -14,7 +15,7 @@ var _regexp = regexp.MustCompile("[^A-Za-z0-9.]+")
1415
// GenerateName generates a new branch name off of the identifier and title specified.
1516
// Both the identifier and title must not be blank.
1617
// Leading hyphens (prefix and suffix) are removed from the name returned.
17-
func GenerateName(id string, title string) (string, error) {
18+
func GenerateName(id string, title string, maxLen int) (string, error) {
1819
if id == "" || strings.TrimSpace(id) == "" {
1920
return "", errors.New("id must not be blank")
2021
}
@@ -29,5 +30,13 @@ func GenerateName(id string, title string) (string, error) {
2930
_regexp.ReplaceAllString(
3031
fmt.Sprintf("%s-%s", id, titleWithoutDotSuffix),
3132
_replacementCharacter))
32-
return strings.TrimPrefix(strings.TrimSuffix(generatedName, _replacementCharacter), _replacementCharacter), nil
33+
generatedName = strings.TrimSuffix(generatedName, _replacementCharacter)
34+
generatedName = strings.TrimPrefix(generatedName, _replacementCharacter)
35+
if maxLen <= 0 {
36+
return generatedName, nil
37+
}
38+
if utf8.RuneCountInString(generatedName) < maxLen {
39+
return generatedName, nil
40+
}
41+
return string([]rune(generatedName)[:maxLen]), nil
3342
}

pkg/branch/branch_test.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88

99
func TestGenerateName(t *testing.T) {
1010
type args struct {
11-
id string
12-
title string
11+
id string
12+
title string
13+
maxLen int
1314
}
1415
for _, tt := range []struct {
15-
args
16+
args args
1617

1718
name string
1819
wantErr bool
@@ -126,9 +127,39 @@ func TestGenerateName(t *testing.T) {
126127
want: "2345-this-is-an-improvement-suggestion",
127128
wantErr: false,
128129
},
130+
{
131+
name: "branch name should not exceed maxLen - negative max length",
132+
args: args{
133+
id: "2345",
134+
title: "THIS IS AN IMPROVEMENT SUGGESTION :-) !!!",
135+
maxLen: -10,
136+
},
137+
want: "2345-this-is-an-improvement-suggestion",
138+
wantErr: false,
139+
},
140+
{
141+
name: "branch name should not exceed maxLen - zero max length",
142+
args: args{
143+
id: "2345",
144+
title: "THIS IS AN IMPROVEMENT SUGGESTION :-) !!!",
145+
maxLen: 0,
146+
},
147+
want: "2345-this-is-an-improvement-suggestion",
148+
wantErr: false,
149+
},
150+
{
151+
name: "branch name should not exceed maxLen",
152+
args: args{
153+
id: "2345",
154+
title: "THIS IS AN IMPROVEMENT SUGGESTION :-) !!!",
155+
maxLen: 27,
156+
},
157+
want: "2345-this-is-an-improvement",
158+
wantErr: false,
159+
},
129160
} {
130161
t.Run(tt.name, func(t *testing.T) {
131-
got, err := GenerateName(tt.id, tt.title)
162+
got, err := GenerateName(tt.args.id, tt.args.title, tt.args.maxLen)
132163
if (err != nil) != tt.wantErr {
133164
t.Errorf("got err: %v, but wanted err set to %v", err, tt.wantErr)
134165
}

0 commit comments

Comments
 (0)