Files
stpweb/backend/services/gitea.go
adamfandClaude Opus 5 64df70e5d2 Document codebase: comments, per-area READMEs, deployment guide
Readability pass across all four areas of the stack. Comments explain why
rather than what, concentrated at the points where behaviour is non-obvious:
concurrency, caching, auth, scraping and nginx templating.

Backend: doc comments on every exported declaration across handlers,
services, models and resolvers. Resolver commentary lives inside function
bodies so gqlgen does not strip it on regeneration; schema files use #
comments rather than description blocks so introspection output is
unchanged. Extracted the duplicated auth-cookie blocks into helpers.

Frontend: header comments on every component, store and view, including
STATUS: notes marking which of the parallel/legacy views are still live.
Documented the non-reactive animation-state pattern, the homeData hub and
the WebSocket stale-socket guard. Removed a few dead imports and named the
reconnect-backoff and poll-interval constants.

Python: module and function docstrings covering the Travelodge scraping
contract, the deadline threading, the two-executor concurrency and the
NDJSON streaming protocol.

Infra: header blocks on every compose file, nginx template, Dockerfile and
entrypoint, covering the config selection, envsubst allow-lists, rate-limit
semantics and volume persistence. Hoisted the duplicated envsubst variable
list in nginx/entrypoint.sh.

New: backend/README.md, python/README.md, DEPLOYMENT.md, .env.example,
backend/graph/doc.go. Refreshed vue/README.md. Corrected CLAUDE.md, which
stated a 24h access token where main.go sets 7 days.

No behaviour change. go build/vet, vite build, py_compile and docker
compose config all pass.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-09-16 15:57:48 +01:00

97 lines
2.8 KiB
Go

package services
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// This file talks to the self-hosted Gitea instance over the private Docker
// network to surface "what did Adam push most recently" on the home page.
// The response is cached by the caller (handlers.Store, 1 minute TTL).
// GiteaFeedCommit is one commit inside a push event's Content payload.
// The capitalised JSON keys are Gitea's, not a typo.
type GiteaFeedCommit struct {
Message string `json:"Message"`
}
// GiteaFeedContent is the shape of the JSON *string* stored in
// GiteaFeedResponse.Content for push events; see ParseCommitMessage.
type GiteaFeedContent struct {
Commits []GiteaFeedCommit `json:"Commits"`
}
// GiteaFeedUser is the acting user on a feed entry.
type GiteaFeedUser struct {
AvatarURL string `json:"avatar_url"`
}
// GiteaFeedRepo is the repository a feed entry belongs to.
type GiteaFeedRepo struct {
FullName string `json:"full_name"`
HTMLURL string `json:"html_url"`
}
// GiteaFeedResponse is a single Gitea activity feed entry. Only the fields
// the front end renders are declared; the rest of the payload is discarded.
type GiteaFeedResponse struct {
ActUser GiteaFeedUser `json:"act_user"`
Repo GiteaFeedRepo `json:"repo"`
OpType string `json:"op_type"`
Content string `json:"content"`
Created time.Time `json:"created"`
}
// FetchLatestFeed returns the single most recent public activity entry for
// user "adamf", or (nil, nil) when the feed is empty — callers must handle
// that nil-without-error case.
//
// The username is hard-coded and the call is plain HTTP with no token: this
// only ever reaches the Gitea container over the internal Docker network,
// where the public feed needs no authentication.
func FetchLatestFeed(host, port string) (*GiteaFeedResponse, error) {
url := fmt.Sprintf("http://%s:%s/api/v1/users/adamf/activities/feeds?limit=1", host, port)
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var items []GiteaFeedResponse
if err := json.Unmarshal(body, &items); err != nil {
return nil, err
}
if len(items) == 0 {
return nil, nil
}
return &items[0], nil
}
// ParseCommitMessage pulls the first commit message out of a feed entry's
// Content field, which Gitea delivers as JSON encoded inside a JSON string
// (so it needs a second Unmarshal). Non-push events have Content in a
// different shape entirely, so any parse failure is intentionally swallowed
// and reported as "no commit message".
func ParseCommitMessage(content string) string {
var c GiteaFeedContent
if err := json.Unmarshal([]byte(content), &c); err != nil {
return ""
}
if len(c.Commits) == 0 {
return ""
}
return c.Commits[0].Message
}