-
Notifications
You must be signed in to change notification settings - Fork 23.2k
290 lines (248 loc) · 11.8 KB
/
pr-check-lint_content.yml
File metadata and controls
290 lines (248 loc) · 11.8 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
name: Lint and review content files
on:
pull_request:
paths:
- .github/workflows/pr-check-lint_content.yml
pull_request_target:
paths:
- .nvmrc
- "*.md"
- "files/**/*.md"
permissions:
# Compare commits and add reviewdog comments.
pull-requests: write
concurrency:
group: ci-${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
lint-and-review-docs:
runs-on: ubuntu-latest
steps:
- name: Check if PR reviews can be posted
id: permissions
env:
EVENT_NAME: ${{ github.event_name }}
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
BASE_REPO: ${{ github.repository }}
run: |
if [[ "${EVENT_NAME}" == "pull_request_target" || "${HEAD_REPO}" == "${BASE_REPO}" ]]; then
echo "CAN_POST_REVIEWS=true" >> "$GITHUB_OUTPUT"
fi
- name: Checkout BASE
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
# explicitly specify base ref to ensure `pull_request` runs
# behave the same as `pull_request_target` runs
ref: ${{ github.event.pull_request.base.sha }}
persist-credentials: false
- name: Get changed files
id: check
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
# Use the GitHub API to get the list of changed files
# documentation: https://docs.github.com/rest/commits/commits#compare-two-commits
# Get files as newline-separated list
FILTERED_FILES=$(gh api repos/{owner}/{repo}/compare/${BASE_SHA}...${HEAD_SHA} \
--jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename' | \
egrep -i "^.*\.md$" || true)
# Store as multiline output
EOF="$(openssl rand -hex 8)"
echo "DIFF_DOCUMENTS<<${EOF}" >> "$GITHUB_OUTPUT"
echo "${FILTERED_FILES}" >> "$GITHUB_OUTPUT"
echo "${EOF}" >> "$GITHUB_OUTPUT"
# Also set a simple flag for whether we have files
if [ -n "${FILTERED_FILES// /}" ]; then # Remove all spaces and check if anything remains
echo "HAS_FILES=true" >> "$GITHUB_OUTPUT"
else
echo "HAS_FILES=false" >> "$GITHUB_OUTPUT"
fi
- name: Checkout HEAD
if: steps.check.outputs.HAS_FILES == 'true'
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ github.event.pull_request.head.sha }}
path: pr_head
persist-credentials: false
- name: Get changed content from HEAD
if: steps.check.outputs.HAS_FILES == 'true'
run: |
git config --global user.email "108879845+mdn-bot@users.noreply.github.com"
git config --global user.name "mdn-bot"
# Remove non-Markdown files from BASE.
find files -type f ! -name '*.md' -delete
git commit --quiet -m "Remove non-Markdown files from PR base" files
# Remove files from BASE.
rm -r files *.md
# Remove non-Markdown files from HEAD.
find pr_head -type f ! -name '*.md' -delete
# Move Markdown files from HEAD into BASE.
mv pr_head/files pr_head/*.md .
# Remove HEAD checkout.
rm -r pr_head
# To avoid contents of PR getting into the diff that we are going to generate
# after running the linters, here we make a dummy commit.
# Note, this commit is not getting pushed.
git add .
git commit -m "Use Markdown files from PR head" .
- name: Setup Node.js environment
if: steps.check.outputs.HAS_FILES == 'true'
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version-file: ".nvmrc"
cache: npm
- name: Install
if: steps.check.outputs.HAS_FILES == 'true'
run: npm ci
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Check CRLF line endings
id: crlf
if: steps.check.outputs.HAS_FILES == 'true'
env:
DIFF_DOCUMENTS: ${{ steps.check.outputs.DIFF_DOCUMENTS }}
run: |
EOF="$(openssl rand -hex 8)"
readarray -t files_to_lint <<< "$DIFF_DOCUMENTS"
printf "Files to process (%d files):\n" "${#files_to_lint[@]}"
printf "'%s'\n" "${files_to_lint[@]}"
CRLF_FAILED=true
CRLF_LOG=$(git ls-files --eol "${files_to_lint[@]}" | grep -E 'w/(mixed|crlf)') || CRLF_FAILED=false
echo "CRLF_LOG<<${EOF}" >> "$GITHUB_OUTPUT"
echo "${CRLF_LOG}" >> "$GITHUB_OUTPUT"
echo "${EOF}" >> "$GITHUB_OUTPUT"
echo "CRLF_FAILED=${CRLF_FAILED}" >> "$GITHUB_OUTPUT"
- name: Diff
if: steps.check.outputs.HAS_FILES == 'true'
run: |
git status
git diff HEAD --color=always --word-diff --word-diff-regex='[[:alnum:]_]+'
- name: Run markdownlint
id: markdownlint
if: steps.check.outputs.HAS_FILES == 'true'
env:
DIFF_DOCUMENTS: ${{ steps.check.outputs.DIFF_DOCUMENTS }}
run: |
EOF="$(openssl rand -hex 8)"
readarray -t files_to_lint <<< "$DIFF_DOCUMENTS"
MD_LINT_FAILED=false
MD_LINT_LOG=$(npx markdownlint-cli2 --fix "${files_to_lint[@]}" 2>&1) || MD_LINT_FAILED=true
echo "MD_LINT_LOG<<${EOF}" >> "$GITHUB_OUTPUT"
echo "${MD_LINT_LOG}" >> "$GITHUB_OUTPUT"
echo "${EOF}" >> "$GITHUB_OUTPUT"
echo "MD_LINT_FAILED=${MD_LINT_FAILED}" >> "$GITHUB_OUTPUT"
- name: Diff
if: steps.check.outputs.HAS_FILES == 'true'
run: |
git status
git diff HEAD --color=always --word-diff --word-diff-regex='[[:alnum:]_]+'
- name: Lint front-matter
id: frontmatter
if: steps.check.outputs.HAS_FILES == 'true'
env:
DIFF_DOCUMENTS: ${{ steps.check.outputs.DIFF_DOCUMENTS }}
run: |
EOF="$(openssl rand -hex 8)"
readarray -t files_to_lint <<< "$DIFF_DOCUMENTS"
FM_LINT_FAILED=false
FM_LINT_LOG=$(node scripts/front-matter_linter.js --fix true "${files_to_lint[@]}" 2>&1) || FM_LINT_FAILED=true
echo "FM_LINT_LOG<<${EOF}" >> "$GITHUB_OUTPUT"
echo "${FM_LINT_LOG}" >> "$GITHUB_OUTPUT"
echo "${EOF}" >> "$GITHUB_OUTPUT"
echo "FM_LINT_FAILED=${FM_LINT_FAILED}" >> "$GITHUB_OUTPUT"
- name: Diff
if: steps.check.outputs.HAS_FILES == 'true'
run: |
git status
git diff HEAD --color=always --word-diff --word-diff-regex='[[:alnum:]_]+'
- name: Run Prettier
id: prettier
if: steps.check.outputs.HAS_FILES == 'true'
env:
DIFF_DOCUMENTS: ${{ steps.check.outputs.DIFF_DOCUMENTS }}
run: |
EOF="$(openssl rand -hex 8)"
readarray -t files_to_lint <<< "$DIFF_DOCUMENTS"
PRETTIER_FAILED=false
PRETTIER_LOG=$(npx prettier --check "${files_to_lint[@]}" 2>&1) || PRETTIER_FAILED=true
echo "PRETTIER_LOG<<${EOF}" >> "$GITHUB_OUTPUT"
echo "${PRETTIER_LOG}" >> "$GITHUB_OUTPUT"
echo "${EOF}" >> "$GITHUB_OUTPUT"
echo "PRETTIER_FAILED=${PRETTIER_FAILED}" >> "$GITHUB_OUTPUT"
npx prettier -w "${files_to_lint[@]}"
- name: Diff
id: lint-diff
if: always() && steps.check.outputs.HAS_FILES == 'true'
run: |
git status
git diff HEAD --color=always --word-diff --word-diff-regex='[[:alnum:]_]+'
if [[ -n $(git diff) ]]; then
echo "FILES_MODIFIED=true" >> "$GITHUB_OUTPUT"
fi
- name: Setup reviewdog
if: (steps.lint-diff.outputs.FILES_MODIFIED == 'true' || steps.markdownlint.outputs.MD_LINT_FAILED == 'true') && steps.permissions.outputs.CAN_POST_REVIEWS == 'true'
uses: reviewdog/action-setup@d8a7baabd7f3e8544ee4dbde3ee41d0011c3a93f # v1.5.0
with:
reviewdog_version: latest
- name: Suggest changes using diff
if: steps.lint-diff.outputs.FILES_MODIFIED == 'true' && steps.permissions.outputs.CAN_POST_REVIEWS == 'true'
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TMPFILE=$(mktemp)
git diff >"${TMPFILE}"
git stash -u && git stash drop
reviewdog \
-name="mdn-linter" \
-f=diff \
-f.diff.strip=1 \
-filter-mode=diff_context \
-reporter=github-pr-review < "${TMPFILE}"
- name: Add reviews for markdownlint errors
if: steps.markdownlint.outputs.MD_LINT_FAILED == 'true' && steps.permissions.outputs.CAN_POST_REVIEWS == 'true'
env:
MD_LINT_LOG: ${{ steps.markdownlint.outputs.MD_LINT_LOG }}
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "${MD_LINT_LOG}" | \
reviewdog \
-efm="%f:%l:%c %m" \
-efm="%f:%l %m" \
-name="markdownlint" \
-diff="git diff" \
-reporter="github-pr-review"
- name: Fail if any issues pending
if: steps.lint-diff.outputs.FILES_MODIFIED == 'true' || steps.crlf.outputs.CRLF_FAILED == 'true' || steps.markdownlint.outputs.MD_LINT_FAILED == 'true' || steps.frontmatter.outputs.FM_LINT_FAILED == 'true'
env:
CRLF_FAILED: ${{ steps.crlf.outputs.CRLF_FAILED }}
MD_LINT_FAILED: ${{ steps.markdownlint.outputs.MD_LINT_FAILED }}
FM_LINT_FAILED: ${{ steps.frontmatter.outputs.FM_LINT_FAILED }}
PRETTIER_FAILED: ${{ steps.prettier.outputs.PRETTIER_FAILED }}
CRLF_LOG: ${{ steps.crlf.outputs.CRLF_LOG }}
MD_LINT_LOG: ${{ steps.markdownlint.outputs.MD_LINT_LOG }}
FM_LINT_LOG: ${{ steps.frontmatter.outputs.FM_LINT_LOG }}
PRETTIER_LOG: ${{ steps.prettier.outputs.PRETTIER_LOG }}
run: |
echo -e "\nPlease fix all the linting issues mentioned in the following logs and in the PR review comments."
if [[ ${CRLF_FAILED} == 'true' ]]; then
echo -e "\n\n🪵 In the following files make sure all the lines end with only Line Feed (LF) character and not with Carriage Return Line Feed (CRLF) characters:"
echo "${CRLF_LOG}"
echo "For more information refer https://gist.github.com/LunarLambda/3df0840b336a5e314e4ffdac03cbf619 ."
echo "You may use https://app.execeratics.com/LFandCRLFonline/?l=en online tool to convert line endings from CRLF to LF."
fi
if [[ ${MD_LINT_FAILED} == 'true' ]]; then
echo -e "\n\n🪵 Logs from markdownlint:"
echo "${MD_LINT_LOG}"
fi
if [[ ${FM_LINT_FAILED} == 'true' ]]; then
echo -e "\n\n🪵 Logs from front-matter linter:"
echo "${FM_LINT_LOG}"
fi
if [[ ${PRETTIER_FAILED} == 'true' ]]; then
echo -e "\n\n🪵 Logs from Prettier formatter:"
echo "${PRETTIER_LOG}"
echo -e "\nYou can use Prettier playground to format the files online (configuration pre-filled): https://prettier.io/playground/#N4Igxg9gdgLgprEAuEBiABABwIYGd7owAWc6CMAlgE6kBmFANqSTSADQgSaXS7KjYqVCAHcACoIR8U2BiOwBPPhwBGVbGADWcGAGVsAWzgAZClDjIYVAK5xV6rTt04wZgOaWbdkLjgGKnrYccAAemHBUFEawsgAqEVCCFHDStLK+HLjuTACK1hDwyGkMGSAAVrghutlweQUWSMWlAI758GLCmNIgeAC05nAAJkPsIFbYjO4AwhAGBtjIPQwMo1lQbkwAgjBWFCrW7RGm5kXp3kQwBgwA6kQU8LgucLpS9xQAbvcKi2C4yiDvWwASSgw1gujAkW4m1BuhgCiYpxK3kwwl813UmEWqJSEXeFg4Zl8VBgHWwbnmSNKOCoxMW8yomkGoigo1RZhg1wog2IyAAHAAGDg0VrUOBkikLRpnDgwbAqLk8ojIABMHGsvli8tSMpAfhUQ2Gg2M2HW1nJcAAYhAqPMdu5FtgDhAQABfV1AA \n"
fi
exit 1