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:
commit
6a97dbd4c6
24 files changed
+3524
No files matched your search
@@ -0,0 +1,201 @@
|
||||
// Package confluence is a small Confluence REST API client (the classic
|
||||
// /rest/api content API) for Atlassian Cloud and Server/Data Center:
|
||||
// CQL search, pages with their children and ancestors, spaces, the
|
||||
// current user, and the writes create page, update page (with an
|
||||
// optimistic version check), footer comment and label.
|
||||
//
|
||||
// It is deliberately small -- what yamcp uses, taken over from there --
|
||||
// not a complete Confluence SDK. Cloud's newer v2 API (/wiki/api/v2),
|
||||
// attachments and the site-backup endpoints yabaratla drives are not
|
||||
// covered (see the repository's TODO.md).
|
||||
package confluence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"yetano.dev/yetano/yetano-clients/atlassian"
|
||||
"yetano.dev/yetano/yetano-clients/atlassian/internal/transport"
|
||||
)
|
||||
|
||||
// Client is a Confluence client. It is safe for concurrent use.
|
||||
type Client struct {
|
||||
t *transport.Client
|
||||
cloud bool
|
||||
}
|
||||
|
||||
// New builds a Client. BaseURL is the site root: on Cloud without the
|
||||
// /wiki suffix (the client adds it), on Server/Data Center the
|
||||
// Confluence root. It sends no request.
|
||||
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() }
|
||||
|
||||
// IsCloud reports whether the client talks to Atlassian Cloud.
|
||||
func (c *Client) IsCloud() bool { return c.cloud }
|
||||
|
||||
// APIPrefix is /wiki/rest/api on Cloud and /rest/api on Server/Data
|
||||
// Center.
|
||||
func (c *Client) APIPrefix() string {
|
||||
if c.cloud {
|
||||
return "/wiki/rest/api"
|
||||
}
|
||||
return "/rest/api"
|
||||
}
|
||||
|
||||
func (c *Client) api(suffix string) string { return c.APIPrefix() + suffix }
|
||||
|
||||
// WebBase is the root of the browsable site (with /wiki on Cloud).
|
||||
func (c *Client) WebBase() string {
|
||||
if c.cloud {
|
||||
return c.BaseURL() + "/wiki"
|
||||
}
|
||||
return c.BaseURL()
|
||||
}
|
||||
|
||||
// PageURL is the browser URL of a page: from its _links.webui when
|
||||
// known, else the viewpage.action form.
|
||||
func (c *Client) PageURL(id string, links *Links) string {
|
||||
if links != nil && links.WebUI != "" {
|
||||
return c.WebBase() + links.WebUI
|
||||
}
|
||||
return fmt.Sprintf("%s/pages/viewpage.action?pageId=%s", c.WebBase(), id)
|
||||
}
|
||||
|
||||
// Close releases idle connections.
|
||||
func (c *Client) Close() { c.t.Close() }
|
||||
|
||||
// SetSleep replaces every wait -- 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 (for endpoints without a typed
|
||||
// call) 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)
|
||||
}
|
||||
|
||||
func (c *Client) do(ctx context.Context, method, path string, q url.Values, body, out any) error {
|
||||
raw, err := c.t.Do(ctx, method, path, q, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if out == nil {
|
||||
return nil
|
||||
}
|
||||
if err := json.Unmarshal(raw, out); err != nil {
|
||||
return fmt.Errorf("%s %s: decoding response: %w", method, path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func requireID(what, v string) error {
|
||||
if v == "" {
|
||||
return fmt.Errorf("%w: %s must not be empty", atlassian.ErrInvalidArgument, what)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const contentExpand = "space,version,body.storage,ancestors"
|
||||
|
||||
// SearchOptions is one CQL search page. SpaceKey and ContentType are
|
||||
// convenience filters ANDed onto CQL.
|
||||
type SearchOptions struct {
|
||||
CQL string
|
||||
Start int
|
||||
Limit int
|
||||
SpaceKey string
|
||||
ContentType string // "page", "blogpost", ...
|
||||
}
|
||||
|
||||
// Search runs a CQL search and returns one page of content with space,
|
||||
// version, storage body and ancestors expanded.
|
||||
func (c *Client) Search(ctx context.Context, o SearchOptions) (ContentList, error) {
|
||||
var out ContentList
|
||||
cql := o.CQL
|
||||
if o.SpaceKey != "" {
|
||||
cql = addCQLClause(cql, fmt.Sprintf("space = %q", o.SpaceKey))
|
||||
}
|
||||
if o.ContentType != "" {
|
||||
cql = addCQLClause(cql, fmt.Sprintf("type = %q", o.ContentType))
|
||||
}
|
||||
if cql == "" {
|
||||
return out, fmt.Errorf("%w: cql (or space/content type filters) must resolve to a non-empty query", atlassian.ErrInvalidArgument)
|
||||
}
|
||||
q := url.Values{
|
||||
"cql": {cql},
|
||||
"start": {strconv.Itoa(o.Start)},
|
||||
"limit": {strconv.Itoa(o.Limit)},
|
||||
"expand": {contentExpand},
|
||||
}
|
||||
return out, c.do(ctx, http.MethodGet, c.api("/content/search"), q, nil, &out)
|
||||
}
|
||||
|
||||
func addCQLClause(existing, clause string) string {
|
||||
if existing == "" {
|
||||
return clause
|
||||
}
|
||||
return existing + " AND " + clause
|
||||
}
|
||||
|
||||
// Page returns one page (or other content) with space, version, storage
|
||||
// body and ancestors.
|
||||
func (c *Client) Page(ctx context.Context, id string) (Content, error) {
|
||||
var out Content
|
||||
if err := requireID("page id", id); err != nil {
|
||||
return out, err
|
||||
}
|
||||
q := url.Values{"expand": {contentExpand}}
|
||||
return out, c.do(ctx, http.MethodGet, c.api("/content/"+url.PathEscape(id)), q, nil, &out)
|
||||
}
|
||||
|
||||
// PageChildren returns one page of a page's direct child pages.
|
||||
func (c *Client) PageChildren(ctx context.Context, id string, start, limit int) (ContentList, error) {
|
||||
var out ContentList
|
||||
if err := requireID("page id", id); err != nil {
|
||||
return out, err
|
||||
}
|
||||
q := url.Values{"start": {strconv.Itoa(start)}, "limit": {strconv.Itoa(limit)}}
|
||||
return out, c.do(ctx, http.MethodGet, c.api("/content/"+url.PathEscape(id)+"/child/page"), q, nil, &out)
|
||||
}
|
||||
|
||||
// Space returns one space with its plain description and home page.
|
||||
func (c *Client) Space(ctx context.Context, key string) (Space, error) {
|
||||
var out Space
|
||||
if err := requireID("space key", key); err != nil {
|
||||
return out, err
|
||||
}
|
||||
q := url.Values{"expand": {"description.plain,homepage"}}
|
||||
return out, c.do(ctx, http.MethodGet, c.api("/space/"+url.PathEscape(key)), q, nil, &out)
|
||||
}
|
||||
|
||||
// ListSpaces returns one page of the spaces the account can see.
|
||||
func (c *Client) ListSpaces(ctx context.Context, start, limit int) (SpaceList, error) {
|
||||
var out SpaceList
|
||||
q := url.Values{"start": {strconv.Itoa(start)}, "limit": {strconv.Itoa(limit)}}
|
||||
return out, c.do(ctx, http.MethodGet, c.api("/space"), q, nil, &out)
|
||||
}
|
||||
|
||||
// CurrentUser returns the authenticated account.
|
||||
func (c *Client) CurrentUser(ctx context.Context) (User, error) {
|
||||
var out User
|
||||
return out, c.do(ctx, http.MethodGet, c.api("/user/current"), nil, nil, &out)
|
||||
}
|
||||
|
||||
// Ping checks that the API answers (one space listed). Confluence has no
|
||||
// server-info endpoint that is stable across Cloud and Data Center, so
|
||||
// this is reachability only, not a version.
|
||||
func (c *Client) Ping(ctx context.Context) error {
|
||||
return c.do(ctx, http.MethodGet, c.api("/space"), url.Values{"limit": {"1"}}, nil, nil)
|
||||
}
|
||||
Reference in new issue
Block a user