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>
138 lines
4.4 KiB
Go
138 lines
4.4 KiB
Go
// Package jira is a Jira REST API client for Atlassian Cloud (REST API
|
|
// v3, Atlassian Document Format bodies) and Server/Data Center (REST API
|
|
// v2, plain-text/wiki-markup bodies).
|
|
//
|
|
// It offers typed calls for what the yetano programs use (projects,
|
|
// versions, fields, JQL search, issues, comments, changelog, edit
|
|
// metadata, transitions, issue writes) and the raw Get/Post/Put/Delete/
|
|
// Paginate/DownloadFile underneath for everything else (the Agile API,
|
|
// worklogs, attachments, ...). Every call takes a context; retries,
|
|
// rate-limit handling and authentication are described on
|
|
// atlassian.Options.
|
|
package jira
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"yetano.dev/yetano/yetano-clients/atlassian"
|
|
"yetano.dev/yetano/yetano-clients/atlassian/internal/transport"
|
|
)
|
|
|
|
// Client is a Jira client. It is safe for concurrent use.
|
|
type Client struct {
|
|
t *transport.Client
|
|
cloud bool
|
|
}
|
|
|
|
// New builds a Client. It sends no request; a bad TLS CA file or proxy
|
|
// URL is the only error.
|
|
func New(o atlassian.Options) (*Client, error) {
|
|
t, err := transport.New(o)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Client{t: t, cloud: o.Deployment.IsCloud()}, nil
|
|
}
|
|
|
|
// BaseURL is the site root without trailing slash.
|
|
func (c *Client) BaseURL() string { return c.t.BaseURL() }
|
|
|
|
// HasCredentials reports whether any credentials are configured.
|
|
func (c *Client) HasCredentials() bool { return c.t.HasCredentials() }
|
|
|
|
// IsCloud reports whether the client talks to Atlassian Cloud.
|
|
func (c *Client) IsCloud() bool { return c.cloud }
|
|
|
|
// APIPrefix is /rest/api/3 on Cloud and /rest/api/2 on Server/Data
|
|
// Center.
|
|
func (c *Client) APIPrefix() string {
|
|
if c.cloud {
|
|
return "/rest/api/3"
|
|
}
|
|
return "/rest/api/2"
|
|
}
|
|
|
|
func (c *Client) api(suffix string) string { return c.APIPrefix() + suffix }
|
|
|
|
// IssueURL is the browser URL of an issue.
|
|
func (c *Client) IssueURL(key string) string {
|
|
return fmt.Sprintf("%s/browse/%s", c.BaseURL(), key)
|
|
}
|
|
|
|
// Close releases idle connections.
|
|
func (c *Client) Close() { c.t.Close() }
|
|
|
|
// SetSleep replaces every wait (backoff, Retry-After, throttle,
|
|
// APIDelay) -- for tests that must not really wait.
|
|
func (c *Client) SetSleep(f func(ctx context.Context, d time.Duration) error) { c.t.SetSleep(f) }
|
|
|
|
// Get issues a GET against BaseURL+path and returns the JSON body.
|
|
func (c *Client) Get(ctx context.Context, path string, params url.Values) (json.RawMessage, error) {
|
|
return c.t.Do(ctx, http.MethodGet, path, params, nil)
|
|
}
|
|
|
|
// Post issues a POST with a JSON body.
|
|
func (c *Client) Post(ctx context.Context, path string, body any) (json.RawMessage, error) {
|
|
return c.t.Do(ctx, http.MethodPost, path, nil, body)
|
|
}
|
|
|
|
// Put issues a PUT with a JSON body.
|
|
func (c *Client) Put(ctx context.Context, path string, body any) (json.RawMessage, error) {
|
|
return c.t.Do(ctx, http.MethodPut, path, nil, body)
|
|
}
|
|
|
|
// Delete issues a DELETE.
|
|
func (c *Client) Delete(ctx context.Context, path string, params url.Values) (json.RawMessage, error) {
|
|
return c.t.Do(ctx, http.MethodDelete, path, params, nil)
|
|
}
|
|
|
|
// Paginate walks a startAt/maxResults listing and returns every element
|
|
// of the itemsKey array ("values" for most endpoints, "comments" for
|
|
// issue comments) across all pages.
|
|
func (c *Client) Paginate(ctx context.Context, path string, params url.Values, itemsKey string, pageSize int) ([]json.RawMessage, error) {
|
|
return c.t.Paginate(ctx, path, params, itemsKey, pageSize)
|
|
}
|
|
|
|
// DownloadFile streams fileURL (absolute, or a path starting with "/")
|
|
// to dest, retrying and removing partial files; see
|
|
// atlassian.Options.DownloadTimeout. Credentials are not sent along a
|
|
// redirect to another host.
|
|
func (c *Client) DownloadFile(ctx context.Context, fileURL, dest string) error {
|
|
return c.t.DownloadFile(ctx, fileURL, dest)
|
|
}
|
|
|
|
func decodeAll[T any](raw []json.RawMessage) ([]T, error) {
|
|
out := make([]T, 0, len(raw))
|
|
for _, r := range raw {
|
|
var v T
|
|
if err := json.Unmarshal(r, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, v)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (c *Client) getInto(ctx context.Context, path string, params url.Values, out any) error {
|
|
raw, err := c.Get(ctx, path, params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := json.Unmarshal(raw, out); err != nil {
|
|
return fmt.Errorf("GET %s: decoding response: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func requireKey(what, v string) error {
|
|
if v == "" {
|
|
return fmt.Errorf("%w: %s must not be empty", atlassian.ErrInvalidArgument, what)
|
|
}
|
|
return nil
|
|
}
|