Skip to content

Commit 7921272

Browse files
committed
feat(issue): redesign issue name
1 parent 1fd8e26 commit 7921272

File tree

7 files changed

+100
-34
lines changed

7 files changed

+100
-34
lines changed

cmd/gitlab-flow/commands.go

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ func getFeatureCommand() *cli.Command {
6767
Usage: "input the `featureBranchName`",
6868
Required: false,
6969
},
70+
&cli.BoolFlag{
71+
Name: "parse-issue-compatible",
72+
Usage: "switch to parse issue name in comptaible mode",
73+
Required: false,
74+
},
7075
},
7176
Subcommands: getFeatureSubCommands(),
7277
}

cmd/gitlab-flow/support.go

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func getOpFeatureContext(c *cli.Context) *types.OpFeatureContext {
8686
return &types.OpFeatureContext{
8787
ForceCreateMergeRequest: c.Bool("force-create-mr"),
8888
FeatureBranchName: c.String("feature-branch-name"),
89+
ParseIssueCompatible: c.Bool("parse-issue-compatible"),
8990
}
9091
}
9192

internal/dash_impl.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,25 @@ func (d dashImpl) FeatureDetail(branchName string) ([]byte, error) {
112112
return nil, errors.Wrap(errInvalidFeatureName, "dashImpl.FeatureDetail.CurrentBranch")
113113
}
114114
branchName = out
115-
}
116115

117-
// if current branch name could be parsed to feature branch name, then use it.
118-
if !isFeatureName(branchName) {
119-
out, ok := tryParseFeatureNameFrom(branchName)
120-
if !ok {
121-
log.
122-
WithFields(log.Fields{
123-
"branchName": branchName,
124-
"out": out,
125-
"ok": ok,
126-
}).
127-
Debug("dashImpl.FeatureDetail could not parse branch name by default")
128-
return nil, errors.Wrap(errInvalidFeatureName, "dashImpl.FeatureDetail.tryParseFeatureNameFrom")
116+
// if current branch name could be parsed to feature branch name, then use it.
117+
if !isFeatureName(branchName) {
118+
out, ok := tryParseFeatureNameFrom(branchName, false)
119+
if !ok {
120+
log.
121+
WithFields(log.Fields{
122+
"branchName": branchName,
123+
"out": out,
124+
"ok": ok,
125+
}).
126+
Debug("dashImpl.FeatureDetail could not parse branch name by default")
127+
return nil, errors.Wrap(errInvalidFeatureName, "dashImpl.FeatureDetail.tryParseFeatureNameFrom")
128+
}
129+
branchName = out
129130
}
130-
branchName = out
131131
}
132-
branchName = genFeatureBranchName(branchName)
133132

133+
branchName = genFeatureBranchName(branchName)
134134
// locate branch
135135
branch, err := d.repo.QueryBranch(&repository.BranchDO{
136136
ProjectID: d.ctx.Project.ID,

internal/flow.go

+34-15
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const (
9393
FeatureBranchPrefix = "feature/"
9494
HotfixBranchPrefix = "hotfix/"
9595
ConflictResolveBranchPrefix = "conflict-resolve/"
96+
IssueBranchPrefix = "issue/"
9697
)
9798

9899
// genFeatureBranchName
@@ -107,24 +108,25 @@ func genFeatureBranchName(name string) string {
107108
// tryParseFeatureNameFrom try parse feature name from issue name or other cases.
108109
// if branchName has no prefix, which means tryParseFeatureNameFrom could judge it.
109110
// otherwise, branchName must have prefix, which was one of flow branch prefixes.
110-
func tryParseFeatureNameFrom(branchName string) (string, bool) {
111+
func tryParseFeatureNameFrom(branchName string, comptaible bool) (string, bool) {
111112
arr := strings.Split(branchName, "/")
112113
if len(arr) < 2 {
113-
// FIXME(@yeqown): maybe old issue name, so try parse it.
114-
if out := parseFeaturenameFromIssueName(branchName); out != "" {
115-
return out, true
116-
}
117114
return "", false
118115
}
119116

120-
prefix := arr[0]
117+
prefix := arr[0] + "/"
121118
switch prefix {
122119
case FeatureBranchPrefix:
123120
// pass
124121
case HotfixBranchPrefix:
125122
// pass
126123
case ConflictResolveBranchPrefix:
127124
// pass
125+
case IssueBranchPrefix:
126+
out := strings.Join(arr[1:], "/")
127+
if out = parseFeatureFromIssueName(out, comptaible); out != "" {
128+
return out, true
129+
}
128130
case "":
129131
fallthrough
130132
default:
@@ -149,30 +151,47 @@ func genHotfixBranchName(name string) string {
149151
return HotfixBranchPrefix + name
150152
}
151153

152-
// genMRTitle
153-
func genMRTitle(srcBranch, targetBranch string) string {
154+
// genMergeRequestName generate merge request name.
155+
func genMergeRequestName(srcBranch, targetBranch string) string {
154156
return fmt.Sprintf("Merge %s into %s", srcBranch, targetBranch)
155157
}
156158

157159
// genIssueBranchName .
158-
// @result = 1-milestoneTitle as default
159-
// fmt.Sprintf("%d-%s", issue.IID, milestone.Title)
160+
// @result = issue/milestoneTitle-1 as default
160161
func genIssueBranchName(name string, issueIID int) string {
161-
if strings.HasPrefix(name, strconv.Itoa(issueIID)+"-") {
162+
if strings.HasPrefix(name, IssueBranchPrefix) {
162163
return name
163164
}
164165

165-
return fmt.Sprintf("%d-%s", issueIID, name)
166+
return IssueBranchPrefix + name + "-" + strconv.Itoa(issueIID)
166167
}
167168

168-
func parseFeaturenameFromIssueName(issueName string) string {
169-
idx := strings.Index(issueName, "-")
169+
// parseFeatureFromIssueName parse issue name to feature name, there are
170+
// two different cases:
171+
// 1. "1-milestoneName"
172+
// 2. "issue/milestoneName-1"
173+
//
174+
// TODO(@yeqown): comptaible with old.
175+
func parseFeatureFromIssueName(issueName string, compatible bool) string {
176+
// if comptaible, try parse "1-milestoneName"
177+
if compatible {
178+
// DONE(@yeqown): support "1-milestoneName"
179+
idx := strings.Index(issueName, "-")
180+
if idx == -1 {
181+
return ""
182+
}
183+
184+
return issueName[idx+1:]
185+
}
186+
187+
issueName = strings.TrimPrefix(issueName, IssueBranchPrefix)
188+
idx := strings.LastIndex(issueName, "-")
170189
if idx == -1 {
171190
// not errCouldNotFound
172191
return ""
173192
}
174193

175-
return issueName[idx+1:]
194+
return issueName[:idx]
176195
}
177196

178197
// chooseOneProjectInteractively if there are not only one project matched from local or remote,

internal/flow_impl.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func (f flowImpl) FeatureFinishIssue(opc *types.OpFeatureContext, issueBranchNam
331331

332332
// DONE(@yeqown) get feature branch name from issueBranchName
333333
if opc.FeatureBranchName == "" {
334-
opc.FeatureBranchName = parseFeaturenameFromIssueName(issueBranchName)
334+
opc.FeatureBranchName = parseFeatureFromIssueName(issueBranchName, opc.ParseIssueCompatible)
335335
}
336336
if opc.FeatureBranchName == "" {
337337
return errInvalidFeatureName
@@ -386,7 +386,7 @@ func (f flowImpl) FeatureFinishIssue(opc *types.OpFeatureContext, issueBranchNam
386386
}
387387
issueCreateMR:
388388
// not hit, so create one
389-
title := genMRTitle(issueBranchName, opc.FeatureBranchName)
389+
title := genMergeRequestName(issueBranchName, opc.FeatureBranchName)
390390
desc := ""
391391
result, err := f.createMergeRequest(title, desc, milestoneID, issueIID, issueBranchName, opc.FeatureBranchName)
392392
if err != nil {
@@ -473,7 +473,7 @@ func (f flowImpl) HotfixFinish(hotfixBranchName string) error {
473473
}
474474

475475
// then create MR to master
476-
title := genMRTitle(hotfixBranchName, types.MasterBranch.String())
476+
title := genMergeRequestName(hotfixBranchName, types.MasterBranch.String())
477477
result, err := f.createMergeRequest(
478478
title, issue.Desc, 0, issue.IssueIID, hotfixBranchName, types.MasterBranch.String())
479479
if err != nil {
@@ -958,7 +958,7 @@ featureCreateMR:
958958
}
959959
// create MR
960960
targetBranch := targetBranchName.String()
961-
title := genMRTitle(featureBranchName, targetBranch)
961+
title := genMergeRequestName(featureBranchName, targetBranch)
962962
result, err := f.createMergeRequest(
963963
title, milestone.Desc, milestone.MilestoneID, 0, featureBranch.BranchName, targetBranch)
964964
if err != nil {

internal/flow_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package internal
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/suite"
7+
)
8+
9+
type testFlowSuite struct {
10+
suite.Suite
11+
}
12+
13+
func (s *testFlowSuite) SetupAllSuite() {
14+
15+
}
16+
17+
func (s testFlowSuite) Test_genIssueName() {
18+
name := genIssueBranchName("milestone-test", 123)
19+
s.Equal(IssueBranchPrefix+"milestone-test-123", name)
20+
21+
feature := parseFeatureFromIssueName(name, false)
22+
s.Equal("milestone-test", feature)
23+
24+
feature2, ok := tryParseFeatureNameFrom(name, false)
25+
s.True(ok)
26+
s.Equal("milestone-test", feature2)
27+
}
28+
29+
func (s testFlowSuite) Test_parseFeatureFromIssueName_comptaible() {
30+
name := "123-milestone-test"
31+
feature := parseFeatureFromIssueName(name, true)
32+
s.Equal("milestone-test", feature)
33+
34+
}
35+
36+
func Test_flowSuite(t *testing.T) {
37+
suite.Run(t, new(testFlowSuite))
38+
}

internal/types/context.go

+3
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,7 @@ type OpFeatureContext struct {
120120
ForceCreateMergeRequest bool
121121
// FeatureBranchName specify which branch name to use in the lifecycle of feature operations.
122122
FeatureBranchName string
123+
124+
// ParseIssueCompatible if this is true, means parse issueName to feature in compatible way.
125+
ParseIssueCompatible bool
123126
}

0 commit comments

Comments
 (0)