Files

168 lines
4.8 KiB
Go
Raw Permalink Normal View History

package jira
import (
"context"
"encoding/json"
"fmt"
"net/url"
)
// TextBody encodes plain text as a description/comment body: a minimal
// Atlassian Document Format document (one paragraph) on Cloud, the plain
// string on Server/Data Center.
func (c *Client) TextBody(text string) any {
if !c.cloud {
return text
}
return map[string]any{
"type": "doc",
"version": 1,
"content": []any{
map[string]any{
"type": "paragraph",
"content": []any{
map[string]any{"type": "text", "text": text},
},
},
},
}
}
// IssueEdit is one PUT on an issue. Fields replaces whole field values
// ({"summary": "x"}); Update applies operations to them
// ({"labels": [{"add": "x"}], "components": [{"remove": {"id": "10"}}]}),
// touching only the named values, so a value someone else added in the
// meantime stays. An empty part is left out of the request.
type IssueEdit struct {
Fields map[string]any
Update map[string][]map[string]any
}
// EditIssue applies e to an issue.
func (c *Client) EditIssue(ctx context.Context, keyOrID string, e IssueEdit) error {
if err := requireKey("issue key", keyOrID); err != nil {
return err
}
body := map[string]any{}
if len(e.Fields) > 0 {
body["fields"] = e.Fields
}
if len(e.Update) > 0 {
body["update"] = e.Update
}
_, err := c.Put(ctx, c.api("/issue/"+url.PathEscape(keyOrID)), body)
return err
}
// UpdateIssue sets whole field values on an issue (EditIssue with
// Fields only).
func (c *Client) UpdateIssue(ctx context.Context, keyOrID string, fields map[string]any) error {
return c.EditIssue(ctx, keyOrID, IssueEdit{Fields: fields})
}
// UpdateVersion changes a version (e.g. {"released": true,
// "releaseDate": "2026-11-01"}).
func (c *Client) UpdateVersion(ctx context.Context, id string, body map[string]any) error {
if err := requireKey("version id", id); err != nil {
return err
}
_, err := c.Put(ctx, c.api("/version/"+url.PathEscape(id)), body)
return err
}
// CreateIssueInput describes a new issue; ParentKey makes it a subtask.
type CreateIssueInput struct {
ProjectKey string
IssueType string // issue type name
Summary string
Description string // plain text, encoded with TextBody
Labels []string
ParentKey string
}
// CreateIssue creates an issue and returns its key.
func (c *Client) CreateIssue(ctx context.Context, in CreateIssueInput) (string, error) {
fields := map[string]any{
"project": map[string]string{"key": in.ProjectKey},
"issuetype": map[string]string{"name": in.IssueType},
"summary": in.Summary,
}
if in.Description != "" {
fields["description"] = c.TextBody(in.Description)
}
if len(in.Labels) > 0 {
fields["labels"] = in.Labels
}
if in.ParentKey != "" {
fields["parent"] = map[string]string{"key": in.ParentKey}
}
raw, err := c.Post(ctx, c.api("/issue"), map[string]any{"fields": fields})
if err != nil {
return "", err
}
var resp struct {
Key string `json:"key"`
}
if err := json.Unmarshal(raw, &resp); err != nil {
return "", fmt.Errorf("create issue: %w", err)
}
return resp.Key, nil
}
// AddComment adds a plain-text comment (encoded with TextBody) and
// returns its ID.
func (c *Client) AddComment(ctx context.Context, keyOrID, text string) (string, error) {
if err := requireKey("issue key", keyOrID); err != nil {
return "", err
}
raw, err := c.Post(ctx, c.api("/issue/"+url.PathEscape(keyOrID)+"/comment"), map[string]any{"body": c.TextBody(text)})
if err != nil {
return "", err
}
var resp struct {
ID string `json:"id"`
}
if err := json.Unmarshal(raw, &resp); err != nil {
return "", fmt.Errorf("add comment: %w", err)
}
return resp.ID, nil
}
// DoTransition moves an issue along the transition with the given ID
// (see Transitions).
func (c *Client) DoTransition(ctx context.Context, keyOrID, transitionID string) error {
_, err := c.Post(ctx, c.api("/issue/"+url.PathEscape(keyOrID)+"/transitions"),
map[string]any{"transition": map[string]string{"id": transitionID}})
return err
}
// AssignIssue assigns an issue to a Cloud accountId or a Server/Data
// Center username; an empty assignee unassigns it.
func (c *Client) AssignIssue(ctx context.Context, keyOrID, assignee string) error {
body := map[string]any{}
switch {
case c.cloud:
body["accountId"] = nilIfEmpty(assignee)
default:
body["name"] = nilIfEmpty(assignee)
}
_, err := c.Put(ctx, c.api("/issue/"+url.PathEscape(keyOrID)+"/assignee"), body)
return err
}
func nilIfEmpty(s string) any {
if s == "" {
return nil
}
return s
}
// LinkIssues links two issues with the named link type.
func (c *Client) LinkIssues(ctx context.Context, linkType, inwardKey, outwardKey string) error {
_, err := c.Post(ctx, c.api("/issueLink"), map[string]any{
"type": map[string]string{"name": linkType},
"inwardIssue": map[string]string{"key": inwardKey},
"outwardIssue": map[string]string{"key": outwardKey},
})
return err
}