generated from mathias/template-go-web
Adds the repos panel from #4's "Not yet shipped" list, following the shipped
issues slice (b2761a4) structurally rather than inventing a second approach:
- internal/atlas/repos.go — RepoNodes(), a pure parser over an already-decoded
/user/repos response. Archived repos are dropped: the atlas shows where work
happens, and an archived repo is by definition somewhere it does not.
- internal/gitea/client.go — MyRepos(), same PAT and same contract as MyIssues:
errors when GITEA_TOKEN is unset so the caller skips the overlay rather than
issuing an unauthenticated request. That unset path is the one running in
prod today (#4), so it is tested, not assumed.
- internal/web/handler.go — wired into liveOverlay().
Stage 05 is the right home — repos are what the execution stage acts on — but
#11 had just claimed its `generate` key for stuck-badges, and a stage could
only name one source. So `generate` now accepts either spelling:
"generate": "ci-jobs"
"generate": ["stuck-badges", "gitea-repos"]
The bare string stays valid and marshals back as a bare string, so no authored
stage is rewritten and the frontend contract is unchanged. Stages 04 and 08
already share stuck-badges between them; this is the same growth one level in.
The overlay merge also moves out of the handler into atlas.PrependGenerated,
which is unit-testable where the inline loop was not — it sat behind a network
call. It covers the issues path too, and fixes a latent aliasing bug there: the
old `append(liveNodes, authored...)` wrote into the live slice's backing array,
so two stages sharing a key would corrupt each other. That shape is no longer
hypothetical now that stuck-badges spans three stages.
Verified end-to-end both ways: without GITEA_TOKEN the atlas returns 200 with
stage 05's authored nodes intact; with a real token stage 05 serves both
overlays at once — stuck badges, then eight live repos, then the authored
nodes — and a repo with no language emits one tag rather than an empty pill.
task check green (golangci-lint 0 issues, vet, -race).
Closes #10.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
83 lines
2.6 KiB
Go
83 lines
2.6 KiB
Go
// Package gitea reads the repo's own Gitea Actions run history from the
|
|
// in-cluster Gitea service (public read — no token), so the atlas can show the
|
|
// pipeline's live executions.
|
|
package gitea
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// base is the in-cluster Gitea service by default; override with GITEA_BASE.
|
|
func base() string {
|
|
if b := os.Getenv("GITEA_BASE"); b != "" {
|
|
return b
|
|
}
|
|
return "http://gitea-http.gitea.svc.cluster.local:3000"
|
|
}
|
|
|
|
// Runs returns the raw /actions/tasks JSON for mathias/cad-atlas (newest first).
|
|
func Runs() ([]byte, error) {
|
|
return get(base()+"/api/v1/repos/mathias/cad-atlas/actions/tasks?limit=50", "")
|
|
}
|
|
|
|
// MyIssues returns the raw /repos/issues/search JSON for the token owner's
|
|
// own open issues across every repo they can see. Requires GITEA_TOKEN — a
|
|
// read-only PAT for the mathias account (this is a single-operator homelab,
|
|
// not per-visitor OAuth: anyone who clears Authentik forward-auth sees
|
|
// Mathias's own data). Returns an error if GITEA_TOKEN is unset, so callers
|
|
// can skip the overlay gracefully.
|
|
func MyIssues() ([]byte, error) {
|
|
token := os.Getenv("GITEA_TOKEN")
|
|
if token == "" {
|
|
return nil, fmt.Errorf("GITEA_TOKEN not set")
|
|
}
|
|
url := base() + "/api/v1/repos/issues/search?state=open&created=true&type=issues&limit=8"
|
|
return get(url, token)
|
|
}
|
|
|
|
// MyRepos returns the raw /user/repos JSON for the token owner's own
|
|
// repositories. Same PAT and same contract as MyIssues: returns an error if
|
|
// GITEA_TOKEN is unset so callers can skip the overlay gracefully rather than
|
|
// issue an unauthenticated request. Gitea's own visibility rules scope the
|
|
// result, so no additional filtering is applied here.
|
|
func MyRepos() ([]byte, error) {
|
|
token := os.Getenv("GITEA_TOKEN")
|
|
if token == "" {
|
|
return nil, fmt.Errorf("GITEA_TOKEN not set")
|
|
}
|
|
url := base() + "/api/v1/user/repos?limit=8"
|
|
return get(url, token)
|
|
}
|
|
|
|
// get performs a short-lived GET, optionally with a bearer token, and returns
|
|
// the response body.
|
|
func get(url, token string) ([]byte, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "token "+token)
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
body, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("gitea: %s", resp.Status)
|
|
}
|
|
return body, nil
|
|
}
|