Files
yetano-clients/atlassian/confluence/write.go
T

148 lines
4.2 KiB
Go
Raw Normal View History

package confluence
import (
"context"
"fmt"
"html"
"net/http"
"net/url"
"strings"
)
// StorageFromText turns plain text into a minimal storage-format body:
// each blank-line-separated paragraph becomes an HTML-escaped <p>. It
// never passes markup through, so text from an untrusted source cannot
// inject active content into a page.
func StorageFromText(text string) string {
var b strings.Builder
for _, p := range strings.Split(strings.TrimSpace(text), "\n\n") {
p = strings.TrimSpace(p)
if p == "" {
continue
}
b.WriteString("<p>")
b.WriteString(html.EscapeString(p))
b.WriteString("</p>")
}
return b.String()
}
type storageBody struct {
Storage BodyRepresentation `json:"storage"`
}
func storage(text string) storageBody {
return storageBody{Storage: BodyRepresentation{Value: StorageFromText(text), Representation: "storage"}}
}
// CreatePageInput describes a new page; Content is plain text (see
// StorageFromText).
type CreatePageInput struct {
SpaceKey string
Title string
Content string
ParentPageID string
}
// CreatePage creates a page and returns its ID.
func (c *Client) CreatePage(ctx context.Context, in CreatePageInput) (string, error) {
body := map[string]any{
"type": "page",
"title": in.Title,
"space": SpaceRef{Key: in.SpaceKey},
"body": storage(in.Content),
}
if in.ParentPageID != "" {
body["ancestors"] = []ContentRef{{ID: in.ParentPageID}}
}
var resp Content
if err := c.do(ctx, http.MethodPost, c.api("/content"), nil, body, &resp); err != nil {
return "", err
}
return resp.ID, nil
}
// VersionConflictError is returned by UpdatePage when the page changed
// since the caller read it.
type VersionConflictError struct {
PageID string
Current int
Expected int
}
func (e *VersionConflictError) Error() string {
return fmt.Sprintf("page %s has changed since it was last read (current version %d, expected %d); re-read the page and retry",
e.PageID, e.Current, e.Expected)
}
// UpdatePageInput describes an update; Content is plain text.
type UpdatePageInput struct {
ID string
Title string // empty keeps the current title
Content string
ExpectedVersion int // the version the caller last read
NewParentID string // empty keeps the parent
}
// UpdatePage replaces a page's body, never blindly: it re-reads the page
// and returns a *VersionConflictError without writing anything when its
// current version is not ExpectedVersion.
func (c *Client) UpdatePage(ctx context.Context, in UpdatePageInput) error {
current, err := c.Page(ctx, in.ID)
if err != nil {
return err
}
if current.Version == nil {
return fmt.Errorf("page %s has no version information", in.ID)
}
if current.Version.Number != in.ExpectedVersion {
return &VersionConflictError{PageID: in.ID, Current: current.Version.Number, Expected: in.ExpectedVersion}
}
title := in.Title
if title == "" {
title = current.Title
}
spaceKey := ""
if current.Space != nil {
spaceKey = current.Space.Key
}
body := map[string]any{
"id": in.ID,
"type": "page",
"title": title,
"space": SpaceRef{Key: spaceKey},
"body": storage(in.Content),
"version": map[string]int{"number": current.Version.Number + 1},
}
if in.NewParentID != "" {
body["ancestors"] = []ContentRef{{ID: in.NewParentID}}
}
return c.do(ctx, http.MethodPut, c.api("/content/"+url.PathEscape(in.ID)), nil, body, nil)
}
// AddComment adds a plain-text footer comment to a page and returns its
// ID.
func (c *Client) AddComment(ctx context.Context, pageID, text string) (string, error) {
if err := requireID("page id", pageID); err != nil {
return "", err
}
body := map[string]any{
"type": "comment",
"container": map[string]string{"id": pageID, "type": "page"},
"body": storage(text),
}
var resp Content
if err := c.do(ctx, http.MethodPost, c.api("/content"), nil, body, &resp); err != nil {
return "", err
}
return resp.ID, nil
}
// AddLabel adds a global label to a page.
func (c *Client) AddLabel(ctx context.Context, pageID, label string) error {
if err := requireID("page id", pageID); err != nil {
return err
}
body := []map[string]string{{"prefix": "global", "name": label}}
return c.do(ctx, http.MethodPost, c.api("/content/"+url.PathEscape(pageID)+"/label"), nil, body, nil)
}