Some checks failed
ci / cargo test (abacus) (push) Has been cancelled
ci / cargo test (agent-runtime) (push) Has been cancelled
ci / cargo test (context-passport) (push) Has been cancelled
ci / cargo test (game-economy) (push) Has been cancelled
ci / cargo test (ledgerline) (push) Has been cancelled
ci / cargo test (mint) (push) Has been cancelled
ci / cargo test (rago) (push) Has been cancelled
ci / cargo test (relay) (push) Has been cancelled
ci / node suites (push) Has been cancelled
ci / cargo test (toolgate) (push) Has been cancelled
ci / cargo test (turnstile) (push) Has been cancelled
ci / cargo test (vend) (push) Has been cancelled
apps/gitea -> apps/rithub-server; compose services/containers renamed (rithub-server-app/postgres/act-runner) with explicit project name and volume names pinned to the exact existing Docker volumes (no data loss); our own GITEA_* compose/env var names -> RIT_*; new rit-admin CLI wrapper on railyard-box; giteaSession.mjs/giteaOAuth.mjs/giteaInfo.mjs -> ritSession/ritOAuth/ritInfo across 6 apps; GiteaForgeAdapter -> RitForgeAdapter + giteaSyncService/importGiteaRepos/syncGiteaRepos -> rit* in ritgram (tsc clean); apps/console/lib/gitea.ts -> lib/ritProvision.ts; Go internal/giteaclient -> internal/ritclient in rithub-async-import (go build/test clean); new /auth/rit + /forges/rit/connect routes with /gitea back-compat aliases; locale/swagger/footer/captcha template text scrubbed of remaining "Gitea" strings. Deployed to railyard-box: old containers stopped/removed, /srv/railyard-gitea renamed to /srv/rithub-server with a symlink kept, docker compose up -d from the new path, act_runner network-alias fix for its persisted instance URL. Live-verified: docker compose config diff showed zero unintended value changes, DB row counts unchanged (19 users, 635 repos), full route regression 200/expected, 0 "gitea" in headers/bodies on every checked page, real git clone+push over HTTPS and SSH with a disposable account (deleted after). Deferred on purpose (documented in plans/railyard.md): the forge discriminator / DB column names / Postgres role+volume names (data migrations, not a naming change) and the upstream GITEA__ env-to-ini keys and X-Gitea-* wire headers (hardcoded in the vendored engine binary). See plans/railyard.md RIT-OPS/RIT-BRD entry for the full old->new map and live evidence. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
154 lines
5.2 KiB
Go
154 lines
5.2 KiB
Go
// Package ritclient is a minimal client for the handful of upstream
|
|
// Rithub-API-compatible endpoints the async-import service needs:
|
|
// who-am-I, create-an-empty-repo, and update-a-repo's-description. It is
|
|
// intentionally not a general-purpose client — callers that need more
|
|
// should go through apps/rithub-mcp instead.
|
|
package ritclient
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Client talks to one upstream Rithub API base URL. BaseURL should
|
|
// point at the engine directly (its loopback port on railyard-box, e.g.
|
|
// http://127.0.0.1:3350), never back out through Cloudflare's edge — the
|
|
// whole point of this service is to keep that hop off the request path
|
|
// for big migrations.
|
|
type Client struct {
|
|
BaseURL string
|
|
HTTPClient *http.Client
|
|
}
|
|
|
|
// New builds a Client. httpClient may be nil, in which case a client with
|
|
// a sane default timeout is used.
|
|
func New(baseURL string, httpClient *http.Client) *Client {
|
|
if httpClient == nil {
|
|
httpClient = &http.Client{Timeout: 30 * time.Second}
|
|
}
|
|
return &Client{BaseURL: strings.TrimRight(baseURL, "/"), HTTPClient: httpClient}
|
|
}
|
|
|
|
// APIError is returned when the upstream API answers with a non-2xx
|
|
// status; Status/Body let callers relay the exact upstream response
|
|
// instead of inventing their own error text.
|
|
type APIError struct {
|
|
Status int
|
|
Body []byte
|
|
}
|
|
|
|
func (e *APIError) Error() string {
|
|
return fmt.Sprintf("rithub API answered %d: %s", e.Status, string(e.Body))
|
|
}
|
|
|
|
// User is the subset of Rithub's user object this service cares about.
|
|
type User struct {
|
|
Login string `json:"login"`
|
|
}
|
|
|
|
// Repo is the subset of Rithub's repository object this service cares
|
|
// about.
|
|
type Repo struct {
|
|
FullName string `json:"full_name"`
|
|
HTMLURL string `json:"html_url"`
|
|
SSHURL string `json:"ssh_url"`
|
|
CloneURL string `json:"clone_url"`
|
|
Private bool `json:"private"`
|
|
}
|
|
|
|
// CreateRepoOptions mirrors Rithub's CreateRepoOption for the fields this
|
|
// service sets. AutoInit is always false by callers of CreateEmptyRepo so
|
|
// the repo is truly empty before the mirror push lands.
|
|
type CreateRepoOptions struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description,omitempty"`
|
|
Private bool `json:"private"`
|
|
AutoInit bool `json:"auto_init"`
|
|
}
|
|
|
|
func (c *Client) do(ctx context.Context, method, path, token string, body any, out any) error {
|
|
var reader io.Reader
|
|
if body != nil {
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return fmt.Errorf("ritclient: marshaling request body: %w", err)
|
|
}
|
|
reader = strings.NewReader(string(data))
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, method, c.BaseURL+"/api/v1"+path, reader)
|
|
if err != nil {
|
|
return fmt.Errorf("ritclient: building request: %w", err)
|
|
}
|
|
req.Header.Set("User-Agent", "rithub-async-import/0.1")
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "token "+token)
|
|
}
|
|
resp, err := c.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("ritclient: %s %s: %w", method, path, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return fmt.Errorf("ritclient: reading response body: %w", err)
|
|
}
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return &APIError{Status: resp.StatusCode, Body: respBody}
|
|
}
|
|
if out != nil && len(respBody) > 0 {
|
|
if err := json.Unmarshal(respBody, out); err != nil {
|
|
return fmt.Errorf("ritclient: decoding response: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AuthenticatedUser returns the user that owns token, failing with an
|
|
// *APIError the caller can relay verbatim if the token is invalid. This
|
|
// doubles as the async path's up-front auth check, so a bad token fails
|
|
// in the initial request instead of surfacing only inside a background
|
|
// job the caller has already stopped watching.
|
|
func (c *Client) AuthenticatedUser(ctx context.Context, token string) (*User, error) {
|
|
var u User
|
|
if err := c.do(ctx, http.MethodGet, "/user", token, nil, &u); err != nil {
|
|
return nil, err
|
|
}
|
|
return &u, nil
|
|
}
|
|
|
|
// CreateEmptyRepo creates a new, empty repo under owner. owner must
|
|
// either be the token's own login (user repo) or an org the token's
|
|
// owner can create repos in (org repo) — this mirrors the one real
|
|
// restriction of this async path, documented in the package README.
|
|
func (c *Client) CreateEmptyRepo(ctx context.Context, token, owner string, opts CreateRepoOptions, isOrg bool) (*Repo, error) {
|
|
path := "/user/repos"
|
|
if isOrg {
|
|
path = "/orgs/" + url.PathEscape(owner) + "/repos"
|
|
}
|
|
var repo Repo
|
|
if err := c.do(ctx, http.MethodPost, path, token, opts, &repo); err != nil {
|
|
return nil, fmt.Errorf("ritclient: creating empty repo %s/%s: %w", owner, opts.Name, err)
|
|
}
|
|
return &repo, nil
|
|
}
|
|
|
|
// UpdateRepoDescription PATCHes a repo's description — used to clear the
|
|
// "migrating" marker once a mirror push finishes.
|
|
func (c *Client) UpdateRepoDescription(ctx context.Context, token, owner, name, description string) error {
|
|
path := "/repos/" + url.PathEscape(owner) + "/" + url.PathEscape(name)
|
|
body := map[string]string{"description": description}
|
|
if err := c.do(ctx, http.MethodPatch, path, token, body, nil); err != nil {
|
|
return fmt.Errorf("ritclient: updating description for %s/%s: %w", owner, name, err)
|
|
}
|
|
return nil
|
|
}
|