|
| 1 | +package confluence |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// Page represents a Confluence page from the v2 API. |
| 15 | +type Page struct { |
| 16 | + ID string `json:"id"` |
| 17 | + Title string `json:"title"` |
| 18 | + Status string `json:"status"` |
| 19 | + SpaceID string `json:"spaceId"` |
| 20 | + ParentID string `json:"parentId"` |
| 21 | + AuthorID string `json:"authorId"` |
| 22 | + CreatedAt time.Time `json:"createdAt"` |
| 23 | + Version struct { |
| 24 | + Number int `json:"number"` |
| 25 | + AuthorID string `json:"authorId"` |
| 26 | + CreatedAt time.Time `json:"createdAt"` |
| 27 | + } `json:"version"` |
| 28 | + Body struct { |
| 29 | + Storage struct { |
| 30 | + Value string `json:"value"` |
| 31 | + } `json:"storage"` |
| 32 | + } `json:"body"` |
| 33 | + Labels struct { |
| 34 | + Results []Label `json:"results"` |
| 35 | + } `json:"labels"` |
| 36 | + Links struct { |
| 37 | + WebUI string `json:"webui"` |
| 38 | + } `json:"_links"` |
| 39 | +} |
| 40 | + |
| 41 | +// Space represents a Confluence space. |
| 42 | +type Space struct { |
| 43 | + ID string `json:"id"` |
| 44 | + Key string `json:"key"` |
| 45 | + Name string `json:"name"` |
| 46 | + Type string `json:"type"` |
| 47 | + Status string `json:"status"` |
| 48 | + Description struct { |
| 49 | + Plain struct { |
| 50 | + Value string `json:"value"` |
| 51 | + } `json:"plain"` |
| 52 | + } `json:"description"` |
| 53 | + Links struct { |
| 54 | + WebUI string `json:"webui"` |
| 55 | + } `json:"_links"` |
| 56 | +} |
| 57 | + |
| 58 | +// Label represents a Confluence label. |
| 59 | +type Label struct { |
| 60 | + ID string `json:"id"` |
| 61 | + Name string `json:"name"` |
| 62 | +} |
| 63 | + |
| 64 | +type pageResponse struct { |
| 65 | + Results []Page `json:"results"` |
| 66 | + Links struct { |
| 67 | + Next string `json:"next"` |
| 68 | + } `json:"_links"` |
| 69 | +} |
| 70 | + |
| 71 | +type spaceResponse struct { |
| 72 | + Results []Space `json:"results"` |
| 73 | + Links struct { |
| 74 | + Next string `json:"next"` |
| 75 | + } `json:"_links"` |
| 76 | +} |
| 77 | + |
| 78 | +// Client wraps the Confluence REST API v2. |
| 79 | +type Client struct { |
| 80 | + baseURL string |
| 81 | + httpClient *http.Client |
| 82 | + username string |
| 83 | + token string |
| 84 | +} |
| 85 | + |
| 86 | +// NewClient creates a new Confluence API client. |
| 87 | +func NewClient(baseURL, username, token string) *Client { |
| 88 | + return &Client{ |
| 89 | + baseURL: strings.TrimRight(baseURL, "/"), |
| 90 | + httpClient: &http.Client{Timeout: 30 * time.Second}, |
| 91 | + username: username, |
| 92 | + token: token, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// GetSpaces returns all spaces, optionally filtered by keys. |
| 97 | +func (c *Client) GetSpaces(ctx context.Context, keys []string) ([]Space, error) { |
| 98 | + var all []Space |
| 99 | + cursor := "" |
| 100 | + for { |
| 101 | + params := url.Values{} |
| 102 | + params.Set("limit", "25") |
| 103 | + if len(keys) > 0 { |
| 104 | + params.Set("keys", strings.Join(keys, ",")) |
| 105 | + } |
| 106 | + if cursor != "" { |
| 107 | + params.Set("cursor", cursor) |
| 108 | + } |
| 109 | + |
| 110 | + var resp spaceResponse |
| 111 | + if err := c.get(ctx, "/api/v2/spaces", params, &resp); err != nil { |
| 112 | + return nil, fmt.Errorf("get spaces: %w", err) |
| 113 | + } |
| 114 | + all = append(all, resp.Results...) |
| 115 | + |
| 116 | + cursor = parseCursor(resp.Links.Next) |
| 117 | + if cursor == "" { |
| 118 | + break |
| 119 | + } |
| 120 | + } |
| 121 | + return all, nil |
| 122 | +} |
| 123 | + |
| 124 | +// GetPages returns all pages in a space. |
| 125 | +func (c *Client) GetPages(ctx context.Context, spaceID string) ([]Page, error) { |
| 126 | + var all []Page |
| 127 | + cursor := "" |
| 128 | + for { |
| 129 | + params := url.Values{} |
| 130 | + params.Set("space-id", spaceID) |
| 131 | + params.Set("limit", "25") |
| 132 | + params.Set("body-format", "storage") |
| 133 | + if cursor != "" { |
| 134 | + params.Set("cursor", cursor) |
| 135 | + } |
| 136 | + |
| 137 | + var resp pageResponse |
| 138 | + if err := c.get(ctx, "/api/v2/pages", params, &resp); err != nil { |
| 139 | + return nil, fmt.Errorf("get pages for space %s: %w", spaceID, err) |
| 140 | + } |
| 141 | + all = append(all, resp.Results...) |
| 142 | + |
| 143 | + cursor = parseCursor(resp.Links.Next) |
| 144 | + if cursor == "" { |
| 145 | + break |
| 146 | + } |
| 147 | + } |
| 148 | + return all, nil |
| 149 | +} |
| 150 | + |
| 151 | +// GetPageLabels returns labels for a page. |
| 152 | +func (c *Client) GetPageLabels(ctx context.Context, pageID string) ([]Label, error) { |
| 153 | + var resp struct { |
| 154 | + Results []Label `json:"results"` |
| 155 | + } |
| 156 | + if err := c.get(ctx, "/api/v2/pages/"+pageID+"/labels", nil, &resp); err != nil { |
| 157 | + return nil, fmt.Errorf("get labels for page %s: %w", pageID, err) |
| 158 | + } |
| 159 | + return resp.Results, nil |
| 160 | +} |
| 161 | + |
| 162 | +func (c *Client) get(ctx context.Context, path string, params url.Values, out any) error { |
| 163 | + u := c.baseURL + path |
| 164 | + if len(params) > 0 { |
| 165 | + u += "?" + params.Encode() |
| 166 | + } |
| 167 | + |
| 168 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) |
| 169 | + if err != nil { |
| 170 | + return fmt.Errorf("create request: %w", err) |
| 171 | + } |
| 172 | + req.SetBasicAuth(c.username, c.token) |
| 173 | + req.Header.Set("Accept", "application/json") |
| 174 | + |
| 175 | + resp, err := c.httpClient.Do(req) |
| 176 | + if err != nil { |
| 177 | + return fmt.Errorf("execute request: %w", err) |
| 178 | + } |
| 179 | + defer resp.Body.Close() |
| 180 | + |
| 181 | + if resp.StatusCode != http.StatusOK { |
| 182 | + body, _ := io.ReadAll(resp.Body) |
| 183 | + return fmt.Errorf("unexpected status %d: %s", resp.StatusCode, truncate(string(body), 200)) |
| 184 | + } |
| 185 | + |
| 186 | + if err := json.NewDecoder(resp.Body).Decode(out); err != nil { |
| 187 | + return fmt.Errorf("decode response: %w", err) |
| 188 | + } |
| 189 | + return nil |
| 190 | +} |
| 191 | + |
| 192 | +// parseCursor extracts the cursor parameter from a next-link URL. |
| 193 | +func parseCursor(nextLink string) string { |
| 194 | + if nextLink == "" { |
| 195 | + return "" |
| 196 | + } |
| 197 | + u, err := url.Parse(nextLink) |
| 198 | + if err != nil { |
| 199 | + return "" |
| 200 | + } |
| 201 | + return u.Query().Get("cursor") |
| 202 | +} |
| 203 | + |
| 204 | +func truncate(s string, n int) string { |
| 205 | + if len(s) <= n { |
| 206 | + return s |
| 207 | + } |
| 208 | + return s[:n] + "..." |
| 209 | +} |
0 commit comments