Skip to content

Commit 154b12b

Browse files
authored
feat: new sync command (#9)
1 parent 1708764 commit 154b12b

File tree

7 files changed

+90
-34
lines changed

7 files changed

+90
-34
lines changed

cmd/gitlab-flow/commands.go

+10
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,13 @@ func getDashCommand() *cli.Command {
9393
Subcommands: getDashSubCommands(),
9494
}
9595
}
96+
97+
// getSyncCommand
98+
func getSyncCommand() *cli.Command {
99+
return &cli.Command{
100+
Name: "sync",
101+
Usage: "synchronize resource from remote gitlab server",
102+
Category: "sync",
103+
Subcommands: getSyncSubCommands(),
104+
}
105+
}

cmd/gitlab-flow/commands_feature.go

+1-30
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func getFeatureSubCommands() cli.Commands {
1717
getFeatureTestSubCommand(),
1818
getFeatureReleaseSubCommand(),
1919
getFeatureResolveConflictCommand(),
20-
getSyncMilestoneSubCommand(),
20+
// getSyncMilestoneSubCommand(), moved into `sync` commands
2121
}
2222
}
2323

@@ -164,32 +164,3 @@ func getFeatureResolveConflictCommand() *cli.Command {
164164
},
165165
}
166166
}
167-
168-
// getSyncMilestoneSubCommand
169-
func getSyncMilestoneSubCommand() *cli.Command {
170-
return &cli.Command{
171-
Name: "sync",
172-
Usage: "rebuild local data from remote gitlab repository",
173-
ArgsUsage: "",
174-
Category: "feature",
175-
Flags: []cli.Flag{
176-
&cli.IntFlag{
177-
Name: "milestone_id",
178-
Aliases: []string{"m"},
179-
Usage: "choose milestone manually",
180-
Required: false,
181-
},
182-
&cli.BoolFlag{
183-
Name: "interact",
184-
Aliases: []string{"i"},
185-
Usage: "choose milestone in the list load from remote repository",
186-
Value: false,
187-
},
188-
},
189-
Action: func(c *cli.Context) error {
190-
milestoneID := c.Int("milestoneID")
191-
interact := c.Bool("interact")
192-
return getFlow(c).SyncMilestone(milestoneID, interact)
193-
},
194-
}
195-
}

cmd/gitlab-flow/commands_sync.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"github.com/urfave/cli/v2"
5+
)
6+
7+
func getSyncSubCommands() cli.Commands {
8+
return cli.Commands{
9+
getSyncProjectCommand(),
10+
getSyncMilestoneSubCommand(),
11+
}
12+
}
13+
14+
// getSyncProjectCommand synchronize project from remote gitlab server into local database.
15+
func getSyncProjectCommand() *cli.Command {
16+
return &cli.Command{
17+
Name: "project",
18+
Usage: "synchronize project information from remote gitlab server into local database",
19+
ArgsUsage: "",
20+
Category: "sync",
21+
Flags: []cli.Flag{
22+
&cli.BoolFlag{
23+
Name: "sync-project",
24+
Value: true,
25+
Hidden: true,
26+
},
27+
},
28+
Action: func(c *cli.Context) error {
29+
return getFlow(c).SyncProject()
30+
},
31+
}
32+
}
33+
34+
// getSyncMilestoneSubCommand
35+
func getSyncMilestoneSubCommand() *cli.Command {
36+
return &cli.Command{
37+
Name: "milestone",
38+
Usage: "rebuild local features, branches, issues and merges from remote gitlab repository",
39+
ArgsUsage: "",
40+
Category: "sync",
41+
Flags: []cli.Flag{
42+
&cli.IntFlag{
43+
Name: "milestone_id",
44+
Aliases: []string{"m"},
45+
Usage: "choose milestone manually",
46+
Required: false,
47+
},
48+
&cli.BoolFlag{
49+
Name: "interact",
50+
Aliases: []string{"i"},
51+
Usage: "choose milestone in the list load from remote repository",
52+
Value: false,
53+
},
54+
},
55+
Action: func(c *cli.Context) error {
56+
milestoneID := c.Int("milestoneID")
57+
interact := c.Bool("interact")
58+
return getFlow(c).SyncMilestone(milestoneID, interact)
59+
},
60+
}
61+
}

cmd/gitlab-flow/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ func mountCommands(app *cli.App) {
3535
getFeatureCommand(),
3636
getHotfixCommand(),
3737
getDashCommand(),
38+
getSyncCommand(),
3839
}
3940
}

cmd/gitlab-flow/support.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func parseGlobalFlags(c *cli.Context) globalFlags {
7777
DebugMode: c.Bool("debug"),
7878
ProjectName: c.String("project"),
7979
OpenBrowser: c.Bool("web"),
80-
ForceRemote: c.Bool("force-remote"),
80+
ForceRemote: c.Bool("force-remote") || c.Bool("sync-project"),
8181
CWD: c.String("cwd"),
8282
}
8383
}

internal/flow.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ type IFlow interface {
4545
// HotfixFinish open the WebURL of merge request which is from hotfix branch to types.MasterBranch.
4646
HotfixFinish(hotfixBranchName string) error
4747

48-
// SyncMilestone synchronize remote repository milestone and
49-
// related issues / merge requests to local.
48+
// SyncProject synchronize project information from remote gitlab server.
49+
SyncProject() error
50+
// SyncMilestone synchronize remote repository milestone and related issues / merge requests to local.
5051
SyncMilestone(milestoneID int, interact bool) error
5152
}
5253

internal/flow_impl.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewFlow(ctx *types.FlowContext) IFlow {
5252
repo: impl.NewBasedSqlite3(impl.ConnectDB(ctx.ConfPath(), ctx.Conf.DebugMode)),
5353
}
5454

55-
// flowContext with null project information, so we need to fill it.
55+
// if flowContext has NONE project information, so we need to fill it.
5656
if err := flow.fillContextWithProject(); err != nil {
5757
log.
5858
Fatalf("could not locate project(%s): %v", ctx.ProjectName(), err)
@@ -609,6 +609,18 @@ func (f flowImpl) SyncMilestone(milestoneID int, interact bool) error {
609609
return nil
610610
}
611611

612+
// SyncProject want to rebuild project manually while project is not exists in local database.
613+
func (f flowImpl) SyncProject() error {
614+
// NewFlow get project information earlier than SyncProject method, so synchronize project only need to
615+
// set FlowContext.forceRemote as true which will read project information from remote rather than
616+
// load from local database.
617+
618+
// forceRemote was set by `parseGlobalFlags` function.
619+
// lookup parseGlobalFlags for more detail.
620+
621+
return nil
622+
}
623+
612624
// syncFormatResultIntoDO rebuild local data from remote gitlab repository.
613625
// @return issues, mrs, branches, featureBranchName
614626
func (f flowImpl) syncFormatResultIntoDO(

0 commit comments

Comments
 (0)