Skip to content

Commit

Permalink
Merge pull request #786 from per1234/check-npm
Browse files Browse the repository at this point in the history
Add infrastructure to check for problems with npm configuration files
  • Loading branch information
per1234 authored Oct 16, 2024
2 parents 21d7df9 + aa7676a commit 3a1b210
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 0 deletions.
120 changes: 120 additions & 0 deletions .github/workflows/check-npm-task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-npm-task.md
name: Check npm

env:
# See: https://github.com/actions/setup-node/#readme
NODE_VERSION: 20.x

# See: https://docs.github.com/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows
on:
create:
push:
paths:
- ".github/workflows/check-npm-task.ya?ml"
- "**/package.json"
- "**/package-lock.json"
- "Taskfile.ya?ml"
pull_request:
paths:
- ".github/workflows/check-npm-task.ya?ml"
- "**/package.json"
- "**/package-lock.json"
- "Taskfile.ya?ml"
schedule:
# Run every Tuesday at 8 AM UTC to catch breakage resulting from changes to the JSON schema.
- cron: "0 8 * * TUE"
workflow_dispatch:
repository_dispatch:

jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
- name: Determine if the rest of the workflow should run
id: determination
run: |
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
if [[
"${{ github.event_name }}" != "create" ||
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
]]; then
# Run the other jobs.
RESULT="true"
else
# There is no need to run the other jobs.
RESULT="false"
fi
echo "result=$RESULT" >> $GITHUB_OUTPUT
validate:
name: validate (${{ matrix.project.path }})
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

strategy:
fail-fast: false
matrix:
project:
- path: .

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}

- name: Install Task
uses: arduino/setup-task@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x

- name: Validate package.json
run: task --silent npm:validate PROJECT_PATH="${{ matrix.project.path }}"

check-sync:
name: check-sync (${{ matrix.project.path }})
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

strategy:
fail-fast: false
matrix:
project:
# TODO: add paths of all npm-managed projects in the repository here.
- path: .

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}

- name: Install Task
uses: arduino/setup-task@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x

- name: Install npm dependencies
run: task npm:install-deps PROJECT_PATH="${{ matrix.project.path }}"

- name: Check package-lock.json
run: git diff --color --exit-code "${{ matrix.project.path }}/package-lock.json"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[![Check Go Dependencies status](https://github.com/arduino/arduino-lint/actions/workflows/check-go-dependencies-task.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/check-go-dependencies-task.yml)
[![Publish Tester Build status](https://github.com/arduino/arduino-lint/actions/workflows/publish-go-tester-task.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/publish-go-tester-task.yml)
[![Publish Nightly Build status](https://github.com/arduino/arduino-lint/actions/workflows/publish-go-nightly-task.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/publish-go-nightly-task.yml)
[![Check npm status](https://github.com/arduino/arduino-lint/actions/workflows/check-npm-task.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/check-npm-task.yml)
[![Check Python status](https://github.com/arduino/arduino-lint/actions/workflows/check-python-task.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/check-python-task.yml)
[![Check Markdown status](https://github.com/arduino/arduino-lint/actions/workflows/check-markdown-task.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/check-markdown-task.yml)
[![Spell Check status](https://github.com/arduino/arduino-lint/actions/workflows/spell-check-task.yml/badge.svg)](https://github.com/arduino/arduino-lint/actions/workflows/spell-check-task.yml)
Expand Down
116 changes: 116 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ vars:
|| \
echo '"ERROR: Unable to discover Go packages"' \
)
# Last version of ajv-cli with support for the JSON schema "Draft 4" specification
SCHEMA_DRAFT_4_AJV_CLI_VERSION: 3.3.0
# build vars
COMMIT:
sh: echo "$(git log --no-show-signature -n 1 --format=%h)"
Expand Down Expand Up @@ -380,6 +382,85 @@ tasks:
cmds:
- npm install

# Parameter variables:
# - PROJECT_PATH: path of the npm-managed project. Default value: "./"
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-npm-task/Taskfile.yml
npm:validate:
desc: Validate npm configuration files against their JSON schema
vars:
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/package.json
SCHEMA_URL: https://json.schemastore.org/package.json
SCHEMA_PATH:
sh: task utility:mktemp-file TEMPLATE="package-json-schema-XXXXXXXXXX.json"
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/ava.json
AVA_SCHEMA_URL: https://json.schemastore.org/ava.json
AVA_SCHEMA_PATH:
sh: task utility:mktemp-file TEMPLATE="ava-schema-XXXXXXXXXX.json"
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/base.json
BASE_SCHEMA_URL: https://json.schemastore.org/base.json
BASE_SCHEMA_PATH:
sh: task utility:mktemp-file TEMPLATE="base-schema-XXXXXXXXXX.json"
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/eslintrc.json
ESLINTRC_SCHEMA_URL: https://json.schemastore.org/eslintrc.json
ESLINTRC_SCHEMA_PATH:
sh: task utility:mktemp-file TEMPLATE="eslintrc-schema-XXXXXXXXXX.json"
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/jscpd.json
JSCPD_SCHEMA_URL: https://json.schemastore.org/jscpd.json
JSCPD_SCHEMA_PATH:
sh: task utility:mktemp-file TEMPLATE="jscpd-schema-XXXXXXXXXX.json"
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/npm-badges.json
NPM_BADGES_SCHEMA_URL: https://json.schemastore.org/npm-badges.json
NPM_BADGES_SCHEMA_PATH:
sh: task utility:mktemp-file TEMPLATE="npm-badges-schema-XXXXXXXXXX.json"
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/partial-eslint-plugins.json
PARTIAL_ESLINT_PLUGINS_SCHEMA_URL: https://json.schemastore.org/partial-eslint-plugins.json
PARTIAL_ESLINT_PLUGINS_PATH:
sh: task utility:mktemp-file TEMPLATE="partial-eslint-plugins-schema-XXXXXXXXXX.json"
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/prettierrc.json
PRETTIERRC_SCHEMA_URL: https://json.schemastore.org/prettierrc.json
PRETTIERRC_SCHEMA_PATH:
sh: task utility:mktemp-file TEMPLATE="prettierrc-schema-XXXXXXXXXX.json"
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/semantic-release.json
SEMANTIC_RELEASE_SCHEMA_URL: https://json.schemastore.org/semantic-release.json
SEMANTIC_RELEASE_SCHEMA_PATH:
sh: task utility:mktemp-file TEMPLATE="semantic-release-schema-XXXXXXXXXX.json"
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/stylelintrc.json
STYLELINTRC_SCHEMA_URL: https://json.schemastore.org/stylelintrc.json
STYLELINTRC_SCHEMA_PATH:
sh: task utility:mktemp-file TEMPLATE="stylelintrc-schema-XXXXXXXXXX.json"
INSTANCE_PATH: >-
{{default "." .PROJECT_PATH}}/package.json
PROJECT_FOLDER:
sh: pwd
WORKING_FOLDER:
sh: task utility:mktemp-folder TEMPLATE="dependabot-validate-XXXXXXXXXX"
cmds:
- wget --quiet --output-document="{{.SCHEMA_PATH}}" {{.SCHEMA_URL}}
- wget --quiet --output-document="{{.AVA_SCHEMA_PATH}}" {{.AVA_SCHEMA_URL}}
- wget --quiet --output-document="{{.BASE_SCHEMA_PATH}}" {{.BASE_SCHEMA_URL}}
- wget --quiet --output-document="{{.ESLINTRC_SCHEMA_PATH}}" {{.ESLINTRC_SCHEMA_URL}}
- wget --quiet --output-document="{{.JSCPD_SCHEMA_PATH}}" {{.JSCPD_SCHEMA_URL}}
- wget --quiet --output-document="{{.NPM_BADGES_SCHEMA_PATH}}" {{.NPM_BADGES_SCHEMA_URL}}
- wget --quiet --output-document="{{.PARTIAL_ESLINT_PLUGINS_PATH}}" {{.PARTIAL_ESLINT_PLUGINS_SCHEMA_URL}}
- wget --quiet --output-document="{{.PRETTIERRC_SCHEMA_PATH}}" {{.PRETTIERRC_SCHEMA_URL}}
- wget --quiet --output-document="{{.SEMANTIC_RELEASE_SCHEMA_PATH}}" {{.SEMANTIC_RELEASE_SCHEMA_URL}}
- wget --quiet --output-document="{{.STYLELINTRC_SCHEMA_PATH}}" {{.STYLELINTRC_SCHEMA_URL}}
- |
cd "{{.WORKING_FOLDER}}" # Workaround for https://github.com/npm/cli/issues/3210
npx ajv-cli@{{.SCHEMA_DRAFT_4_AJV_CLI_VERSION}} validate \
--all-errors \
-s "{{.SCHEMA_PATH}}" \
-r "{{.AVA_SCHEMA_PATH}}" \
-r "{{.BASE_SCHEMA_PATH}}" \
-r "{{.ESLINTRC_SCHEMA_PATH}}" \
-r "{{.JSCPD_SCHEMA_PATH}}" \
-r "{{.NPM_BADGES_SCHEMA_PATH}}" \
-r "{{.PARTIAL_ESLINT_PLUGINS_PATH}}" \
-r "{{.PRETTIERRC_SCHEMA_PATH}}" \
-r "{{.SEMANTIC_RELEASE_SCHEMA_PATH}}" \
-r "{{.STYLELINTRC_SCHEMA_PATH}}" \
-d "{{.PROJECT_FOLDER}}/{{.INSTANCE_PATH}}"
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/poetry-task/Taskfile.yml
poetry:install-deps:
desc: Install dependencies managed by Poetry
Expand Down Expand Up @@ -465,6 +546,41 @@ tasks:
fi
- shfmt -w "{{.SCRIPT_PATH}}"

# Make a temporary file named according to the passed TEMPLATE variable and print the path passed to stdout
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
utility:mktemp-file:
vars:
RAW_PATH:
sh: mktemp --tmpdir "{{.TEMPLATE}}"
cmds:
- task: utility:normalize-path
vars:
RAW_PATH: "{{.RAW_PATH}}"

# Make a temporary folder named according to the passed TEMPLATE variable and print the path passed to stdout
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
utility:mktemp-folder:
vars:
RAW_PATH:
sh: mktemp --directory --tmpdir "{{.TEMPLATE}}"
cmds:
- task: utility:normalize-path
vars:
RAW_PATH: "{{.RAW_PATH}}"

# Print a normalized version of the path passed via the RAW_PATH variable to stdout
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
utility:normalize-path:
cmds:
- |
if [[ "{{.OS}}" == "Windows_NT" ]] && which cygpath &>/dev/null; then
# Even though the shell handles POSIX format absolute paths as expected, external applications do not.
# So paths passed to such applications must first be converted to Windows format.
cygpath -w "{{.RAW_PATH}}"
else
echo "{{.RAW_PATH}}"
fi
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-mkdocs-task/Taskfile.yml
website:check:
desc: Check whether the MkDocs-based website will build
Expand Down

0 comments on commit 3a1b210

Please sign in to comment.