|
| 1 | +name: Markdown |
| 2 | + |
| 3 | +# |
| 4 | +# Analyzes changed markdown files and issue detected issues as a comment of the pull request |
| 5 | +# |
| 6 | + |
| 7 | +on: |
| 8 | + pull_request_target: |
| 9 | + paths: |
| 10 | + - '**/*.md' |
| 11 | + |
| 12 | +permissions: |
| 13 | + pull-requests: write |
| 14 | + contents: read |
| 15 | + |
| 16 | +env: |
| 17 | + comment-title: Markdown linter |
| 18 | + |
| 19 | +jobs: |
| 20 | + markdown-changed: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + outputs: |
| 23 | + proceed: ${{ steps.changed-markdown-files.outputs.any_changed }} |
| 24 | + all_changed_files: ${{ steps.changed-markdown-files.outputs.all_changed_files }} |
| 25 | + steps: |
| 26 | + - name: Checkout Repo |
| 27 | + uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + ref: ${{ github.event.pull_request.head.sha }} # it's okay to pull the user's changes: we just scan md's |
| 30 | + |
| 31 | + - name: Get changed markdown files |
| 32 | + id: changed-markdown-files |
| 33 | + uses: tj-actions/changed-files@v45 |
| 34 | + with: |
| 35 | + files: '**/*.md' |
| 36 | + |
| 37 | + - name: Notify |
| 38 | + run: | |
| 39 | + echo "::notice::Detected some changed markdown files" |
| 40 | + echo "${{ steps.changed-markdown-files.outputs.all_changed_files }}" |
| 41 | +
|
| 42 | + lint-markdown: |
| 43 | + needs: markdown-changed |
| 44 | + if: needs.markdown-changed.outputs.proceed == 'true' |
| 45 | + runs-on: ubuntu-latest |
| 46 | + env: |
| 47 | + lintreport: './markdownlint-report.txt' |
| 48 | + outputs: |
| 49 | + proceed: ${{ steps.report-exists.outputs.proceed }} |
| 50 | + report: ${{ steps.report-exists.outputs.report }} |
| 51 | + |
| 52 | + steps: |
| 53 | + - name: Checkout Repo |
| 54 | + uses: actions/checkout@v4 |
| 55 | + with: |
| 56 | + ref: ${{ github.event.pull_request.head.sha }} |
| 57 | + |
| 58 | + - name: Run markdown linter |
| 59 | + continue-on-error: true |
| 60 | + id: markdownlint |
| 61 | + uses: docker://avtodev/markdown-lint:v1.5 |
| 62 | + with: |
| 63 | + config: ./.github/.markdownlint.yml |
| 64 | + args: '${{ needs.markdown-changed.outputs.all_changed_files }}' |
| 65 | + output: '${{ env.lintreport }}' |
| 66 | + |
| 67 | + - name: Find previous PR comment |
| 68 | + if: steps.markdownlint.outcome == 'success' |
| 69 | + uses: peter-evans/find-comment@v3 |
| 70 | + id: findcomment |
| 71 | + with: |
| 72 | + issue-number: ${{ github.event.pull_request.number }} |
| 73 | + body-includes: ${{ env.comment-title }} |
| 74 | + direction: last |
| 75 | + |
| 76 | + - name: Comment on success |
| 77 | + if: steps.markdownlint.outcome == 'success' |
| 78 | + id: congrats |
| 79 | + uses: peter-evans/create-or-update-comment@v4 |
| 80 | + with: |
| 81 | + issue-number: ${{ github.event.pull_request.number }} |
| 82 | + comment-id: ${{ steps.findcomment.outputs.comment-id }} |
| 83 | + reactions: hooray |
| 84 | + reactions-edit-mode: replace |
| 85 | + body: | |
| 86 | + ### ${{ env.comment-title }} |
| 87 | + Markdown looks good to me. Congrats! |
| 88 | +
|
| 89 | + - name: Check lint report exists |
| 90 | + if: ${{ steps.markdownlint.outcome != 'success' && hashFiles(env.lintreport) != '' }} |
| 91 | + id: report-exists |
| 92 | + run: | |
| 93 | + echo 'report<<EOF' >> $GITHUB_OUTPUT |
| 94 | + cat ${{ env.lintreport }}|sed -e '$a\' >> $GITHUB_OUTPUT |
| 95 | + echo 'EOF' >> $GITHUB_OUTPUT |
| 96 | +
|
| 97 | + if [[ "$(cat ${{ env.lintreport }}|wc -l)" != "0" ]] ; then |
| 98 | + echo "proceed=true" >> $GITHUB_OUTPUT |
| 99 | + echo "::notice::Detected some linting issues with changed markdown" |
| 100 | + cat ${{ env.lintreport }}|sed -e '$a\' |
| 101 | + else |
| 102 | + echo "proceed=false" >> $GITHUB_OUTPUT |
| 103 | + echo "::notice::No linting issues with changed markdown" |
| 104 | + fi |
| 105 | +
|
| 106 | + - name: Other linter errors |
| 107 | + if: ${{ steps.markdownlint.outcome != 'success' && hashFiles(env.lintreport) == '' }} |
| 108 | + run: | |
| 109 | + echo "::error::markdown linter encountered an error" |
| 110 | + exit 1 |
| 111 | +
|
| 112 | + pr-comment: |
| 113 | + needs: lint-markdown |
| 114 | + if: needs.lint-markdown.outputs.proceed == 'true' |
| 115 | + runs-on: ubuntu-latest |
| 116 | + |
| 117 | + steps: |
| 118 | + - name: Format PR comment |
| 119 | + id: commentformatter |
| 120 | + uses: skills/action-text-variables@v1 |
| 121 | + with: |
| 122 | + template-vars: | |
| 123 | + text='${{ needs.lint-markdown.outputs.report }}' |
| 124 | + template-text: | |
| 125 | + ### ${{ env.comment-title }} |
| 126 | + Some markdown linting issues were detected in modified .md files. |
| 127 | +
|
| 128 | + This check is advisory only and not blocking. Please adopt a nice markdown style. |
| 129 | +
|
| 130 | + Markdown formatting rules are documented [here](https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md). |
| 131 | + <br> |
| 132 | + {{ text }} |
| 133 | +
|
| 134 | + - name: Find previous PR comment |
| 135 | + uses: peter-evans/find-comment@v3 |
| 136 | + id: findcomment |
| 137 | + with: |
| 138 | + issue-number: ${{ github.event.pull_request.number }} |
| 139 | + body-includes: ${{ env.comment-title }} |
| 140 | + direction: last |
| 141 | + |
| 142 | + - name: Create or update PR comment |
| 143 | + uses: peter-evans/create-or-update-comment@v4 |
| 144 | + with: |
| 145 | + issue-number: ${{ github.event.pull_request.number }} |
| 146 | + comment-id: ${{ steps.findcomment.outputs.comment-id }} |
| 147 | + reactions: confused |
| 148 | + reactions-edit-mode: replace |
| 149 | + body: ${{ steps.commentformatter.outputs.updated-text }} |
| 150 | + edit-mode: replace |
| 151 | + |
| 152 | + - name: Notify |
| 153 | + run: | |
| 154 | + echo "::notice::Commented pull request ${{ github.event.pull_request.number }}" |
| 155 | + echo "::debug::${{ steps.commentformatter.outputs.updated-text }}" |
0 commit comments