Shared Atlassian clients: atlassian/jira and atlassian/confluence

New library module yetano.dev/yetano/yetano-clients (owner decision
2026-11-01) replacing the three own Jira clients in yareco, yabaratla
and yamcp:

- atlassian: Options, Auth (api_token, basic, bearer/pat, cookie),
  Deployment (Cloud/Data Center), TLS, APIError and sentinel errors.
- atlassian/internal/transport: retry with backoff on 5xx/connection
  errors, shared adaptive 429 throttle honouring Retry-After (from
  yabaratla/yareco), CA file, proxy, cross-host redirect hardening,
  response cap (from yamcp), startAt pagination, file downloads.
- atlassian/jira: typed reads (projects, versions, fields, issues,
  comments, changelog, editmeta, transitions), JQL search on Cloud
  (/search/jql) and Data Center, classic search pages, issue writes,
  ADF/plain-text bodies per deployment, custom-field helpers.
- atlassian/confluence: small content API client (CQL search, pages,
  spaces, current user, create/update page with version check,
  comment, label), taken over from yamcp.

Standard library only; tests against httptest servers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
terminarandClaude Opus 5.5 committed 2026-11-01 14:11:00 +01:00
commit 6a97dbd4c6
24 files changed
+3524

No files matched your search

+147
View File
@@ -0,0 +1,147 @@
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
}