⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
feat(ci): pr change detection
justin4787746af49f
diff --git a/.github/workflows/browser-tests.yml b/.github/workflows/browser-tests.ymlindex 7c10ee2f1..ed98b2c91 100644--- a/.github/workflows/browser-tests.yml+++ b/.github/workflows/browser-tests.yml@@ -11,7 +11,31 @@ concurrency:cancel-in-progress: truejobs:+ detect-pr-changes:+ permissions:+ contents: read+ pull-requests: read+ runs-on: blacksmith-4vcpu-ubuntu-2404+ timeout-minutes: 5+ outputs:+ skip_expensive: ${{ steps.classify.outputs.skip_expensive }}+ steps:+ - uses: useblacksmith/checkout@25227e61ff9dafe400e22fa487b673eac4e4409a # v1+ with:+ persist-credentials: false+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7+ with:+ node-version: 24.19.0+ package-manager-cache: false+ - id: classify+ name: Classify complete pull request diff+ env:+ GITHUB_TOKEN: ${{ github.token }}+ run: node scripts/ci/pr-change-classification.mjs+smoke:+ needs: detect-pr-changes+ if: ${{ !cancelled() && (needs.detect-pr-changes.result != 'success' || needs.detect-pr-changes.outputs.skip_expensive != 'true') }}name: Chromium smoke testsruns-on: blacksmith-4vcpu-ubuntu-2404timeout-minutes: 30@@ -66,3 +90,22 @@ jobs:- name: Stop disposable test instanceif: always()run: docker compose -f tests/e2e/compose.yml down --volumes++ browser-result:+ if: ${{ always() }}+ needs: [detect-pr-changes, smoke]+ runs-on: blacksmith-4vcpu-ubuntu-2404+ steps:+ - uses: useblacksmith/checkout@25227e61ff9dafe400e22fa487b673eac4e4409a # v1+ with:+ persist-credentials: false+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7+ with:+ node-version: 24.19.0+ package-manager-cache: false+ - name: Reject failed, cancelled, and unexpected skipped checks+ env:+ CI_GATE_WORKFLOW: browser+ CI_SKIP_EXPENSIVE: ${{ needs.detect-pr-changes.outputs.skip_expensive }}+ CI_GATE_JOBS: '{"detect-pr-changes":"${{ needs.detect-pr-changes.result }}","smoke":"${{ needs.smoke.result }}"}'+ run: node scripts/ci/pr-change-classification.mjsdiff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 4d62aafea..128f56707 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -15,6 +15,28 @@ permissions:contents: readjobs:+ detect-pr-changes:+ permissions:+ contents: read+ pull-requests: read+ runs-on: blacksmith-4vcpu-ubuntu-2404+ timeout-minutes: 5+ outputs:+ skip_expensive: ${{ steps.classify.outputs.skip_expensive }}+ steps:+ - uses: useblacksmith/checkout@25227e61ff9dafe400e22fa487b673eac4e4409a # v1+ with:+ persist-credentials: false+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7+ with:+ node-version: 24.19.0+ package-manager-cache: false+ - id: classify+ name: Classify complete pull request diff+ env:+ GITHUB_TOKEN: ${{ github.token }}+ run: node scripts/ci/pr-change-classification.mjs+peekareq-tests:runs-on: blacksmith-4vcpu-ubuntu-2404timeout-minutes: 5@@ -207,6 +229,8 @@ jobs:run: pnpm --filter @kaneo/site seo:checkintegration:+ needs: detect-pr-changes+ if: ${{ !cancelled() && (needs.detect-pr-changes.result != 'success' || needs.detect-pr-changes.outputs.skip_expensive != 'true') }}timeout-minutes: 15runs-on: blacksmith-4vcpu-ubuntu-2404services:@@ -252,6 +276,8 @@ jobs:run: pnpm test:integrationdocker-build:+ needs: detect-pr-changes+ if: ${{ !cancelled() && (needs.detect-pr-changes.result != 'success' || needs.detect-pr-changes.outputs.skip_expensive != 'true') }}runs-on: blacksmith-4vcpu-ubuntu-2404timeout-minutes: 30steps:@@ -358,6 +384,8 @@ jobs:fistorage:+ needs: detect-pr-changes+ if: ${{ !cancelled() && (needs.detect-pr-changes.result != 'success' || needs.detect-pr-changes.outputs.skip_expensive != 'true') }}runs-on: blacksmith-4vcpu-ubuntu-2404timeout-minutes: 10steps:@@ -396,6 +424,8 @@ jobs:run: docker rm -f -v kaneo-storage-cisplit-images:+ needs: detect-pr-changes+ if: ${{ !cancelled() && (needs.detect-pr-changes.result != 'success' || needs.detect-pr-changes.outputs.skip_expensive != 'true') }}runs-on: blacksmith-4vcpu-ubuntu-2404timeout-minutes: 20strategy:@@ -508,3 +538,21 @@ jobs:--data-binary @"$RUNNER_TEMP/discord-payload.json" \"$TRUFFLEHOG_WEBHOOK" \|| echo "::warning::Discord notification could not be delivered."++ ci-result:+ if: ${{ always() }}+ needs: [detect-pr-changes, peekareq-tests, slop-labeler-tests, lint, i18n, openapi, typecheck, unit, build, integration, docker-build, workflows, storage, split-images, secret-scan]+ runs-on: blacksmith-4vcpu-ubuntu-2404+ steps:+ - uses: useblacksmith/checkout@25227e61ff9dafe400e22fa487b673eac4e4409a # v1+ with:+ persist-credentials: false+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7+ with:+ node-version: 24.19.0+ package-manager-cache: false+ - name: Reject failed, cancelled, and unexpected skipped checks+ env:+ CI_SKIP_EXPENSIVE: ${{ needs.detect-pr-changes.outputs.skip_expensive }}+ CI_GATE_JOBS: '{"detect-pr-changes":"${{ needs.detect-pr-changes.result }}","peekareq-tests":"${{ needs.peekareq-tests.result }}","slop-labeler-tests":"${{ needs.slop-labeler-tests.result }}","lint":"${{ needs.lint.result }}","i18n":"${{ needs.i18n.result }}","openapi":"${{ needs.openapi.result }}","typecheck":"${{ needs.typecheck.result }}","unit":"${{ needs.unit.result }}","build":"${{ needs.build.result }}","integration":"${{ needs.integration.result }}","docker-build":"${{ needs.docker-build.result }}","workflows":"${{ needs.workflows.result }}","storage":"${{ needs.storage.result }}","split-images":"${{ needs.split-images.result }}","secret-scan":"${{ needs.secret-scan.result }}"}'+ run: node scripts/ci/pr-change-classification.mjsdiff --git a/scripts/ci/README.md b/scripts/ci/README.mdindex 5b809be5f..70eb27a06 100644--- a/scripts/ci/README.md+++ b/scripts/ci/README.md@@ -25,6 +25,14 @@ the previous image. It runs the MinIO binary directly with temporary test data.example a release commit containing `[skip ci]`), dispatch **CI** on main beforedispatching **Release**. Dry runs don't wait or publish.+PR change detection uses the whole paginated GitHub PR-files list and verifies+its base/head revisions and declared file count before allowing only the+integration, storage, Docker image, and browser suites to skip for clearly+allowlisted documentation-only changes. Any uncertainty runs the full suite.+Both workflows have final result gates; configure these new checks as required+in GitHub branch rules if they should block merges (workflow changes do not+update branch protection).+The separate PR title check uses the base branch's commitlint policy and rerunswhen the title changes. Helm validation also executes the existing secret andupgrade-rendering regression checks. Configure the new job checks as required indiff --git a/scripts/ci/pr-change-classification.mjs b/scripts/ci/pr-change-classification.mjsnew file mode 100644index 000000000..1d9fc3058--- /dev/null+++ b/scripts/ci/pr-change-classification.mjs@@ -0,0 +1,205 @@+import { readFile } from "node:fs/promises";+import { appendFile } from "node:fs/promises";+import { pathToFileURL } from "node:url";++const expensive = ["integration", "storage", "docker-build", "split-images"];+const configName =+ /(^|\/)(package\.json|pnpm-lock\.yaml|package-lock\.json|yarn\.lock|bun\.lockb?|pnpm-workspace\.yaml|turbo\.json|vite\.config\.[^/]+|next\.config\.[^/]+|tsconfig[^/]*\.json|Dockerfile(?:\.[^/]*)?|docker-compose[^/]*\.ya?ml|Makefile|.*\.config\.[^/]+|.*\.ya?ml|.*\.toml|.*\.json|.*\.lock)$/i;++function allowedPath(path) {+ if (+ typeof path !== "string" ||+ !path ||+ path.startsWith("/") ||+ path.split("/").includes("..")+ )+ return false;+ if (path.split("/").some((segment) => segment.startsWith("."))) return false;+ if (configName.test(path)) return false;+ return (+ path === "README.md" || /^(docs|apps\/docs|apps\/site)\/[^/].*$/.test(path)+ );+}++export function classifyChanges({ eventName, files, complete, error = false }) {+ const full = { skipExpensive: false };+ if (+ eventName !== "pull_request" ||+ error ||+ !complete ||+ !Array.isArray(files) ||+ files.length === 0+ )+ return full;+ for (const file of files) {+ if (typeof file === "string") {+ if (!allowedPath(file)) return full;+ continue;+ }+ if (!file || typeof file !== "object" || !allowedPath(file.filename))+ return full;+ if (!new Set(["added", "modified", "removed", "renamed"]).has(file.status))+ return full;+ if (file.status === "renamed" && !allowedPath(file.previous_filename))+ return full;+ if (+ file.previous_filename !== undefined &&+ !allowedPath(file.previous_filename)+ )+ return full;+ if (file.status === "renamed" && typeof file.previous_filename !== "string")+ return full;+ }+ return { skipExpensive: true };+}++export function assessGate({+ workflow = "ci",+ eventName,+ skipExpensive,+ jobs,+}) {+ if (!jobs || typeof jobs !== "object") return "failure";+ const allowedSkipped = new Set(+ skipExpensive && eventName === "pull_request"+ ? [...expensive, "smoke"]+ : [],+ );+ for (const [name, status] of Object.entries(jobs)) {+ if (+ name === "secret-scan" &&+ eventName === "schedule" &&+ status === "skipped"+ )+ continue;+ if (allowedSkipped.has(name) && status === "skipped") continue;+ if (status !== "success") return "failure";+ }+ const required =+ workflow === "browser"+ ? ["detect-pr-changes", "smoke"]+ : [+ "detect-pr-changes",+ "peekareq-tests",+ "slop-labeler-tests",+ "lint",+ "i18n",+ "openapi",+ "typecheck",+ "unit",+ "build",+ "integration",+ "docker-build",+ "workflows",+ "storage",+ "split-images",+ "secret-scan",+ ];+ if (required.some((name) => !(name in jobs))) return "failure";+ return "success";+}++export async function getPrFiles({+ token,+ repository,+ pullNumber,+ expectedBase,+ expectedHead,+}) {+ const headers = {+ authorization: `Bearer ${token}`,+ accept: "application/vnd.github+json",+ "x-github-api-version": "2022-11-28",+ };+ const request = (url) =>+ fetch(url, { headers, signal: AbortSignal.timeout(15_000) });+ const root = `https://api.github.com/repos/${repository}`;+ const pullResponse = await request(`${root}/pulls/${pullNumber}`);+ if (!pullResponse.ok)+ throw new Error(`PR metadata API returned ${pullResponse.status}`);+ const pull = await pullResponse.json();+ if (+ pull.base?.sha !== expectedBase ||+ pull.head?.sha !== expectedHead ||+ !Number.isInteger(pull.changed_files) ||+ pull.changed_files > 3000+ )+ throw new Error("PR revision mismatch or file count exceeds API limit");+ const files = [];+ for (let page = 1; page <= 30; page += 1) {+ const response = await request(+ `${root}/pulls/${pullNumber}/files?per_page=100&page=${page}`,+ );+ if (!response.ok)+ throw new Error(`PR files API returned ${response.status}`);+ const batch = await response.json();+ if (!Array.isArray(batch)) throw new Error("Malformed PR files response");+ files.push(...batch);+ if (batch.length < 100) break;+ if (page === 30) throw new Error("PR file pagination limit reached");+ }+ if (files.length !== pull.changed_files)+ throw new Error("Incomplete PR file list");+ const verify = await request(`${root}/pulls/${pullNumber}`);+ if (!verify.ok) throw new Error("Could not revalidate PR revision");+ const latest = await verify.json();+ if (+ latest.base?.sha !== expectedBase ||+ latest.head?.sha !== expectedHead ||+ latest.changed_files !== pull.changed_files+ )+ throw new Error("PR changed while files were fetched");+ return files;+}++async function main() {+ if (process.env.CI_GATE_JOBS) {+ const jobs = JSON.parse(process.env.CI_GATE_JOBS);+ const result = assessGate({+ workflow: process.env.CI_GATE_WORKFLOW ?? "ci",+ eventName: process.env.GITHUB_EVENT_NAME,+ skipExpensive: process.env.CI_SKIP_EXPENSIVE === "true",+ jobs,+ });+ console.log(`CI result gate: ${result}`);+ if (result !== "success") process.exitCode = 1;+ return;+ }+ const event = JSON.parse(+ await readFile(process.env.GITHUB_EVENT_PATH, "utf8"),+ );+ let files = null;+ let error = false;+ if (process.env.GITHUB_EVENT_NAME === "pull_request") {+ try {+ files = await getPrFiles({+ token: process.env.GITHUB_TOKEN,+ repository: process.env.GITHUB_REPOSITORY,+ pullNumber: event.number,+ expectedBase: event.pull_request.base.sha,+ expectedHead: event.pull_request.head.sha,+ });+ } catch (cause) {+ error = true;+ console.error(`Change detection failed closed: ${cause.message}`);+ }+ }+ const result = classifyChanges({+ eventName: process.env.GITHUB_EVENT_NAME,+ files,+ complete: !error,+ error,+ });+ console.log(`skip_expensive=${result.skipExpensive}`);+ await appendFile(+ process.env.GITHUB_OUTPUT,+ `skip_expensive=${result.skipExpensive}\n`,+ );+}++if (+ process.argv[1] &&+ import.meta.url === pathToFileURL(process.argv[1]).href &&+ process.env.GITHUB_EVENT_PATH+)+ await main();diff --git a/scripts/ci/pr-change-classification.test.mjs b/scripts/ci/pr-change-classification.test.mjsnew file mode 100644index 000000000..51f0d70a8--- /dev/null+++ b/scripts/ci/pr-change-classification.test.mjs@@ -0,0 +1,273 @@+import test from "node:test";+import assert from "node:assert/strict";+import { classifyChanges, assessGate } from "./pr-change-classification.mjs";++test("known GitHub modified-file status allows an allowlisted docs path", () => {+ assert.equal(+ classifyChanges({+ eventName: "pull_request",+ files: [{ filename: "apps/site/page.md", status: "modified" }],+ complete: true,+ }).skipExpensive,+ true,+ );+});++test("missing and unknown GitHub file statuses fail closed", () => {+ for (const file of [+ { filename: "apps/site/page.md" },+ { filename: "apps/site/page.md", status: "copied" },+ ]) {+ assert.equal(+ classifyChanges({+ eventName: "pull_request",+ files: [file],+ complete: true,+ }).skipExpensive,+ false,+ JSON.stringify(file),+ );+ }+});++test("hidden and environment configuration paths require full validation", () => {+ for (const path of [+ "apps/site/.npmrc",+ "apps/site/.nvmrc",+ "apps/site/.node-version",+ "apps/site/.env.production",+ ]) {+ assert.equal(+ classifyChanges({+ eventName: "pull_request",+ files: [path],+ complete: true,+ }).skipExpensive,+ false,+ path,+ );+ }+});++test("only complete docs-only PR changes skip the expensive suites", () => {+ for (const path of [+ "README.md",+ "docs/guide.md",+ "apps/docs/page.mdx",+ "apps/site/index.tsx",+ ]) {+ assert.equal(+ classifyChanges({+ eventName: "pull_request",+ files: [path],+ complete: true,+ }).skipExpensive,+ true,+ );+ }+ assert.equal(+ classifyChanges({+ eventName: "pull_request",+ files: ["docs/a.md", "apps/api/index.ts"],+ complete: true,+ }).skipExpensive,+ false,+ );+ for (const path of [+ "package.json",+ "pnpm-lock.yaml",+ "apps/site/package.json",+ "apps/docs/vite.config.ts",+ "Dockerfile.kaneo",+ ".github/workflows/ci.yml",+ "apps/mobile/page.md",+ ]) {+ assert.equal(+ classifyChanges({+ eventName: "pull_request",+ files: [path],+ complete: true,+ }).skipExpensive,+ false,+ path,+ );+ }+});++test("rename endpoints are both classified and incomplete/unknown detection fails full", () => {+ assert.equal(+ classifyChanges({+ eventName: "pull_request",+ files: [+ {+ filename: "apps/site/new.md",+ previous_filename: "apps/api/old.md",+ status: "renamed",+ },+ ],+ complete: true,+ }).skipExpensive,+ false,+ );+ assert.equal(+ classifyChanges({+ eventName: "pull_request",+ files: [+ {+ filename: "apps/site/new.md",+ previous_filename: "docs/old.md",+ status: "renamed",+ },+ ],+ complete: true,+ }).skipExpensive,+ true,+ );+ assert.equal(+ classifyChanges({+ eventName: "pull_request",+ files: [{ filename: "apps/site/new.md", status: "renamed" }],+ complete: true,+ }).skipExpensive,+ false,+ );+ for (const input of [+ { eventName: "pull_request", files: [], complete: true },+ { eventName: "pull_request", files: ["README.md"], complete: false },+ { eventName: "pull_request", files: ["docs/a.md", null], complete: true },+ {+ eventName: "pull_request",+ files: ["docs/a.md"],+ complete: true,+ error: true,+ },+ ])+ assert.equal(classifyChanges(input).skipExpensive, false);+});++test("non-PR events always run the full checks", () => {+ for (const eventName of [+ "push",+ "workflow_call",+ "schedule",+ "workflow_dispatch",+ "release",+ ]) {+ assert.equal(+ classifyChanges({ eventName, files: ["README.md"], complete: true })+ .skipExpensive,+ false,+ );+ }+});++test("result gate accepts only successful jobs and deliberate expensive skips", () => {+ const ciJobs = {+ "detect-pr-changes": "success",+ "peekareq-tests": "success",+ "slop-labeler-tests": "success",+ lint: "success",+ i18n: "success",+ openapi: "success",+ typecheck: "success",+ unit: "success",+ build: "success",+ integration: "skipped",+ "docker-build": "skipped",+ workflows: "success",+ storage: "skipped",+ "split-images": "skipped",+ "secret-scan": "success",+ };+ assert.equal(+ assessGate({+ workflow: "ci",+ eventName: "pull_request",+ skipExpensive: true,+ jobs: ciJobs,+ }),+ "success",+ );+ assert.equal(+ assessGate({+ workflow: "ci",+ eventName: "pull_request",+ skipExpensive: true,+ jobs: { ...ciJobs, "secret-scan": "skipped" },+ }),+ "failure",+ );+ for (const state of ["failure", "cancelled", "skipped"]) {+ assert.equal(+ assessGate({+ workflow: "ci",+ eventName: "pull_request",+ skipExpensive: true,+ jobs: { ...ciJobs, lint: state },+ }),+ "failure",+ );+ assert.equal(+ assessGate({+ workflow: "ci",+ eventName: "pull_request",+ skipExpensive: false,+ jobs: { ...ciJobs, integration: state },+ }),+ "failure",+ );+ }+ assert.equal(+ assessGate({+ workflow: "ci",+ eventName: "push",+ skipExpensive: false,+ jobs: ciJobs,+ }),+ "failure",+ );+ const scheduled = {+ ...ciJobs,+ integration: "success",+ storage: "success",+ "docker-build": "success",+ "split-images": "success",+ "secret-scan": "skipped",+ };+ assert.equal(+ assessGate({+ workflow: "ci",+ eventName: "schedule",+ skipExpensive: false,+ jobs: scheduled,+ }),+ "success",+ );+ assert.equal(+ assessGate({+ workflow: "ci",+ eventName: "schedule",+ skipExpensive: false,+ jobs: { ...scheduled, integration: "skipped" },+ }),+ "failure",+ );+ assert.equal(+ assessGate({+ workflow: "ci",+ eventName: "pull_request",+ skipExpensive: true,+ jobs: { lint: "success" },+ }),+ "failure",+ );+ assert.equal(+ assessGate({+ workflow: "browser",+ eventName: "pull_request",+ skipExpensive: true,+ jobs: { "detect-pr-changes": "success", smoke: "skipped" },+ }),+ "success",+ );+});