diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..eb772da --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,81 @@ +name: CI + +on: + push: + branches: [master, rewrite-modern] + pull_request: + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + go: + name: Go tests and checks + runs-on: ubuntu-latest + defaults: + run: + working-directory: backend + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-go@v7 + with: + go-version-file: backend/go.mod + check-latest: true + cache-dependency-path: backend/go.mod + - name: Check formatting + run: test -z "$(gofmt -l *.go)" + - name: Test with race detector + run: go test -race -shuffle=on -count=1 ./... + - name: Vet + run: go vet ./... + - name: Check Go vulnerabilities + run: | + go install golang.org/x/vuln/cmd/govulncheck@latest + "$(go env GOPATH)/bin/govulncheck" ./... + + go-lint: + name: Go lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-go@v7 + with: + go-version-file: backend/go.mod + check-latest: true + cache-dependency-path: backend/go.mod + - uses: golangci/golangci-lint-action@v9 + with: + version: v2.15.0 + working-directory: backend + + frontend: + name: Frontend tests and build + runs-on: ubuntu-latest + defaults: + run: + working-directory: frontend + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: + node-version: '22' + cache: npm + cache-dependency-path: frontend/package-lock.json + - run: npm ci + - run: npm test + - run: npm run typecheck + - run: npm run build + - run: npm audit --audit-level=moderate + + docker: + name: Docker images + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - run: docker build -t githubble-backend:ci ./backend + - run: docker build -t githubble-frontend:ci ./frontend diff --git a/README.md b/README.md index bfc4c43..460bcda 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Compose. Never put a GitHub token into `NEXT_PUBLIC_*` variables. ## Local development -Requires Go 1.26+ and Node.js 22.12+. +Requires Go 1.26.9+ and Node.js 22.12+. ```sh cd backend @@ -57,3 +57,7 @@ not include token values or event payloads. cd backend && go test -race ./... && go vet ./... cd frontend && npm test && npm run typecheck && npm run build ``` + +GitHub Actions runs these checks on pull requests and pushes to `master` or +`rewrite-modern`, plus Go lint/vulnerability scanning, npm audit, and both +Docker builds. No GitHub API token is needed for CI; images are not published. diff --git a/backend/.golangci.yml b/backend/.golangci.yml new file mode 100644 index 0000000..ca457a2 --- /dev/null +++ b/backend/.golangci.yml @@ -0,0 +1,4 @@ +version: "2" + +linters: + default: standard diff --git a/backend/Dockerfile b/backend/Dockerfile index e4ea27d..c6ebc32 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,5 +1,5 @@ # ---- build stage ---- -FROM golang:1.26-alpine AS build +FROM golang:1.26.9-alpine AS build WORKDIR /app COPY go.mod ./ RUN go mod download diff --git a/backend/fetcher.go b/backend/fetcher.go index 8d8997f..78b7a64 100644 --- a/backend/fetcher.go +++ b/backend/fetcher.go @@ -112,13 +112,13 @@ func (f *fetcher) poll(ctx context.Context) error { case http.StatusNotModified: f.broker.publish(ssePayload{Event: "ratelimits", Data: []byte(fmt.Sprintf("token-%d/%s/%s", f.idx+1, resp.Header.Get("X-RateLimit-Remaining"), resp.Header.Get("X-RateLimit-Limit")))}) f.publishChecked() - resp.Body.Close() + _ = resp.Body.Close() slog.InfoContext(ctx, "github_feed_unchanged", "poll_interval_seconds", int(f.interval.Seconds()), "rate_remaining", f.remaining, "rate_limit", f.limit) return nil case http.StatusOK: var events []json.RawMessage err = json.NewDecoder(io.LimitReader(resp.Body, 8<<20)).Decode(&events) - resp.Body.Close() + _ = resp.Body.Close() if err != nil { return fmt.Errorf("decode events page %d: %w", page, err) } @@ -134,7 +134,7 @@ func (f *fetcher) poll(ctx context.Context) error { } case http.StatusForbidden, http.StatusTooManyRequests: reset, err := strconv.ParseInt(resp.Header.Get("X-RateLimit-Reset"), 10, 64) - resp.Body.Close() + _ = resp.Body.Close() if err == nil && reset > time.Now().Unix() { f.resetAt[f.idx] = time.Unix(reset, 0).Add(time.Second) } else { @@ -144,13 +144,13 @@ func (f *fetcher) poll(ctx context.Context) error { f.idx = (f.idx + 1) % len(f.tokens) return nil case http.StatusUnauthorized: - resp.Body.Close() + _ = resp.Body.Close() f.resetAt[f.idx] = time.Now().Add(time.Hour) slog.ErrorContext(ctx, "github_token_rejected", "token_index", f.idx+1, "page", page) f.idx = (f.idx + 1) % len(f.tokens) return nil default: - resp.Body.Close() + _ = resp.Body.Close() return fmt.Errorf("unexpected status %s", resp.Status) } } diff --git a/backend/fetcher_test.go b/backend/fetcher_test.go index f03d3ce..ceece9b 100644 --- a/backend/fetcher_test.go +++ b/backend/fetcher_test.go @@ -24,7 +24,7 @@ func TestFetcherPollDeduplicatesAndOrders(t *testing.T) { } w.Header().Set("X-RateLimit-Remaining", "42") w.Header().Set("X-RateLimit-Limit", "5000") - fmt.Fprint(w, feed) + _, _ = fmt.Fprint(w, feed) })) defer srv.Close() b := newBroker() @@ -67,11 +67,11 @@ func TestFetcherFindsStarsBeyondFirstPageAndRespectsPollInterval(t *testing.T) { return } w.Header().Set("ETag", `"feed-v1"`) - fmt.Fprint(w, `[{"id":"3","type":"PushEvent"}]`) + _, _ = fmt.Fprint(w, `[{"id":"3","type":"PushEvent"}]`) case "2": - fmt.Fprint(w, `[{"id":"2","type":"WatchEvent"}]`) + _, _ = fmt.Fprint(w, `[{"id":"2","type":"WatchEvent"}]`) case "3": - fmt.Fprint(w, `[{"id":"1","type":"ForkEvent"}]`) + _, _ = fmt.Fprint(w, `[{"id":"1","type":"ForkEvent"}]`) default: t.Errorf("unexpected page %q", r.URL.Query().Get("page")) } @@ -109,7 +109,7 @@ func TestFetcherRotatesRateLimitedToken(t *testing.T) { w.WriteHeader(http.StatusForbidden) return } - fmt.Fprint(w, `[]`) + _, _ = fmt.Fprint(w, `[]`) })) defer srv.Close() f, _ := newFetcher([]string{"exhausted", "working"}, newBroker()) diff --git a/backend/main.go b/backend/main.go index f5cf116..f673d88 100644 --- a/backend/main.go +++ b/backend/main.go @@ -46,7 +46,7 @@ func run() error { mux := http.NewServeMux() mux.Handle("/sse", b) mux.HandleFunc("/healthz", func(rw http.ResponseWriter, r *http.Request) { - rw.Write([]byte("ok")) + _, _ = rw.Write([]byte("ok")) }) srv := &http.Server{Addr: "0.0.0.0:3003", Handler: mux, ReadHeaderTimeout: 5 * time.Second} diff --git a/backend/sse_test.go b/backend/sse_test.go index ac5f704..02e5008 100644 --- a/backend/sse_test.go +++ b/backend/sse_test.go @@ -20,7 +20,7 @@ func TestSSEBroadcastAndDisconnect(t *testing.T) { if err != nil { t.Fatal(err) } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() if got := resp.Header.Get("Content-Type"); got != "text/event-stream" { t.Fatalf("Content-Type = %q", got) } @@ -61,7 +61,7 @@ func TestSSEReplaysRecentEventsToNewClient(t *testing.T) { if err != nil { t.Fatal(err) } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() scanner := bufio.NewScanner(resp.Body) var events []string for scanner.Scan() {