Skip to content

Commit 6c2001a

Browse files
committed
close #257 jira bitbucket smart commit expect hashtag in front of magic word
1 parent 6c72b3e commit 6c2001a

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

cmd/wrapup.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ var wrapupCmd = &cobra.Command{
2020
cmdperm.RequiredCurDirRepository()
2121
},
2222
Run: func(cmd *cobra.Command, args []string) {
23-
phlow.WrapUp()
23+
phlow.WrapUpCaller()
2424
},
2525
}
2626

2727
func init() {
2828
RootCmd.AddCommand(wrapupCmd)
2929

3030
wrapupCmd.Flags().StringVar(&options.GlobalFlagForceMessage, "force", "", "use a custom commit message instead")
31+
32+
wrapupCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in your .gitconfig files")
3133
}

phlow/wrapup.go

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,59 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/praqma/git-phlow/plugins"
8+
79
"os"
810

11+
"errors"
12+
"strconv"
13+
14+
"github.com/praqma/git-phlow/executor"
915
"github.com/praqma/git-phlow/githandler"
16+
"github.com/praqma/git-phlow/setting"
1017
"github.com/praqma/git-phlow/options"
11-
"github.com/praqma/git-phlow/executor"
12-
"strconv"
13-
"errors"
1418
)
1519

20+
//WrapUpCaller ...
21+
func WrapUpCaller() {
22+
conf := setting.NewProjectStg(options.GlobalFlagTarget)
23+
24+
if conf.Service == "jira" {
25+
WrapUp("#close ", options.GlobalFlagForceMessage, plugins.KeyFromBranchName)
26+
return
27+
}
28+
29+
if conf.Service == "github" {
30+
WrapUp("close #", options.GlobalFlagForceMessage, plugins.IssueFromBranchName)
31+
return
32+
}
33+
34+
}
35+
36+
//GenerateMessage ...
37+
//generates a message from a branch with a given issue extractor
38+
func GenerateMessage(branch string, smartCommitPrefix string, extractor plugins.IssueExtractor, forceMessage string) (string, error) {
39+
iss, err := extractor(branch)
40+
if err != nil {
41+
return "", err
42+
}
43+
44+
//remove the issue from the branch name
45+
branch = strings.TrimPrefix(branch, iss)
46+
47+
if forceMessage != "" {
48+
return smartCommitPrefix + iss + " " + forceMessage, nil
49+
}
50+
51+
//Replace - with white space
52+
msg := strings.Replace(branch, "-", " ", -1)
53+
54+
//prepend smart commit prefix
55+
return smartCommitPrefix + iss + msg, nil
56+
}
57+
1658
//WrapUp ...
17-
func WrapUp() {
59+
func WrapUp(smartCommitPrefix, force string, extractor plugins.IssueExtractor) {
1860
git := githandler.Git{Run: executor.RunGit}
1961

2062
//Add all files to index
@@ -32,34 +74,20 @@ func WrapUp() {
3274

3375
//Retrieve branch info - current branch
3476
info := githandler.AsList(out)
35-
var commitMessage string
3677

37-
issue, err := GetJIRAIssue(info.Current)
78+
msg, err := GenerateMessage(info.Current, smartCommitPrefix, extractor, force)
3879
if err != nil {
39-
40-
if options.GlobalFlagForceMessage != "" {
41-
commitMessage = "close #" + strings.Split(info.Current, "-")[0] + " " + options.GlobalFlagForceMessage
42-
} else {
43-
commitMessage = "close #" + strings.Replace(info.Current, "-", " ", -1)
44-
}
45-
} else {
46-
msg := strings.TrimPrefix(info.Current, issue)
47-
48-
if options.GlobalFlagForceMessage != "" {
49-
commitMessage = "close #" + issue + " " + options.GlobalFlagForceMessage
50-
} else {
51-
commitMessage = "close #" + issue + strings.Replace(msg, "-", " ", -1)
52-
}
53-
80+
fmt.Println(err)
81+
os.Exit(1)
5482
}
5583

56-
_, err = git.Commit("-m", commitMessage)
84+
_, err = git.Commit("-m", msg)
5785
if err != nil {
5886
fmt.Println(err)
5987
return
6088
}
6189

62-
fmt.Fprintln(os.Stdout, commitMessage)
90+
fmt.Println(msg)
6391
}
6492

6593
func GetJIRAIssue(branch string) (string, error) {

0 commit comments

Comments
 (0)