autocrossbow/issues/jira.go

153 lines
3.6 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"
2025-11-26 11:34:13 -05:00
"github.com/eliziario/jira-lib/pkg/adf"
)
2025-09-13 08:40:40 -04:00
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
}
type UserSearchResp struct {
AccountID string `json:"accountId"`
Name string `json:"name"`
}
func getUserAccountID(instance, user string) (string, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/rest/api/3/user/search?query=%s", instance, user), nil)
if err != nil {
return "", fmt.Errorf("error building jira user search request: %w", err)
}
req.SetBasicAuth(os.Getenv("JIRA_USER"), os.Getenv("JIRA_TOKEN"))
req.Header.Add("Accept", "application/json")
resp, err := jcl.Do(req)
if err != nil {
return "", fmt.Errorf("error executing jira user search request: %w", err)
}
if resp.StatusCode >= 400 {
b := bytes.NewBuffer(nil)
io.Copy(b, resp.Body)
return "", fmt.Errorf("error talking to jira: %s", b.String())
}
dec := json.NewDecoder(resp.Body)
var users []UserSearchResp
err = dec.Decode(&users)
if err != nil {
return "", fmt.Errorf("error decoding jira user search response: %w", err)
}
if len(users) == 0 {
return "", fmt.Errorf("no user found with query %s", user)
}
return users[0].AccountID, nil
}
2025-09-13 08:40:40 -04:00
func GetIssues(instance, user, from, to string) ([]Issue, error) {
// First get the user's account ID
accountID, err := getUserAccountID(instance, user)
if err != nil {
return nil, fmt.Errorf("error getting user account ID: %w", err)
}
2025-10-28 12:28:04 -04:00
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/rest/api/3/search/jql", instance), nil)
2025-09-13 08:40:40 -04:00
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", accountID, from, to))
2025-10-28 12:28:04 -04:00
q.Add("fields", "*all")
2025-11-26 11:34:13 -05:00
q.Add("expand", "renderedFields")
2025-09-13 08:40:40 -04:00
req.URL.RawQuery = q.Encode()
req.SetBasicAuth(os.Getenv("JIRA_USER"), os.Getenv("JIRA_TOKEN"))
2025-10-28 12:28:04 -04:00
req.Header.Add("Accept", "application/json")
2025-09-13 08:40:40 -04:00
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
err = dec.Decode(&jsr)
if err != nil {
return nil, fmt.Errorf("error decoding jira search response: %w", err)
}
2025-09-13 08:40:40 -04:00
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 {
2025-11-26 11:34:13 -05:00
doc := ifaceToADF(d)
iss.Description = adf.NewTranslator(doc, adf.NewMarkdownTranslator()).Translate()
2025-09-13 08:40:40 -04:00
}
2025-10-28 12:28:04 -04:00
if comms, ok := i.Fields["comment"].([]map[string]any); ok && len(comms) > 0 {
iss.Comments = make([]Comment, 0, len(comms))
for _, c2 := range comms {
iss.Comments = append(iss.Comments, Comment{Author{DisplayName: c2["displayName"].(string), EmailAddress: c2["emailAddress"].(string)}, c2["body"].(string)})
2025-09-13 08:40:40 -04:00
}
}
out = append(out, iss)
}
return out, nil
}
2025-11-26 11:34:13 -05:00
func ifaceToADF(v any) *adf.ADF {
if v == nil {
return nil
}
var doc *adf.ADF
js, err := json.Marshal(v)
if err != nil {
return nil // ignore invalid data
}
if err = json.Unmarshal(js, &doc); err != nil {
return nil // ignore invalid data
}
return doc
}