147 lines
4.0 KiB
Go
147 lines
4.0 KiB
Go
package jira
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"yetano.dev/yetano/yetano-clients/atlassian"
|
||
|
|
)
|
||
|
|
|
||
|
|
// SearchQuery is one JQL search walked to the end by Search/SearchAll.
|
||
|
|
type SearchQuery struct {
|
||
|
|
JQL string
|
||
|
|
Fields []string
|
||
|
|
Expand string // comma-separated, e.g. "renderedFields,changelog"
|
||
|
|
PageSize int // default 100
|
||
|
|
}
|
||
|
|
|
||
|
|
// Search runs a JQL query and calls fn once per page, so a caller never
|
||
|
|
// has to hold more than it needs. On Cloud it uses the enhanced search
|
||
|
|
// endpoint (/rest/api/3/search/jql, nextPageToken pagination); on
|
||
|
|
// Server/Data Center POST /rest/api/2/search with startAt/total.
|
||
|
|
func (c *Client) Search(ctx context.Context, q SearchQuery, fn func([]Issue) error) error {
|
||
|
|
if strings.TrimSpace(q.JQL) == "" {
|
||
|
|
return fmt.Errorf("%w: jql must not be empty", atlassian.ErrInvalidArgument)
|
||
|
|
}
|
||
|
|
if q.PageSize <= 0 {
|
||
|
|
q.PageSize = 100
|
||
|
|
}
|
||
|
|
if !c.cloud {
|
||
|
|
return c.searchOffset(ctx, q, fn)
|
||
|
|
}
|
||
|
|
next := ""
|
||
|
|
for {
|
||
|
|
body := map[string]any{"jql": q.JQL, "fields": q.Fields, "maxResults": q.PageSize}
|
||
|
|
if q.Expand != "" {
|
||
|
|
body["expand"] = q.Expand
|
||
|
|
}
|
||
|
|
if next != "" {
|
||
|
|
body["nextPageToken"] = next
|
||
|
|
}
|
||
|
|
raw, err := c.Post(ctx, c.api("/search/jql"), body)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
var page struct {
|
||
|
|
Issues []Issue `json:"issues"`
|
||
|
|
NextPageToken string `json:"nextPageToken"`
|
||
|
|
IsLast *bool `json:"isLast"`
|
||
|
|
}
|
||
|
|
if err := json.Unmarshal(raw, &page); err != nil {
|
||
|
|
return fmt.Errorf("search: %w", err)
|
||
|
|
}
|
||
|
|
if len(page.Issues) > 0 {
|
||
|
|
if err := fn(page.Issues); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
next = page.NextPageToken
|
||
|
|
if next == "" || len(page.Issues) == 0 || (page.IsLast != nil && *page.IsLast) {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *Client) searchOffset(ctx context.Context, q SearchQuery, fn func([]Issue) error) error {
|
||
|
|
start := 0
|
||
|
|
for {
|
||
|
|
page, err := c.SearchPage(ctx, PageQuery{JQL: q.JQL, StartAt: start, MaxResults: q.PageSize, Fields: q.Fields, Expand: splitList(q.Expand)})
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if len(page.Issues) > 0 {
|
||
|
|
if err := fn(page.Issues); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
start += len(page.Issues)
|
||
|
|
if len(page.Issues) == 0 || start >= page.Total {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// SearchAll is Search collecting every issue.
|
||
|
|
func (c *Client) SearchAll(ctx context.Context, q SearchQuery) ([]Issue, error) {
|
||
|
|
var all []Issue
|
||
|
|
err := c.Search(ctx, q, func(page []Issue) error {
|
||
|
|
all = append(all, page...)
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
return all, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// PageQuery is one page of a classic offset search.
|
||
|
|
type PageQuery struct {
|
||
|
|
JQL string
|
||
|
|
StartAt int
|
||
|
|
MaxResults int
|
||
|
|
Fields []string
|
||
|
|
Expand []string
|
||
|
|
}
|
||
|
|
|
||
|
|
// SearchResult is one page of a classic offset search.
|
||
|
|
type SearchResult struct {
|
||
|
|
StartAt int `json:"startAt"`
|
||
|
|
MaxResults int `json:"maxResults"`
|
||
|
|
Total int `json:"total"`
|
||
|
|
Issues []Issue `json:"issues"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// SearchPage fetches one page through the classic POST {prefix}/search
|
||
|
|
// endpoint (startAt/maxResults/total). It is what Server/Data Center
|
||
|
|
// offers; on Cloud Atlassian has deprecated it in favour of
|
||
|
|
// /rest/api/3/search/jql (Search), so new Cloud code should use Search.
|
||
|
|
func (c *Client) SearchPage(ctx context.Context, q PageQuery) (SearchResult, error) {
|
||
|
|
var out SearchResult
|
||
|
|
if strings.TrimSpace(q.JQL) == "" {
|
||
|
|
return out, fmt.Errorf("%w: jql must not be empty", atlassian.ErrInvalidArgument)
|
||
|
|
}
|
||
|
|
body := struct {
|
||
|
|
JQL string `json:"jql"`
|
||
|
|
StartAt int `json:"startAt"`
|
||
|
|
MaxResults int `json:"maxResults"`
|
||
|
|
Fields []string `json:"fields,omitempty"`
|
||
|
|
Expand []string `json:"expand,omitempty"`
|
||
|
|
}{q.JQL, q.StartAt, q.MaxResults, q.Fields, q.Expand}
|
||
|
|
raw, err := c.Post(ctx, c.api("/search"), body)
|
||
|
|
if err != nil {
|
||
|
|
return out, err
|
||
|
|
}
|
||
|
|
if err := json.Unmarshal(raw, &out); err != nil {
|
||
|
|
return out, fmt.Errorf("search: %w", err)
|
||
|
|
}
|
||
|
|
return out, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func splitList(s string) []string {
|
||
|
|
var out []string
|
||
|
|
for _, p := range strings.Split(s, ",") {
|
||
|
|
if p = strings.TrimSpace(p); p != "" {
|
||
|
|
out = append(out, p)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|