Files

150 lines
5.1 KiB
Go
Raw Permalink Normal View History

package jira
import (
"context"
"net/url"
"strconv"
"strings"
)
// Myself returns the authenticated account -- the cheapest call that
// proves URL and credentials work.
func (c *Client) Myself(ctx context.Context) (User, error) {
var u User
return u, c.getInto(ctx, c.api("/myself"), nil, &u)
}
// ServerInfo returns the instance's /serverInfo.
func (c *Client) ServerInfo(ctx context.Context) (ServerInfo, error) {
var si ServerInfo
return si, c.getInto(ctx, c.api("/serverInfo"), nil, &si)
}
// ListProjects returns every project the account can browse, ordered by
// key. On Cloud it walks /project/search (archived and trashed projects
// are not included); Server/Data Center returns all from /project.
func (c *Client) ListProjects(ctx context.Context) ([]Project, error) {
if !c.cloud {
var out []Project
return out, c.getInto(ctx, c.api("/project"), nil, &out)
}
raw, err := c.Paginate(ctx, c.api("/project/search"), url.Values{"orderBy": []string{"key"}}, "values", 50)
if err != nil {
return nil, err
}
return decodeAll[Project](raw)
}
// Project returns one project with description, lead and components.
func (c *Client) Project(ctx context.Context, keyOrID string) (Project, error) {
var p Project
if err := requireKey("project key", keyOrID); err != nil {
return p, err
}
return p, c.getInto(ctx, c.api("/project/"+url.PathEscape(keyOrID)), nil, &p)
}
// ProjectVersions returns all versions of a project (unpaginated on this
// endpoint).
func (c *Client) ProjectVersions(ctx context.Context, keyOrID string) ([]Version, error) {
var out []Version
return out, c.getInto(ctx, c.api("/project/"+url.PathEscape(keyOrID)+"/versions"), nil, &out)
}
// Version returns one version.
func (c *Client) Version(ctx context.Context, id string) (Version, error) {
var v Version
return v, c.getInto(ctx, c.api("/version/"+url.PathEscape(id)), nil, &v)
}
// Fields returns every system and custom field of the site.
func (c *Client) Fields(ctx context.Context) ([]Field, error) {
var out []Field
return out, c.getInto(ctx, c.api("/field"), nil, &out)
}
// Issue returns one issue. fields limits the returned fields (nil: Jira's
// default, all navigable fields); expand is passed on as the expand
// parameter (e.g. "renderedFields", "changelog").
func (c *Client) Issue(ctx context.Context, keyOrID string, fields []string, expand ...string) (Issue, error) {
var is Issue
if err := requireKey("issue key", keyOrID); err != nil {
return is, err
}
params := url.Values{}
if len(fields) > 0 {
params.Set("fields", strings.Join(fields, ","))
}
if len(expand) > 0 {
params.Set("expand", strings.Join(expand, ","))
}
return is, c.getInto(ctx, c.api("/issue/"+url.PathEscape(keyOrID)), params, &is)
}
// CommentPage returns one page of an issue's comments, oldest first.
func (c *Client) CommentPage(ctx context.Context, keyOrID string, startAt, maxResults int) (CommentPage, error) {
var page CommentPage
q := url.Values{
"orderBy": {"created"},
"startAt": {strconv.Itoa(startAt)},
"maxResults": {strconv.Itoa(maxResults)},
}
return page, c.getInto(ctx, c.api("/issue/"+url.PathEscape(keyOrID)+"/comment"), q, &page)
}
// Comments returns an issue's comments from startAt on, oldest first. A
// later start is useful because a search response already embeds the
// first page.
func (c *Client) Comments(ctx context.Context, keyOrID string, startAt int) ([]Comment, error) {
var out []Comment
for {
page, err := c.CommentPage(ctx, keyOrID, startAt, 100)
if err != nil {
return out, err
}
out = append(out, page.Comments...)
startAt += len(page.Comments)
if len(page.Comments) == 0 || startAt >= page.Total {
return out, nil
}
}
}
// Changelog returns an issue's complete change history. The endpoint
// exists on Cloud only; Server/Data Center has the history in an issue
// fetched with expand "changelog".
func (c *Client) Changelog(ctx context.Context, keyOrID string) ([]ChangelogEntry, error) {
raw, err := c.Paginate(ctx, c.api("/issue/"+url.PathEscape(keyOrID)+"/changelog"), nil, "values", 100)
if err != nil {
return nil, err
}
return decodeAll[ChangelogEntry](raw)
}
// EditMeta returns, by field ID, the edit operations ("add", "set",
// "remove") the authenticated user may use on the issue. A missing field
// cannot be edited by this user (screen or permission).
func (c *Client) EditMeta(ctx context.Context, keyOrID string) (map[string][]string, error) {
var meta struct {
Fields map[string]struct {
Operations []string `json:"operations"`
} `json:"fields"`
}
if err := c.getInto(ctx, c.api("/issue/"+url.PathEscape(keyOrID)+"/editmeta"), nil, &meta); err != nil {
return nil, err
}
out := make(map[string][]string, len(meta.Fields))
for id, f := range meta.Fields {
out[id] = f.Operations
}
return out, nil
}
// Transitions returns the workflow transitions currently available on an
// issue.
func (c *Client) Transitions(ctx context.Context, keyOrID string) ([]Transition, error) {
var result struct {
Transitions []Transition `json:"transitions"`
}
return result.Transitions, c.getInto(ctx, c.api("/issue/"+url.PathEscape(keyOrID)+"/transitions"), nil, &result)
}