From 66443e1153ff6f80bd17186c3394ebcda9ec0787 Mon Sep 17 00:00:00 2001 From: poli0981 Date: Tue, 24 Nov 2026 08:26:57 +0700 Subject: [PATCH 1/2] fix(publish): record the build as the status main's protection requires MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first publish from the admin after the move stopped at its pull request: CI's publish job tried to merge twice and Gitea answered 405 "Not all required status checks successful". Gitea 28.4 writes commit statuses for push and pull_request runs only, never for a dispatched run, so the content/bump commit never got the `CI / build (*)` status branch protection requires. The publish job (needs: build, now if: always()) records build's result as `CI / build (workflow_dispatch)` on the commit it built, with a link to the run, and only then merges and dispatches the deploy. A failed build is recorded as a failure and merges nothing, so the admin shows "xuất bản lỗi" instead of building forever; a cancelled one records nothing. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/ci.yml | 21 +++++++++------ scripts/content-publish.mjs | 48 ++++++++++++++++++++++++++++++----- tests/content-publish.test.ts | 45 ++++++++++++++++++++++++++++---- 3 files changed, 94 insertions(+), 20 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 4cc199b..cc77097 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -197,18 +197,21 @@ jobs: exit "$status" # Content publish, second half (content-bump.yml is the first): when the admin's publish - # dispatched this run on content/bump and `build` passed, merge the content/bump PR - # (Markdown/JSON only — an MDX publish waits for a review) and dispatch this workflow on - # main, which deploys. The dispatch is needed because nothing the job token does starts a - # workflow, its merge commit included. If the merge is refused, the job fails and says so - # on the PR; a merge by hand deploys as usual. + # dispatched this run on content/bump, record `build`'s result as the commit status main's + # branch protection requires — Gitea writes none for a dispatched run, so without this the + # merge is refused (405) — and, when it passed, merge the content/bump PR (Markdown/JSON + # only — an MDX publish waits for a review) and dispatch this workflow on main, which + # deploys. The dispatch is needed because nothing the job token does starts a workflow, its + # merge commit included. If the merge is refused, the job fails and says so on the PR; a + # merge by hand deploys as usual. `always()`: a failed build is recorded too, so the admin + # shows "xuất bản lỗi" instead of building forever. publish: needs: build - if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/content/bump' + if: always() && github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/content/bump' runs-on: ubuntu-latest timeout-minutes: 5 permissions: - contents: write # the merge + contents: write # the commit status, the merge pull-requests: write # the merge, a comment when it fails actions: write # dispatch ci.yml on main steps: @@ -216,12 +219,14 @@ jobs: with: persist-credentials: false - - name: Merge the publish PR and deploy main + - name: Record the build, merge the publish PR, deploy main env: TOKEN: ${{ github.token }} API: ${{ github.api_url }} REPO: ${{ github.repository }} SHA: ${{ github.sha }} + BUILD: ${{ needs.build.result }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} run: node scripts/content-publish.mjs merge # Production. Cloudflare Workers Builds pulled from GitHub and stopped with it, so main diff --git a/scripts/content-publish.mjs b/scripts/content-publish.mjs index be56b54..a9c20cf 100644 --- a/scripts/content-publish.mjs +++ b/scripts/content-publish.mjs @@ -5,13 +5,15 @@ // node scripts/content-publish.mjs open content-bump.yml, after pushing content/bump: // open or update the PR, say in its body whether it // merges itself, and dispatch ci.yml on the branch -// node scripts/content-publish.mjs merge ci.yml's `publish` job, after `build` passed on -// content/bump: merge the PR when its body says -// auto, then dispatch ci.yml on main to deploy +// node scripts/content-publish.mjs merge ci.yml's `publish` job, after `build` on +// content/bump: record build's result as the +// commit status branch protection requires; when it +// passed and the PR body says auto, merge the PR, +// then dispatch ci.yml on main to deploy // // Environment: API (github.api_url), REPO (owner/name), TOKEN (the job token); `open` also // NEW (the content commit) and AUTO ("true" when only .md/.json changed); `merge` also SHA -// (the commit CI built). +// (the commit CI built), BUILD (needs.build.result) and RUN_URL (the run, for the status). // // Why the dispatches: Gitea, like GitHub, starts no workflow for what the job token does — // neither for the PR it opens nor for the merge commit it pushes to main. @@ -95,7 +97,39 @@ export async function open({ call, contentCommit, auto, log = console.log }) { return pull.number; } -export async function merge({ call, sha, log = console.log }) { +/** + * The status main's branch protection requires (`CI / build (*)`). Gitea 28.4 writes commit + * statuses for push and pull_request runs only, never for a dispatched one, so the run CI + * makes on content/bump leaves the bump commit without it and every merge is refused (405, + * "Not all required status checks successful" — the first publish on 2026-11-24). The + * publish job runs after `build` in the same run and records its result here instead. + */ +export const BUILD_CONTEXT = "CI / build (workflow_dispatch)"; + +/** + * @param {{ + * call: (method: string, path: string, body?: unknown) => Promise, + * sha: string, + * build?: string, + * runUrl?: string, + * log?: (message: string) => void, + * }} options + */ +export async function merge({ call, sha, build = "success", runUrl, log = console.log }) { + if (build !== "success" && build !== "failure") { + log(`build ${build}: nothing to record or merge.`); + return "skipped"; + } + await call("POST", `/statuses/${sha}`, { + state: build, + context: BUILD_CONTEXT, + description: + build === "success" ? "build passed in this dispatched run" : "build failed — see the run", + ...(runUrl ? { target_url: runUrl } : {}), + }); + log(`Recorded ${BUILD_CONTEXT}: ${build} on ${sha}.`); + if (build === "failure") return "failed"; + const pull = await findPull(call); if (!pull) { log(`No open PR from ${BRANCH}: nothing to merge.`); @@ -139,9 +173,9 @@ async function main(command) { if (!NEW) throw new Error("NEW must be set"); await open({ call, contentCommit: NEW, auto: AUTO === "true" }); } else if (command === "merge") { - const { SHA } = process.env; + const { SHA, BUILD, RUN_URL } = process.env; if (!SHA) throw new Error("SHA must be set"); - await merge({ call, sha: SHA }); + await merge({ call, sha: SHA, build: BUILD || "success", runUrl: RUN_URL || undefined }); } else { throw new Error(`unknown command ${command ?? "(none)"}: use open or merge`); } diff --git a/tests/content-publish.test.ts b/tests/content-publish.test.ts index c130c91..d1e193f 100644 --- a/tests/content-publish.test.ts +++ b/tests/content-publish.test.ts @@ -3,6 +3,7 @@ import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { + BUILD_CONTEXT, gitea, isAuto, MARK_AUTO, @@ -112,24 +113,54 @@ describe("content publish: open", () => { }); describe("content publish: merge", () => { - it("merges the PR CI built when it is marked auto, then dispatches main", async () => { + const RUN = "https://git.poli0981.dev/poli0981/poli0981-dev/actions/runs/7"; + + it("records the build as the required status, merges an auto PR, then dispatches main", async () => { const { call, sent } = fakeGitea([bumpPull(prBody("abc", true))]); - assert.equal(await merge({ call, sha: "head-sha", log: quiet }), "merged"); + assert.equal(await merge({ call, sha: "head-sha", runUrl: RUN, log: quiet }), "merged"); assert.deepEqual( sent.map((s) => `${s.method} ${s.path}`), [ + "POST /statuses/head-sha", "GET /pulls?state=open&limit=50", "POST /pulls/5/merge", "POST /actions/workflows/ci.yml/dispatches", ], ); - assert.deepEqual(sent[1].body, { + assert.deepEqual(sent[0].body, { + state: "success", + context: BUILD_CONTEXT, + description: "build passed in this dispatched run", + target_url: RUN, + }); + // Branch protection requires "CI / build (*)"; the status must match it. + assert.ok(BUILD_CONTEXT.startsWith("CI / build (") && BUILD_CONTEXT.endsWith(")")); + assert.deepEqual(sent[2].body, { Do: "squash", MergeTitleField: "chore(content): publish from admin (#5)", head_commit_id: "head-sha", delete_branch_after_merge: true, }); - assert.deepEqual(sent[2].body, { ref: "main" }); + assert.deepEqual(sent[3].body, { ref: "main" }); + }); + + it("records a failed build and merges nothing; a cancelled one is left alone", async () => { + const failed = fakeGitea([bumpPull(prBody("abc", true))]); + assert.equal( + await merge({ call: failed.call, sha: "head-sha", build: "failure", log: quiet }), + "failed", + ); + assert.deepEqual( + failed.sent.map((s) => `${s.method} ${s.path}`), + ["POST /statuses/head-sha"], + ); + assert.equal((failed.sent[0].body as { state: string }).state, "failure"); + const cancelled = fakeGitea([bumpPull(prBody("abc", true))]); + assert.equal( + await merge({ call: cancelled.call, sha: "head-sha", build: "cancelled", log: quiet }), + "skipped", + ); + assert.deepEqual(cancelled.sent, []); }); it("leaves alone: no PR, a PR that moved on, a PR waiting for review", async () => { @@ -139,7 +170,11 @@ describe("content publish: merge", () => { const review = fakeGitea([bumpPull(prBody("abc", false))]); assert.equal(await merge({ call: review.call, sha: "head-sha", log: quiet }), "review"); for (const fake of [moved, review]) { - assert.ok(!fake.sent.some((s) => s.method !== "GET")); + // The status is still recorded (the owner's merge by hand needs it); nothing else. + assert.deepEqual( + fake.sent.filter((s) => s.method !== "GET").map((s) => s.path), + ["/statuses/head-sha"], + ); } }); -- 2.54.1 From 9bb5350a342fe244f2cab07411c5836ef9debd83 Mon Sep 17 00:00:00 2001 From: poli0981 Date: Tue, 24 Nov 2026 08:26:59 +0700 Subject: [PATCH 2/2] =?UTF-8?q?docs:=202.4.1=20=E2=80=94=20dispatched=20ru?= =?UTF-8?q?ns=20get=20no=20commit=20status=20on=20Gitea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 14 +++++++++++++- poli0981.dev-docs/08_CI_CD.md | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 509d036..73660d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,17 @@ no longer exists; the commits themselves are all on Gitea. ## [Unreleased] +## [2.4.1] — 2026-11-24 + +### Fixed + +- **Publishing from the admin merges again.** The first publish after the move stopped at its + pull request: Gitea 28.4 writes no commit status for a dispatched CI run, so the content PR + never had the `CI / build` check main's branch protection requires and every merge was refused + (405). CI's publish job now records the build's result as that status itself — and a failed + build as a failure, so the admin shows "xuất bản lỗi" instead of building forever — before it + merges and deploys. + ## [2.4.0] — 2026-11-24 The move from GitHub to the owner's Gitea. The GitHub account is gone, and with it the @@ -336,7 +347,8 @@ The public launch is tagged `v1-launch`; the 1.x changes after it were not tagge - **2026-10** — Custom Worker entry in front of every page; Workers Rate Limiting; Web Analytics; topic email aliases; R2 media; the Turnstile gate; the admin at `admin.poli0981.dev`. -[Unreleased]: https://git.poli0981.dev/poli0981/poli0981-dev/compare/v2.4.0...main +[Unreleased]: https://git.poli0981.dev/poli0981/poli0981-dev/compare/v2.4.1...main +[2.4.1]: https://git.poli0981.dev/poli0981/poli0981-dev/compare/v2.4.0...v2.4.1 [2.4.0]: https://git.poli0981.dev/poli0981/poli0981-dev/compare/v2.3.0...v2.4.0 [2.3.0]: https://git.poli0981.dev/poli0981/poli0981-dev/compare/v2.2.0...v2.3.0 [2.2.0]: https://git.poli0981.dev/poli0981/poli0981-dev/compare/v2.1.2...v2.2.0 diff --git a/poli0981.dev-docs/08_CI_CD.md b/poli0981.dev-docs/08_CI_CD.md index c7079a8..c8a620e 100644 --- a/poli0981.dev-docs/08_CI_CD.md +++ b/poli0981.dev-docs/08_CI_CD.md @@ -26,7 +26,7 @@ `npm ci --ignore-scripts` → `check-install-scripts` → `wrangler types` (site + widgets) → prettier → eslint → `astro check` (TS 6) → **`typecheck` bằng TypeScript 7** (in `tsc -v`, kiểm site + widgets) → knip → **`test:coverage`** (`node:test`, ngưỡng 80% dòng, xuất `coverage/lcov.info`) → `npm audit --omit=dev --audit-level=high` → `gen-notices --check` → `check-exif --changed` → build (+ OG, Pagefind, `check-csp-hashes`) → **smoke test** (`npm run smoke`: chạy worker đã build bằng `wrangler dev`, kiểm trang/304/HEAD/`/media`/`/ext-img`/admin 403/`frame-ancestors`, pha ép bật cổng Turnstile kể cả cookie sai định dạng, pha denylist) → **cổng trợ năng** (từ v2.2.0: `npm run preview` chạy nền rồi `npm run test:a11y` — Playwright lái Chrome có sẵn nếu image có; image của Gitea runner không có nên CI cài Chromium của Playwright đúng bản trong lockfile, cache theo phiên bản, `A11Y_CHANNEL=bundled` (trên runner GitHub đến 2026-11-23: dùng Chrome có sẵn, không tải trình duyệt); axe-core quét mọi trang trong sitemap của bản build, một trang mỗi template ở theme sáng và bề rộng điện thoại, và menu/tìm kiếm/lightbox/hộp báo lỗi/bảng Aa/bảng pháp lý khi đang mở; sau đó là test bàn phím, reflow 320/360/390px và `forced-colors`). Bước upload coverage làm artifact (cho Sonar) bỏ ngày 2026-11-24. Check `build` là **bắt buộc** (branch protection `main`, §0), nên PR có vi phạm axe không merge được. - Job **`sonarcloud`** (đến 2026-11-23; đã bỏ khi chuyển sang Gitea — Qodana + Opengrep thay) (sau `build`; chạy cho push `main` và PR cùng repo, bỏ qua Dependabot): tải coverage → `SonarSource/sonarqube-scan-action` (pin SHA) với `sonar-project.properties`; trên PR chờ **quality gate** ("Sonar way" cho new code: coverage ≥ 80%, trùng lặp ≤ 3%, rating A, hotspot đã review). Automatic analysis trên SonarCloud **tắt** (CI analysis thay thế); `SONAR_TOKEN` là secret của repo. `.svelte`/`.astro` không được Sonar phân tích — logic quan trọng nằm trong `.ts` để đo được. - **CodeQL** (default setup, repo public) · **Snyk** (GitHub app) · **`osv-scanner.yml`** (reusable workflow v2.7.0, SARIF, hằng tuần + khi đổi lockfile) — cả ba đến 2026-11-23; nay: `security.yml` (§0). **`lighthouse.yml`** (`treosh/lighthouse-ci-action` pin SHA, preset mobile, chỉ cảnh báo; trên Gitea: Chromium của Playwright qua `CHROME_PATH`, nginx tự cài, report là artifact `lighthouse-reports` vì upload của action chỉ chạy với GitHub). -- **`content-bump.yml`** (từ v2.3.0 admin tự gọi sau **mỗi lần lưu/xoá**; nút **Xuất bản** còn lại để chạy lại; `concurrency` gộp các lần gọi dồn dập, không huỷ run đang chạy): admin dispatch bằng `GITEA_BOT_TOKEN` → trỏ submodule `src/content` về `content/main`, commit lên nhánh `content/bump`, mở/cập nhật PR qua API Gitea (job token), ghi vào thân PR marker `` (diff chỉ `.md/.json`) hoặc `` (API Gitea không có trường auto-merge), rồi dispatch `ci.yml` trên `content/bump` để có check `build` (PR mở bằng job token không kích hoạt workflow nào). Job **`publish`** của `ci.yml` (sau `build`, chỉ khi dispatch trên `content/bump`) squash-merge PR mang marker auto rồi dispatch `ci.yml` trên `main` ⇒ job `deploy`. MDX chờ review; merge tay cũng deploy. Script chung: `scripts/content-publish.mjs`. (Trên GitHub đến 2026-11-23: `gh pr merge --auto --squash`, `gh workflow run ci.yml --ref content/bump`, merge ⇒ Workers Builds deploy.) +- **`content-bump.yml`** (từ v2.3.0 admin tự gọi sau **mỗi lần lưu/xoá**; nút **Xuất bản** còn lại để chạy lại; `concurrency` gộp các lần gọi dồn dập, không huỷ run đang chạy): admin dispatch bằng `GITEA_BOT_TOKEN` → trỏ submodule `src/content` về `content/main`, commit lên nhánh `content/bump`, mở/cập nhật PR qua API Gitea (job token), ghi vào thân PR marker `` (diff chỉ `.md/.json`) hoặc `` (API Gitea không có trường auto-merge), rồi dispatch `ci.yml` trên `content/bump` (PR mở bằng job token không kích hoạt workflow nào). **Gitea 28.4 không ghi commit status cho run `workflow_dispatch`** (chỉ push/pull_request) ⇒ commit bump không có `CI / build (*)` mà branch protection đòi, merge bị từ chối 405 — lần xuất bản đầu tiên 2026-11-24 dính đúng lỗi này. Vì vậy job **`publish`** của `ci.yml` (`needs: build`, `if: always()`, chỉ khi dispatch trên `content/bump`) tự ghi status `CI / build (workflow_dispatch)` = kết quả của `build` (kèm link run), rồi nếu build xanh và PR mang marker auto thì squash-merge và dispatch `ci.yml` trên `main` ⇒ job `deploy`; build đỏ ⇒ status failure ⇒ admin hiện "xuất bản lỗi". MDX chờ review; merge tay cũng deploy. Script chung: `scripts/content-publish.mjs`. (Trên GitHub đến 2026-11-23: `gh pr merge --auto --squash`, `gh workflow run ci.yml --ref content/bump`, merge ⇒ Workers Builds deploy.) - **`qodana.yml`** (từ v2.3.0, PR + push `main` + tay): Qodana for JS (gói Ultimate, token `QODANA_TOKEN`), native mode sau `npm ci` + sinh types, profile `qodana.recommended` quét cả repo, `failThreshold: 0` (`qodana.yaml` ghi các inspection tắt kèm lý do). Từ 2026-11-24 chạy bằng **Qodana CLI 2026.3.1** (pin phiên bản + SHA-256, nâng tay cùng repo ops) thay `JetBrains/qodana-action`; kết quả lên Qodana Cloud và vào artifact `qodana-report` (SARIF + HTML) — SARIF vào Code scanning chỉ có thời GitHub. Bỏ qua PR Renovate (Gitea đưa secret cho PR cùng repo, mà PR Renovate cài chính bản cập nhật đang xét) và run không có token, nên chỉ là check tư vấn. Không phân tích `.astro`/`.svelte`. - **Kiểm license**: `gen-notices --check` (dependency trực tiếp, bảng trong `THIRD-PARTY-NOTICES.md`) + `check-licenses` (toàn bộ lockfile, ngoại lệ theo gói) trong job `build` — chính sách ở `scripts/license-policy.mjs` (`12_LEGAL/THIRD_PARTY_NOTICES.md`). Qodana license audit cần Ultimate Plus nên không dùng. - **`triage.yml`** (đến 2026-11-23; đã bỏ cùng `scripts/triage.mjs` — xem §8) (issues + `pull_request_target`; **không checkout gì** — đọc `scripts/triage.mjs` của nhánh mặc định qua API rồi import từ bộ nhớ, nên không bao giờ chạy code của PR; Sonar S7631 bắt mọi checkout trong `pull_request_target`): đề xuất tính năng ⇒ nhãn `enhancement` + `out-of-scope`, bình luận song ngữ, đóng. Chạy khô bằng `workflow_dispatch`. -- 2.54.1