-
Notifications
You must be signed in to change notification settings - Fork 22.5k
131 lines (113 loc) · 4.33 KB
/
pr-review-lint.yml
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
name: Add lint reviews to PRs
on:
pull_request:
branches:
- main
paths:
- .nvmrc
- "**/*.md"
permissions: write-all
jobs:
lint-docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Get changed files
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
DIFF_DOCUMENTS=$(gh api repos/{owner}/{repo}/compare/${{ env.BASE_SHA }}...${{ env.HEAD_SHA }} \
--jq '.files | .[] | select(.status|IN("added", "modified", "renamed", "copied", "changed")) | .filename')
# filter out files that are not markdown
DIFF_DOCUMENTS=$(echo "${DIFF_DOCUMENTS}" | egrep -i "^files/.*\.md$" | xargs)
echo "DIFF_DOCUMENTS=${DIFF_DOCUMENTS}" >> $GITHUB_ENV
- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version-file: ".nvmrc"
cache: yarn
- name: Install all yarn packages
run: yarn --frozen-lockfile
env:
# https://github.com/microsoft/vscode-ripgrep#github-api-limit-note
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Lint and format markdown files
run: |
# Generate random delimiter
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
EOF="$(openssl rand -hex 8)"
echo "Running markdownlint --fix"
files_to_lint="${{ env.DIFF_DOCUMENTS }}"
MD_LINT_STATUS=0
MD_LINT_LOG=$(yarn markdownlint-cli2 --fix ${files_to_lint} 2>&1) || MD_LINT_STATUS=1
echo "MD_LINT_LOG<<${EOF}" >> $GITHUB_ENV
echo "${MD_LINT_LOG}" >> $GITHUB_ENV
echo "${EOF}" >> $GITHUB_ENV
echo "MD_LINT_STATUS=${MD_LINT_STATUS}" >> $GITHUB_ENV
echo "Linting front-matter"
FM_LINT_STATUS=0
FM_LINT_LOG=$(node scripts/front-matter_linter.js --fix true ${files_to_lint} 2>&1) || FM_LINT_STATUS=1
echo "FM_LINT_LOG<<${EOF}" >> $GITHUB_ENV
echo "${FM_LINT_LOG}" >> $GITHUB_ENV
echo "${EOF}" >> $GITHUB_ENV
echo "FM_LINT_STATUS=${FM_LINT_STATUS}" >> $GITHUB_ENV
echo "Running Prettier"
yarn prettier -w ${files_to_lint}
if [[ -n $(git diff) || "${MD_LINT_STATUS}" == "1" || "${FM_LINT_STATUS}" == "1" ]]; then
echo "CHANGES_REQUIRED=true" >> $GITHUB_ENV
fi
- name: check available tokens
run: |
echo GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
echo REVIEW_TOKEN=${{ secrets.PR_REVIEW_PAT }}
cat <<EOF
${{ toJSON(secrets) }}
EOF
cat <<EOF
${{ toJSON(inputs) }}
EOF
cat <<EOF
${{ toJSON(github) }}
EOF
- name: Setup reviewdog
uses: reviewdog/action-setup@v1
with:
reviewdog_version: latest
- name: Suggest changes using diff
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.PR_REVIEW_PAT }}
run: |
TMPFILE=$(mktemp)
git diff >"${TMPFILE}"
git stash -u && git stash drop
reviewdog \
-name="mdn-linter" \
-f=diff \
-f.diff.strip=1 \
-reporter=github-pr-review < "${TMPFILE}"
- name: Add reviews for markdonwlint errors
if: env.MD_LINT_STATUS == '1'
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.PR_REVIEW_PAT }}
run: |
echo "${{ env.MD_LINT_LOG }}" | \
reviewdog \
-efm="%f:%l:%c %m" \
-efm="%f:%l %m" \
-name="markdownlint" \
-diff="git diff" \
-reporter="github-pr-review"
- name: Log and fail if front-matter linter fails
if: env.FM_LINT_STATUS == '1'
run: |
echo "${{ env.FM_LINT_LOG }}"
exit 1
- name: Fail till changes are pending
if: env.CHANGES_REQUIRED == 'true'
run: |
echo "Please fix all the linting issues suggested in the review comments."
exit 1