-
Notifications
You must be signed in to change notification settings - Fork 11
206 lines (181 loc) · 7.85 KB
/
Copy pathprettier-autofix.yml
File metadata and controls
206 lines (181 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
name: Prettier Auto Fix
on:
pull_request:
pull_request_target:
workflow_dispatch:
permissions:
contents: write
pull-requests: write
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
autofix:
name: Format and propose changes
if: ${{ github.event_name != 'pull_request_target' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020
with:
node-version: "22"
cache: "npm"
cache-dependency-path: package.json
- name: Install dependencies
run: |
if [ -f package-lock.json ]; then
npm ci
else
npm i --no-audit --no-fund
fi
- name: Run Prettier (write fixes)
if: ${{ github.event.pull_request.user.login == 'dependabot[bot]' }}
run: |
npm run format:md
npm run format:json
npm run format:yaml
npm run format:js
- name: Markdownlint (auto-fix)
if: ${{ github.event.pull_request.user.login == 'dependabot[bot]' }}
run: npx markdownlint --config .markdownlint.json --ignore-path .markdownlintignore --fix -- "**/*.md" "**/*.markdown"
- name: Commit formatting changes to PR branch (Dependabot only)
if: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.event.pull_request.user.login == 'dependabot[bot]' }}
uses: stefanzweifel/git-auto-commit-action@4a55954c782fc1ea30b9056cd3e7a2b40ca8887d
with:
commit_message: "chore(format): apply Prettier/markdownlint fixes"
branch: ${{ github.head_ref }}
file_pattern: |
**/*.md
**/*.markdown
**/*.json
**/*.jsonc
**/*.js
**/*.asmdef
**/*.asmref
**/*.yml
**/*.yaml
- name: Prettier check (Markdown)
run: npm run format:md:check
- name: Prettier check (JSON / asmdef / asmref)
run: npm run format:json:check
- name: Prettier check (YAML)
run: npm run format:yaml:check
- name: Prettier check (JS)
run: npm run format:js:check
- name: Markdown lint (CI gate)
run: npm run lint:markdown
- name: Enforce EOL (CRLF) and No BOM
shell: pwsh
run: ./scripts/check-eol.ps1 -VerboseOutput
autofix_fork:
name: Fork PR bot formatting PR (Dependabot only)
if: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository && github.event.pull_request.user.login == 'dependabot[bot]' }}
runs-on: ubuntu-latest
steps:
- name: Checkout fork PR HEAD
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
persist-credentials: false
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020
with:
node-version: "22"
cache: "npm"
cache-dependency-path: package.json
- name: Install dependencies
run: |
if [ -f package-lock.json ]; then
npm ci
else
npm i --no-audit --no-fund
fi
- name: Run Prettier (write fixes)
run: |
npm run format:md
npm run format:json
npm run format:yaml
npm run format:js
- name: Markdownlint (auto-fix)
run: npx markdownlint --config .markdownlint.json --ignore-path .markdownlintignore --fix -- "**/*.md" "**/*.markdown"
- name: Detect changes
id: changes
shell: bash
run: |
if git diff --quiet; then
echo "has_changes=false" >> "$GITHUB_OUTPUT"
else
echo "has_changes=true" >> "$GITHUB_OUTPUT"
fi
- name: Create bot branch and push to base repo
if: steps.changes.outputs.has_changes == 'true'
shell: bash
env:
GH_REPO: ${{ github.repository }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
BRANCH="bot/prettier/pr-${{ github.event.pull_request.number }}"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git checkout -B "$BRANCH"
# Stage only files we format; avoid workflows to prevent permission issues
git add '**/*.md' '**/*.markdown' '**/*.json' '**/*.jsonc' '**/*.js' '**/*.asmdef' '**/*.asmref' '**/*.yml' '**/*.yaml'
git commit -m "chore(format): apply Prettier/markdownlint fixes for PR #${{ github.event.pull_request.number }}"
git remote add upstream "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git"
git fetch upstream
git push upstream "$BRANCH" --force
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
- name: Open or update formatting PR in base repo
if: steps.changes.outputs.has_changes == 'true'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3
with:
script: |
const prNumber = context.payload.pull_request.number;
const baseRef = context.payload.pull_request.base.ref;
const headBranch = `bot/prettier/pr-${prNumber}`;
const {owner, repo} = context.repo;
const title = `chore(format): Apply Prettier/markdownlint to PR #${prNumber}`;
const body = [
`This automated PR applies Prettier and markdownlint fixes to the changes from PR #${prNumber}.`,
'',
`- Source PR (fork): #${prNumber}`,
`- Target branch: ${baseRef}`,
'',
'If this PR is merged, it will include the contributor\'s changes plus required formatting.',
'You can then close the original PR or ask the author to rebase.',
].join('\n');
const existing = await github.rest.pulls.list({ owner, repo, state: 'open', head: `${owner}:${headBranch}` });
if (existing.data.length === 0) {
await github.rest.pulls.create({ owner, repo, head: headBranch, base: baseRef, title, body });
}
- name: Comment link on original PR
if: steps.changes.outputs.has_changes == 'true'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3
with:
script: |
const prNumber = context.payload.pull_request.number;
const {owner, repo} = context.repo;
const headBranch = `bot/prettier/pr-${prNumber}`;
const resp = await github.rest.pulls.list({ owner, repo, state: 'open', head: `${owner}:${headBranch}` });
if (resp.data.length > 0) {
const fmtPr = resp.data[0];
const body = `A formatting PR has been opened: #${fmtPr.number} (applies Prettier/markdownlint fixes to this PR).`;
const comments = await github.rest.issues.listComments({ owner, repo, issue_number: prNumber, per_page: 50 });
const already = comments.data.some(
(c) =>
c.body &&
c.body.includes(`#${fmtPr.number}`) &&
c.user &&
c.user.login === 'github-actions[bot]'
);
if (!already) {
await github.rest.issues.createComment({ owner, repo, issue_number: prNumber, body });
}
}