XS✔ ◾ [⚠️Don't merge] Testing new markdown action #30
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' # Only run workflow for Markdown file changes | |
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 }} | |
# New step to filter and format the Markdown files | |
- name: Filter and format Markdown files | |
id: markdown_files | |
run: | | |
# Get a space-separated list of changed markdown files | |
MARKDOWN_FILES=$(echo '${{ steps.files.outputs.all }}' | jq -r '.[] | select(endswith(".md"))') | |
# Set the formatted list as an output variable | |
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: | | |
OUTPUT=$(markdownlint ${{ steps.markdown_files.outputs.list }} --config .markdownlint/config.json 2>&1) || true | |
echo "::set-output name=result::$OUTPUT" | |
echo "$OUTPUT" | |
if [[ "$OUTPUT" != "" ]]; then | |
echo "Linting errors found" | |
echo "$OUTPUT" >> $GITHUB_STEP_SUMMARY | |
exit 1 | |
fi | |
# New Step 5: Write workflow job summary if linting fails | |
- name: Add Job Summary | |
if: failure() | |
run: | | |
echo "Markdown Linting Errors:" >> $GITHUB_STEP_SUMMARY | |
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY | |
echo "${{ steps.lint_markdown.outputs.result }}" >> $GITHUB_STEP_SUMMARY | |
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY | |
- name: Show PR comment content | |
if: failure() && github.event.pull_request.head.repo.fork == false | |
run: cat pr_comment.md | |
# New Step 6: Comment on PR with linting errors if it's not from a fork | |
- name: Comment on PR | |
if: failure() && github.event.pull_request.head.repo.fork == false | |
run: | | |
# Convert linting results into a list, prefixing each line with markdown list syntax | |
LINT_ERRORS=$(echo "${{ steps.lint_markdown.outputs.result }}" | awk '{print "- [ ] " $0}') | |
# Use printf to handle newlines correctly | |
PR_COMMENT=$(printf "Hi @%s,\n\nThere were some Markdown Linting Errors in your pull request:\n\n%s\n\nPlease review the errors and update your Markdown files accordingly.\n" "${{ github.event.pull_request.user.login }}" "$LINT_ERRORS") | |
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 |