Merge pull request #1 from jchenry/submit-to-jira

Submit to jira
This commit is contained in:
Colin Henry 2015-10-29 21:02:29 -07:00
commit 693d094143
3 changed files with 55 additions and 18 deletions

View File

@ -26,7 +26,7 @@ type project struct {
Key string `json:"key"`
}
func newIssue(f []string) *issue {
func newIssue(f []string, prj string) *issue {
return &issue{
Fields: fields{
Title: f[0],
@ -36,7 +36,7 @@ func newIssue(f []string) *issue {
Description: f[2],
Labels: strings.Split(f[3], ","),
Project: project{
Key: *prj,
Key: prj,
},
},
}

41
jira.go
View File

@ -3,16 +3,18 @@ package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
)
// func init() {
// client =
// }
var client *http.Client
func submitIssue(i []string) error {
client := &http.Client{
func init() {
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
@ -20,17 +22,40 @@ func submitIssue(i []string) error {
},
}
issue := newIssue(i)
}
func submitIssue(i []string, user string, pass string, svr string, prj string) error {
issue := newIssue(i, prj)
issueJSON := toJSON(issue)
fmt.Printf("* %s :", issue.Fields.Title)
uri := fmt.Sprintf("%s/rest/api/2/issue/", svr)
req, _ := http.NewRequest("POST", uri, bytes.NewBuffer(issueJSON))
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(*user, *pass)
/*resp*/ _, err := client.Do(req)
req.SetBasicAuth(user, pass)
resp, err := client.Do(req)
if err != nil {
return err
}
r := make(map[string]interface{})
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(contents, &r)
if err != nil {
return err
}
fmt.Printf(" created as %s", r["key"])
// printResponse(resp)
return nil
}
func printResponse(resp *http.Response) {
r, _ := httputil.DumpResponse(resp, true)
fmt.Printf(string(r))
}

28
main.go
View File

@ -21,16 +21,28 @@ func main() {
return
}
flag.Parse()
// fmt.Printf("%v", os.Args)
// fmt.Printf(*svr)
bufrdr := bufio.NewReader(os.Stdin)
bufrdr.ReadLine()
bufrdr.ReadLine()
csvrdr := csv.NewReader(bufrdr)
processIssue(csvrdr, printJSON)
// processIssue(csvrdr, printJSON)
processIssue(bufrdr, submitIssue, *user, *pass, *svr, *prj)
}
func processIssue(r *csv.Reader, process func(issue []string) error) error {
type procFunc func(i []string, user string, pass string, srv string, prj string) error
func processIssue(r io.Reader, process procFunc, user string, pass string, svr string, prj string) error {
csvrdr := csv.NewReader(r)
for {
record, err := r.Read()
record, err := csvrdr.Read()
if err == io.EOF {
break
}
@ -38,7 +50,7 @@ func processIssue(r *csv.Reader, process func(issue []string) error) error {
log.Fatal(err)
}
err = process(record)
err = process(record, user, pass, svr, prj)
if err != nil {
log.Fatal(err)
}
@ -47,12 +59,12 @@ func processIssue(r *csv.Reader, process func(issue []string) error) error {
return nil
}
func printCSV(issue []string) error {
func printCSV(issue []string, user string, pass string, svr string, prj string) error {
fmt.Println(issue)
return nil
}
func printJSON(issue []string) error {
i := newIssue(issue)
func printJSON(issue []string, user string, pass string, svr string, prj string) error {
i := newIssue(issue, prj)
fmt.Println(string(toJSON(i)))
return nil
}