2025-12-08 09:49:07 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-16 10:51:37 +01:00
|
|
|
"bytes"
|
2025-12-08 11:16:35 +01:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
feat: v0.3.0d — add PR checks, iostreams, aliases, and broad enhancements
Add PR checks command, iostreams/text packages for colored table output,
top-level run/workflow aliases matching gh CLI structure. Enhance actions,
issues, PRs, releases, repos, labels, milestones, and wiki commands with
improved flags, JSON output, and error handling.
2026-03-23 11:42:44 -06:00
|
|
|
"time"
|
2025-12-08 11:16:35 +01:00
|
|
|
|
2025-12-08 09:49:07 +01:00
|
|
|
"code.gitea.io/sdk/gitea"
|
feat: v0.3.0a — add api command, pr diff/comment/review, structured errors
New commands:
- fgj api: raw REST API passthrough with field inference and path interpolation
- fgj pr diff: view PR diffs with color, --name-only, --stat
- fgj pr comment: add comments to pull requests
- fgj pr review: approve, request changes, or comment on PRs
Agentic enhancements:
- --json-errors flag for structured JSON error output on stderr
- APIError type wrapping HTTP status codes for machine consumption
- Error codes: auth_required, not_found, api_error, invalid_input, etc.
Docs updated for forgejo.zerova.net/sid/fgj-sid fork.
2026-03-21 21:50:24 -06:00
|
|
|
"forgejo.zerova.net/sid/fgj-sid/internal/config"
|
2025-12-08 09:49:07 +01:00
|
|
|
)
|
|
|
|
|
|
feat: v0.3.0d — add PR checks, iostreams, aliases, and broad enhancements
Add PR checks command, iostreams/text packages for colored table output,
top-level run/workflow aliases matching gh CLI structure. Enhance actions,
issues, PRs, releases, repos, labels, milestones, and wiki commands with
improved flags, JSON output, and error handling.
2026-03-23 11:42:44 -06:00
|
|
|
var sharedHTTPClient = &http.Client{
|
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-08 09:49:07 +01:00
|
|
|
type Client struct {
|
|
|
|
|
*gitea.Client
|
|
|
|
|
hostname string
|
2025-12-08 11:16:35 +01:00
|
|
|
token string
|
2025-12-08 09:49:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewClient(hostname, token string) (*Client, error) {
|
|
|
|
|
if hostname == "" {
|
|
|
|
|
hostname = "codeberg.org"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client, err := gitea.NewClient("https://"+hostname, gitea.SetToken(token))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
|
Client: client,
|
|
|
|
|
hostname: hostname,
|
2025-12-08 11:16:35 +01:00
|
|
|
token: token,
|
2025-12-08 09:49:07 +01:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-05 12:47:28 +01:00
|
|
|
func NewClientFromConfig(cfg *config.Config, hostname string, detectedHost string) (*Client, error) {
|
|
|
|
|
host, err := cfg.GetHost(hostname, detectedHost)
|
2025-12-08 09:49:07 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NewClient(host.Hostname, host.Token)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) Hostname() string {
|
|
|
|
|
return c.hostname
|
|
|
|
|
}
|
2025-12-08 11:16:35 +01:00
|
|
|
|
|
|
|
|
// GetJSON performs a GET request to the specified path and decodes the JSON response
|
|
|
|
|
func (c *Client) GetJSON(path string, result any) error {
|
|
|
|
|
baseURL := "https://" + c.hostname
|
|
|
|
|
url := baseURL + path
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set authentication header
|
|
|
|
|
if c.token != "" {
|
|
|
|
|
req.Header.Set("Authorization", "token "+c.token)
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
|
feat: v0.3.0d — add PR checks, iostreams, aliases, and broad enhancements
Add PR checks command, iostreams/text packages for colored table output,
top-level run/workflow aliases matching gh CLI structure. Enhance actions,
issues, PRs, releases, repos, labels, milestones, and wiki commands with
improved flags, JSON output, and error handling.
2026-03-23 11:42:44 -06:00
|
|
|
resp, err := sharedHTTPClient.Do(req)
|
2025-12-08 11:16:35 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to perform request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
|
|
|
|
|
err = fmt.Errorf("failed to close response body: %w", closeErr)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
feat: v0.3.0d — add PR checks, iostreams, aliases, and broad enhancements
Add PR checks command, iostreams/text packages for colored table output,
top-level run/workflow aliases matching gh CLI structure. Enhance actions,
issues, PRs, releases, repos, labels, milestones, and wiki commands with
improved flags, JSON output, and error handling.
2026-03-23 11:42:44 -06:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
|
|
body, readErr := io.ReadAll(resp.Body)
|
|
|
|
|
if readErr != nil {
|
|
|
|
|
return fmt.Errorf("failed to read error response body: %w", readErr)
|
|
|
|
|
}
|
2026-03-21 21:50:24 -06:00
|
|
|
return &APIError{
|
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
|
Body: string(body),
|
|
|
|
|
Message: fmt.Sprintf("API request failed with status %d: %s", resp.StatusCode, string(body)),
|
|
|
|
|
}
|
2025-12-08 11:16:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to decode response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 10:51:37 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PostJSON performs a POST request to the specified path with JSON body
|
|
|
|
|
func (c *Client) PostJSON(path string, body any, result any) error {
|
2026-01-18 11:50:02 +01:00
|
|
|
_, err := c.DoJSON(http.MethodPost, path, body, result)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DoJSON performs an HTTP request with a JSON body and decodes the JSON response.
|
|
|
|
|
// Returns the HTTP status code and any error encountered.
|
|
|
|
|
func (c *Client) DoJSON(method string, path string, body any, result any) (int, error) {
|
2026-01-16 10:51:37 +01:00
|
|
|
baseURL := "https://" + c.hostname
|
|
|
|
|
url := baseURL + path
|
|
|
|
|
|
|
|
|
|
var bodyReader io.Reader
|
|
|
|
|
if body != nil {
|
|
|
|
|
bodyBytes, err := json.Marshal(body)
|
|
|
|
|
if err != nil {
|
2026-01-18 11:50:02 +01:00
|
|
|
return 0, fmt.Errorf("failed to marshal request body: %w", err)
|
2026-01-16 10:51:37 +01:00
|
|
|
}
|
|
|
|
|
bodyReader = bytes.NewReader(bodyBytes)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 11:50:02 +01:00
|
|
|
req, err := http.NewRequest(method, url, bodyReader)
|
2026-01-16 10:51:37 +01:00
|
|
|
if err != nil {
|
2026-01-18 11:50:02 +01:00
|
|
|
return 0, fmt.Errorf("failed to create request: %w", err)
|
2026-01-16 10:51:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set authentication header
|
|
|
|
|
if c.token != "" {
|
|
|
|
|
req.Header.Set("Authorization", "token "+c.token)
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2026-01-18 11:50:02 +01:00
|
|
|
if body != nil {
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
}
|
2026-01-16 10:51:37 +01:00
|
|
|
|
feat: v0.3.0d — add PR checks, iostreams, aliases, and broad enhancements
Add PR checks command, iostreams/text packages for colored table output,
top-level run/workflow aliases matching gh CLI structure. Enhance actions,
issues, PRs, releases, repos, labels, milestones, and wiki commands with
improved flags, JSON output, and error handling.
2026-03-23 11:42:44 -06:00
|
|
|
resp, err := sharedHTTPClient.Do(req)
|
2026-01-16 10:51:37 +01:00
|
|
|
if err != nil {
|
2026-01-18 11:50:02 +01:00
|
|
|
return 0, fmt.Errorf("failed to perform request: %w", err)
|
2026-01-16 10:51:37 +01:00
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
|
|
|
|
|
err = fmt.Errorf("failed to close response body: %w", closeErr)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
feat: v0.3.0d — add PR checks, iostreams, aliases, and broad enhancements
Add PR checks command, iostreams/text packages for colored table output,
top-level run/workflow aliases matching gh CLI structure. Enhance actions,
issues, PRs, releases, repos, labels, milestones, and wiki commands with
improved flags, JSON output, and error handling.
2026-03-23 11:42:44 -06:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
|
|
bodyBytes, readErr := io.ReadAll(resp.Body)
|
|
|
|
|
if readErr != nil {
|
|
|
|
|
return resp.StatusCode, fmt.Errorf("failed to read error response body: %w", readErr)
|
|
|
|
|
}
|
2026-03-21 21:50:24 -06:00
|
|
|
return resp.StatusCode, &APIError{
|
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
|
Body: string(bodyBytes),
|
|
|
|
|
Message: fmt.Sprintf("API request failed with status %d: %s", resp.StatusCode, string(bodyBytes)),
|
|
|
|
|
}
|
2026-01-16 10:51:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result != nil && resp.StatusCode != http.StatusNoContent {
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
|
2026-01-18 11:50:02 +01:00
|
|
|
return resp.StatusCode, fmt.Errorf("failed to decode response: %w", err)
|
2026-01-16 10:51:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 11:50:02 +01:00
|
|
|
return resp.StatusCode, nil
|
2025-12-08 11:16:35 +01:00
|
|
|
}
|
2025-12-09 13:41:08 +01:00
|
|
|
|
feat: v0.3.0d — add PR checks, iostreams, aliases, and broad enhancements
Add PR checks command, iostreams/text packages for colored table output,
top-level run/workflow aliases matching gh CLI structure. Enhance actions,
issues, PRs, releases, repos, labels, milestones, and wiki commands with
improved flags, JSON output, and error handling.
2026-03-23 11:42:44 -06:00
|
|
|
// Token returns the client's authentication token.
|
|
|
|
|
func (c *Client) Token() string {
|
|
|
|
|
return c.token
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DownloadFile performs an authenticated GET request and writes the response body to the given writer.
|
|
|
|
|
func (c *Client) DownloadFile(url string, w io.Writer) error {
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.token != "" {
|
|
|
|
|
req.Header.Set("Authorization", "token "+c.token)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := sharedHTTPClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to perform request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
|
return fmt.Errorf("download failed with status %d: %s", resp.StatusCode, string(body))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := io.Copy(w, resp.Body); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to write file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-09 13:41:08 +01:00
|
|
|
// GetRawLog performs a GET request and returns the raw response body as string
|
|
|
|
|
func (c *Client) GetRawLog(url string) (string, error) {
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to create request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set authentication header
|
|
|
|
|
if c.token != "" {
|
|
|
|
|
req.Header.Set("Authorization", "token "+c.token)
|
|
|
|
|
}
|
|
|
|
|
|
feat: v0.3.0d — add PR checks, iostreams, aliases, and broad enhancements
Add PR checks command, iostreams/text packages for colored table output,
top-level run/workflow aliases matching gh CLI structure. Enhance actions,
issues, PRs, releases, repos, labels, milestones, and wiki commands with
improved flags, JSON output, and error handling.
2026-03-23 11:42:44 -06:00
|
|
|
resp, err := sharedHTTPClient.Do(req)
|
2025-12-09 13:41:08 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to perform request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
|
|
|
|
|
err = fmt.Errorf("failed to close response body: %w", closeErr)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
feat: v0.3.0d — add PR checks, iostreams, aliases, and broad enhancements
Add PR checks command, iostreams/text packages for colored table output,
top-level run/workflow aliases matching gh CLI structure. Enhance actions,
issues, PRs, releases, repos, labels, milestones, and wiki commands with
improved flags, JSON output, and error handling.
2026-03-23 11:42:44 -06:00
|
|
|
body, readErr := io.ReadAll(resp.Body)
|
|
|
|
|
if readErr != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to read error response body: %w", readErr)
|
|
|
|
|
}
|
2026-03-21 21:50:24 -06:00
|
|
|
return "", &APIError{
|
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
|
Body: string(body),
|
|
|
|
|
Message: fmt.Sprintf("API request failed with status %d: %s", resp.StatusCode, string(body)),
|
|
|
|
|
}
|
2025-12-09 13:41:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bodyBytes, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to read response body: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string(bodyBytes), nil
|
|
|
|
|
}
|