Skip to content

Commit d4f0c07

Browse files
committed
fix: hotfix close with query first.
1 parent 0852833 commit d4f0c07

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

cmd/gitlab-flow/commands_hotfix.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,13 @@ func getHotfixFinishSubCommand() *cli.Command {
6363
},
6464
},
6565
Action: func(c *cli.Context) error {
66-
log.
67-
WithFields(log.Fields{"args": c.Args().Slice()}).
68-
Debug("finish hotfix")
69-
70-
hotfixBranchName := c.Args().Get(0)
71-
if hotfixBranchName == "" {
72-
return errors.New("title could not be empty")
73-
}
66+
defer func() {
67+
log.
68+
WithFields(log.Fields{"args": c.Args().Slice()}).
69+
Debug("finish hotfix")
70+
}()
7471

72+
hotfixBranchName := c.String("hotfix_branch_name")
7573
confPath := c.String("conf_path")
7674
debug := c.Bool("debug")
7775
return getFlow(confPath, debug).HotfixFinish(hotfixBranchName)

internal/flow_impl.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -339,20 +339,40 @@ func (f flowImpl) HotfixFinish(hotfixBranchName string) error {
339339
return errors.Wrap(err, "locate issue failed")
340340
}
341341

342-
// MR to master
342+
// locate MR first
343+
mr, err := f.repo.QueryMergeRequest(&repository.MergeRequestDO{
344+
ProjectID: f.ctx.Project.ID,
345+
MilestoneID: issue.MilestoneID,
346+
IssueIID: issue.IssueIID,
347+
//SourceBranch: "",
348+
TargetBranch: types.MasterBranch.String(),
349+
})
350+
if err != nil && !repository.IsErrNotFound(err) {
351+
return errors.Wrap(err, "query database failed")
352+
}
353+
354+
// hit hotfix merge request
355+
if mr != nil {
356+
f.printAndOpenBrowser("Hotfix Merge Request", mr.WebURL)
357+
return nil
358+
}
359+
360+
// then create MR to master
343361
title := genMRTitle(hotfixBranchName, types.MasterBranch.String())
344-
mr, err := f.createMergeRequest(
362+
result, err := f.createMergeRequest(
345363
title, issue.Desc, 0, issue.IssueIID, hotfixBranchName, types.MasterBranch.String())
346364
if err != nil {
347365
return errors.Wrap(err, "create hotfix MR failed")
348366
}
349367

368+
f.printAndOpenBrowser("Hotfix Merge Request", result.WebURL)
369+
350370
log.
351371
WithFields(log.Fields{
352372
"issue": issue,
353-
"mergeRequest": mr,
373+
"mergeRequest": result,
354374
}).
355-
Debug("hotfix begin finished")
375+
Debug("hotfix finish done")
356376

357377
return nil
358378
}

internal/repository/flow_repo.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package repository
22

33
import (
4+
"github.com/pkg/errors"
45
gorm2 "gorm.io/gorm"
56
)
67

@@ -31,6 +32,10 @@ type IFlowRepository interface {
3132
QueryMergeRequests(filter *MergeRequestDO) ([]*MergeRequestDO, error)
3233
}
3334

35+
func IsErrNotFound(err error) bool {
36+
return errors.Is(err, gorm2.ErrRecordNotFound)
37+
}
38+
3439
// ProjectDO data model
3540
type ProjectDO struct {
3641
gorm2.Model

0 commit comments

Comments
 (0)