Files
yetano-clients/README.md
T

155 lines
6.8 KiB
Markdown
Raw Permalink Normal View History

# yetano-clients
Shared Go clients for the external services Yetano programs talk to.
This is a library module (`yetano.dev/yetano/yetano-clients`), imported
like `core` -- nothing here is built or published as a program.
Today it holds the Atlassian clients that `yareco`, `yabaratla` and
`yamcp` used to carry as three separate copies (owner decision
2026-11-01):
| Package | What |
|---|---|
| `atlassian` | `Options`, `Auth`, `Deployment`, `TLS`, the error types (`APIError`, `StatusOf`, `ErrNotJSON`, `ErrResponseTooLarge`, `ErrInvalidArgument`) |
| `atlassian/jira` | Jira REST API client (Cloud v3 and Server/Data Center v2) |
| `atlassian/confluence` | small Confluence REST API client (classic content API) |
| `atlassian/internal/transport` | the HTTP machinery both share; not importable from outside |
| `yaiam` | sign a command-line program in at yaiam (device flow, RFC 8628) and keep its tokens (YAIAM-710, 2026-11-09) |
Standard library only, no third-party dependencies.
`yaiam` signs a command-line program in at yaiam (device flow) and keeps
its tokens; see below.
## Usage
```go
c, err := jira.New(atlassian.Options{
BaseURL: "https://example.atlassian.net",
Deployment: atlassian.Cloud, // or atlassian.DataCenter
Auth: atlassian.Auth{Type: atlassian.AuthAPIToken, Username: email, Password: token},
Timeout: 60 * time.Second,
MaxRetries: 3,
})
issues, err := c.SearchAll(ctx, jira.SearchQuery{JQL: "project = DEV", Fields: []string{"summary"}})
```
`New` never sends a request. Every call takes a context; cancelling it
also ends a backoff or rate-limit wait.
## Transport behaviour
One `Options` drives both clients:
- **Auth**: `api_token` (e-mail + API token as basic auth, Cloud),
`basic`, `bearer`/`pat` (Data Center personal access token), `cookie`
(a raw Cookie header). The zero `Type` picks from the fields that are
set. A type whose fields are empty sends no header at all.
- **TLS/proxy**: optional CA bundle, explicit `InsecureSkipVerify`,
`ProxyURL` or the environment's `HTTPS_PROXY`.
- **Redirects** to another host drop `Authorization` and `Cookie`
(also for downloads, e.g. a signed media URL).
- **Retries**: 5xx and connection errors are retried `MaxRetries`
attempts in total with backoff 2s, 4s, ... (cap 30s). 4xx is never
retried.
- **429**: pauses every request of the client until `Retry-After` and
widens a shared minimum gap between requests, which narrows again on
success; up to 10 such retries that do not count against `MaxRetries`.
`DisableRateLimitRetry` returns the 429 at once instead.
- **APIDelay**: fixed pause after every response.
- **Bodies**: an empty 2xx body reads as `{}`; a non-JSON 2xx body
(e.g. an SSO login page) is `ErrNotJSON`, never an empty result.
`MaxResponseBytes` caps what is read into memory.
- **Errors**: a non-2xx answer is an `*atlassian.APIError` (method,
path, status, body; `Message()` extracts Atlassian's error text).
Credentials never appear in an error or a log line; a download URL's
query is dropped from errors and logs.
- **Pagination**: `Paginate` walks `startAt`/`maxResults` listings and
advances by the number of items actually returned (Jira may cap
`maxResults`).
## Jira
Typed calls: `Myself`, `ServerInfo`, `ListProjects`, `Project`,
`ProjectVersions`, `Version`, `UpdateVersion`, `Fields`, `Issue`,
`Search`/`SearchAll` (Cloud: `/search/jql` with `nextPageToken`; Data
Center: `/search` with offsets), `SearchPage` (one classic offset page),
`CommentPage`, `Comments`, `Changelog`, `EditMeta`, `Transitions`,
`EditIssue`/`UpdateIssue`, `CreateIssue`, `AddComment`, `DoTransition`,
`AssignIssue`, `LinkIssues`; `TextBody` encodes plain text as ADF on
Cloud and as a string on Data Center. Field helpers for custom fields:
`OptionValues`, `SprintValues`, `CustomFieldNumber`, `Names`.
Everything else goes through the raw `Get`/`Post`/`Put`/`Delete`/
`Paginate`/`DownloadFile` (paths are absolute, e.g.
`/rest/agile/1.0/board`).
## Confluence
`Search` (CQL), `Page`, `PageChildren`, `Space`, `ListSpaces`,
`CurrentUser`, `Ping`, `CreatePage`, `UpdatePage` (re-reads the page and
returns `*VersionConflictError` instead of overwriting a newer version),
`AddComment`, `AddLabel`; `StorageFromText` turns plain text into an
escaped storage-format body. Kept small on purpose -- see `TODO.md` for
what is not covered.
## Where the code came from
| Part | Origin |
|---|---|
| retry/backoff, 429 throttle, `APIDelay`, `Paginate`, `DownloadFile`, cookie auth | `yabaratla/internal/jira` (via `yareco`, which added contexts and `log/slog`) |
| typed Jira reads, `Search` with `nextPageToken`, field helpers | `yareco/internal/jira` |
| bearer/PAT/basic auth, CA file, proxy, redirect hardening, response cap, Atlassian error messages, Cloud/Data Center split, Jira writes, the Confluence client | `yamcp/internal/provider/atlassian` |
Program-specific parts stayed in the programs: yareco's German operator
messages and field resolution, yamcp's error taxonomy and MCP output
types, yabaratla's Confluence site-backup client.
## Checks
```sh
make # fmt + vet + test (-race) + vulncheck
make test
```
## yaiam
A command-line program signs its user in at yaiam -- or any OpenID
Connect provider with the device flow -- and keeps the tokens:
```go
c := &yaiam.Client{Issuer: "https://login.example.org/t/default", ClientID: id}
store := yaiam.FileStore{Path: filepath.Join(stateDir, "token.json")}
c.Login(ctx, store, func(d *yaiam.DeviceAuth) error {
fmt.Printf("Open %s and enter %s\n", d.VerificationURI, d.UserCode)
return nil
})
err := c.Authorize(ctx, store, req) // Bearer header; refreshes when needed
c.Logout(ctx, store) // revokes at the provider, forgets them
```
- Only standards: discovery, device authorization, token endpoint
(device code, refresh token), revocation (RFC 7009), UserInfo. A
public client, no secret.
- `FileStore` keeps the tokens in a 0600 file and refuses one others
may read. A stored token is used only for its own issuer and client;
a refused refresh token removes it (`ErrLoginRequired`).
- Every endpoint must be https (http only on a loopback host), the
discovery document must name exactly the issuer, redirects are never
followed, answers are capped at 1 MiB. Tokens appear in no error, and
`fmt` prints a `Token` without them.
- Checking tokens on the server side is not here: yaiam's
`pkg/httpauth` (JWT against the issuer's keys, optional
introspection).
The user's guide is yaiam's `docs/manual/yetano-programme.md`; an
example program is yaiam's `examples/device-login`.
## Versioning
A release is a plain semver git tag (`v0.1.0`, ...) on this repository;
consumers require that tag. Never force-move a tag -- always cut a new
one, even for a one-line fix (see the meta-repository's `AGENTS.md`).
## License
Proprietary, see [LICENSE](LICENSE).