Implement attach command

This commit is contained in:
Olivier Tremblay 2018-06-20 13:53:05 -04:00
parent 22a1ba56e5
commit 96cadaf4df
No known key found for this signature in database
GPG key ID: D1C73ACB855E3A6D
4 changed files with 87 additions and 7 deletions

45
jkl.go
View file

@ -8,6 +8,7 @@ import (
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
@ -322,6 +323,50 @@ func LinkIssue(params []string) error {
return nil
}
func Attach(issueKey string, filename string) error {
bootHttpClient()
// Prepare a form that you will submit to that URL.
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Add your image file
f, err := os.Open(filename)
if err != nil {
return err
}
fi, err := os.Lstat(filename)
fw, err := w.CreateFormFile("file", fi.Name())
if err != nil {
return err
}
if _, err = io.Copy(fw, f); err != nil {
return err
}
// Don't forget to close the multipart writer.
// If you don't close it, your request will be missing the terminating boundary.
w.Close()
req, err := http.NewRequest("POST", "", &b)
if err != nil {
return err
}
req.URL, err = url.Parse(httpClient.jiraRoot + "rest/" + fmt.Sprintf("api/2/issue/%s/attachments", issueKey))
if err != nil {
return err
}
req.Header.Add("X-Atlassian-Token", "no-check")
req.Header.Add("Content-Type", w.FormDataContentType())
res, err := httpClient.DoEvenLess(req)
if err != nil {
s, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(s))
return err
}
return nil
}
func formatPayload(issue *JiraIssue) (io.Reader, error) {
if issue.Fields != nil &&
issue.Fields.Project != nil &&