autocrossbow/issues/jira.go

88 lines
2 KiB
Go
Raw Normal View History

2025-09-13 08:40:40 -04:00
package issues
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
//curl -u olivier@circleci.com:"$(cat ~/jiratoken)" 'https://circleci.atlassian.net/rest/api/2/search?jql=sprint+in+openSprints()+and+project=CIAM'
var jcl = http.Client{}
type Issue struct {
Summary string
Description string
Comments []Comment
}
type Comment struct {
Author Author
Body string
}
type Author struct {
DisplayName string
EmailAddress string
}
type JiraSearchResp struct {
Issues []RespIssue
}
type RespIssue struct {
Key string
Fields map[string]any
}
func GetIssues(instance, user, from, to string) ([]Issue, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/rest/api/2/search", instance), nil)
if err != nil {
return nil, fmt.Errorf("error building jira search request: %w", err)
}
q := req.URL.Query()
q.Add("jql", fmt.Sprintf("assignee was '%s' and resolved >= %s and resolved <= %s", user, from, to))
req.URL.RawQuery = q.Encode()
req.SetBasicAuth(os.Getenv("JIRA_USER"), os.Getenv("JIRA_TOKEN"))
resp, err := jcl.Do(req)
if err != nil {
return nil, fmt.Errorf("error executing jira search request: %w", err)
}
if resp.StatusCode >= 400 {
b := bytes.NewBuffer(nil)
io.Copy(b, resp.Body)
return nil, fmt.Errorf("error talking to jira: %s", b.String())
}
dec := json.NewDecoder(resp.Body)
var jsr *JiraSearchResp
dec.Decode(&jsr)
out := []Issue{}
for _, i := range jsr.Issues {
iss := Issue{}
if s, ok := i.Fields["summary"]; ok {
iss.Summary, _ = s.(string)
}
if d, ok := i.Fields["description"]; ok {
iss.Description, _ = d.(string)
}
if comms, ok := i.Fields["comment"].(map[string]any); ok {
if c := comms["comments"].([]map[string]any); len(c) > 0 {
iss.Comments = make([]Comment, 0, len(c))
for _, c2 := range c {
iss.Comments = append(iss.Comments, Comment{Author{DisplayName: c2["displayName"].(string), EmailAddress: c2["emailAddress"].(string)}, c2["body"].(string)})
}
}
}
out = append(out, iss)
}
return out, nil
}