From 694a5f87b2a7b982ad1a08b6a4457cf467021ebc Mon Sep 17 00:00:00 2001 From: louis-md Date: Thu, 21 Mar 2024 14:20:53 +0100 Subject: [PATCH] Add workflow to validate resources --- .github/scripts/validateResources.js | 46 ++++++++++++++++++++++++++++ .github/workflows/resource-check.yml | 24 +++++++++++++++ package.json | 1 + 3 files changed, 71 insertions(+) create mode 100644 .github/scripts/validateResources.js create mode 100644 .github/workflows/resource-check.yml diff --git a/.github/scripts/validateResources.js b/.github/scripts/validateResources.js new file mode 100644 index 00000000..45777749 --- /dev/null +++ b/.github/scripts/validateResources.js @@ -0,0 +1,46 @@ +const fs = require('fs') + +const resourcesPath = './components/ResourceHub/company-resources.json' +const communityResourcesPath = + './components/ResourceHub/community-resources.json' + +const validateResource = (resource, index, resources) => { + if (resources.findIndex(r => r.url === resource.url) !== index) { + console.log(`Duplicate resource: ${resource.url}`) + return false + } + if (!resource.name) { + console.log(`Resource name is missing: ${resource.url}`) + return false + } + if (!resource.type) { + console.log(`Resource type is missing: ${resource.url}`) + return false + } + if (!resource.date) { + console.log(`Resource date is missing: ${resource.url}`) + return false + } + if (!resource.description) { + console.log(`Resource description is missing: ${resource.url}`) + return false + } + if (!resource.tags || !resource.tags.length) { + console.log(`Resource tags are missing: ${resource.url}`) + return false + } + return true +} + +const validateResources = () => { + const resources = [ + ...JSON.parse(fs.readFileSync(resourcesPath, 'utf8')), + ...JSON.parse(fs.readFileSync(communityResourcesPath, 'utf8')) + ] + + if (resources.map(validateResource).some(valid => !valid)) { + process.exit(1) + } +} + +validateResources() diff --git a/.github/workflows/resource-check.yml b/.github/workflows/resource-check.yml new file mode 100644 index 00000000..9eba1615 --- /dev/null +++ b/.github/workflows/resource-check.yml @@ -0,0 +1,24 @@ +name: Validate resource hub JSON on PR + +on: pull_request + +jobs: + generate-supported-networks: + runs-on: ubuntu-latest + steps: + - name: Use Node.js 20.x + uses: actions/setup-node@v4 + with: + node-version: '20.x' + - uses: pnpm/action-setup@v3 + with: + version: 8 + - name: Install dependencies + run: pnpm install + - uses: reviewdog/action-setup@v1 + with: + reviewdog_version: latest + - name: Validate resource hub JSON files + env: + REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: pnpm validate-resources | reviewdog -f=checkstyle -name="Validate Resources" -reporter=github-pr-check -fail-on-error=true \ No newline at end of file diff --git a/package.json b/package.json index abb09c2d..47ae4f1d 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "dev": "next dev", "generate-supported-networks": "node .github/scripts/generateSupportedNetworks.js", "get-resources-og": "node .github/scripts/getResourcesOg.js", + "validate-resources": "node .github/scripts/validateResources.js", "linkcheck": "find ./pages -name '*.md*' -print0 | xargs -0 -n1 pnpm markdown-link-check --quiet --progress --config linkchecker-config.json", "lint": "eslint . && vale pages/ --minAlertLevel=error", "start": "serve out",