Skip to content

Commit 17864c4

Browse files
committed
close #232 fix assign and state
1 parent dc5e789 commit 17864c4

File tree

9 files changed

+463
-343
lines changed

9 files changed

+463
-343
lines changed

phlow/workon.go

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

1819
//WorkOnUpdate ...
@@ -21,21 +22,21 @@ import (
2122
type WorkOnUpdate func(key string, projectSetting *setting.ProjectSetting) (string, error)
2223

2324
//WorkOnCaller ...
25+
//Toplevel workon function called from cmd
2426
func WorkOnCaller(keyOrID string) {
2527

2628
INIBlock := options.GlobalFlagTarget
2729
conf := setting.NewProjectStg(INIBlock)
2830

2931
if "jira" == strings.ToLower(conf.Service) {
30-
3132
WorkOn(keyOrID, conf, UpdateJIRAIssue)
3233
return
3334
}
3435

3536
if "github" == strings.ToLower(conf.Service) {
3637
_, err := strconv.Atoi(keyOrID)
3738
if err != nil {
38-
fmt.Fprintf(os.Stdout, "Whoops \nYour argument, %s, is not a number! I only accept numbers \n", keyOrID)
39+
fmt.Println("Whoops \nYour argument, " + keyOrID + ", is not a valid GitHub issue number \n")
3940
os.Exit(0)
4041
}
4142
WorkOn(keyOrID, conf, UpdateGithubIssue)
@@ -45,12 +46,13 @@ func WorkOnCaller(keyOrID string) {
4546
fmt.Println(conf.Service + "Is an unknown Service in you project .phlow file")
4647
}
4748

49+
//WorkOn ...
50+
//creates a new workspace from issue by given WorkOnUpdate and configuration
4851
func WorkOn(keyOrID string, conf *setting.ProjectSetting, update WorkOnUpdate) {
4952

5053
git := githandler.Git{Run: executor.RunGit}
5154

52-
ui.PhlowSpinner.Start("Setting up workspace")
53-
defer ui.PhlowSpinner.Stop()
55+
fmt.Println("Preparing workspace...")
5456

5557
if _, err := git.Fetch("--all"); err != nil {
5658
fmt.Println(err)
@@ -65,7 +67,6 @@ func WorkOn(keyOrID string, conf *setting.ProjectSetting, update WorkOnUpdate) {
6567

6668
branchInfo := githandler.AsList(out)
6769

68-
fmt.Println(plugins.IssueFromBranchName(branchInfo.Current))
6970
//Are we already on the branch we want to work on
7071
if plugins.IssueFromBranchName(branchInfo.Current) == keyOrID {
7172
fmt.Fprintf(os.Stdout, "You are already on branch %s \n", ui.Format.Branch(branchInfo.Current))
@@ -77,28 +78,23 @@ func WorkOn(keyOrID string, conf *setting.ProjectSetting, update WorkOnUpdate) {
7778

7879
if _, err = git.CheckOut(branch); err != nil {
7980
fmt.Println(err)
81+
return
8082
}
81-
ui.PhlowSpinner.Stop()
83+
8284
fmt.Fprintf(os.Stdout, "Resuming to workspace: %s \n", ui.Format.Branch(branch))
8385
return
8486
}
8587
}
8688

87-
//Get issue and do the transition
88-
8989
name, err := update(keyOrID, conf)
9090

91-
//Proceed to create the new workspace
92-
9391
_, err = git.CheckOut("-b", name, conf.Remote+"/"+conf.IntegrationBranch)
9492
if err != nil {
9593
fmt.Println(err)
9694
return
9795
}
9896

99-
ui.PhlowSpinner.Stop()
10097
fmt.Fprintf(os.Stdout, "Created workspace: %s \n", ui.Format.Branch(name))
101-
10298
}
10399

104100
//UpdateJIRAIssue ...
@@ -109,48 +105,39 @@ func UpdateJIRAIssue(key string, conf *setting.ProjectSetting) (string, error) {
109105
user, _ := git.Config("--get", "phlow.jirauser")
110106
token, _ := git.Config("--get", "phlow.jiratoken")
111107

112-
//Get jira issue or fail
113108
issue, err := plugins.GetJiraIssue(conf.IssueURL, key, user, token)
114109
if err != nil {
115110
return "", err
116111
}
117112

118113
var transitionErr error
119-
var assignErr error
114+
assignErr := plugins.AssignUser(conf.IssueURL, key, user, token)
120115

121116
//Get transition
122117
transition, err := plugins.GetTransitions(conf.IssueURL, key, user, token)
123118
if err == nil {
124119
for _, tran := range transition.Transitions {
125-
if tran.To.Name == "In Progress" {
120+
if tran.To.StatusCategory.Name == "In Progress" {
126121
transitionErr = plugins.DoTransition(conf.IssueURL, key, user, token, tran.ID)
122+
127123
break
128124
}
129125
}
126+
transitionErr = errors.New("No 'In Progress' transition ")
130127
}
131128

132-
assignErr = plugins.AssignUser(conf.IssueURL, key, user, token)
133-
134-
ui.PhlowSpinner.Stop()
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-
}
129+
fmt.Printf("\n-------- Issue %s-------- \n", ui.Format.Issue(issue.Key))
143130

144131
if transitionErr != nil {
145-
fmt.Fprintf(os.Stdout, "Moved to => %s \n", ui.Format.Label.G4Move(plugins.PhlowLabels["In Progress"].Title))
132+
fmt.Printf("Issue %s could not be moved to 'In Progress'\n", ui.Format.Issue(issue.Key))
133+
} else {
134+
fmt.Printf("Moved to => %s \n", ui.Format.Label.G4Move("In Progress"))
146135
}
147136

148137
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("----------------------------------")
138+
fmt.Printf("Assingee '%s' could not be assigned to issue %s\n", ui.Format.Assignee(user), ui.Format.Issue(issue.Key))
139+
} else {
140+
fmt.Printf("Assignee => %s \n", ui.Format.Assignee(user))
154141
}
155142

156143
return plugins.BranchNameFromIssue(issue.Key, issue.Fields.Summary), nil
@@ -170,21 +157,19 @@ func UpdateGithubIssue(issue string, conf *setting.ProjectSetting) (string, erro
170157
panic(err)
171158
}
172159

160+
oap := githandler.OrgAndRepo(remote)
173161

174-
orgAndRepo := githandler.OrgAndRepo(remote)
175-
fmt.Println(orgAndRepo.Repository)
176-
177-
issueOb, err := plugins.GetIssueGitHub(conf.IssueURL, orgAndRepo.Organisation, orgAndRepo.Repository, issue, token)
162+
issueOb, err := plugins.GetIssueGitHub(conf.IssueURL, oap.Organisation, oap.Repository, issue, token)
178163
if err != nil {
179-
fmt.Println("No matching issues")
164+
fmt.Println(err)
180165
os.Exit(0)
181166
}
182167

183-
if _, err := plugins.GitHub.SetLabel(plugins.PhlowLabels["Status - in progress"].Title, issueOb.Number); err != nil {
168+
if err := plugins.SetAssigneeGitHub(conf.IssueURL, oap.Organisation, oap.Repository, token, issue, user); err != nil {
184169
fmt.Println(err)
185170
}
186171

187-
if err := plugins.GitHub.SetAssignee(user, issueOb.Number); err != nil {
172+
if _, err := plugins.SetLabelGitHub(conf.IssueURL, oap.Organisation, oap.Repository, token, plugins.PhlowLabels["Status - in progress"].Title, issue); err != nil {
188173
fmt.Println(err)
189174
}
190175

plugins/gh.go

Lines changed: 7 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ 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"
1716
)
1817

18+
//------------------------------------DEPRECATED SECTION------------------------------//
19+
//This file are being exhanged with github.go
20+
1921
var GitHub *GitHubImpl
2022
var urls *pluginWebURL
2123

@@ -57,111 +59,7 @@ func init() {
5759
}
5860
}
5961

60-
//AuthorizeGitHub ...
61-
//Retrieve token from github for authorization
62-
func AuthorizeGitHub(githubBaseURL, user, pass string) (token string, err error) {
63-
64-
perm, err := createGHPermissions()
65-
if err != nil {
66-
return "", err
67-
}
68-
69-
req, _ := http.NewRequest("POST", githubBaseURL+"/authorizations", bytes.NewBuffer([]byte(perm)))
70-
//req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
71-
req.Header.Add("accept", "application/json")
72-
req.Header.Add("content-type", "application/json")
73-
req.SetBasicAuth(user, pass)
74-
75-
body, err := NewPWRequest().Do(req)
76-
if err != nil {
77-
return "", err
78-
}
79-
80-
re := Auth{}
81-
if err = json.Unmarshal(body, &re); err != nil {
82-
return "", err
83-
}
84-
return re.Token, nil
85-
}
86-
87-
//AuthenticateGitHub
88-
//Checks personal access token validity by requesting private repositories and checking status code
89-
func AuthenticateGitHub(githubBaseURL string, user, token string) (bool, error) {
90-
91-
req, _ := http.NewRequest("GET", githubBaseURL+"/user/repos", nil)
92-
q := req.URL.Query()
93-
q.Add("access_token", token)
94-
req.URL.RawQuery = q.Encode()
95-
client := http.DefaultClient
96-
97-
res, err := client.Do(req)
98-
if err != nil {
99-
return false, err
100-
}
101-
defer res.Body.Close()
102-
103-
if res.StatusCode != http.StatusOK {
104-
return false, errors.New(strconv.Itoa(res.StatusCode))
105-
}
106-
return true, nil
107-
}
108-
109-
//DefaultBranchGitHub ...
110-
//return the default branch of the repository
111-
func DefaultBranchGitHub(URL, org, repo, token string) (defaultBranch string, err error) {
112-
113-
req, _ := http.NewRequest("GET", URL+fmt.Sprintf("/repos/%s/%s", org, repo), nil)
114-
q := req.URL.Query()
115-
q.Add("access_token", token)
116-
req.URL.RawQuery = q.Encode()
117-
118-
client := http.DefaultClient
119-
120-
res, err := client.Do(req)
121-
if err != nil {
122-
return "", err
123-
}
124-
defer res.Body.Close()
125-
126-
re := Repo{}
127-
err = json.NewDecoder(res.Body).Decode(&re)
128-
if err != nil {
129-
return "", err
130-
}
131-
return re.DefaultBranch, nil
132-
}
133-
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-
62+
//Deprecated
16563
//GetIssues ...
16664
func (g *GitHubImpl) GetIssues() (issues []Issue, err error) {
16765
URL := fmt.Sprintf(g.URLNoEsc(urls.issueURL), g.org, g.repo)
@@ -184,6 +82,7 @@ func (g *GitHubImpl) GetIssues() (issues []Issue, err error) {
18482
return re, nil
18583
}
18684

85+
//Deprecated
18786
//SetLabel ...
18887
func (g *GitHubImpl) SetLabel(label string, issue int) (labels []Label, err error) {
18988

@@ -229,6 +128,7 @@ func (g *GitHubImpl) Default() (defaultBranch string, err error) {
229128
return re.DefaultBranch, nil
230129
}
231130

131+
//Deprecated
232132
//SetAssignee ...
233133
//Set assignee on a GitHub Issue
234134
func (g *GitHubImpl) SetAssignee(assignee string, issue int) (err error) {
@@ -254,6 +154,7 @@ type GhPermissions struct {
254154
Note string `json:"note"`
255155
}
256156

157+
//This should be rewritten
257158
//createGHPermissions ...
258159
func createGHPermissions() (string, error) {
259160
hostname, err := os.Hostname()

0 commit comments

Comments
 (0)