Skip to content

Commit 0a08a5a

Browse files
authored
ci(guard): block non-docs changes into main unless labeled code-ok (#70)
1 parent 6160a8e commit 0a08a5a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

.github/workflows/guard-code.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Guard
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, reopened, synchronize, labeled]
6+
7+
permissions:
8+
contents: read
9+
pull-requests: read
10+
11+
jobs:
12+
block-code-changes:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Guard non-docs PRs into main
16+
uses: actions/github-script@v7
17+
with:
18+
script: |
19+
const pr = context.payload.pull_request;
20+
const base = pr.base.ref; // target branch
21+
const labels = (pr.labels || []).map(l => l.name);
22+
const labelOK = labels.includes('code-ok') || labels.includes('safe-to-merge');
23+
const {owner, repo} = context.repo;
24+
const files = await github.paginate(github.rest.pulls.listFiles, {owner, repo, pull_number: pr.number, per_page: 100});
25+
const onlyDocs = files.every(f => f.filename === 'README.md' || f.filename.startsWith('docs/'));
26+
// Allow all for non-main targets; enforce only on main
27+
if (base !== 'main') {
28+
core.info(`Guard skipped: base is ${base}, only enforcing on main`);
29+
return;
30+
}
31+
if (onlyDocs) {
32+
core.info('Docs-only changes detected; guard passes');
33+
return;
34+
}
35+
if (labelOK) {
36+
core.info('Non-docs changes allowed by label');
37+
return;
38+
}
39+
core.setFailed('Guard: PR to main modifies non-docs files. Add label "code-ok" to proceed, or limit changes to docs/** or README.md.');

0 commit comments

Comments
 (0)