-
Notifications
You must be signed in to change notification settings - Fork 3
/
dash_impl.go
425 lines (374 loc) · 11.8 KB
/
dash_impl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package internal
import (
"bytes"
"fmt"
"os"
"strconv"
"strings"
"text/template"
gitop "github.com/yeqown/gitlab-flow/internal/git-operator"
"github.com/yeqown/gitlab-flow/internal/repository"
"github.com/yeqown/gitlab-flow/internal/repository/impl"
"github.com/yeqown/gitlab-flow/internal/types"
"github.com/yeqown/gitlab-flow/pkg"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/yeqown/log"
)
type dashImpl struct {
ctx *types.FlowContext
repo repository.IFlowRepository
gitOperator gitop.IGitOperator
}
func NewDash(ctx *types.FlowContext) IDash {
if ctx == nil {
log.Fatal("empty FlowContext initialized")
panic("can not reach")
}
log.
WithField("context", ctx).
Debugf("constructing dash")
dash := dashImpl{
ctx: ctx,
repo: impl.NewBasedSqlite3(impl.ConnectDB(ctx.ConfPath(), ctx.IsDebug())),
gitOperator: gitop.NewBasedCmd(ctx.CWD()),
}
// DONE(@yeqown): need load project info from local database.
if err := dash.fillContextWithProject(); err != nil {
log.
Fatalf("could not locate project(%s): %v", ctx.ProjectName(), err)
}
return dash
}
var (
detailTpl *template.Template
detailTplPattern = `
🚗 Project's Name : {{.projectTitle}} (ID:{{.projectID}})
🚕 Project's URL : {{.projectURL}}
🚌 Milestone Title : {{.milestoneTitle}} (ID:{{.milestoneID}})
🎯 Milestone Desc : {{.milestoneDesc}}
🤡 Feature Branch : {{.featureBranch}}
👽 Milestone URL : {{.milestoneWebURL}}
`
_featureDetailTblHeader = []string{"MR#Src", "MR#Target", "MR#WebURL", "Issue#IID", "Issue#Desc"}
_featureDetailIssueTblHeader = []string{"Issue#IID", "Issue#Title", "Issue#Desc", "Issue#WebURL"}
)
func init() {
detailTpl = template.Must(
template.New("detail").Parse(detailTplPattern))
}
func (d dashImpl) fillContextWithProject() error {
// DONE(@yeqown): fill project information from local repository or remote gitlab repository.
// DONE(@yeqown): projectName would be different from project path, use git repository name as project name.
projectName := d.ctx.ProjectName()
// get from local
projects, err := d.repo.QueryProjects(&repository.ProjectDO{ProjectName: projectName})
if err == nil && len(projects) != 0 {
// should let user choose one
matched, err2 := chooseOneProjectInteractively(projects)
if err2 == nil {
d.ctx.InjectProject(&types.ProjectBasics{
ID: matched.ProjectID,
Name: matched.ProjectName,
WebURL: matched.WebURL,
})
return nil
}
}
log.
WithFields(log.Fields{"project": projectName}).
Fatalf("could not found from local: %v", err)
return fmt.Errorf("could not match project(%s) from remote: %v", projectName, err)
}
// FeatureDetail get feature detail of current project:
// * basic information to current milestone.
// * all merge request and its related issue created in current milestone.
// * all issues created in current milestone with web url.
func (d dashImpl) FeatureDetail(branchName string) ([]byte, error) {
if branchName == "" {
out, err := d.gitOperator.CurrentBranch()
if out == "" || err != nil {
log.
WithFields(log.Fields{
"branch": branchName,
"err": err,
}).
Debug("dashImpl.FeatureDetail get branch name failed")
return nil, errors.Wrap(errInvalidFeatureName, "dashImpl.FeatureDetail.CurrentBranch")
}
branchName = out
// if current branch name could be parsed to feature branch name, then use it.
if !isFeatureName(branchName) {
out, ok := tryParseFeatureNameFrom(branchName, false)
if !ok {
log.
WithFields(log.Fields{
"branchName": branchName,
"out": out,
"ok": ok,
}).
Debug("dashImpl.FeatureDetail could not parse branch name by default")
return nil, errors.Wrap(errInvalidFeatureName, "dashImpl.FeatureDetail.tryParseFeatureNameFrom")
}
branchName = out
}
}
branchName = genFeatureBranchName(branchName)
// locate branch
branch, err := d.repo.QueryBranch(&repository.BranchDO{
ProjectID: d.ctx.Project().ID,
BranchName: branchName,
})
if err != nil {
return nil, errors.Wrap(err, "could not locate branch:"+branchName)
}
// query milestone
milestone, err := d.repo.QueryMilestone(&repository.MilestoneDO{
ProjectID: d.ctx.Project().ID,
MilestoneID: branch.MilestoneID,
})
if err != nil {
return nil, errors.Wrap(err, "dashImpl.FeatureDetail query milestone")
}
// query all merge requests related to milestone.
mrs, err := d.repo.QueryMergeRequests(&repository.MergeRequestDO{
ProjectID: d.ctx.Project().ID,
MilestoneID: branch.MilestoneID,
})
if err != nil {
return nil, errors.Wrap(err, "dashImpl.FeatureDetail query mergeRequest")
}
// query all issues related to milestone.
issues, err := d.repo.QueryIssues(&repository.IssueDO{
ProjectID: d.ctx.Project().ID,
MilestoneID: branch.MilestoneID,
})
if err != nil {
return nil, errors.Wrap(err, "dashImpl.FeatureDetail query issues")
}
return d.dealDataIntoFeatureDetail(branch, milestone, issues, mrs)
}
// dealDataIntoFeatureDetail deal all data related to feature branch into template and tables.
func (d dashImpl) dealDataIntoFeatureDetail(
bm *repository.BranchDO, milestone *repository.MilestoneDO,
issues []*repository.IssueDO, mrs []*repository.MergeRequestDO,
) ([]byte, error) {
// rework issue
issueCache := make(map[int]*repository.IssueDO)
issueTblData := make([][]string, len(issues))
for idx, v := range issues {
issueCache[v.IssueIID] = v
issueTblData[idx] = []string{
strconv.Itoa(v.IssueIID),
v.Title,
v.Desc,
v.WebURL,
}
}
mrTblData := make([][]string, len(mrs))
for idx, mr := range mrs {
issue, ok := issueCache[mr.IssueIID]
if !ok {
log.
WithFields(log.Fields{
"mergeRequestURL": mr.WebURL,
}).
Warn("no issues found with merge request")
issue = new(repository.IssueDO)
}
mrTblData[idx] = []string{
mr.SourceBranch, //
mr.TargetBranch, //
mr.WebURL, // MR-URL
strconv.Itoa(issue.IssueIID), // issue.issueIID
issue.Desc, // issue.Title
}
}
log.
WithFields(log.Fields{"mrTblData": mrTblData}).
Debug("mrTblData is conducted")
data := map[string]interface{}{
"projectTitle": d.ctx.Project().Name,
"projectID": d.ctx.Project().ID,
"projectURL": d.ctx.Project().WebURL,
"milestoneTitle": milestone.Title,
"milestoneID": milestone.MilestoneID,
"milestoneDesc": milestone.Desc,
"featureBranch": bm.BranchName,
"milestoneWebURL": milestone.WebURL,
}
buf := bytes.NewBuffer(nil)
if err := detailTpl.Execute(buf, data); err != nil {
return nil, errors.Wrap(err, "detailTpl.Execute")
}
buf.WriteString("All Merge Requests:\n")
// output all merge request into table
w := tablewriter.NewWriter(buf)
w.SetHeader(_featureDetailTblHeader)
w.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
w.SetAlignment(tablewriter.ALIGN_LEFT)
for _, row := range mrTblData {
// if master merge request
if row[1] == types.MasterBranch.String() {
w.Rich(row, []tablewriter.Colors{
{tablewriter.Bold, tablewriter.FgHiRedColor},
{tablewriter.Bold, tablewriter.FgHiRedColor},
{},
{tablewriter.Bold, tablewriter.FgBlackColor},
})
continue
}
if row[1] == types.TestBranch.String() {
w.Rich(row, []tablewriter.Colors{
{tablewriter.Bold, tablewriter.FgHiGreenColor},
{tablewriter.Bold, tablewriter.FgHiGreenColor},
{},
{tablewriter.Bold, tablewriter.FgBlackColor},
})
continue
}
w.Append(row)
}
w.Render()
buf.WriteString("All Issues:\n")
// output all issues into detail
w2 := tablewriter.NewWriter(buf)
w2.SetHeader(_featureDetailIssueTblHeader)
w2.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
w2.SetAlignment(tablewriter.ALIGN_LEFT)
w2.AppendBulk(issueTblData)
w2.Render()
return buf.Bytes(), nil
}
var (
_milestoneOverviewTblHeader = []string{"🏝Project", "MR#Action", "🏕MR#WebURL"}
)
func (d dashImpl) MilestoneOverview(milestoneName, branchFilter string) ([]byte, error) {
log.
WithFields(log.Fields{
"milestoneName": milestoneName,
"targetBranchFilter": branchFilter,
}).
Debug("MilestoneOverview called")
if milestoneName == "" {
// get milestoneName from feature branchName
// query milestone name automatically when no milestone name provided.
featureBranchName, _ := d.gitOperator.CurrentBranch()
featureBranchName = genFeatureBranchName(featureBranchName)
// DONE(@yeqown): optimize this logic, using repo method.
milestone, err := d.repo.QueryMilestoneByBranchName(d.ctx.Project().ID, featureBranchName)
if milestone != nil {
milestoneName = milestone.Title
}
log.
WithFields(log.Fields{
"featureBranchName": featureBranchName,
"error": err,
}).
Debugf("locate milestone of current branch")
}
if milestoneName == "" {
return nil, errors.New("you must specify a milestone name or " +
"sure you are using a branch which could get milestone")
}
// query milestone by name (milestone-project), so there are many.
projectMilestones, err := d.repo.QueryMilestones(&repository.MilestoneDO{
Title: milestoneName,
})
if err != nil {
return nil, errors.Wrap(err, "could not got any projectMilestones")
}
log.
WithFields(log.Fields{
"count": len(projectMilestones),
"projectMilestones": projectMilestones,
}).
Debug("catching milestone result")
// handle data and format output
tblData := make([][]string, 0, 8)
for _, v := range projectMilestones {
// locate project info
project, err := d.repo.QueryProject(&repository.ProjectDO{ProjectID: v.ProjectID})
if err != nil {
log.
WithFields(log.Fields{
"projectId": v.ProjectID,
}).
Warnf("could not locate project: %v", err)
}
// catching mergeRequest of each project
mrs, err := d.repo.QueryMergeRequests(&repository.MergeRequestDO{
ProjectID: v.ProjectID,
MilestoneID: v.MilestoneID,
TargetBranch: branchFilter,
})
if err != nil {
log.
WithFields(log.Fields{
"projectId": v.ProjectID,
"milestoneID": v.MilestoneID,
"targetBranch": branchFilter,
}).
Warnf("could not locate project merge request: %v", err)
}
// insert all merge requests of current project into tblData
for _, mr := range mrs {
tblData = append(tblData, []string{
project.ProjectName,
fmt.Sprintf("%s => %s", mr.SourceBranch, mr.TargetBranch),
mr.WebURL,
})
}
}
buf := bytes.NewBuffer(nil)
w := tablewriter.NewWriter(buf)
w.SetHeader(_milestoneOverviewTblHeader)
w.SetColumnColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgGreenColor},
tablewriter.Colors{},
tablewriter.Colors{},
)
w.SetRowLine(true)
w.SetAutoMergeCells(true)
w.AppendBulk(tblData)
w.Render()
return buf.Bytes(), nil
}
func (d dashImpl) ProjectDetail(module string) ([]byte, error) {
switch module {
case "home":
d.printAndOpenBrowser(d.ctx.Project().Name, d.ctx.Project().WebURL)
case "branch":
d.printAndOpenBrowser("branches", genProjectURL(d.ctx.Project().WebURL, "/-/branches"))
case "tag":
d.printAndOpenBrowser("tags", genProjectURL(d.ctx.Project().WebURL, "/-/tags"))
case "commit":
d.printAndOpenBrowser("commits", genProjectURL(d.ctx.Project().WebURL, "/commits/master"))
}
return nil, nil
}
func genProjectURL(base, suffix string) string {
return base + suffix
}
// printAndOpenBrowser print WebURL into stdout and open web browser.
func (d dashImpl) printAndOpenBrowser(title, url string) {
if len(title) == 0 && len(url) == 0 {
log.Warn("could not execute printAndOpenBrowser with empty title and url")
return
}
if !strings.HasPrefix(url, "http") {
log.Warnf("invalid url format: %s", url)
return
}
var (
err1, err2 error
)
_, err1 = fmt.Fprintf(os.Stdout, _printTpl, title, url)
if d.ctx.ShouldOpenBrowser() {
err2 = pkg.OpenBrowser(url)
}
log.WithFields(log.Fields{
"err1": err1,
"err2": err2,
}).Debugf("printAndOpenBrowser")
}