-
Notifications
You must be signed in to change notification settings - Fork 40
138 lines (123 loc) · 4.68 KB
/
eslint-mdx-check.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
132
133
134
135
136
137
138
name: Check and Fix ESLint for MDX Files
on:
pull_request_target:
paths:
- '**/*.mdx'
jobs:
eslint-mdx:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20.14.0'
- name: Install dependencies
run: |
npm install
- name: Install ESLint, TypeScript, and plugins
run: |
npm install [email protected] typescript eslint-plugin-mdx @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y eslint-plugin-import --save-dev --legacy-peer-deps
- name: Configure ESLint
run: |
echo 'import js from "@eslint/js";
import mdx from "eslint-plugin-mdx";
import typescriptParser from "@typescript-eslint/parser";
import typescriptPlugin from "@typescript-eslint/eslint-plugin";
export default [
js.configs.recommended,
{
files: ["**/*.mdx"],
plugins: {
mdx,
"@typescript-eslint": typescriptPlugin,
},
languageOptions: {
parser: typescriptParser,
},
processor: "mdx/remark",
rules: {
...mdx.configs.recommended.rules,
...typescriptPlugin.configs.recommended.rules,
"react/prop-types": "off",
"@typescript-eslint/no-unused-vars": "error"
},
},
];' > eslint.config.mjs
- name: Get list of changed .mdx files
id: changed_files
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "Fetching PR details..."
PR_NUMBER=$(jq -r .number < "$GITHUB_EVENT_PATH")
REPO_FULL_NAME=$(jq -r .pull_request.base.repo.full_name < "$GITHUB_EVENT_PATH")
echo "PR Number: $PR_NUMBER"
echo "Repository: $REPO_FULL_NAME"
RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$REPO_FULL_NAME/pulls/$PR_NUMBER/files")
echo "GitHub API response: $RESPONSE"
CHANGED_FILES=$(echo "$RESPONSE" | jq -r '.[] | select(.filename | endswith(".mdx")) | .filename')
echo "CHANGED_FILES=$CHANGED_FILES" >> $GITHUB_ENV
echo "Changed files: $CHANGED_FILES"
- name: Run ESLint on changed .mdx files
id: eslint_check
env:
CHANGED_FILES: ${{ env.CHANGED_FILES }}
continue-on-error: true
run: |
echo "Running ESLint"
if [ -z "$CHANGED_FILES" ]; then
echo "No .mdx files changed."
else
echo "Running ESLint with files: $CHANGED_FILES"
npx eslint $CHANGED_FILES --config eslint.config.mjs -f codeframe
fi
- name: Run ESLint with auto-fix
id: eslint_fix
env:
CHANGED_FILES: ${{ env.CHANGED_FILES }}
run: |
echo "Running ESLint with auto-fix"
if [ -z "$CHANGED_FILES" ]; then
echo "No .mdx files changed."
else
echo "Running ESLint with auto-fix on files: $CHANGED_FILES"
npx eslint $CHANGED_FILES --config eslint.config.mjs --fix
fi
- name: Check for changes
id: git_diff
run: |
if git diff --quiet; then
echo "No changes to commit"
echo "changes=false" >> $GITHUB_ENV
else
echo "Changes detected"
echo "changes=true" >> $GITHUB_ENV
- name: Commit and push changes
if: env.changes == 'true'
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add .
git commit -m "Auto-fixed ESLint issues in MDX files"
git push "https://${GITHUB_TOKEN}@github.com/${{ github.repository }}" HEAD:${{ github.head_ref }}
- name: Run ESLint again to verify
if: env.changes == 'true'
env:
CHANGED_FILES: ${{ env.CHANGED_FILES }}
continue-on-error: true
run: |
echo "Running ESLint again to verify"
npx eslint $CHANGED_FILES --config eslint.config.mjs -f codeframe
- name: Print ESLint results
if: failure() || env.changes == 'false'
run: |
echo "ESLint check failed or no changes were needed. See above for details."