2026-03-13 13:38:01 +00:00
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
2026-02-21 15:14:29 +00:00
## Overview
2026-03-13 13:38:01 +00:00
Full-coverage Go client for the Forgejo API (~450 endpoints). Uses a generic `Resource[T,C,U]` pattern for CRUD operations and types generated from `swagger.v1.json` .
2026-02-21 15:14:29 +00:00
2026-03-22 01:51:29 +00:00
**Module:** `dappco.re/go/core/forge` (Go 1.26, depends on `go-io` and `go-log` )
2026-02-21 15:14:29 +00:00
## Build & Test
```bash
go test ./... # Run all tests
go test -v -run TestName ./... # Run single test
go generate ./types/... # Regenerate types from swagger spec
go run ./cmd/forgegen/ -spec testdata/swagger.v1.json -out types/ # Manual codegen
```
2026-03-13 13:38:01 +00:00
No linter or formatter is configured beyond standard `go vet` .
2026-02-21 15:14:29 +00:00
## Architecture
2026-03-13 13:38:01 +00:00
The library is a flat package (`package forge` ) with a layered design:
1. ** `client.go` ** — Low-level HTTP client (`Client` ). All requests go through `doJSON()` . Handles auth (token header), JSON marshalling, rate-limit tracking, and error parsing into `*APIError` . Also provides `GetRaw` /`PostRaw` for non-JSON responses.
2. ** `resource.go` ** — Generic `Resource[T, C, U]` struct (T=response type, C=create options, U=update options). Provides List, ListAll, Iter, Get, Create, Update, Delete. Covers ~91% of endpoints.
3. ** `pagination.go` ** — Generic `ListPage[T]` , `ListAll[T]` , `ListIter[T]` functions. Uses `X-Total-Count` header for pagination. `ListIter` returns `iter.Seq2[T, error]` for Go 1.26 range-over-func.
4. ** `params.go` ** — `Params` map type and `ResolvePath()` for substituting `{placeholders}` in API paths.
5. ** `config.go` ** — Config resolution: flags > env (`FORGE_URL` , `FORGE_TOKEN` ) > defaults (`http://localhost:3000` ).
2026-04-02 06:08:46 +00:00
6. ** `forge.go` ** — Top-level `Forge` struct aggregating all 20 service fields. Created via `NewForge(url, token)` or `NewForgeFromConfig(flagURL, flagToken)` .
2026-02-21 15:14:29 +00:00
2026-04-02 06:08:46 +00:00
7. **Service files** (`repos.go` , `issues.go` , etc.) — Each service struct embeds `Resource[T,C,U]` for standard CRUD, then adds hand-written action methods (e.g. `Fork` , `Pin` , `MirrorSync` ). 20 services total: repos, issues, pulls, orgs, users, teams, admin, branches, releases, labels, webhooks, notifications, packages, actions, contents, wiki, commits, milestones, misc, activitypub.
2026-02-21 15:14:29 +00:00
2026-03-13 13:38:01 +00:00
8. ** `types/` ** — Generated Go types from `swagger.v1.json` (229 types). The `//go:generate` directive lives in `types/generate.go` . **Do not hand-edit generated type files** — modify `cmd/forgegen/` instead.
2026-02-21 15:14:29 +00:00
2026-03-13 13:38:01 +00:00
9. ** `cmd/forgegen/` ** — Code generator: parses swagger spec (`parser.go` ), extracts types and CRUD pairs, generates Go files (`generator.go` ).
2026-02-21 15:14:29 +00:00
2026-03-13 13:38:01 +00:00
## Test Patterns
2026-02-21 15:14:29 +00:00
2026-03-13 13:38:01 +00:00
- Tests use `httptest.NewServer` with inline handlers — no mocks or external services
- Test naming uses `_Good` , `_Bad` , `_Ugly` suffixes (happy path, expected errors, edge cases/panics)
- Service-level tests live in `*_test.go` files alongside each service
2026-02-21 15:14:29 +00:00
## Coding Standards
- All methods accept `context.Context` as first parameter
2026-03-13 13:38:01 +00:00
- Errors wrapped as `*APIError` with StatusCode, Message, URL; use `IsNotFound()` , `IsForbidden()` , `IsConflict()` helpers
2026-03-22 01:51:29 +00:00
- Internal errors use `coreerr.E()` from `go-log` (imported as `coreerr "dappco.re/go/core/log"` ), never `fmt.Errorf` or `errors.New`
- File I/O uses `go-io` (imported as `coreio "dappco.re/go/core/io"` ), never `os.ReadFile` /`os.WriteFile` /`os.MkdirAll`
2026-02-21 15:14:29 +00:00
- UK English in comments (organisation, colour, etc.)
- `Co-Authored-By: Virgil <virgil@lethean.io>` in commits
2026-03-13 13:38:01 +00:00
- `Client` uses functional options pattern (`WithHTTPClient` , `WithUserAgent` )
2026-02-21 15:14:29 +00:00
2026-03-13 13:38:01 +00:00
## Adding a New Service
2026-02-21 15:14:29 +00:00
2026-03-13 13:38:01 +00:00
1. Create `newservice.go` with a struct embedding `Resource[T, C, U]`
2. Add action methods that call `s.client.Get/Post/Patch/Put/Delete` directly
3. Wire it into `Forge` struct in `forge.go` and `NewForge()`
4. Add tests in `newservice_test.go`
2026-02-21 15:14:29 +00:00
## Forge Remote
```bash
git remote add forge ssh://git@forge .lthn.ai:2223/core/go-forge.git
```