Skip to content

Commit 6c72b3e

Browse files
committed
close #247 web command for jira
1 parent efba4a1 commit 6c72b3e

File tree

9 files changed

+154
-82
lines changed

9 files changed

+154
-82
lines changed

.gitconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
remote = origin
33
service = github
44
integration-branch = master
5-
issue-url = https://api.github.com
5+
issue-api = https://api.github.com
6+
issue-web = https://github.com
67
delivery-branch-prefix = ready
78

cmd/config.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ Default configuration:
2828
service = jira
2929
issue_url = https://my.jira.instance.com
3030
delivery_branch_prefix = ready
31-
32-
33-
34-
35-
3631
`,
3732
Run: func(cmd *cobra.Command, args []string) {
3833
cmd.Help()

cmd/web.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,39 @@ package cmd
33
import (
44
"fmt"
55

6-
"os"
7-
"strconv"
8-
9-
"github.com/praqma/git-phlow/phlow"
106
"github.com/spf13/cobra"
117

128
"github.com/praqma/git-phlow/cmd/cmdperm"
139
"github.com/praqma/git-phlow/ui"
10+
"github.com/praqma/git-phlow/options"
11+
"github.com/praqma/git-phlow/phlow"
1412
)
1513

1614
// webCmd represents the web command
1715
var webCmd = &cobra.Command{
1816
Use: "web [issue]",
19-
Short: "open your issues on github",
17+
Short: "open your issues in the browser",
2018
Long: fmt.Sprintf(`
21-
%s opens a GitHub issue or GitHub issue list based on it's arguments.
19+
%s opens an issue in your default browser
20+
21+
The command uses a targetet configuration to figure out where you have your issues hosted e.g. Github or jira.
22+
Use the 'issue-web' field in the configuration to point to the url of the issue management system.
23+
24+
The command can take an argument of the issue you want to use.
2225
If no argument is given, it tries to find the issue of the currently checked out branch, if that fails it simply opens the GitHub issue list in your default browser.
2326
`, ui.Format.Bold("web")),
2427
PreRun: func(cmd *cobra.Command, args []string) {
2528
cmdperm.RequiredCurDirRepository()
2629
},
2730
Run: func(cmd *cobra.Command, args []string) {
28-
if len(args) > 0 {
29-
if val, err := strconv.Atoi(args[0]); err == nil {
30-
phlow.Web(val)
31-
} else {
32-
fmt.Println("Argument must be a number")
33-
os.Exit(0)
34-
}
35-
} else {
36-
phlow.Web(-1)
37-
}
31+
32+
phlow.WebCaller(args)
33+
3834
},
3935
}
4036

4137
func init() {
4238
RootCmd.AddCommand(webCmd)
39+
40+
webCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in your .phlow files")
4341
}

phlow/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Auth(INIBlock string, authorization plugins.Authorization, authentication p
4040

4141
if token != "" && user != "" {
4242
fmt.Printf("Checking token validity for %s... \n", service)
43-
isAuthenticated, err := authentication(conf.IssueURL, user, token)
43+
isAuthenticated, err := authentication(conf.IssueApi, user, token)
4444
if !isAuthenticated {
4545
fmt.Println("Token test expected HTTP code 200 but received " + err.Error())
4646
if ReadInput("Delete local token and reauthenticate? (y/n): ", os.Stdin) == "y" {
@@ -63,7 +63,7 @@ func Auth(INIBlock string, authorization plugins.Authorization, authentication p
6363
//Read user input password
6464
password := ReadPassword("password: ")
6565

66-
token, err = authorization(conf.IssueURL, username, password)
66+
token, err = authorization(conf.IssueApi, username, password)
6767
if err != nil {
6868
fmt.Println()
6969
fmt.Println(err)

phlow/web.go

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,106 @@ package phlow
22

33
import (
44
"fmt"
5-
"os"
6-
75
"github.com/praqma/git-phlow/executor"
86
"github.com/praqma/git-phlow/githandler"
9-
"github.com/praqma/git-phlow/plugins"
107
"github.com/praqma/git-phlow/setting"
8+
"github.com/praqma/git-phlow/options"
9+
"runtime"
10+
"github.com/praqma/git-phlow/plugins"
1111
)
1212

13-
//Web ...
14-
//opens the browser on the current or specified issue
15-
func Web(userIssue int) {
13+
//WebCaller ...
14+
//Executes command to web
15+
func WebCaller(args []string) {
16+
conf := setting.NewProjectStg(options.GlobalFlagTarget)
17+
18+
var issue = ""
19+
if len(args) > 0 {
20+
issue = args[0]
21+
}
22+
23+
if conf.Service == "github" {
24+
OpenGitHub(issue, conf, plugins.IssueFromBranchName, executor.RunCommand)
25+
return
26+
}
27+
28+
if conf.Service == "jira" {
29+
OpenJira(issue, conf, plugins.KeyFromBranchName, executor.RunCommand)
30+
return
31+
}
32+
33+
}
34+
35+
//OpenJira ...
36+
//try to display jira configuration
37+
func OpenJira(issue string, conf *setting.ProjectSetting, extractor plugins.IssueExtractor, runner executor.Runner) {
1638
git := githandler.Git{Run: executor.RunGit}
17-
conf := setting.NewProjectStg("default")
1839

19-
var issueListURL = "https://github.com/%s/%s/issues"
20-
var issueInfoURL = "https://github.com/%s/%s/issues/%d"
40+
branch, err := git.Branch()
41+
if err != nil {
42+
fmt.Println(err)
43+
return
44+
}
45+
branchInfo := githandler.AsList(branch)
2146

22-
//Get organisation and repository for repository
23-
remote, err := git.LSRemote("--get-url", conf.Remote)
47+
if issue != "" {
48+
LaunchBrowser(conf.IssueWeb+"/browse/"+issue, runner)
49+
return
50+
}
51+
52+
//No issues to get, just open the browser with the code.. For good measure
53+
key, err := extractor(branchInfo.Current)
2454
if err != nil {
2555
fmt.Println(err)
56+
LaunchBrowser(conf.IssueWeb, runner)
2657
return
2758
}
59+
LaunchBrowser(conf.IssueWeb+"/browse/"+key, runner)
60+
}
61+
62+
//OpenGitHub ...
63+
//Try to display github issues
64+
func OpenGitHub(issue string, conf *setting.ProjectSetting, extractor plugins.IssueExtractor, runner executor.Runner) {
65+
git := githandler.Git{Run: executor.RunGit}
2866

2967
branch, err := git.Branch()
3068
if err != nil {
3169
fmt.Println(err)
3270
return
3371
}
72+
3473
branchInfo := githandler.AsList(branch)
74+
75+
remote, err := git.LSRemote("--get-url", conf.Remote)
76+
if err != nil {
77+
fmt.Println(err)
78+
return
79+
}
80+
3581
orgAndRepo := githandler.OrgAndRepo(remote)
3682

37-
branchIssue := plugins.IssueFromBranchName(branchInfo.Current)
83+
ext := "/" + orgAndRepo.Organisation + "/" + orgAndRepo.Repository
3884

39-
if userIssue != -1 {
40-
issueInfoURL = fmt.Sprintf(issueInfoURL, orgAndRepo.Organisation, orgAndRepo.Repository, userIssue)
41-
_, err := executor.RunCommand("open", issueInfoURL)
42-
if err != nil {
43-
fmt.Println(err)
44-
}
45-
os.Exit(0)
85+
if issue != "" {
86+
LaunchBrowser(conf.IssueWeb+ext+"/issues/"+issue, runner)
87+
return
4688
}
4789

48-
if branchIssue != "-1" {
49-
issueInfoURL = fmt.Sprintf(issueInfoURL, orgAndRepo.Organisation, orgAndRepo.Repository, branchIssue)
50-
executor.RunCommand("open", issueInfoURL)
51-
os.Exit(0)
90+
//No issues to get, just open the browser with the code.. For good measure
91+
key, err := extractor(branchInfo.Current)
92+
if err != nil {
93+
fmt.Println(err)
94+
LaunchBrowser(conf.IssueWeb+ext, runner)
95+
return
5296
}
97+
LaunchBrowser(conf.IssueWeb+ext+"/issues/"+key, runner)
98+
}
99+
100+
func LaunchBrowser(link string, run executor.Runner) {
53101

54-
issueListURL = fmt.Sprintf(issueListURL, orgAndRepo.Organisation, orgAndRepo.Repository)
55-
executor.RunCommand("open", issueListURL)
102+
if runtime.GOOS == "windows" {
103+
run("explorer", link)
104+
return
105+
}
106+
run("open", link)
56107
}

phlow/workon.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,20 @@ func UpdateJIRAIssue(key string, conf *setting.ProjectSetting) (string, error) {
109109
user, _ := git.Config("--get", "phlow.jirauser")
110110
token, _ := git.Config("--get", "phlow.jiratoken")
111111

112-
issue, err := plugins.GetJiraIssue(conf.IssueURL, key, user, token)
112+
issue, err := plugins.GetJiraIssue(conf.IssueApi, key, user, token)
113113
if err != nil {
114114
return "", err
115115
}
116116

117117
var transitionErr error
118-
assignErr := plugins.AssignUser(conf.IssueURL, key, user, token)
118+
assignErr := plugins.AssignUser(conf.IssueApi, key, user, token)
119119

120120
//Get transition
121-
transition, err := plugins.GetTransitions(conf.IssueURL, key, user, token)
121+
transition, err := plugins.GetTransitions(conf.IssueApi, key, user, token)
122122
if err == nil {
123123
for _, tran := range transition.Transitions {
124124
if tran.To.StatusCategory.Name == "In Progress" {
125-
transitionErr = plugins.DoTransition(conf.IssueURL, key, user, token, tran.ID)
125+
transitionErr = plugins.DoTransition(conf.IssueApi, key, user, token, tran.ID)
126126

127127
break
128128
}
@@ -163,17 +163,17 @@ func UpdateGithubIssue(issue string, conf *setting.ProjectSetting) (string, erro
163163

164164
oap := githandler.OrgAndRepo(remote)
165165

166-
issueOb, err := plugins.GetIssueGitHub(conf.IssueURL, oap.Organisation, oap.Repository, issue, token)
166+
issueOb, err := plugins.GetIssueGitHub(conf.IssueApi, oap.Organisation, oap.Repository, issue, token)
167167
if err != nil {
168168
fmt.Println(err)
169169
os.Exit(0)
170170
}
171171

172-
if err := plugins.SetAssigneeGitHub(conf.IssueURL, oap.Organisation, oap.Repository, token, issue, user); err != nil {
172+
if err := plugins.SetAssigneeGitHub(conf.IssueApi, oap.Organisation, oap.Repository, token, issue, user); err != nil {
173173
fmt.Println(err)
174174
}
175175

176-
if _, err := plugins.SetLabelGitHub(conf.IssueURL, oap.Organisation, oap.Repository, token, plugins.PhlowLabels["Status - in progress"].Title, issue); err != nil {
176+
if _, err := plugins.SetLabelGitHub(conf.IssueApi, oap.Organisation, oap.Repository, token, plugins.PhlowLabels["Status - in progress"].Title, issue); err != nil {
177177
fmt.Println(err)
178178
}
179179

plugins/plugin.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package plugins
22

33
import (
4-
"bytes"
54
"regexp"
65
"strings"
6+
"strconv"
7+
"github.com/go-errors/errors"
78
)
89

910
//PhlowLabels ...
@@ -61,18 +62,34 @@ func BranchNameFromIssue(issue string, name string) string {
6162
return issue + "-" + result
6263
}
6364

65+
66+
67+
type IssueExtractor func(branch string) (string, error)
68+
69+
6470
//IssueFromBranchName ...
65-
//Extracts the issue number from the branch name
66-
func IssueFromBranchName(branch string) string {
67-
return strings.Split(branch, "-")[0]
71+
//Extracts github issue from the branch name. Will error if no issue ID is identified
72+
func IssueFromBranchName(branch string) (string, error) {
73+
arr := strings.Split(branch, "-")
74+
_, err := strconv.Atoi(arr[0])
75+
if err != nil {
76+
return "", errors.New("Could not extract github issue ID from branch")
77+
}
78+
return arr[0], nil
6879
}
6980

70-
//efficientConcatString
71-
//Concatenate strings in an effective way
72-
func efficientConcatString(args ...string) string {
73-
buffer := bytes.Buffer{}
74-
for _, str := range args {
75-
buffer.WriteString(str)
81+
//KeyFromBranchName ...
82+
//Extracts a Jira key from a branch name. Will error if no key is identified
83+
func KeyFromBranchName(branch string) (string, error) {
84+
parts := strings.Split(branch, "-")
85+
//Jira issues must have 3 parts PRJ-123-NAME
86+
if len(parts) < 3 {
87+
return "", errors.New("Could not get Jira Key from branch name")
88+
}
89+
90+
if _, err := strconv.Atoi(parts[0]); err != nil && len(parts) > 1 {
91+
return parts[0] + "-" + parts[1], nil
7692
}
77-
return buffer.String()
93+
return "", errors.New("could not get Jira key from branch name")
94+
7895
}

0 commit comments

Comments
 (0)