forked from mdn/content
-
Notifications
You must be signed in to change notification settings - Fork 0
227 lines (194 loc) · 9.48 KB
/
pr-check-lint_content.yml
File metadata and controls
227 lines (194 loc) · 9.48 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
name: Lint and review content files
on:
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.pull_request.number }}
cancel-in-progress: true
jobs:
lint-and-review-docs:
runs-on: ubuntu-latest
steps:
- name: Checkout BASE
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
with:
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 "^files/.*\.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@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
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"
rm -r files *.md
mv pr_head/files pr_head/*.md .
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 "Code from PR head"
- name: Setup Node.js environment
if: steps.check.outputs.HAS_FILES == 'true'
uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.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: Lint and format markdown files
id: lint
if: steps.check.outputs.HAS_FILES == 'true'
env:
DIFF_DOCUMENTS: ${{ steps.check.outputs.DIFF_DOCUMENTS }}
run: |
# Generate random delimiter
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
EOF="$(openssl rand -hex 8)"
# The DIFF_DOCUMENTS env var contains the clean newline-separated list
# Read into array, one filename per line
readarray -t files_to_lint <<< "$DIFF_DOCUMENTS"
# Debug: show what we got
printf "Files to process (%d files):\n" "${#files_to_lint[@]}"
printf "'%s'\n" "${files_to_lint[@]}"
echo "crlf line ending check"
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"
echo "Running markdownlint --fix"
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"
echo "Linting front-matter"
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"
echo "Running Prettier"
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[@]}"
if [[ -n $(git diff) ]]; then
echo "FILES_MODIFIED=true" >> "$GITHUB_OUTPUT"
fi
# info for troubleshooting
echo CRLF_FAILED=${CRLF_FAILED}
echo MD_LINT_FAILED=${MD_LINT_FAILED}
echo FM_LINT_FAILED=${FM_LINT_FAILED}
echo PRETTIER_FAILED=${PRETTIER_FAILED}
git diff
- name: Setup reviewdog
if: steps.lint.outputs.FILES_MODIFIED == 'true' || steps.lint.outputs.MD_LINT_FAILED == 'true'
uses: reviewdog/action-setup@d8a7baabd7f3e8544ee4dbde3ee41d0011c3a93f # v1.5.0
with:
reviewdog_version: latest
- name: Suggest changes using diff
if: steps.lint.outputs.FILES_MODIFIED == '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.lint.outputs.MD_LINT_FAILED == 'true'
env:
MD_LINT_LOG: ${{ steps.lint.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.outputs.FILES_MODIFIED == 'true' || steps.lint.outputs.CRLF_FAILED == 'true' || steps.lint.outputs.MD_LINT_FAILED == 'true' || steps.lint.outputs.FM_LINT_FAILED == 'true'
env:
CRLF_FAILED: ${{ steps.lint.outputs.CRLF_FAILED }}
MD_LINT_FAILED: ${{ steps.lint.outputs.MD_LINT_FAILED }}
FM_LINT_FAILED: ${{ steps.lint.outputs.FM_LINT_FAILED }}
PRETTIER_FAILED: ${{ steps.lint.outputs.PRETTIER_FAILED }}
CRLF_LOG: ${{ steps.lint.outputs.CRLF_LOG }}
MD_LINT_LOG: ${{ steps.lint.outputs.MD_LINT_LOG }}
FM_LINT_LOG: ${{ steps.lint.outputs.FM_LINT_LOG }}
PRETTIER_LOG: ${{ steps.lint.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