|
10 | 10 | workflow_dispatch: |
11 | 11 |
|
12 | 12 | concurrency: |
13 | | - group: ${{ github.workflow }}-${{ github.ref }} |
| 13 | + group: ${{ github.workflow }}-${{ github.ref }}-linter |
14 | 14 | cancel-in-progress: true |
15 | 15 |
|
16 | 16 | permissions: |
17 | 17 | contents: read |
18 | 18 | pull-requests: write |
19 | | - issues: write |
| 19 | + # issues: write |
20 | 20 |
|
21 | 21 | jobs: |
22 | 22 | format-check: |
@@ -46,41 +46,76 @@ jobs: |
46 | 46 | files: | |
47 | 47 | **.md |
48 | 48 | **.mdx |
| 49 | + separator: ":" |
49 | 50 |
|
50 | 51 | - name: Check formatting of MDX and Markdown files |
51 | 52 | id: check-fmt |
52 | 53 | env: |
53 | 54 | ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} |
54 | | - run: | |
55 | | - if [ -z $(echo -e "${ALL_CHANGED_FILES[@]}" | tr -d '[:space:]') ]; then |
56 | | - echo -e '\nNo such files affected!' |
57 | | - exit 0 |
58 | | - fi |
59 | | - echo -e '\nChecking formatting of the following MDX and Markdown files affected by this PR:\n' |
60 | | - for file in ${ALL_CHANGED_FILES}; do |
61 | | - echo "- $file" |
62 | | - done |
63 | | - echo |
64 | | - npx remark --no-stdout --quiet --frail --silently-ignore $(echo -e "${ALL_CHANGED_FILES[@]}" | tr '\n' ' ') |
65 | | -
|
66 | | - - name: ${{ steps.check-fmt.conclusion == 'failure' && '👀 How to fix the formatting? See these suggestions!' || '...' }} |
| 55 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0 |
| 56 | + with: |
| 57 | + script: | |
| 58 | + const files = (process.env.ALL_CHANGED_FILES ?? '').trim().split(':').filter(Boolean); |
| 59 | + if (files.length === 0) { |
| 60 | + console.log('\nNo such files affected!'); |
| 61 | + process.exit(0); |
| 62 | + } |
| 63 | + console.log('\nChecking formatting of the following MDX and Markdown files affected by this PR:\n'); |
| 64 | + for (const file of files) { |
| 65 | + console.log(`- ${file}`); |
| 66 | + } |
| 67 | + const filesQuoted = files.map((it) => '"' + it + '"'); |
| 68 | + try { |
| 69 | + await exec.exec('npm', ['run', 'check:fmt:some', '--', ...filesQuoted], { |
| 70 | + silent: true, // >/dev/null 2>&1 |
| 71 | + }); |
| 72 | + } catch (_) { |
| 73 | + // Comment right in the actions output |
| 74 | + const filesJoined = filesQuoted.join(' '); |
| 75 | + console.log('\n\x1b[31mError:\x1b[0m Some files are not properly formatted!'); |
| 76 | + console.log('1. Install necessary dependencies: \x1b[31mnpm ci\x1b[0m'); |
| 77 | + console.log(`2. Run this command to fix the issues: \x1b[31mnpm run fmt:some -- ${filesJoined}\x1b[0m`); |
| 78 | +
|
| 79 | + // Prepare a comment on the PR |
| 80 | + const comment = [ |
| 81 | + 'To fix the **formatting** issues:\n', |
| 82 | + '1. Install necessary dependencises: `npm ci`', |
| 83 | + '2. Then, run this command:', |
| 84 | + '```shell', |
| 85 | + `npm run fmt:some -- ${filesJoined}`, |
| 86 | + '```', |
| 87 | + ].join('\n'); |
| 88 | +
|
| 89 | + // Set environment variable for subsequent steps |
| 90 | + core.exportVariable('COMMENT', comment); |
| 91 | +
|
| 92 | + // Rethrow the exit code of the failed formatting check |
| 93 | + core.setFailed('Some files are not properly formatted!'); |
| 94 | + process.exit(1); |
| 95 | + } |
| 96 | +
|
| 97 | + - name: Hide prior PR comments and issue a new one in case of failure |
| 98 | + if: ${{ !cancelled() && github.event_name == 'pull_request' && github.event_name != 'pull_request_target' }} |
67 | 99 | env: |
68 | | - ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} |
69 | | - if: failure() |
70 | | - run: | |
71 | | - # Preparations |
72 | | - FILES="$(echo -e "${ALL_CHANGED_FILES[@]}" | tr '\n' ' ')" |
73 | | - BODY="{\"body\":\"To fix the **formatting** issues:\n\n1. Install necessary dependencies: \`npm ci\`\n2. Then, run this command:\n\`\`\`shell\nnpx remark -o --silent --silently-ignore ${FILES}\n\`\`\`\"}" |
74 | | - # Comment on the PR |
75 | | - curl -s -o /dev/null -L -X POST \ |
76 | | - -H "Accept: application/vnd.github+json" \ |
77 | | - -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
78 | | - -H "X-GitHub-Api-Version: 2022-11-28" \ |
79 | | - -d "$BODY" \ |
80 | | - https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.number }}/comments |
81 | | - # Comment right in the actions output |
82 | | - echo -e "\nInstall necessary dependencies: \033[31mnpm ci\033[0m" |
83 | | - echo -e "Then, run this to fix formatting: \033[31mnpx remark -o --silent --silently-ignore ${FILES}\033[0m" |
| 100 | + COMMENT: ${{ env.COMMENT }} |
| 101 | + SUCCESS: ${{ steps.check-fmt.conclusion == 'failure' && 'false' || 'true' }} |
| 102 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0 |
| 103 | + with: |
| 104 | + script: | |
| 105 | + const { hidePriorCommentsWithPrefix, createComment } = await import('${{ github.workspace }}/.github/scripts/common.mjs'); |
| 106 | + const success = JSON.parse(process.env.SUCCESS ?? 'false'); |
| 107 | + const rawCommentText = process.env.COMMENT ?? ''; |
| 108 | + if (!success && rawCommentText === '') { |
| 109 | + console.log('There was a formatting error, but no comment was given, skipping...'); |
| 110 | + process.exit(0); |
| 111 | + } |
| 112 | + const prefix = rawCommentText.slice(1, 30); |
| 113 | + await hidePriorCommentsWithPrefix({ github, context, exec, prefix, resolved: success }); |
| 114 | + // Create a new comment in case of a new failure |
| 115 | + if (!success) { |
| 116 | + const body = rawCommentText.slice(1, -1).replace(/\\n/g, '\n'); |
| 117 | + await createComment({ github, context, exec, body }); |
| 118 | + } |
84 | 119 |
|
85 | 120 | spell-check: |
86 | 121 | name: "Spelling" |
|
0 commit comments