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>
144 lines
3.7 KiB
Go
144 lines
3.7 KiB
Go
package jira
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// Names returns the names of a list of Named/Version values.
|
|
func Names[T Named | Version](items []T) []string {
|
|
out := make([]string, 0, len(items))
|
|
for _, it := range items {
|
|
switch v := any(it).(type) {
|
|
case Named:
|
|
out = append(out, v.Name)
|
|
case Version:
|
|
out = append(out, v.Name)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// OptionValues reads a select/multi-select/label-like field value as a
|
|
// list of strings: null, a string, a list of strings, an option object
|
|
// {"value": ...} (or {"name": ...}) or a list of those. A value of any
|
|
// other shape is an error, never silently empty -- a field mapped to the
|
|
// wrong field ID must be noticed.
|
|
func OptionValues(raw json.RawMessage) ([]string, error) {
|
|
s := strings.TrimSpace(string(raw))
|
|
if len(raw) == 0 || s == "null" {
|
|
return nil, nil
|
|
}
|
|
var str string
|
|
if json.Unmarshal(raw, &str) == nil {
|
|
return nonEmpty(str), nil
|
|
}
|
|
if strings.HasPrefix(s, "{") {
|
|
var opt struct {
|
|
Value *string `json:"value"`
|
|
Name *string `json:"name"`
|
|
}
|
|
if err := json.Unmarshal(raw, &opt); err != nil {
|
|
return nil, err
|
|
}
|
|
switch {
|
|
case opt.Value != nil:
|
|
return nonEmpty(*opt.Value), nil
|
|
case opt.Name != nil:
|
|
return nonEmpty(*opt.Name), nil
|
|
}
|
|
return nil, fmt.Errorf("unexpected field value %s", abbreviate(s))
|
|
}
|
|
var list []json.RawMessage
|
|
if err := json.Unmarshal(raw, &list); err != nil {
|
|
return nil, fmt.Errorf("unexpected field value %s", abbreviate(s))
|
|
}
|
|
var out []string
|
|
for _, item := range list {
|
|
vals, err := OptionValues(item)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, vals...)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Sprint is one entry of the Jira Software sprint field.
|
|
type Sprint struct {
|
|
Name string `json:"name"`
|
|
State string `json:"state"` // active, future or closed
|
|
}
|
|
|
|
var rxLegacySprint = regexp.MustCompile(`(?:^|[\[,])(state|name)=([^,\]]*)`)
|
|
|
|
// SprintValues reads the sprint field: null, a list of sprint objects
|
|
// (Cloud) or a list of the legacy
|
|
// "com.atlassian.greenhopper.service.sprint.Sprint@…[state=…,name=…]"
|
|
// strings (older Data Center). States are lower-cased. Like
|
|
// OptionValues, any other shape is an error, never silently "no sprint".
|
|
func SprintValues(raw json.RawMessage) ([]Sprint, error) {
|
|
s := strings.TrimSpace(string(raw))
|
|
if len(raw) == 0 || s == "null" {
|
|
return nil, nil
|
|
}
|
|
var list []json.RawMessage
|
|
if err := json.Unmarshal(raw, &list); err != nil {
|
|
return nil, fmt.Errorf("unexpected sprint value %s", abbreviate(s))
|
|
}
|
|
var out []Sprint
|
|
for _, item := range list {
|
|
var legacy string
|
|
if json.Unmarshal(item, &legacy) == nil {
|
|
var sp Sprint
|
|
for _, m := range rxLegacySprint.FindAllStringSubmatch(legacy, -1) {
|
|
if m[1] == "name" {
|
|
sp.Name = m[2]
|
|
} else {
|
|
sp.State = strings.ToLower(m[2])
|
|
}
|
|
}
|
|
if sp.Name == "" {
|
|
return nil, fmt.Errorf("unexpected sprint value %s", abbreviate(legacy))
|
|
}
|
|
out = append(out, sp)
|
|
continue
|
|
}
|
|
var sp Sprint
|
|
if err := json.Unmarshal(item, &sp); err != nil || sp.Name == "" {
|
|
return nil, fmt.Errorf("unexpected sprint value %s", abbreviate(string(item)))
|
|
}
|
|
sp.State = strings.ToLower(sp.State)
|
|
out = append(out, sp)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func nonEmpty(s string) []string {
|
|
if s = strings.TrimSpace(s); s == "" {
|
|
return nil
|
|
}
|
|
return []string{s}
|
|
}
|
|
|
|
func abbreviate(s string) string {
|
|
if len(s) > 80 {
|
|
return s[:80] + "…"
|
|
}
|
|
return s
|
|
}
|
|
|
|
var rxCustomField = regexp.MustCompile(`^customfield_(\d+)$`)
|
|
|
|
// CustomFieldNumber returns "11411" for "customfield_11411" (the cf[N]
|
|
// JQL form), ok=false for a system field.
|
|
func CustomFieldNumber(id string) (string, bool) {
|
|
m := rxCustomField.FindStringSubmatch(id)
|
|
if m == nil {
|
|
return "", false
|
|
}
|
|
return m[1], true
|
|
}
|