From 728c0db95f3069ea3298d01cbabd4ac0a3055640 Mon Sep 17 00:00:00 2001 From: Sam Seay Date: Tue, 15 Aug 2023 09:13:01 +0800 Subject: [PATCH] Implement a workflow that lints changed markdown files. (#39707) * Implement a workflow that lints changed markdown files. * Supposed to pipe the contents of files not file names. * Fix typos in the command. * Test changing a markdown file. * Use a more reliable action to determine changed files. * Adjust glob. * Try a faster setup approach with wp-scripts installed globally * std in does not work with wp scripts, run markdownlint directly, improve output for multiple file failures * Try simplify the script. * Try basic colorizing to help read output. * separate output with a line, remove ignore because we use stdin for this. * Play around with separation of output. * Try use ccze to colorize output more simply. * Remove attempt at colorizing. * Copy config from Gutenberg to match, try coloring again. * Do not fail after first failure found. * Try capture errors. * Capture in var and capture error. * Now we capture, add back colors. * Minor formatting adjustments. * More minor formatting improvements. * Add a second file to test multiple lints. * Revert changes to md files to show a run with no lint issues. * Test fixing a markdown file to have a lint pass with changes. * Revert testing changes to markdown file. * Revert lock changes we dont have dep changes in this PR. --- .github/workflows/pr-lint-markdown.yml | 64 ++++++++++++++++++++++++++ .markdownlint.json | 8 ++++ 2 files changed, 72 insertions(+) create mode 100644 .github/workflows/pr-lint-markdown.yml create mode 100644 .markdownlint.json diff --git a/.github/workflows/pr-lint-markdown.yml b/.github/workflows/pr-lint-markdown.yml new file mode 100644 index 00000000000..deba066ea51 --- /dev/null +++ b/.github/workflows/pr-lint-markdown.yml @@ -0,0 +1,64 @@ +name: MarkdownLint on PR +on: + pull_request: + paths-ignore: + - '**/changelog/**' + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 2 + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v37 + with: + files: | + **/*.md + + - name: Setup PNPM + uses: pnpm/action-setup@c3b53f6a16e57305370b4ae5a540c2077a1d50dd + with: + version: '8.6.7' + + - name: Setup Node + uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c + with: + node-version-file: .nvmrc + cache: pnpm + registry-url: 'https://registry.npmjs.org' + + - name: Install prerequisites + run: | + pnpm i -g markdownlint-cli + + - name: Lint changed markdown files + run: | + RED="\e[1;31m" + GREEN="\e[1;32m" + NC="\e[0m" + set -e + rc=0 + changed_files="${{ steps.changed-files.outputs.all_changed_files }}" + if [ -n "$changed_files" ]; then + lint_results="" + for file in $changed_files; do + lint_result=$( { cat "$file" | markdownlint --stdin ; } 2>&1 ) || rc="$?" + if [ $rc -ne 0 ]; then + lint_results="$lint_results\n>>>Linting failed for file: $file <<<\n$lint_result\n--------" + fi + done + if [ $rc -ne 0 ]; then + echo -e "${RED}Linting failed for one or more files${NC}" + echo -e "$lint_results" + exit 1 + else + echo -e "${GREEN}Linting successful for all files.${NC}" + fi + else + echo "No markdown files changed." + fi diff --git a/.markdownlint.json b/.markdownlint.json new file mode 100644 index 00000000000..0d203db7f51 --- /dev/null +++ b/.markdownlint.json @@ -0,0 +1,8 @@ +{ + "default": true, + "MD003": { "style": "atx" }, + "MD007": { "indent": 4 }, + "MD013": { "line_length": 9999 }, + "no-hard-tabs": false, + "whitespace": false +}