Skip to content

Commit d0bc0a2

Browse files
committed
feat(types/context): hide all attributes of context, avoiding wrong modifying from other package.
1 parent 97f8c0f commit d0bc0a2

File tree

3 files changed

+128
-73
lines changed

3 files changed

+128
-73
lines changed

internal/dash_impl.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func NewDash(ctx *types.FlowContext) IDash {
3737

3838
dash := dashImpl{
3939
ctx: ctx,
40-
repo: impl.NewBasedSqlite3(impl.ConnectDB(ctx.ConfPath(), ctx.Conf.DebugMode)),
41-
gitOperator: gitop.NewBasedCmd(ctx.CWD),
40+
repo: impl.NewBasedSqlite3(impl.ConnectDB(ctx.ConfPath(), ctx.IsDebug())),
41+
gitOperator: gitop.NewBasedCmd(ctx.CWD()),
4242
}
4343

4444
// DONE(@yeqown): need load project info from local database.
@@ -77,14 +77,14 @@ func (d dashImpl) fillContextWithProject() error {
7777
// get from local
7878
projects, err := d.repo.QueryProjects(&repository.ProjectDO{ProjectName: projectName})
7979
if err == nil && len(projects) != 0 {
80-
// should let user to choose one
81-
matched, err := chooseOneProjectInteractively(projects)
82-
if err == nil {
83-
d.ctx.Project = &types.ProjectBasics{
80+
// should let user choose one
81+
matched, err2 := chooseOneProjectInteractively(projects)
82+
if err2 == nil {
83+
d.ctx.InjectProject(&types.ProjectBasics{
8484
ID: matched.ProjectID,
8585
Name: matched.ProjectName,
8686
WebURL: matched.WebURL,
87-
}
87+
})
8888
return nil
8989
}
9090
}
@@ -133,7 +133,7 @@ func (d dashImpl) FeatureDetail(branchName string) ([]byte, error) {
133133
branchName = genFeatureBranchName(branchName)
134134
// locate branch
135135
branch, err := d.repo.QueryBranch(&repository.BranchDO{
136-
ProjectID: d.ctx.Project.ID,
136+
ProjectID: d.ctx.Project().ID,
137137
BranchName: branchName,
138138
})
139139
if err != nil {
@@ -142,7 +142,7 @@ func (d dashImpl) FeatureDetail(branchName string) ([]byte, error) {
142142

143143
// query milestone
144144
milestone, err := d.repo.QueryMilestone(&repository.MilestoneDO{
145-
ProjectID: d.ctx.Project.ID,
145+
ProjectID: d.ctx.Project().ID,
146146
MilestoneID: branch.MilestoneID,
147147
})
148148
if err != nil {
@@ -151,7 +151,7 @@ func (d dashImpl) FeatureDetail(branchName string) ([]byte, error) {
151151

152152
// query all merge requests related to milestone.
153153
mrs, err := d.repo.QueryMergeRequests(&repository.MergeRequestDO{
154-
ProjectID: d.ctx.Project.ID,
154+
ProjectID: d.ctx.Project().ID,
155155
MilestoneID: branch.MilestoneID,
156156
})
157157
if err != nil {
@@ -160,7 +160,7 @@ func (d dashImpl) FeatureDetail(branchName string) ([]byte, error) {
160160

161161
// query all issues related to milestone.
162162
issues, err := d.repo.QueryIssues(&repository.IssueDO{
163-
ProjectID: d.ctx.Project.ID,
163+
ProjectID: d.ctx.Project().ID,
164164
MilestoneID: branch.MilestoneID,
165165
})
166166
if err != nil {
@@ -214,9 +214,9 @@ func (d dashImpl) dealDataIntoFeatureDetail(
214214
Debug("mrTblData is conducted")
215215

216216
data := map[string]interface{}{
217-
"projectTitle": d.ctx.Project.Name,
218-
"projectID": d.ctx.Project.ID,
219-
"projectURL": d.ctx.Project.WebURL,
217+
"projectTitle": d.ctx.Project().Name,
218+
"projectID": d.ctx.Project().ID,
219+
"projectURL": d.ctx.Project().WebURL,
220220
"milestoneTitle": milestone.Title,
221221
"milestoneID": milestone.MilestoneID,
222222
"milestoneDesc": milestone.Desc,
@@ -292,7 +292,7 @@ func (d dashImpl) MilestoneOverview(milestoneName, branchFilter string) ([]byte,
292292
featureBranchName, _ := d.gitOperator.CurrentBranch()
293293
featureBranchName = genFeatureBranchName(featureBranchName)
294294
// DONE(@yeqown): optimize this logic, using repo method.
295-
milestone, err := d.repo.QueryMilestoneByBranchName(d.ctx.Project.ID, featureBranchName)
295+
milestone, err := d.repo.QueryMilestoneByBranchName(d.ctx.Project().ID, featureBranchName)
296296
if milestone != nil {
297297
milestoneName = milestone.Title
298298
}
@@ -383,13 +383,13 @@ func (d dashImpl) MilestoneOverview(milestoneName, branchFilter string) ([]byte,
383383
func (d dashImpl) ProjectDetail(module string) ([]byte, error) {
384384
switch module {
385385
case "home":
386-
d.printAndOpenBrowser(d.ctx.Project.Name, d.ctx.Project.WebURL)
386+
d.printAndOpenBrowser(d.ctx.Project().Name, d.ctx.Project().WebURL)
387387
case "branch":
388-
d.printAndOpenBrowser("branches", genProjectURL(d.ctx.Project.WebURL, "/-/branches"))
388+
d.printAndOpenBrowser("branches", genProjectURL(d.ctx.Project().WebURL, "/-/branches"))
389389
case "tag":
390-
d.printAndOpenBrowser("tags", genProjectURL(d.ctx.Project.WebURL, "/-/tags"))
390+
d.printAndOpenBrowser("tags", genProjectURL(d.ctx.Project().WebURL, "/-/tags"))
391391
case "commit":
392-
d.printAndOpenBrowser("commits", genProjectURL(d.ctx.Project.WebURL, "/commits/master"))
392+
d.printAndOpenBrowser("commits", genProjectURL(d.ctx.Project().WebURL, "/commits/master"))
393393
}
394394

395395
return nil, nil
@@ -415,7 +415,7 @@ func (d dashImpl) printAndOpenBrowser(title, url string) {
415415
)
416416

417417
_, err1 = fmt.Fprintf(os.Stdout, _printTpl, title, url)
418-
if d.ctx.Conf.OpenBrowser {
418+
if d.ctx.ShouldOpenBrowser() {
419419
err2 = pkg.OpenBrowser(url)
420420
}
421421
log.WithFields(log.Fields{

internal/flow_impl.go

+35-36
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ func checkOAuthAccessToken(ctx *types.FlowContext) {
5959

6060
c.OAuth.AccessToken = accessToken
6161
c.OAuth.RefreshToken = refreshToken
62-
6362
// update context oauth configuration
64-
ctx.Conf.OAuth.AccessToken = accessToken
65-
ctx.Conf.OAuth.RefreshToken = refreshToken
63+
ctx.GetOAuth().AccessToken = accessToken
64+
ctx.GetOAuth().RefreshToken = refreshToken
6665

6766
if err := conf.Save(ctx.ConfPath(), c, nil); err != nil {
6867
log.
@@ -88,9 +87,9 @@ func NewFlow(ctx *types.FlowContext) IFlow {
8887

8988
flow := &flowImpl{
9089
ctx: ctx,
91-
gitlabOperator: gitlabop.NewGitlabOperator(ctx.Conf.OAuth.AccessToken, ctx.Conf.GitlabAPIURL),
92-
gitOperator: gitop.NewBasedCmd(ctx.CWD),
93-
repo: impl.NewBasedSqlite3(impl.ConnectDB(ctx.ConfPath(), ctx.Conf.DebugMode)),
90+
gitlabOperator: gitlabop.NewGitlabOperator(ctx.GetOAuth().AccessToken, ctx.APIEndpoint()),
91+
gitOperator: gitop.NewBasedCmd(ctx.CWD()),
92+
repo: impl.NewBasedSqlite3(impl.ConnectDB(ctx.ConfPath(), ctx.IsDebug())),
9493
}
9594

9695
// if flowContext has NONE project information, so we need to fill it.
@@ -122,13 +121,13 @@ func (f flowImpl) fillContextWithProject() error {
122121
projects, err = f.repo.QueryProjects(&repository.ProjectDO{ProjectName: projectName})
123122
if err == nil && len(projects) != 0 {
124123
// locate project from local, and there are maybe more than one project.
125-
matched, err := chooseOneProjectInteractively(projects)
126-
if err == nil {
127-
f.ctx.Project = &types.ProjectBasics{
124+
matched, err2 := chooseOneProjectInteractively(projects)
125+
if err2 == nil {
126+
f.ctx.InjectProject(&types.ProjectBasics{
128127
ID: matched.ProjectID,
129128
Name: matched.ProjectName,
130129
WebURL: matched.WebURL,
131-
}
130+
})
132131
return nil
133132
}
134133
}
@@ -166,7 +165,7 @@ locateFromRemote:
166165
projectDO := repository.ProjectDO{
167166
ProjectName: projectName,
168167
ProjectID: v.ID,
169-
LocalDir: f.ctx.CWD,
168+
LocalDir: f.ctx.CWD(),
170169
WebURL: v.WebURL,
171170
}
172171
remoteMatched = append(remoteMatched, &projectDO)
@@ -182,11 +181,11 @@ locateFromRemote:
182181
Warn("could not save project")
183182
}
184183

185-
f.ctx.Project = &types.ProjectBasics{
184+
f.ctx.InjectProject(&types.ProjectBasics{
186185
ID: matched.ProjectID,
187186
Name: matched.ProjectName,
188187
WebURL: matched.WebURL,
189-
}
188+
})
190189
return nil
191190
}
192191

@@ -268,7 +267,7 @@ func (f flowImpl) FeatureResolveConflict(opc *types.OpFeatureContext, targetBran
268267

269268
// locate feature branch
270269
featureBranch, err := f.repo.QueryBranch(&repository.BranchDO{
271-
ProjectID: f.ctx.Project.ID,
270+
ProjectID: f.ctx.Project().ID,
272271
BranchName: opc.FeatureBranchName,
273272
})
274273
if err != nil {
@@ -303,7 +302,7 @@ func (f flowImpl) FeatureBeginIssue(opc *types.OpFeatureContext, title, desc str
303302
opc.FeatureBranchName = genFeatureBranchName(opc.FeatureBranchName)
304303

305304
featureBranch, err := f.repo.QueryBranch(&repository.BranchDO{
306-
ProjectID: f.ctx.Project.ID,
305+
ProjectID: f.ctx.Project().ID,
307306
BranchName: opc.FeatureBranchName,
308307
})
309308
if err != nil {
@@ -312,7 +311,7 @@ func (f flowImpl) FeatureBeginIssue(opc *types.OpFeatureContext, title, desc str
312311

313312
// query milestone
314313
milestone, err := f.repo.QueryMilestone(&repository.MilestoneDO{
315-
ProjectID: f.ctx.Project.ID,
314+
ProjectID: f.ctx.Project().ID,
316315
MilestoneID: featureBranch.MilestoneID,
317316
})
318317
if err != nil {
@@ -361,7 +360,7 @@ func (f flowImpl) FeatureFinishIssue(opc *types.OpFeatureContext, issueBranchNam
361360
)
362361
// locate issue branch.
363362
if b, err := f.repo.QueryBranch(&repository.BranchDO{
364-
ProjectID: f.ctx.Project.ID,
363+
ProjectID: f.ctx.Project().ID,
365364
BranchName: issueBranchName,
366365
}); err != nil {
367366
return errors.Wrapf(err, "locate issue branch(%s) failed", issueBranchName)
@@ -379,7 +378,7 @@ func (f flowImpl) FeatureFinishIssue(opc *types.OpFeatureContext, issueBranchNam
379378
}
380379
opc.FeatureBranchName = genFeatureBranchName(opc.FeatureBranchName)
381380
//if _, err := f.repo.QueryBranch(&repository.BranchDO{
382-
// ProjectID: f.ctx.Project.ID,
381+
// ProjectID: f.ctx.Project().ID,
383382
// BranchName: featureBranchName,
384383
// MilestoneID: milestoneID,
385384
//}); err != nil {
@@ -393,7 +392,7 @@ func (f flowImpl) FeatureFinishIssue(opc *types.OpFeatureContext, issueBranchNam
393392

394393
// locate MR
395394
mr, err = f.repo.QueryMergeRequest(&repository.MergeRequestDO{
396-
ProjectID: f.ctx.Project.ID,
395+
ProjectID: f.ctx.Project().ID,
397396
IssueIID: issueIID,
398397
MilestoneID: milestoneID,
399398
SourceBranch: issueBranchName,
@@ -402,7 +401,7 @@ func (f flowImpl) FeatureFinishIssue(opc *types.OpFeatureContext, issueBranchNam
402401
if err != nil && !repository.IsErrNotFound(err) {
403402
log.
404403
WithFields(log.Fields{
405-
"projectID": f.ctx.Project.ID,
404+
"projectID": f.ctx.Project().ID,
406405
"issueIID": issueIID,
407406
"milestoneID": milestoneID,
408407
"sourceBranch": issueBranchName,
@@ -479,7 +478,7 @@ func (f flowImpl) HotfixFinish(hotfixBranchName string) error {
479478

480479
hotfixBranchName = genHotfixBranchName(hotfixBranchName)
481480
_, err := f.repo.QueryBranch(&repository.BranchDO{
482-
ProjectID: f.ctx.Project.ID,
481+
ProjectID: f.ctx.Project().ID,
483482
BranchName: hotfixBranchName,
484483
})
485484
if err != nil {
@@ -488,7 +487,7 @@ func (f flowImpl) HotfixFinish(hotfixBranchName string) error {
488487

489488
// locate issue
490489
issue, err := f.repo.QueryIssue(&repository.IssueDO{
491-
ProjectID: f.ctx.Project.ID,
490+
ProjectID: f.ctx.Project().ID,
492491
RelatedBranch: hotfixBranchName,
493492
})
494493
if err != nil {
@@ -497,7 +496,7 @@ func (f flowImpl) HotfixFinish(hotfixBranchName string) error {
497496

498497
// locate MR first
499498
mr, err := f.repo.QueryMergeRequest(&repository.MergeRequestDO{
500-
ProjectID: f.ctx.Project.ID,
499+
ProjectID: f.ctx.Project().ID,
501500
MilestoneID: issue.MilestoneID,
502501
IssueIID: issue.IssueIID,
503502
//SourceBranch: "",
@@ -541,7 +540,7 @@ func (f flowImpl) HotfixFinish(hotfixBranchName string) error {
541540
//
542541
func (f flowImpl) SyncMilestone(milestoneID int, interact bool) error {
543542
ctx := context.Background()
544-
projectId := f.ctx.Project.ID
543+
projectId := f.ctx.Project().ID
545544

546545
// parameter checking
547546
if milestoneID == 0 && !interact {
@@ -677,7 +676,7 @@ func (f flowImpl) syncFormatResultIntoDO(
677676

678677
c = make(map[int]*repository.IssueDO)
679678
featureBranchName string
680-
projectID = f.ctx.Project.ID
679+
projectID = f.ctx.Project().ID
681680
milestoneID = milestone.ID
682681
)
683682

@@ -779,7 +778,7 @@ func (f flowImpl) createBranch(
779778
req := gitlabop.CreateBranchRequest{
780779
TargetBranch: targetBranchName,
781780
SrcBranch: srcBranch,
782-
ProjectID: f.ctx.Project.ID,
781+
ProjectID: f.ctx.Project().ID,
783782
//MilestoneID: milestoneID,
784783
//IssueID: issueIID,
785784
}
@@ -815,7 +814,7 @@ func (f flowImpl) createBranch(
815814
go func() {
816815
wg.Done()
817816
m := repository.BranchDO{
818-
ProjectID: f.ctx.Project.ID,
817+
ProjectID: f.ctx.Project().ID,
819818
MilestoneID: milestoneID,
820819
IssueIID: issueIID,
821820
BranchName: targetBranchName,
@@ -845,14 +844,14 @@ func (f flowImpl) createMilestone(title, desc string) (*gitlabop.CreateMilestone
845844
result, err := f.gitlabOperator.CreateMilestone(ctx, &gitlabop.CreateMilestoneRequest{
846845
Title: title,
847846
Desc: desc,
848-
ProjectID: f.ctx.Project.ID,
847+
ProjectID: f.ctx.Project().ID,
849848
})
850849
if err != nil {
851850
return nil, errors.Wrap(err, "CreateMilestone failed")
852851
}
853852

854853
if err = f.repo.SaveMilestone(&repository.MilestoneDO{
855-
ProjectID: f.ctx.Project.ID,
854+
ProjectID: f.ctx.Project().ID,
856855
MilestoneID: result.ID,
857856
Title: title,
858857
Desc: desc,
@@ -878,7 +877,7 @@ func (f flowImpl) createIssue(title, desc, relatedBranch string, milestoneID int
878877
Desc: desc,
879878
RelatedBranch: relatedBranch,
880879
MilestoneID: milestoneID,
881-
ProjectID: f.ctx.Project.ID,
880+
ProjectID: f.ctx.Project().ID,
882881
})
883882
if err != nil {
884883
return nil, errors.Wrap(err, "create Issue failed")
@@ -888,7 +887,7 @@ func (f flowImpl) createIssue(title, desc, relatedBranch string, milestoneID int
888887
IssueIID: result.IID,
889888
Title: title,
890889
Desc: desc,
891-
ProjectID: f.ctx.Project.ID,
890+
ProjectID: f.ctx.Project().ID,
892891
MilestoneID: milestoneID,
893892
RelatedBranch: relatedBranch,
894893
WebURL: result.WebURL,
@@ -923,7 +922,7 @@ func (f flowImpl) createMergeRequest(
923922
TargetBranch: targetBranch,
924923
MilestoneID: milestoneID,
925924
IssueIID: issueIID,
926-
ProjectID: f.ctx.Project.ID,
925+
ProjectID: f.ctx.Project().ID,
927926
})
928927
if err != nil {
929928
return nil, errors.Wrap(err, "create MR failed")
@@ -939,7 +938,7 @@ func (f flowImpl) createMergeRequest(
939938
Debug("create mr success")
940939

941940
if err = f.repo.SaveMergeRequest(&repository.MergeRequestDO{
942-
ProjectID: f.ctx.Project.ID,
941+
ProjectID: f.ctx.Project().ID,
943942
MilestoneID: milestoneID,
944943
IssueIID: issueIID,
945944
MergeRequestID: result.ID,
@@ -962,7 +961,7 @@ func (f flowImpl) createMergeRequest(
962961
func (f flowImpl) featureProcessMR(
963962
featureBranchName string, targetBranchName types.BranchTyp, forceCreateMR bool) error {
964963
featureBranch, err := f.repo.QueryBranch(&repository.BranchDO{
965-
ProjectID: f.ctx.Project.ID,
964+
ProjectID: f.ctx.Project().ID,
966965
BranchName: featureBranchName,
967966
})
968967
if err != nil {
@@ -976,7 +975,7 @@ func (f flowImpl) featureProcessMR(
976975
}
977976
// query feature MR first
978977
mr, err = f.repo.QueryMergeRequest(&repository.MergeRequestDO{
979-
ProjectID: f.ctx.Project.ID,
978+
ProjectID: f.ctx.Project().ID,
980979
MilestoneID: featureBranch.MilestoneID,
981980
IssueIID: featureBranch.IssueIID,
982981
SourceBranch: featureBranchName,
@@ -1032,7 +1031,7 @@ func (f flowImpl) printAndOpenBrowser(title, url string) {
10321031
)
10331032

10341033
_, err1 = fmt.Fprintf(os.Stdout, _printTpl, title, url)
1035-
if f.ctx.Conf.OpenBrowser {
1034+
if f.ctx.ShouldOpenBrowser() {
10361035
err2 = pkg.OpenBrowser(url)
10371036
}
10381037
log.WithFields(log.Fields{

0 commit comments

Comments
 (0)