Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Simplify change approval workflow

Manav Rathifd6439e08646
diff --git a/.github/checks/change-approval/check.mjs b/.github/checks/change-approval/check.mjs
index c4d217e7e8..4da2ed7fbb 100644
--- a/.github/checks/change-approval/check.mjs
+++ b/.github/checks/change-approval/check.mjs
@@ -166,9 +166,10 @@ const dependencies = present
const added = dependencies.reduce((n, { added }) => n + added.length, 0);
const moved = dependencies.reduce((n, { moved }) => n + moved.length, 0);
-const guardrails = kept.filter(
- (file) => guardrailDirs.some((dir) => file.startsWith(dir)) || guardrailFiles.has(path.basename(file)),
-);
+const guardrails = [
+ ...kept.filter((file) => guardrailDirs.some((dir) => file.startsWith(dir)) || guardrailFiles.has(path.basename(file))),
+ ...[...numstat("--diff-filter=A"), ...additions].map(([, , file]) => file).filter((file) => file.startsWith(".github/")),
+];
const configs = [...numstat(), ...additions].map(([, , file]) => file).filter((file) => configFile.test(file));
const categories = [
@@ -199,4 +200,5 @@ if (local) {
}
appendFileSync(GITHUB_OUTPUT, `categories=${JSON.stringify(categories.map(([, many]) => many))}\n`);
-if (detail) appendFileSync(GITHUB_STEP_SUMMARY, `${detail}\n`);
+if (summary) console.log(`::warning title=Change approval needed::${summary}`);
+appendFileSync(GITHUB_STEP_SUMMARY, summary ? `${summary}\n\n${detail}\n` : "No approval needed.\n");
diff --git a/.github/checks/change-approval/test.mjs b/.github/checks/change-approval/test.mjs
index 99211a5670..ad79f15a93 100644
--- a/.github/checks/change-approval/test.mjs
+++ b/.github/checks/change-approval/test.mjs
@@ -114,7 +114,7 @@ test("CI preserves tabs and newlines in binary and large filenames", (t) => {
const size = 1024 * 1024 + 1;
const { output, summary } = scan(t, {}, { [binary]: Buffer.alloc(16), [large]: "a".repeat(size) }, { ci: true });
assert.equal(output, 'categories=["binary files","large files"]\n');
- assert.equal(summary, `## Binary files\n\n- \`${binary}\` (16 bytes)\n\n## Large files\n\n- \`${large}\` (${size} bytes)\n`);
+ assert.equal(summary, `1 binary file, 1 large file\n\n## Binary files\n\n- \`${binary}\` (16 bytes)\n\n## Large files\n\n- \`${large}\` (${size} bytes)\n`);
});
test("routine image, font, and xcassets binaries are ignored", (t) => {
@@ -331,7 +331,7 @@ test("go.sum new module", (t) => {
assert.match(output, /- b\.org\/y\/v2 v2\.0\.1\n/);
});
-test("guardrail paths modified or deleted, not added", (t) => {
+test("existing guardrails modified or deleted", (t) => {
const output = scan(
t,
{
@@ -349,7 +349,7 @@ test("guardrail paths modified or deleted, not added", (t) => {
"rust/checks/x/check.py": "\n",
"web/apps/x/eslint.config.mjs": null,
"web/checks/x/check.mjs": "\n",
- ".github/CODEOWNERS": "",
+ "web/checks/new.mjs": "",
},
);
assert.equal(
@@ -358,6 +358,19 @@ test("guardrail paths modified or deleted, not added", (t) => {
);
});
+test("new GitHub workflows, actions and policies need approval", (t) => {
+ const files = {
+ ".github/workflows/new.yml": "on: push\n",
+ ".github/actions/new/action.yml": "name: new\n",
+ ".github/checks/new/check.mjs": "export {};\n",
+ };
+ const { output, summary } = scan(t, {}, files, { ci: true });
+ assert.equal(output, 'categories=["guardrail files"]\n');
+ assert.match(summary, /3 guardrail files/);
+ for (const file of Object.keys(files)) assert.ok(summary.includes(`\`${file}\``));
+ assert.match(scan(t, {}, files, { commit: false }), /^3 guardrail files\n/);
+});
+
test("toolchain and registry config added, modified, or deleted", (t) => {
const output = scan(
t,
@@ -390,7 +403,7 @@ test("Git attributes require config approval even when they hide binary changes"
{ ci: true },
);
assert.equal(output, 'categories=["config files"]\n');
- assert.equal(summary, "## Toolchain and registry config\n\n- `.gitattributes`\n- `added/.gitattributes`\n- `deleted/.gitattributes`\n- `modified/.gitattributes`\n");
+ assert.equal(summary, "4 config files\n\n## Toolchain and registry config\n\n- `.gitattributes`\n- `added/.gitattributes`\n- `deleted/.gitattributes`\n- `modified/.gitattributes`\n");
});
test("uncommitted and untracked changes are scanned locally", (t) => {
@@ -408,9 +421,9 @@ test("uncommitted and untracked changes are scanned locally", (t) => {
test("CI mode writes categories and the step summary, and tolerates gitlinks", (t) => {
const { stdout, output, summary } = scan(t, { "a.txt": "a\n" }, { "a.bin": Buffer.alloc(16) }, { gitlink: "sub", ci: true });
- assert.equal(stdout, "");
+ assert.equal(stdout, "::warning title=Change approval needed::1 binary file\n");
assert.equal(output, 'categories=["binary files"]\n');
- assert.equal(summary, "## Binary files\n\n- `a.bin` (16 bytes)\n");
+ assert.equal(summary, "1 binary file\n\n## Binary files\n\n- `a.bin` (16 bytes)\n");
});
test("untracked dangling symlink is skipped locally", (t) => {
@@ -422,7 +435,7 @@ test("CI mode with nothing flagged", (t) => {
const { stdout, output, summary } = scan(t, { "a.txt": "a\n" }, { "a.txt": "b\n" }, { ci: true });
assert.equal(stdout, "");
assert.equal(output, "categories=[]\n");
- assert.equal(summary, "");
+ assert.equal(summary, "No approval needed.\n");
});
test("ordinary change is silent", (t) => {
diff --git a/.github/workflows/change-approval.yml b/.github/workflows/change-approval.yml
index 82af1d165a..66456466a2 100644
--- a/.github/workflows/change-approval.yml
+++ b/.github/workflows/change-approval.yml
@@ -11,57 +11,33 @@ concurrency:
cancel-in-progress: true
jobs:
- test:
- runs-on: ubuntu-latest
- steps:
- - name: Checkout PR
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
-
- - run: node --test .github/checks/change-approval/test.mjs
-
detect:
+ name: Scan changes
runs-on: ubuntu-latest
outputs:
- categories: ${{ steps.scan.outputs.categories }}
+ environment: ${{ steps.scan.outputs.categories != '[]' && 'change-approval' || 'no-approval' }}
steps:
- name: Checkout PR
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
fetch-depth: 2
-
- - name: Checkout trusted scanner
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
- with:
- ref: ${{ github.event.pull_request.base.sha }}
- path: trusted
- sparse-checkout: .github/checks/change-approval/check.mjs
- sparse-checkout-cone-mode: false
+ persist-credentials: false
- name: Scan changes
id: scan
- run: node trusted/.github/checks/change-approval/check.mjs ${{ github.event.pull_request.base.sha }}
-
- approve:
- needs: detect
- if: needs.detect.outputs.categories != '[]'
- name: Approve ${{ matrix.category }}
- runs-on: ubuntu-latest
- strategy:
- matrix:
- category: ${{ fromJSON(needs.detect.outputs.categories) }}
- environment:
- # Require admin approval for flagged changes.
- name: change-approval
- steps:
- - run: "true"
+ run: |
+ git fetch --no-tags --depth=1 origin +refs/heads/main:refs/remotes/origin/main
+ git show origin/main:.github/checks/change-approval/check.mjs > "$RUNNER_TEMP/change-approval.mjs"
+ node "$RUNNER_TEMP/change-approval.mjs" HEAD^1
gate:
- needs: [test, detect, approve]
- if: always()
+ name: Change approval
+ needs: detect
+ if: ${{ !cancelled() }}
runs-on: ubuntu-latest
+ permissions: {}
+ environment: ${{ needs.detect.result == 'success' && needs.detect.outputs.environment || 'no-approval' }}
steps:
- - run: test "$test" = success && test "$detect" = success && { test "$approve" = success || test "$approve" = skipped; }
+ - run: test "$SCAN_RESULT" = success
env:
- test: ${{ needs.test.result }}
- detect: ${{ needs.detect.result }}
- approve: ${{ needs.approve.result }}
+ SCAN_RESULT: ${{ needs.detect.result }}
diff --git a/.github/workflows/workflow-security-checks.yml b/.github/workflows/workflow-security-checks.yml
index ae6767b537..5d187c2e20 100644
--- a/.github/workflows/workflow-security-checks.yml
+++ b/.github/workflows/workflow-security-checks.yml
@@ -5,6 +5,7 @@ on:
paths:
- '.github/workflows/**'
- '.github/actions/**'
+ - '.github/checks/change-approval/**'
- '.github/checks/workflow-security/**'
permissions:
@@ -13,9 +14,6 @@ permissions:
jobs:
workflow-security-checks:
runs-on: ubuntu-latest
- environment:
- # Require admin approval for workflow changes.
- name: workflow-change-approval
steps:
- name: Checkout PR
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
@@ -23,10 +21,12 @@ jobs:
- name: Checkout trusted checker
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
- ref: ${{ github.event.pull_request.base.sha }}
+ ref: ${{ github.base_ref }}
path: trusted
sparse-checkout: .github/checks/workflow-security/check.rb
sparse-checkout-cone-mode: false
- name: Run workflow security checks
run: ruby trusted/.github/checks/workflow-security/check.rb
+
+ - run: node --test .github/checks/change-approval/test.mjs