Skip to content

Commit 82a1496

Browse files
committed
feat(dash): enhance dash milestone display; and finish one todo.
1 parent b0a9c06 commit 82a1496

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

cmd/gitlab-flow/commands_dash.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ func getDashMilestoneOverviewSubCommand() *cli.Command {
5656
Category: "dash",
5757
Flags: []cli.Flag{
5858
&cli.StringFlag{
59-
Name: "milestone_name",
60-
Aliases: []string{"m"},
61-
Usage: "input `milestoneName` which you want to get its information",
62-
Required: true,
59+
Name: "milestone_name",
60+
Aliases: []string{"m"},
61+
Usage: "input `milestoneName` which you want to get its information, " +
62+
"default current branch milestone",
63+
Required: false,
6364
},
6465
&cli.StringFlag{
6566
Name: "branch_name",

internal/dash_impl.go

+26-15
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (d dashImpl) dealDataIntoFeatureDetail(
252252
}
253253

254254
var (
255-
_milestoneOverviewTblHeader = []string{"🏝Project🏖", "🎠MergeRequests🏕"}
255+
_milestoneOverviewTblHeader = []string{"🏝Project", "MR#Action", "🏕MR#WebURL"}
256256
)
257257

258258
func (d dashImpl) MilestoneOverview(milestoneName, branchFilter string) ([]byte, error) {
@@ -266,24 +266,25 @@ func (d dashImpl) MilestoneOverview(milestoneName, branchFilter string) ([]byte,
266266
if milestoneName == "" {
267267
// get milestoneName from feature branchName
268268
// query milestone name automatically when no milestone name provided.
269-
// TODO(@yeqown): optimize this logic, using repo method.
270269
featureBranchName, _ := d.gitOperator.CurrentBranch()
271270
featureBranchName = genFeatureBranchName(featureBranchName)
272-
branch, err := d.repo.QueryBranch(&repository.BranchDO{
273-
ProjectID: d.ctx.Project.ID,
274-
BranchName: featureBranchName,
275-
})
276-
if err == nil && branch != nil {
277-
milestone, _ := d.repo.QueryMilestone(&repository.MilestoneDO{MilestoneID: branch.MilestoneID})
278-
if milestone != nil {
279-
milestoneName = milestone.Title
280-
}
271+
// DONE(@yeqown): optimize this logic, using repo method.
272+
milestone, err := d.repo.QueryMilestoneByBranchName(d.ctx.Project.ID, featureBranchName)
273+
if milestone != nil {
274+
milestoneName = milestone.Title
281275
}
276+
282277
log.
283278
WithFields(log.Fields{
284279
"featureBranchName": featureBranchName,
280+
"error": err,
285281
}).
286-
Debugf("could not locate branch of current branch: %v", err)
282+
Debugf("locate milestone of current branch")
283+
}
284+
285+
if milestoneName == "" {
286+
return nil, errors.New("you must specify a milestone name or " +
287+
"sure you are using a branch which could get milestone")
287288
}
288289

289290
// query milestone by name (milestone-project), so there are many.
@@ -330,16 +331,26 @@ func (d dashImpl) MilestoneOverview(milestoneName, branchFilter string) ([]byte,
330331
Warnf("could not locate project merge request: %v", err)
331332
}
332333

333-
uris := make([]string, 0, len(mrs))
334+
// insert all merge requests of current project into tblData
334335
for _, mr := range mrs {
335-
uris = append(uris, fmt.Sprintf("%s➡️%s 🧲%s", mr.SourceBranch, mr.TargetBranch, mr.WebURL))
336+
tblData = append(tblData, []string{
337+
project.ProjectName,
338+
fmt.Sprintf("%s => %s", mr.SourceBranch, mr.TargetBranch),
339+
mr.WebURL,
340+
})
336341
}
337-
tblData = append(tblData, []string{project.ProjectName, strings.Join(uris, "\n")})
338342
}
339343

340344
buf := bytes.NewBuffer(nil)
341345
w := tablewriter.NewWriter(buf)
342346
w.SetHeader(_milestoneOverviewTblHeader)
347+
w.SetColumnColor(
348+
tablewriter.Colors{tablewriter.Bold, tablewriter.FgGreenColor},
349+
tablewriter.Colors{},
350+
tablewriter.Colors{},
351+
)
352+
w.SetRowLine(true)
353+
w.SetAutoMergeCells(true)
343354
w.AppendBulk(tblData)
344355
w.Render()
345356

internal/repository/flow_repo.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type IFlowRepository interface {
1717
SaveMilestone(m *MilestoneDO, txs ...*gorm2.DB) error
1818
QueryMilestone(filter *MilestoneDO) (*MilestoneDO, error)
1919
QueryMilestones(filter *MilestoneDO) ([]*MilestoneDO, error)
20+
QueryMilestoneByBranchName(projectId int, branchName string) (*MilestoneDO, error)
2021

2122
SaveBranch(m *BranchDO, txs ...*gorm2.DB) error
2223
BatchCreateBranch(records []*BranchDO, txs ...*gorm2.DB) error

internal/repository/impl/flow_repo_sqlite3.go

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/cenkalti/backoff/v4"
1212
"github.com/mattn/go-sqlite3"
13+
"github.com/pkg/errors"
1314
"github.com/yeqown/log"
1415
"gorm.io/driver/sqlite"
1516
gorm2 "gorm.io/gorm"
@@ -95,6 +96,7 @@ func ConnectDB(path string, debug bool) func() *gorm2.DB {
9596
}
9697
}
9798

99+
// NewBasedSqlite3
98100
// DONE(@yeqown): load or create sqlite3 database.
99101
func NewBasedSqlite3(connectFunc func() *gorm2.DB) repository.IFlowRepository {
100102
repo := sqliteFlowRepositoryImpl{
@@ -203,6 +205,21 @@ func (repo *sqliteFlowRepositoryImpl) QueryMilestones(
203205
return out, nil
204206
}
205207

208+
func (repo *sqliteFlowRepositoryImpl) QueryMilestoneByBranchName(projectId int, branchName string,
209+
) (*repository.MilestoneDO, error) {
210+
branch, err := repo.QueryBranch(&repository.BranchDO{
211+
ProjectID: projectId,
212+
BranchName: branchName,
213+
})
214+
215+
if err != nil {
216+
return nil, errors.Wrapf(err, "could not locate branch:"+branchName)
217+
}
218+
219+
milestone, err := repo.QueryMilestone(&repository.MilestoneDO{MilestoneID: branch.MilestoneID})
220+
return milestone, err
221+
}
222+
206223
func (repo *sqliteFlowRepositoryImpl) SaveBranch(m *repository.BranchDO, txs ...*gorm2.DB) (err error) {
207224
return repo.insertRecordWithCheck(repo.txIn(txs...), m)
208225
}

0 commit comments

Comments
 (0)