commit - 945b3fc34aaeca2019e8045bb23d2c1c5fe62539
commit + 82b8536397b0c1fe84af1b0ca1aabf33faaabceb
blob - bf513862428129dca85813e77ec2022e3cdf8478
blob + fb5a932b3fc71fc0b29256b428ffa3c53f47ffad
--- jira/http.go
+++ jira/http.go
return &com, nil
}
+func (c *Client) CreateIssue(issue Issue) (*Issue, error) {
+ u := *c.APIRoot
+ u.Path = path.Join(u.Path, "issue")
+ resp, err := c.postJSON(u.String(), &issue)
+ if err != nil {
+ return nil, fmt.Errorf("post json: %w", err)
+ }
+ if resp.StatusCode >= http.StatusBadRequest {
+ return nil, fmt.Errorf("non-ok status: %s", resp.Status)
+ }
+ return nil, fmt.Errorf("TODO")
+}
+
func (c *Client) PostComment(issueKey string, body io.Reader) error {
b, err := io.ReadAll(body)
if err != nil {
return fmt.Errorf("read body: %w", err)
}
cm := Comment{Body: string(b)}
- hbody, err := json.Marshal(&cm)
- if err != nil {
- return fmt.Errorf("to json: %w", err)
- }
u := fmt.Sprintf("%s/issue/%s/comment", c.APIRoot, issueKey)
- req, err := http.NewRequest(http.MethodPost, u, bytes.NewReader(hbody))
+ resp, err := c.postJSON(u, &cm)
if err != nil {
- return err
+ return fmt.Errorf("post json: %w", err)
}
- req.Header.Add("Content-Type", "application/json")
- resp, err := c.do(req)
- if err != nil {
- return err
- }
if resp.StatusCode >= http.StatusBadRequest {
return fmt.Errorf("non-ok status: %s", resp.Status)
}
return nil
-
}
-func Create(APIRoot string, issue Issue) (*Issue, error) {
- b, err := json.Marshal(&issue)
- if err != nil {
- return nil, fmt.Errorf("to json: %w", err)
- }
- u := APIRoot + "/issue"
- resp, err := http.Post(u, "application/json", bytes.NewReader(b))
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("non-ok status %s", resp.Status)
- }
- var i Issue
- if err := json.NewDecoder(resp.Body).Decode(&i); err != nil {
- return nil, fmt.Errorf("decode created issue: %w", err)
- }
- return &i, nil
-}
-
-func CreateComment(APIRoot, issueKey string, body io.Reader) error {
- b, err := io.ReadAll(body)
- if err != nil {
- return fmt.Errorf("read body: %w", err)
- }
- c := Comment{Body: string(b)}
- hbody, err := json.Marshal(&c)
- if err != nil {
- return fmt.Errorf("to json: %w", err)
- }
- u := fmt.Sprintf("%s/issue/%s/comment", APIRoot, issueKey)
- resp, err := http.Post(u, "application/json", bytes.NewReader(hbody))
- if err != nil {
- return err
- }
- if resp.StatusCode >= http.StatusBadRequest {
- return fmt.Errorf("non-ok status: %s", resp.Status)
- }
- return nil
-}
-
func (c *Client) get(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return c.do(req)
}
+func (c *Client) postJSON(url string, v any) (*http.Response, error) {
+ body, err := json.Marshal(v)
+ if err != nil {
+ return nil, err
+ }
+ req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Set("Content-Type", "application/json")
+ return c.do(req)
+}
+
func (c *Client) do(req *http.Request) (*http.Response, error) {
+ if c.Debug {
+ fmt.Fprintln(os.Stderr, req.Method, req.URL)
+ }
+
if c.Client == nil {
c.Client = http.DefaultClient
}
+
if c.Username != "" && c.Password != "" {
req.SetBasicAuth(c.Username, c.Password)
}
- if c.Debug {
- fmt.Fprintln(os.Stderr, req.Method, req.URL)
- }
return c.Do(req)
}