Skip to content

Commit 5db377a

Browse files
committed
feat: flow and commands defines stage2;
1 parent bd85af3 commit 5db377a

File tree

13 files changed

+571
-121
lines changed

13 files changed

+571
-121
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@
1515
# vendor/
1616

1717
.idea/
18-
testdata/
18+
testdata/
19+
gitlab-flow
20+
flow-debug
21+
!gitlab-flow/

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build:
2+
go build -o ${BIN} ./cmd/gitlab-flow
3+
4+
debug:
5+
BIN=flow-debug make build
6+
cp ./flow-debug ~/go/bin

cmd/gitlab-flow/commands.go

+25-21
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ func getInitCommand() *cli.Command {
2222
},
2323
&cli.StringFlag{
2424
Name: "gitlab_host",
25-
Aliases: []string{"h"},
25+
Aliases: []string{"hh"},
2626
Required: true,
2727
Usage: "gitlab_host is the domain of YOUR gitlab server.",
2828
},
2929
&cli.StringFlag{
30-
Name: "conf_path",
31-
Aliases: []string{"c"},
32-
Value: conf.DefaultConfPath(),
33-
Required: true,
34-
Usage: "conf_path is the directory which contains your config and local database.",
30+
Name: "conf_path",
31+
Aliases: []string{"c"},
32+
Value: conf.DefaultConfPath(),
33+
DefaultText: conf.DefaultConfPath(),
34+
Required: false,
35+
Usage: "conf_path is the directory which contains your config and local database.",
3536
},
3637
},
3738
ArgsUsage: "gitlab-flow init -s ACCESS_TOKEN -h GITLAB_HOST [-c CONF_PATH]",
@@ -65,11 +66,12 @@ func getFeatureCommand() *cli.Command {
6566
Category: "feature",
6667
Flags: []cli.Flag{
6768
&cli.StringFlag{
68-
Name: "conf_path",
69-
Aliases: []string{"c"},
70-
Value: conf.DefaultConfPath(),
71-
Usage: "-c, --conf_path",
72-
Required: true,
69+
Name: "conf_path",
70+
Aliases: []string{"c"},
71+
Value: conf.DefaultConfPath(),
72+
DefaultText: conf.DefaultConfPath(),
73+
Usage: "-c, --conf_path",
74+
Required: false,
7375
},
7476
&cli.BoolFlag{
7577
Name: "debug",
@@ -92,11 +94,12 @@ func getHotfixCommand() *cli.Command {
9294
Category: "hotfix",
9395
Flags: []cli.Flag{
9496
&cli.StringFlag{
95-
Name: "conf_path",
96-
Aliases: []string{"c"},
97-
Value: conf.DefaultConfPath(),
98-
Usage: "-c, --conf_path",
99-
Required: true,
97+
Name: "conf_path",
98+
Aliases: []string{"c"},
99+
Value: conf.DefaultConfPath(),
100+
DefaultText: conf.DefaultConfPath(),
101+
Usage: "-c, --conf_path",
102+
Required: false,
100103
},
101104
&cli.BoolFlag{
102105
Name: "debug",
@@ -119,11 +122,12 @@ func getDashCommand() *cli.Command {
119122
Category: "dash",
120123
Flags: []cli.Flag{
121124
&cli.StringFlag{
122-
Name: "conf_path",
123-
Aliases: []string{"c"},
124-
Value: conf.DefaultConfPath(),
125-
Usage: "-c, --conf_path",
126-
Required: true,
125+
Name: "conf_path",
126+
Aliases: []string{"c"},
127+
Value: conf.DefaultConfPath(),
128+
DefaultText: conf.DefaultConfPath(),
129+
Usage: "-c, --conf_path",
130+
Required: false,
127131
},
128132
&cli.BoolFlag{
129133
Name: "debug",

cmd/gitlab-flow/commands_feature.go

+2-36
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,11 @@ func getFeatureBeginIssueSubCommand() *cli.Command {
7575

7676
issueTitle := c.Args().Get(0)
7777
issueDesc := c.Args().Get(1)
78-
params := make([]string, 0, 2)
79-
if issueTitle != "" {
80-
params = append(params, issueTitle)
81-
}
82-
if issueDesc != "" {
83-
params = append(params, issueDesc)
84-
}
8578

8679
confPath := c.String("conf_path")
8780
featureBranchName := c.String("feature_branch_name")
8881
debug := c.Bool("debug")
89-
return getFlow(confPath, debug).FeatureBeginIssue(featureBranchName, params...)
82+
return getFlow(confPath, debug).FeatureBeginIssue(featureBranchName, issueTitle, issueDesc)
9083
},
9184
}
9285
}
@@ -224,34 +217,7 @@ func getSyncMilestoneSubCommand() *cli.Command {
224217
interact := c.Bool("interact")
225218

226219
f := getFlow(confPath, debug)
227-
if !interact {
228-
return f.SyncMilestone(milestoneID)
229-
}
230-
//
231-
//milestoneOptions, err := f.gitlabOperator.Milestones()
232-
//if err != nil {
233-
// return errors.Wrap(err, "获取里程碑列表失败")
234-
//}
235-
//
236-
//qs := []*survey.Question{
237-
// {
238-
// Name: "milestones",
239-
// Prompt: &survey.Select{
240-
// Message: "choosing one by moving your arrow up and down",
241-
// Options: milestoneOptions,
242-
// },
243-
// },
244-
//}
245-
//type milestoneAnswer struct {
246-
// MilestoneID string `survey:"milestones"`
247-
//}
248-
//response := new(milestoneAnswer)
249-
//if err = survey.Ask(qs, response); err != nil {
250-
// return err
251-
//}
252-
//
253-
//milestoneID, _ = strconv.Atoi(strings.Split(a.MilestoneID, ":")[1])
254-
return errors.New("not support yet")
220+
return f.SyncMilestone(milestoneID, interact)
255221
},
256222
}
257223
}

cmd/gitlab-flow/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ func mountCommands(app *cli.App) {
3333
getInitCommand(),
3434
getFeatureCommand(),
3535
getHotfixCommand(),
36-
//getDashCommand(),
36+
getDashCommand(),
3737
}
3838
}

cmd/gitlab-flow/support.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func getFlow(confPath string, debug bool) internal.IFlow {
2525
panic("could not reach")
2626
}
2727

28-
// FIXME(@yeqown) get cwd correctly.
28+
// DONE(@yeqown) get cwd correctly.
2929
cwd, _ := os.Getwd()
3030
return internal.NewFlow(types.NewContext(cwd, confPath, cfg))
3131
}

internal/conf/conf.go

+15-36
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ type ConfigParser interface {
2020
Marshal(cfg *types.Config) ([]byte, error)
2121
}
2222

23-
// Load to load config from confpath with specified parser.
24-
func Load(confpath string, parser ConfigParser) (cfg *types.Config, err error) {
23+
// Load to load config from confPath with specified parser.
24+
func Load(confPath string, parser ConfigParser) (cfg *types.Config, err error) {
2525
if parser == nil {
2626
parser = NewTOMLParser()
2727
}
@@ -30,22 +30,15 @@ func Load(confpath string, parser ConfigParser) (cfg *types.Config, err error) {
3030
r io.Reader
3131
)
3232
cfg = new(types.Config)
33-
p, create := precheckConfigDirectory(confpath)
34-
if create {
35-
cfg = defaultConf
36-
if err = Save(p, cfg, parser); err != nil {
37-
return nil, fmt.Errorf("init config file failed: %v", err)
38-
}
39-
}
40-
33+
p := precheckConfigDirectory(confPath)
4134
r, err = os.OpenFile(p, os.O_RDONLY, 0777)
4235
err = parser.Unmarshal(r, cfg)
4336

4437
return cfg, err
4538
}
4639

4740
// Save to save config with specified parser.
48-
func Save(confpath string, cfg *types.Config, parser ConfigParser) error {
41+
func Save(confPath string, cfg *types.Config, parser ConfigParser) error {
4942
if parser == nil {
5043
parser = NewTOMLParser()
5144
}
@@ -54,12 +47,14 @@ func Save(confpath string, cfg *types.Config, parser ConfigParser) error {
5447
if err != nil {
5548
return err
5649
}
57-
w, err := os.OpenFile(confpath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0777)
50+
51+
p := precheckConfigDirectory(confPath)
52+
w, err := os.OpenFile(p, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0777)
5853
if err != nil {
5954
return err
6055
}
6156

62-
_, err = fmt.Fprint(w, data)
57+
_, err = fmt.Fprint(w, string(data))
6358
return err
6459
}
6560

@@ -106,32 +101,16 @@ const (
106101
)
107102

108103
// precheckConfigDirectory could parse filename and path from configDirectory.
109-
func precheckConfigDirectory(configDirectory string) (s string, create bool) {
110-
if configDirectory != "" {
111-
return configDirectory, false
112-
}
113-
114-
// generate default config directory
115-
home, err := os.UserHomeDir()
104+
func precheckConfigDirectory(configDirectory string) string {
105+
fi, err := os.Stat(configDirectory)
116106
if err != nil {
117-
log.Errorf("get user home failed: %v", err)
107+
log.Fatalf("could not stat config file: %v", err)
108+
panic("could not reach")
118109
}
119110

120-
configDirectory = filepath.Join(home, _defaultConfigDirectory)
121-
s = filepath.Join(configDirectory, _configFilename)
122-
if _, err = os.Stat(configDirectory); err == nil {
123-
return s, false
111+
if fi.IsDir() {
112+
return filepath.Join(configDirectory, _configFilename)
124113
}
125114

126-
// check directory exists or not.
127-
if os.IsNotExist(err) {
128-
// could not find the directory, then mkdir
129-
if err = os.MkdirAll(configDirectory, 0777); err != nil {
130-
panic(err)
131-
}
132-
return s, true
133-
}
134-
135-
// other errors while stat config directory.
136-
panic(err)
115+
return configDirectory
137116
}

internal/flow.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package internal
22

33
import (
4+
"fmt"
45
"path/filepath"
56
"regexp"
67
"strconv"
@@ -31,7 +32,7 @@ type IFlow interface {
3132

3233
// FeatureBeginIssue checkout a issue branch from feature branch, also open a merge request
3334
// which is from issue branch to feature branch.
34-
FeatureBeginIssue(featureBranchName string, params ...string) error
35+
FeatureBeginIssue(featureBranchName string, title, desc string) error
3536
// FeatureFinishIssue open the WebURL of merge request which is from issue branch to feature branch.
3637
FeatureFinishIssue(featureBranchName, issueBranchName string) error
3738

@@ -43,7 +44,7 @@ type IFlow interface {
4344

4445
// SyncMilestone synchronize remote repository milestone and
4546
// related issues / merge requests to local.
46-
SyncMilestone(milestoneID int) error
47+
SyncMilestone(milestoneID int, interact bool) error
4748
}
4849

4950
// extractProjectNameFromCWD get project name from current working directory.
@@ -87,3 +88,40 @@ func notBuiltinBranch(branchName string) bool {
8788
}
8889
return false
8990
}
91+
92+
const (
93+
FeatureBranchPrefix = "feature/"
94+
HotfixBranchPrefix = "hotfix/"
95+
)
96+
97+
func GenFeatureBranchName(name string) string {
98+
if strings.HasPrefix(name, FeatureBranchPrefix) {
99+
return name
100+
}
101+
102+
return FeatureBranchPrefix + name
103+
}
104+
105+
// GenHotfixBranchName .
106+
func GenHotfixBranchName(name string) string {
107+
if strings.HasPrefix(name, HotfixBranchPrefix) {
108+
return name
109+
}
110+
111+
return HotfixBranchPrefix + name
112+
}
113+
114+
func GenMRTitle(srcBranch, targetBranch string) string {
115+
return fmt.Sprintf("Merge %s to %s", srcBranch, targetBranch)
116+
}
117+
118+
// GenIssueBranchName .
119+
// @result = 1-milestoneTitle as default
120+
// fmt.Sprintf("%d-%s", issue.IID, milestone.Title)
121+
func GenIssueBranchName(name string, issueIID int) string {
122+
if strings.HasPrefix(name, strconv.Itoa(issueIID)+"-") {
123+
return name
124+
}
125+
126+
return fmt.Sprintf("%d-%s", issueIID, name)
127+
}

0 commit comments

Comments
 (0)