Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) <aider@aider.chat>
136 lines
3.4 KiB
Go
136 lines
3.4 KiB
Go
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
|
|
}
|
|
|
|
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(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
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/rest/api/3/search/jql", 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", accountID, from, to))
|
|
q.Add("fields", "*all")
|
|
req.URL.RawQuery = q.Encode()
|
|
req.SetBasicAuth(user, os.Getenv("JIRA_TOKEN"))
|
|
req.Header.Add("Accept", "application/json")
|
|
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)
|
|
}
|
|
|
|
out := []Issue{}
|
|
|
|
for _, i := range jsr.Issues {
|
|
fmt.Println(i)
|
|
iss := Issue{}
|
|
if s, ok := i.Fields["summary"]; ok {
|
|
fmt.Println(s)
|
|
iss.Summary, _ = s.(string)
|
|
}
|
|
if d, ok := i.Fields["description"]; ok {
|
|
fmt.Println(d)
|
|
iss.Description, _ = d.(string)
|
|
}
|
|
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)})
|
|
|
|
}
|
|
}
|
|
out = append(out, iss)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
}
|