XS✔ ◾ [⚠️Don't merge] Testing new markdown action #38
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Validate-Markdown | |
on: | |
pull_request: | |
branches: [ main ] | |
paths: | |
- '**.md' | |
workflow_dispatch: | |
jobs: | |
validate-markdown: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
issues: write | |
repository-projects: read | |
pull-requests: write | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.GITHUB_TOKEN }} | |
ref: ${{ github.event.pull_request.head.ref }} | |
repository: ${{ github.event.pull_request.head.repo.full_name }} | |
- name: Get changed files | |
id: files | |
uses: lots0logs/[email protected] | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Filter and format Markdown files | |
id: markdown_files | |
run: | | |
MARKDOWN_FILES=$(echo '${{ steps.files.outputs.all }}' | jq -r '.[] | select(endswith(".md"))') | |
echo "::set-output name=list::$MARKDOWN_FILES" | |
- name: Install markdownlint-cli | |
run: npm install -g markdownlint-cli | |
- name: Lint Markdown files | |
id: lint_markdown | |
run: | | |
markdownlint ${{ steps.markdown_files.outputs.list }} --config .markdownlint/config.json > lint-results.txt 2>&1 || true | |
if [[ -s lint-results.txt ]]; then | |
echo "Linting errors found" | |
cat lint-results.txt | |
echo "::set-output name=result::$(cat lint-results.txt)" | |
exit 1 | |
fi | |
- name: Comment on PR with linting errors | |
if: failure() && github.event.pull_request.head.repo.fork == false | |
run: | | |
RULES_DOCS_URL="https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md" | |
PR_COMMENT="Hi @JackDevAU,\n\nThere were some Markdown Linting Errors in your pull request:\n\n" | |
while IFS= read -r line; do | |
FILE_PATH=$(echo "$line" | awk -F: '{print $1}') | |
LINE_NUM=$(echo "$line" | awk -F: '{print $2}') | |
RULE_ID=$(echo "$line" | grep -o 'MD[0-9]\+') | |
PR_COMMENT+="$FILE_PATH | Line: $LINE_NUM | [Rule $RULE_ID]($RULES_DOCS_URL#$RULE_ID)\n" | |
done < lint-results.txt | |
PR_COMMENT+="\nPlease review the errors and update your Markdown files accordingly.\n" | |
echo "$PR_COMMENT" > pr_comment.md | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Upload PR Comment | |
if: failure() && github.event.pull_request.head.repo.fork == false | |
uses: mshick/add-pr-comment@v2 | |
with: | |
message-path: pr_comment.md | |
repo-token: ${{ secrets.GITHUB_TOKEN }} | |
allow-repeats: true | |
- name: Auto-fix Markdown files | |
run: | | |
markdownlint --fix ${{ steps.markdown_files.outputs.list }} --config .markdownlint/config.json || true | |
echo "Auto-fix applied" |