@@ -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 \n Your 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 ("\n Issue %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}
0 commit comments