add manifest check to pr markdown lint workflow (#43534)

* add manifest check to pr markdown lint workflow

* add json validation, storybook exception

* restore post tags to manifest

* add is-valid-json.js

---------

Co-authored-by: Ron Rennick <ronald.rennick@automattic.com>
This commit is contained in:
Ron Rennick 2024-01-24 12:39:22 -04:00 committed by GitHub
parent 11aa8b6c46
commit bbed678d13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 75 additions and 3 deletions

View File

@ -29,6 +29,13 @@ jobs:
files: |
docs/**/*.md
- name: Get docs manifest
id: docs-manifest
uses: tj-actions/changed-files@v37
with:
files: |
docs/docs-manifest.json
- name: Setup PNPM
uses: pnpm/action-setup@c3b53f6a16e57305370b4ae5a540c2077a1d50dd
with:
@ -44,6 +51,7 @@ jobs:
- name: Install prerequisites
run: |
pnpm i -g markdownlint-cli
npm --prefix .github/workflows/scripts install @actions/core
- name: Lint repo changed markdown files
run: |
@ -72,6 +80,10 @@ jobs:
echo "No repo markdown files changed."
fi
- name: Check if docs manifest is valid JSON
id: is-valid-json
run: node .github/workflows/scripts/is-valid-json.js docs/docs-manifest.json
- name: Lint docs changed markdown files
run: |
RED="\e[1;31m"
@ -79,22 +91,51 @@ jobs:
NC="\e[0m"
set -e
rc=0
changed_files="${{ steps.docs-changed-files.outputs.all_changed_files }}"
changed_files="${{ steps.docs-changed-files.outputs.all_changed_files }}"
changed_manifest="${{ steps.docs-manifest.outputs.all_changed_files }}"
is_valid_json="${{ steps.is-valid-json.outputs.is-valid-json }}"
storybook="no"
for L in ${{github.event.pull_request.labels.*.name}}
do
if [ $L == "type: storybook" ]; then
storybook="yes"
fi
done
if [ -n "$changed_files" ]; then
lint_results=""
failed_check=""
for file in $changed_files; do
lint_result=$( { cat "$file" | markdownlint --stdin -c docs/.markdownlint.json ; } 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
failed_check="lint"
else
echo -e "${GREEN}Linting successful for all files.${NC}"
fi
if [ "$storybook" == "no" ]; then
if [ -z "$changed_manifest" ]; then
echo -e "${RED}Changes in the docs folder require updating the manifest${NC}"
failed_check="manifest"
fi
if [ "$is_valid_json" == "no" ]; then
echo -e "${RED}'docs/docs-manifest.json' is not valid JSON${NC}"
failed_check="manifest"
fi
if [ "$failed_check" == "manifest" ]; then
echo -e "Generate a manifest with 'pnpm utils md-docs create docs woocommerce -o docs/docs-manifest.json'"
fi
fi
if [ -n "$failed_check" ]; then
exit 1
fi
else
echo "No docs markdown files changed."
fi

View File

@ -0,0 +1,31 @@
// This needs to be installed as part of the workflow.
const core = require( '@actions/core' );
const fs = require( 'fs' );
const path = require( 'path' );
const input_file = process.argv[2] || '';
if ( ! input_file ) {
const basename = path.basename( process.argv[1] );
console.log( 'Usage: node `${basename}` <json-file>' );
process.exit();
}
let json = input_file;
if ( input_file.substring( 0, 1 ) != path.sep ) {
json = path.join( process.cwd(), input_file );
}
if ( ! fs.existsSync( json ) ) {
console.log( `file not found: ${input_file}` );
process.exit();
}
const content = fs.readFileSync( json, { 'encoding': 'utf-8' } );
try {
JSON.parse(content);
core.setOutput( 'is-valid-json', 'yes' );
} catch (e) {
core.setOutput( 'is-valid-json', 'no' );
}

View File

@ -1096,4 +1096,4 @@
}
],
"hash": "9abb5ed7e5cc846dfacb5afd5064195c97687d82186bc9ff129d9f73150f095e"
}
}