Skip to content

Commit dc5e789

Browse files
committed
close #230 add workon cmd
1 parent 195d22d commit dc5e789

File tree

6 files changed

+170
-83
lines changed

6 files changed

+170
-83
lines changed

cmd/workon.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package cmd
33
import (
44
"fmt"
55
"os"
6-
"strconv"
7-
86
"github.com/praqma/git-phlow/cmd/cmdperm"
9-
"github.com/praqma/git-phlow/phlow"
107
"github.com/praqma/git-phlow/ui"
118
"github.com/spf13/cobra"
129
"github.com/praqma/git-phlow/options"
10+
"github.com/praqma/git-phlow/phlow"
1311
)
1412

1513
// workonCmd represents the workon command
@@ -26,18 +24,13 @@ A new branch will be created, based on your remote default branch and named afte
2624
},
2725
Run: func(cmd *cobra.Command, args []string) {
2826

29-
if len(args) > 0 {
30-
issue, err := strconv.Atoi(args[0])
31-
if err != nil {
32-
fmt.Fprintf(os.Stdout, "Whoops \nYour argument, %s, is not a number! I only accept numbers \n", args[0])
33-
os.Exit(0)
34-
}
35-
36-
phlow.WorkOn(issue)
27+
if len(args) == 0 {
28+
fmt.Fprintln(os.Stdout, cmd.UsageString())
3729
os.Exit(0)
3830
}
3931

40-
fmt.Fprintln(os.Stdout, cmd.UsageString())
32+
phlow.WorkOnCaller(args[0])
33+
4134
},
4235
}
4336

main.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
11
package main
22

33
import (
4-
"github.com/praqma/git-phlow/plugins"
5-
"fmt"
4+
"github.com/praqma/git-phlow/cmd"
65
)
76

87
func main() {
9-
//cmd.Execute()
10-
/*issue, err := plugins.GetJiraIssue("http://jira.teamsinspace.com:8080", "TIS-41", "admin", "Charlie!")
11-
if err != nil {
12-
fmt.Println(err)
13-
}
14-
fmt.Println(issue.Key, issue.Fields.Summary, issue.Fields.Status.Name)
15-
16-
tr, err := plugins.GetTranstions("http://jira.teamsinspace.com:8080", "TIS-41", "admin", "Charlie!")
17-
if err != nil {
18-
fmt.Println(err)
19-
}
20-
21-
fmt.Println(tr.Transitions)
22-
23-
err = plugins.DoTransition("http://jira.teamsinspace.com:8080", "TIS-41", "admin", "Charlie!",tr.Transitions[0].ID)
24-
if err != nil {
25-
fmt.Println(err)
26-
}*/
27-
28-
err := plugins.AssignUser("http://jira.teamsinspace.com:8080", "TIS-41", "admin", "Charlie!")
29-
if err != nil {
30-
fmt.Println(err)
31-
}
328

9+
cmd.Execute()
3310

3411
}

phlow/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func Web(userIssue int) {
4545
os.Exit(0)
4646
}
4747

48-
if branchIssue != -1 {
48+
if branchIssue != "-1" {
4949
issueInfoURL = fmt.Sprintf(issueInfoURL, orgAndRepo.Organisation, orgAndRepo.Repository, branchIssue)
5050
executor.RunCommand("open", issueInfoURL)
5151
os.Exit(0)

phlow/workon.go

Lines changed: 124 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,43 @@ import (
1111
"github.com/praqma/git-phlow/ui"
1212
"github.com/praqma/git-phlow/setting"
1313
"github.com/praqma/git-phlow/executor"
14+
"github.com/praqma/git-phlow/options"
15+
"strings"
1416
)
1517

16-
//WorkonCaller ...
17-
func WorkonCaller() {
18+
//WorkOnUpdate ...
19+
//Type for updating issues
20+
//returns a name or an error
21+
type WorkOnUpdate func(key string, projectSetting *setting.ProjectSetting) (string, error)
1822

19-
}
23+
//WorkOnCaller ...
24+
func WorkOnCaller(keyOrID string) {
25+
26+
INIBlock := options.GlobalFlagTarget
27+
conf := setting.NewProjectStg(INIBlock)
2028

29+
if "jira" == strings.ToLower(conf.Service) {
2130

31+
WorkOn(keyOrID, conf, UpdateJIRAIssue)
32+
return
33+
}
34+
35+
if "github" == strings.ToLower(conf.Service) {
36+
_, err := strconv.Atoi(keyOrID)
37+
if err != nil {
38+
fmt.Fprintf(os.Stdout, "Whoops \nYour argument, %s, is not a number! I only accept numbers \n", keyOrID)
39+
os.Exit(0)
40+
}
41+
WorkOn(keyOrID, conf, UpdateGithubIssue)
42+
return
43+
}
44+
45+
fmt.Println(conf.Service + "Is an unknown Service in you project .phlow file")
46+
}
47+
48+
func WorkOn(keyOrID string, conf *setting.ProjectSetting, update WorkOnUpdate) {
2249

23-
//WorkOn ...
24-
func WorkOn(issue int) {
2550
git := githandler.Git{Run: executor.RunGit}
26-
conf := setting.NewProjectStg("default")
2751

2852
ui.PhlowSpinner.Start("Setting up workspace")
2953
defer ui.PhlowSpinner.Stop()
@@ -40,13 +64,16 @@ func WorkOn(issue int) {
4064
}
4165

4266
branchInfo := githandler.AsList(out)
43-
if plugins.IssueFromBranchName(branchInfo.Current) == issue {
67+
68+
fmt.Println(plugins.IssueFromBranchName(branchInfo.Current))
69+
//Are we already on the branch we want to work on
70+
if plugins.IssueFromBranchName(branchInfo.Current) == keyOrID {
4471
fmt.Fprintf(os.Stdout, "You are already on branch %s \n", ui.Format.Branch(branchInfo.Current))
4572
return
4673
}
47-
74+
//Does another workspace already exist with the key or ID
4875
for _, branch := range branchInfo.List {
49-
if plugins.IssueFromBranchName(branch) == issue {
76+
if plugins.IssueFromBranchName(branch) == keyOrID {
5077

5178
if _, err = git.CheckOut(branch); err != nil {
5279
fmt.Println(err)
@@ -57,55 +84,116 @@ func WorkOn(issue int) {
5784
}
5885
}
5986

60-
//Get list of gh issues
61-
gitHubIssues, err := plugins.GitHub.GetIssues()
87+
//Get issue and do the transition
88+
89+
name, err := update(keyOrID, conf)
90+
91+
//Proceed to create the new workspace
92+
93+
_, err = git.CheckOut("-b", name, conf.Remote+"/"+conf.IntegrationBranch)
6294
if err != nil {
6395
fmt.Println(err)
6496
return
6597
}
6698

67-
//Loop through all issues verifying the work-on issue exists
68-
for _, iss := range gitHubIssues {
69-
if iss.Number == issue {
70-
name := plugins.BranchNameFromIssue(issue, iss.Title)
99+
ui.PhlowSpinner.Stop()
100+
fmt.Fprintf(os.Stdout, "Created workspace: %s \n", ui.Format.Branch(name))
71101

72-
_, err := git.CheckOut("-b", name, conf.Remote+"/"+conf.IntegrationBranch)
73-
if err != nil {
74-
fmt.Println(err)
75-
return
76-
}
102+
}
77103

78-
ui.PhlowSpinner.Stop()
79-
fmt.Fprintf(os.Stdout, "Created workspace: %s \n", ui.Format.Branch(name))
104+
//UpdateJIRAIssue ...
105+
//Updates the issue on jira and returns the name of the branch
106+
func UpdateJIRAIssue(key string, conf *setting.ProjectSetting) (string, error) {
107+
git := githandler.Git{Run: executor.RunGit}
80108

81-
//Set labels and Assignee
82-
UpdateIssue(issue)
83-
return
109+
user, _ := git.Config("--get", "phlow.jirauser")
110+
token, _ := git.Config("--get", "phlow.jiratoken")
111+
112+
//Get jira issue or fail
113+
issue, err := plugins.GetJiraIssue(conf.IssueURL, key, user, token)
114+
if err != nil {
115+
return "", err
116+
}
117+
118+
var transitionErr error
119+
var assignErr error
120+
121+
//Get transition
122+
transition, err := plugins.GetTransitions(conf.IssueURL, key, user, token)
123+
if err == nil {
124+
for _, tran := range transition.Transitions {
125+
if tran.To.Name == "In Progress" {
126+
transitionErr = plugins.DoTransition(conf.IssueURL, key, user, token, tran.ID)
127+
break
128+
}
84129
}
85130
}
131+
132+
assignErr = plugins.AssignUser(conf.IssueURL, key, user, token)
133+
86134
ui.PhlowSpinner.Stop()
87-
fmt.Println("No matching issues")
135+
if transitionErr != nil || assignErr != nil {
136+
fmt.Fprintf(os.Stdout, "\n-------- Issue %s updated -------- \n", ui.Format.Issue(issue.Key))
137+
} else {
138+
fmt.Printf("\nIssue %s could not be moved to 'In Progress'\n", ui.Format.Issue(issue.Key))
139+
fmt.Printf("Assingee '%s' could not be assigned to issue %s\n", ui.Format.Assignee(user), ui.Format.Issue(issue.Key))
140+
fmt.Println(ui.Format.Bold("Go to Jira and manually set the assignee and state"))
141+
142+
}
143+
144+
if transitionErr != nil {
145+
fmt.Fprintf(os.Stdout, "Moved to => %s \n", ui.Format.Label.G4Move(plugins.PhlowLabels["In Progress"].Title))
146+
}
147+
148+
if assignErr != nil {
149+
fmt.Fprintf(os.Stdout, "Assignee => %s \n", ui.Format.Assignee(user))
150+
}
151+
152+
if transitionErr != nil || assignErr != nil {
153+
fmt.Println("----------------------------------")
154+
}
155+
156+
return plugins.BranchNameFromIssue(issue.Key, issue.Fields.Summary), nil
88157
}
89158

90-
//UpdateIssue ...
91-
//Set Label and assignee on a GitHub issue
92-
func UpdateIssue(issue int) {
93-
//Retrieve token
94-
stg := setting.NewToolStg()
95-
user := stg.User
159+
//UpdateGithubIssue ...
160+
//Updating an issue on github and returns the branch name
161+
func UpdateGithubIssue(issue string, conf *setting.ProjectSetting) (string, error) {
162+
git := githandler.Git{Run: executor.RunGit}
163+
164+
token, _ := git.Config("--get", "phlow.token")
165+
user, _ := git.Config("--get", "phlow.user")
166+
167+
//Get organisation and repository for repository
168+
remote, err := git.LSRemote("--get-url", conf.Remote)
169+
if err != nil {
170+
panic(err)
171+
}
172+
96173

97-
if _, err := plugins.GitHub.SetLabel(plugins.PhlowLabels["Status - in progress"].Title, issue); err != nil {
174+
orgAndRepo := githandler.OrgAndRepo(remote)
175+
fmt.Println(orgAndRepo.Repository)
176+
177+
issueOb, err := plugins.GetIssueGitHub(conf.IssueURL, orgAndRepo.Organisation, orgAndRepo.Repository, issue, token)
178+
if err != nil {
179+
fmt.Println("No matching issues")
180+
os.Exit(0)
181+
}
182+
183+
if _, err := plugins.GitHub.SetLabel(plugins.PhlowLabels["Status - in progress"].Title, issueOb.Number); err != nil {
98184
fmt.Println(err)
99185
}
100186

101-
if err := plugins.GitHub.SetAssignee(user, issue); err != nil {
187+
if err := plugins.GitHub.SetAssignee(user, issueOb.Number); err != nil {
102188
fmt.Println(err)
103189
}
104190

105-
is := strconv.Itoa(issue)
191+
is := strconv.Itoa(issueOb.Number)
106192
fmt.Fprintf(os.Stdout, "\n-------- Issue %s updated -------- \n", ui.Format.Issue(is))
107193
fmt.Fprintf(os.Stdout, "Label => %s \n", ui.Format.Label.G4Move(plugins.PhlowLabels["Status - in progress"].Title))
108194
fmt.Fprintf(os.Stdout, "Assignee => %s \n", ui.Format.Assignee(user))
109195
fmt.Println("----------------------------------")
110-
return
196+
197+
return plugins.BranchNameFromIssue(is, issueOb.Title), nil
198+
111199
}

plugins/gh.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/praqma/git-phlow/githandler"
1414
"github.com/praqma/git-phlow/options"
1515
"github.com/praqma/git-phlow/executor"
16+
"io/ioutil"
1617
)
1718

1819
var GitHub *GitHubImpl
@@ -130,6 +131,37 @@ func DefaultBranchGitHub(URL, org, repo, token string) (defaultBranch string, er
130131
return re.DefaultBranch, nil
131132
}
132133

134+
//GetIssueGitHub ...
135+
//return an issue with from the number of the issue
136+
func GetIssueGitHub(URL, org, repo, key, token string) (*Issue, error) {
137+
138+
req, _ := http.NewRequest("GET", URL+fmt.Sprintf("/repos/%s/%s/issues/", org, repo)+key, nil)
139+
req.Header.Set("Content-Type", "application/json")
140+
q := req.URL.Query()
141+
q.Add("access_token", token)
142+
req.URL.RawQuery = q.Encode()
143+
144+
client := http.DefaultClient
145+
146+
res, err := client.Do(req)
147+
if err != nil {
148+
return nil, err
149+
}
150+
defer res.Body.Close()
151+
152+
body, err := ioutil.ReadAll(res.Body);
153+
if err != nil {
154+
return nil, err
155+
}
156+
157+
re := Issue{}
158+
if err = json.Unmarshal(body, &re); err != nil {
159+
return nil, err
160+
}
161+
162+
return &re, err
163+
}
164+
133165
//GetIssues ...
134166
func (g *GitHubImpl) GetIssues() (issues []Issue, err error) {
135167
URL := fmt.Sprintf(g.URLNoEsc(urls.issueURL), g.org, g.repo)

plugins/plugin.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package plugins
33
import (
44
"bytes"
55
"regexp"
6-
"strconv"
76
"strings"
87
)
98

@@ -19,6 +18,8 @@ type Authentication func(URL, user, pass string) (authenticated bool, err error)
1918
//interface for getting the default branch of the external service
2019
type DefaultBranch func(URL, org, repo, token string) (defaultBranch string, err error)
2120

21+
22+
2223
//PhlowLabels ...
2324
//Map of labels in the phlow
2425
var PhlowLabels map[string]*PhlowLabel
@@ -60,7 +61,7 @@ func GroupID(name string) int {
6061

6162
//BranchNameFromIssue ...
6263
//Converts issues to branch names nby removing illegal characters and inserting hyphens
63-
func BranchNameFromIssue(issue int, name string) string {
64+
func BranchNameFromIssue(issue string, name string) string {
6465
var result string
6566

6667
removeNonAlphaNumeric := regexp.MustCompile("([^\\w-])+")
@@ -71,17 +72,13 @@ func BranchNameFromIssue(issue int, name string) string {
7172

7273
result = strings.Trim(result, "-")
7374
result = strings.ToLower(result)
74-
return strconv.Itoa(issue) + "-" + result
75+
return issue + "-" + result
7576
}
7677

7778
//IssueFromBranchName ...
7879
//Extracts the issue number from the branch name
79-
func IssueFromBranchName(branch string) int {
80-
iss, err := strconv.Atoi(strings.Split(branch, "-")[0])
81-
if err != nil {
82-
return -1
83-
}
84-
return iss
80+
func IssueFromBranchName(branch string) string {
81+
return strings.Split(branch, "-")[0]
8582
}
8683

8784
//efficientConcatString

0 commit comments

Comments
 (0)