Skip to content

Commit b200b16

Browse files
committed
initial attempt
1 parent 4f470a2 commit b200b16

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

.github/scripts/common.mjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,34 @@ export async function createComment({
7474
);
7575
}
7676

77+
export async function createCheckButton({
78+
github, // injected by GitHub
79+
context, // injected by GitHub
80+
exec, // injected by GitHub
81+
checkName = 'A button',
82+
btnLabel = 'Fix',
83+
btnDescr = 'Fix',
84+
btnID = 'btn',
85+
}) {
86+
await withRetry(() =>
87+
github.rest.checks.create({
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
head_sha: context.sha,
91+
name: checkName,
92+
// NOTE: try different statuses,
93+
// https://docs.github.com/en/rest/guides/using-the-rest-api-to-interact-with-checks?apiVersion=2022-11-28#about-check-runs
94+
status: 'completed',
95+
conclusion: 'action_required',
96+
actions: [{
97+
label: btnLabel,
98+
description: btnDescr,
99+
identifier: btnID,
100+
}],
101+
})
102+
);
103+
}
104+
77105
/** @param fn {() => Promise<any>} */
78106
async function withRetry(fn, maxRetries = 3, baseDelayMs = 1500) {
79107
let lastError;

.github/workflows/checks.yml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# They only run in direct branches and are not available in forks.
2+
name: ✅ Checks with autofixes
3+
4+
env:
5+
HUSKY: 0
6+
NODE_VERSION: 20
7+
8+
on:
9+
check_run:
10+
types: [requested_action]
11+
# NOTE: created? then close?
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}-checks
15+
cancel-in-progress: true
16+
17+
permissions:
18+
contents: write
19+
checks: write
20+
21+
jobs:
22+
format-fix:
23+
name: "Fix formatting"
24+
runs-on: ubuntu-latest
25+
if: |
26+
github.event.requested_action.identifier == 'btn_fmt' &&
27+
(
28+
github.event.pull_request.head.repo.fork == true ||
29+
github.event.pull_request.head.repo.full_name != github.repository
30+
)
31+
steps:
32+
- name: Checkout PR branch
33+
uses: actions/checkout@v4
34+
with:
35+
ref: ${{ github.event.check_run.head_sha }}
36+
37+
- name: Setup Node.js
38+
uses: actions/setup-node@v4
39+
with:
40+
node-version: ${{ env.NODE_VERSION }}
41+
cache: "npm"
42+
43+
- name: Install dependencies
44+
run: |
45+
corepack enable
46+
npm ci
47+
48+
- name: Get changed MDX and Markdown files
49+
id: changed-files
50+
uses: tj-actions/changed-files@24d32ffd492484c1d75e0c0b894501ddb9d30d62 # v47
51+
with:
52+
files: |
53+
**.md
54+
**.mdx
55+
separator: " "
56+
57+
- name: Apply formatting
58+
id: fix-fmt
59+
env:
60+
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
61+
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
62+
with:
63+
script: |
64+
const files = (process.env.ALL_CHANGED_FILES ?? '')
65+
.trim().split(' ').filter(Boolean).filter((it) => it.match(/\.mdx?$/) !== null);
66+
if (files.length === 0) {
67+
console.log('\nNo such files affected!');
68+
process.exit(0);
69+
}
70+
try {
71+
await exec.exec('npm', ['run', 'check:fmt:some', '--', ...files], {
72+
silent: true, // >/dev/null 2>&1
73+
});
74+
console.log('\nNo issues');
75+
core.setOutput('changes', 'false');
76+
} catch (_) {
77+
console.log('\nFound issues, fixing...');
78+
await exec.exec('npm', ['run', 'fmt:some', '--', ...files], {
79+
silent: true, // >/dev/null 2>&1
80+
});
81+
core.setOutput('changes', 'true');
82+
}
83+
84+
- name: Commit changes, if any
85+
if: steps.fix-fmt.outputs.changes == 'true'
86+
uses: stefanzweifel/git-auto-commit-action@28e16e81777b558cc906c8750092100bbb34c5e3 # v7.0.0
87+
with:
88+
commit_message: "fix: apply automatic changes"

.github/workflows/linter.yml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ concurrency:
1616
permissions:
1717
contents: read
1818
pull-requests: write
19-
# issues: write
19+
checks: write
2020

2121
jobs:
2222
format-check:
@@ -80,6 +80,31 @@ jobs:
8080
process.exit(1);
8181
}
8282
83+
- name: Create a button to apply formatting fixes in a new commit from the bot
84+
if: |
85+
(
86+
steps.check-fmt.conclusion == 'failure' &&
87+
github.event_name == 'pull_request' &&
88+
(
89+
github.event.pull_request.head.repo.fork == true ||
90+
github.event.pull_request.head.repo.full_name != github.repository
91+
)
92+
)
93+
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
94+
with:
95+
script: |
96+
const { createCheckButton } = await import('${{ github.workspace }}/.github/scripts/common.mjs');
97+
await createCheckButton({
98+
github,
99+
context,
100+
exec,
101+
checkName = 'A button to apply formatting fixes',
102+
btnLabel = 'Fix formatting',
103+
btnLabel = 'Applies formatting fixes on the changed files, in a new commit from the bot',
104+
btnID = 'btn_fmt',
105+
});
106+
107+
# TODO: remove
83108
- name: Hide prior PR comments and issue a new one in case of failure
84109
if: |
85110
(

0 commit comments

Comments
 (0)