issues

Github, Jira, Gitlab issues from Plan 9's acme
Log | Files | Refs | README | LICENSE

commit 82b8536397b0c1fe84af1b0ca1aabf33faaabceb
parent 945b3fc34aaeca2019e8045bb23d2c1c5fe62539
Author: Oliver Lowe <o@olowe.co>
Date:   Fri, 13 Jun 2025 16:45:06 +1000

jira: implement issue creation

Diffstat:
Mjira/http.go | 80++++++++++++++++++++++++++++---------------------------------------------------
1 file changed, 28 insertions(+), 52 deletions(-)

diff --git a/jira/http.go b/jira/http.go @@ -150,92 +150,68 @@ func (c *Client) Comment(ikey, id string) (*Comment, error) { 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 - } - req.Header.Add("Content-Type", "application/json") - resp, err := c.do(req) - if err != nil { - return err + return fmt.Errorf("post json: %w", 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)) +func (c *Client) get(url string) (*http.Response, error) { + req, err := http.NewRequest(http.MethodGet, url, nil) 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 + return c.do(req) } -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) +func (c *Client) postJSON(url string, v any) (*http.Response, error) { + body, err := json.Marshal(v) if err != nil { - return fmt.Errorf("to json: %w", err) + return nil, 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) + 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) }