Skip to content

Commit e04c3c5

Browse files
committed
close #226 implement workon jira rest calls
1 parent 67ae58c commit e04c3c5

File tree

9 files changed

+528
-5
lines changed

9 files changed

+528
-5
lines changed

cmd/workon.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/praqma/git-phlow/phlow"
1010
"github.com/praqma/git-phlow/ui"
1111
"github.com/spf13/cobra"
12+
"github.com/praqma/git-phlow/options"
1213
)
1314

1415
// workonCmd represents the workon command
@@ -42,4 +43,8 @@ A new branch will be created, based on your remote default branch and named afte
4243

4344
func init() {
4445
RootCmd.AddCommand(workonCmd)
46+
47+
//Target for configuration
48+
workonCmd.Flags().StringVarP(&options.GlobalFlagTarget, "target", "t", "", "the name of the INI block in your .phlow files")
49+
4550
}

main.go

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

3-
import "github.com/praqma/git-phlow/cmd"
3+
import (
4+
"github.com/praqma/git-phlow/plugins"
5+
"fmt"
6+
)
47

58
func main() {
6-
cmd.Execute()
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+
}
32+
33+
734
}

phlow/workon.go

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

16+
//WorkonCaller ...
17+
func WorkonCaller() {
18+
19+
}
20+
21+
22+
1623
//WorkOn ...
1724
func WorkOn(issue int) {
1825
git := githandler.Git{Run: executor.RunGit}

plugins/jira.go

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package plugins
33
import (
44
"net/http"
55
"github.com/go-errors/errors"
6+
"encoding/json"
7+
"io/ioutil"
8+
"fmt"
9+
"bytes"
610
)
711

8-
912
//AuthenticateJIRA ...
1013
func AuthenticateJIRA(url, user, pass string) (bool, error) {
1114
verifyURL := "/rest/api/latest/mypermissions"
@@ -41,3 +44,113 @@ func AuthorizeJIRA(URL, user, pass string) (token string, err error) {
4144

4245
return pass, nil
4346
}
47+
48+
//GetJiraIssue ...
49+
func GetJiraIssue(URL, key, user, pass string) (*JiraIssue, error) {
50+
51+
issueURL := "/rest/api/latest/issue/"
52+
req, _ := http.NewRequest("GET", URL+issueURL+key, nil)
53+
req.Header.Set("Content-Type", "application/json")
54+
req.SetBasicAuth(user, pass)
55+
client := http.DefaultClient
56+
57+
res, err := client.Do(req)
58+
if err != nil {
59+
return nil, err
60+
}
61+
defer res.Body.Close()
62+
63+
body, err := ioutil.ReadAll(res.Body);
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
re := JiraIssue{}
69+
if err = json.Unmarshal(body, &re); err != nil {
70+
return nil, err
71+
}
72+
73+
return &re, err
74+
}
75+
76+
//GetTranstions ...
77+
//Retrieve transitions for a specific issue
78+
func GetTransitions(URL, key, user, pass string) (*Transitions, error) {
79+
issueURL := "/rest/api/latest/issue/%s/transitions"
80+
81+
req, _ := http.NewRequest("GET", URL+fmt.Sprintf(issueURL, key), nil)
82+
req.Header.Set("Content-Type", "application/json")
83+
req.SetBasicAuth(user, pass)
84+
client := http.DefaultClient
85+
86+
res, err := client.Do(req)
87+
if err != nil {
88+
return nil, err
89+
}
90+
defer res.Body.Close()
91+
92+
body, err := ioutil.ReadAll(res.Body);
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
re := Transitions{}
98+
if err = json.Unmarshal(body, &re); err != nil {
99+
return nil, err
100+
}
101+
102+
return &re, err
103+
}
104+
105+
//DoTransition ...
106+
func DoTransition(URL, key, user, pass, transitionID string) (error) {
107+
issueURL := "/rest/api/latest/issue/%s/transitions"
108+
transition := TransitionBody{}
109+
110+
transition.Transition.ID = transitionID
111+
data, err := json.Marshal(&transition)
112+
if err != nil {
113+
return err
114+
}
115+
116+
req, _ := http.NewRequest("POST", URL+fmt.Sprintf(issueURL, key), bytes.NewBuffer(data))
117+
req.Header.Set("Content-Type", "application/json")
118+
req.SetBasicAuth(user, pass)
119+
client := http.DefaultClient
120+
121+
res, err := client.Do(req)
122+
if err != nil {
123+
return err
124+
}
125+
defer res.Body.Close()
126+
127+
if res.StatusCode != 204 {
128+
return errors.New("Could not transition issue")
129+
}
130+
return nil
131+
}
132+
133+
//AssignUser ...
134+
func AssignUser(URL, key, user, pass string) (error) {
135+
issueURL := "/rest/api/latest/issue/%s/assignee"
136+
137+
data, err := json.Marshal(AssignBody{Name: user})
138+
if err != nil {
139+
return err
140+
}
141+
req, _ := http.NewRequest("PUT", URL+fmt.Sprintf(issueURL, key), bytes.NewBuffer(data))
142+
req.Header.Set("Content-Type", "application/json")
143+
req.SetBasicAuth(user, pass)
144+
client := http.DefaultClient
145+
146+
res, err := client.Do(req)
147+
if err != nil {
148+
return err
149+
}
150+
defer res.Body.Close()
151+
152+
if res.StatusCode != 204 {
153+
return errors.New("Could not assign user")
154+
}
155+
return nil
156+
}

plugins/jira_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package plugins_test
2+
3+
import (
4+
. "github.com/praqma/git-phlow/plugins"
5+
6+
. "github.com/onsi/ginkgo"
7+
"net/http/httptest"
8+
"net/http"
9+
. "github.com/onsi/gomega"
10+
)
11+
12+
var _ = Describe("Jira", func() {
13+
14+
Describe("Testing GetJiraIssue", func() {
15+
16+
Context("call to a valid url", func() {
17+
18+
It("should return a jira issue object", func() {
19+
20+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21+
Ω(r.Method).Should(Equal("GET"))
22+
Ω(r.Header.Get("Content-Type")).Should(Equal("application/json"))
23+
Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41"))
24+
25+
w.WriteHeader(http.StatusOK)
26+
w.Write([]byte(jiraIssueResponse))
27+
28+
}))
29+
defer ts.Close()
30+
31+
issue, err := GetJiraIssue(ts.URL, "TIS-41", "user", "pass")
32+
Ω(issue.Fields.Status.Name).Should(Equal("open"))
33+
Ω(err).Should(BeNil())
34+
})
35+
})
36+
37+
Context("call to a invalid url", func() {
38+
39+
It("send no response and should error", func() {
40+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
41+
Ω(r.Method).Should(Equal("GET"))
42+
Ω(r.Header.Get("Content-Type")).Should(Equal("application/json"))
43+
44+
}))
45+
defer ts.Close()
46+
47+
_, err := GetJiraIssue(ts.URL, "TIS-41", "user", "pass")
48+
Ω(err).ShouldNot(BeNil())
49+
})
50+
})
51+
52+
})
53+
54+
Describe("Testing GetTransitions", func() {
55+
56+
Context("with a valid url", func() {
57+
58+
It("Should return transitions object", func() {
59+
60+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61+
Ω(r.Method).Should(Equal("GET"))
62+
Ω(r.Header.Get("Content-Type")).Should(Equal("application/json"))
63+
Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41/transitions"))
64+
65+
w.WriteHeader(http.StatusOK)
66+
w.Write([]byte(jiraTransitionResponse))
67+
68+
}))
69+
defer ts.Close()
70+
71+
transitions, err := GetTransitions(ts.URL, "TIS-41", "user", "pass")
72+
Ω(transitions.Transitions[0].To.Name).Should(Equal("In Progress"))
73+
Ω(err).Should(BeNil())
74+
})
75+
76+
})
77+
78+
})
79+
80+
Describe("Testing DoTransition", func() {
81+
82+
Context("Transition with a valid transition ID", func() {
83+
84+
//Consult API documentation for status codes - https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-doTransition
85+
It("Should return status code 204 ", func() {
86+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
87+
Ω(r.Method).Should(Equal("POST"))
88+
Ω(r.Header.Get("Content-Type")).Should(Equal("application/json"))
89+
Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41/transitions"))
90+
91+
w.WriteHeader(204)
92+
w.Write([]byte(jiraTransitionResponse))
93+
94+
}))
95+
defer ts.Close()
96+
97+
err := DoTransition(ts.URL, "TIS-41", "user", "pass", "11")
98+
Ω(err).Should(BeNil())
99+
})
100+
101+
})
102+
103+
Context("Transition with an invalid transition ID", func() {
104+
105+
//Consult API documentation for status codes - https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-doTransition
106+
It("Should return status code 400 ", func() {
107+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
108+
Ω(r.Method).Should(Equal("POST"))
109+
Ω(r.Header.Get("Content-Type")).Should(Equal("application/json"))
110+
Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41/transitions"))
111+
112+
w.WriteHeader(400)
113+
w.Write([]byte("error"))
114+
115+
}))
116+
defer ts.Close()
117+
118+
err := DoTransition(ts.URL, "TIS-41", "user", "pass", "111")
119+
Ω(err).ShouldNot(BeNil())
120+
})
121+
122+
})
123+
124+
})
125+
126+
Describe("Testing assign user", func() {
127+
128+
Context("Assign with a valid user", func() {
129+
It("Should return status code 204 ", func() {
130+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
131+
Ω(r.Method).Should(Equal("PUT"))
132+
Ω(r.Header.Get("Content-Type")).Should(Equal("application/json"))
133+
Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41/assignee"))
134+
135+
w.WriteHeader(204)
136+
w.Write([]byte(jiraTransitionResponse))
137+
138+
}))
139+
defer ts.Close()
140+
141+
err := AssignUser(ts.URL, "TIS-41", "admin", "pass")
142+
Ω(err).Should(BeNil())
143+
})
144+
})
145+
})
146+
})

plugins/jiramodels.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package plugins
2+
3+
//JiraIssue ...
4+
type JiraIssue struct {
5+
Key string `json:"key"`
6+
Fields struct {
7+
Summary string `json:"summary"`
8+
Status struct {
9+
Name string `json:"name"`
10+
}`json:"status"`
11+
}`json:"fields"`
12+
}
13+
14+
//transitions ...
15+
type Transitions struct {
16+
Transitions []Transition `json:"transitions"`
17+
}
18+
19+
//Transition ...
20+
type Transition struct {
21+
ID string `json:"id"`
22+
To struct {
23+
Name string `json:"name"`
24+
} `json:"to"`
25+
}
26+
27+
//AssignBody ...
28+
type AssignBody struct {
29+
Name string `json:"name"`
30+
}
31+
32+
//TransitionBody ...
33+
type TransitionBody struct {
34+
Transition struct {
35+
ID string `json:"id"`
36+
} `json:"transition"`
37+
}

0 commit comments

Comments
 (0)