From ed4618284b2a6e1bc878b0bc2fd4aaa5077ca876 Mon Sep 17 00:00:00 2001 From: per1234 Date: Sat, 25 Mar 2023 05:50:46 -0700 Subject: [PATCH] Add infrastructure to lint YAML files A task and GitHub Actions workflow are provided here for linting the project's YAML files. On every push and pull request that affects relevant files, the worflow will run yamllint to check the YAML files of the repository for issues. The .yamllint.yml file is used to configure yamllint: https://yamllint.readthedocs.io/en/stable/configuration.html --- .github/workflows/check-yaml-task.yml | 115 ++++++++++++++++++++++++++ .yamllint.yml | 76 +++++++++++++++++ README.md | 1 + Taskfile.yml | 11 +++ poetry.lock | 35 +++++++- pyproject.toml | 1 + 6 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/check-yaml-task.yml create mode 100644 .yamllint.yml diff --git a/.github/workflows/check-yaml-task.yml b/.github/workflows/check-yaml-task.yml new file mode 100644 index 0000000..6d0dc56 --- /dev/null +++ b/.github/workflows/check-yaml-task.yml @@ -0,0 +1,115 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-yaml-task.md +name: Check YAML + +# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows +on: + create: + push: + paths: + - ".python-version" + - ".yamllint*" + - "poetry.lock" + - "pyproject.toml" + # Source: https://github.com/ikatyang/linguist-languages/blob/master/data/YAML.json (used by Prettier) + - "**/.clang-format" + - "**/.clang-tidy" + - "**/.gemrc" + - "**/glide.lock" + - "**.ya?ml*" + - "**.mir" + - "**.reek" + - "**.rviz" + - "**.sublime-syntax" + - "**.syntax" + pull_request: + paths: + - ".python-version" + - ".yamllint*" + - "poetry.lock" + - "pyproject.toml" + # Source: https://github.com/ikatyang/linguist-languages/blob/master/data/YAML.json (used by Prettier) + - "**/.clang-format" + - "**/.clang-tidy" + - "**/.gemrc" + - "**/glide.lock" + - "**.ya?ml*" + - "**.mir" + - "**.reek" + - "**.rviz" + - "**.sublime-syntax" + - "**.syntax" + schedule: + # Run periodically to catch breakage caused by external changes. + - cron: "0 9 * * WED" + workflow_dispatch: + repository_dispatch: + +jobs: + run-determination: + runs-on: ubuntu-latest + 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 + + check: + name: ${{ matrix.configuration.name }} + needs: run-determination + if: needs.run-determination.outputs.result == 'true' + runs-on: ubuntu-latest + + strategy: + fail-fast: false + + matrix: + configuration: + - name: Generate problem matcher output + # yamllint's "github" output type produces annotated diffs, but is not useful to humans reading the log. + format: github + # The other matrix job is used to set the result, so this job is configured to always pass. + continue-on-error: true + - name: Check formatting + # yamllint's "colored" output type is most suitable for humans reading the log. + format: colored + continue-on-error: false + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Install Python + uses: actions/setup-python@v4 + with: + python-version-file: .python-version + + - name: Install Poetry + run: | + pipx install \ + --python "$(which python)" \ + poetry + + - name: Install Task + uses: arduino/setup-task@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + version: 3.x + + - name: Check YAML + continue-on-error: ${{ matrix.configuration.continue-on-error }} + run: task yaml:lint YAMLLINT_FORMAT=${{ matrix.configuration.format }} diff --git a/.yamllint.yml b/.yamllint.yml new file mode 100644 index 0000000..67630dc --- /dev/null +++ b/.yamllint.yml @@ -0,0 +1,76 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-yaml/.yamllint.yml +# See: https://yamllint.readthedocs.io/en/stable/configuration.html +# The code style defined in this file is the official standardized style to be used in all Arduino tooling projects and +# should not be modified. +# Note: Rules disabled solely because they are redundant to Prettier are marked with a "Prettier" comment. + +rules: + braces: + level: error + forbid: non-empty + min-spaces-inside: -1 # Prettier + max-spaces-inside: -1 # Prettier + min-spaces-inside-empty: -1 # Prettier + max-spaces-inside-empty: -1 # Prettier + brackets: + level: error + forbid: non-empty + min-spaces-inside: -1 # Prettier + max-spaces-inside: -1 # Prettier + min-spaces-inside-empty: -1 # Prettier + max-spaces-inside-empty: -1 # Prettier + colons: disable # Prettier + commas: disable # Prettier + comments: disable # Prettier + comments-indentation: disable # Prettier + document-end: disable # Prettier + document-start: disable + empty-lines: disable # Prettier + empty-values: disable + hyphens: disable # Prettier + indentation: disable # Prettier + key-duplicates: disable # Prettier + key-ordering: disable + line-length: + level: warning + max: 120 + allow-non-breakable-words: true + allow-non-breakable-inline-mappings: true + new-line-at-end-of-file: disable # Prettier + new-lines: disable # Prettier + octal-values: + level: warning + forbid-implicit-octal: true + forbid-explicit-octal: false + quoted-strings: disable + trailing-spaces: disable # Prettier + truthy: + level: error + allowed-values: + - "true" + - "false" + - "on" # Used by GitHub Actions as a workflow key. + check-keys: true + +yaml-files: + # Source: https://github.com/ikatyang/linguist-languages/blob/master/data/YAML.json (used by Prettier) + - ".clang-format" + - ".clang-tidy" + - ".gemrc" + - ".yamllint" + - "glide.lock" + - "*.yml" + - "*.mir" + - "*.reek" + - "*.rviz" + - "*.sublime-syntax" + - "*.syntax" + - "*.yaml" + - "*.yaml-tmlanguage" + - "*.yaml.sed" + - "*.yml.mysql" + +ignore: | + /.git/ + __pycache__/ + node_modules/ diff --git a/README.md b/README.md index 4000c7c..c91cfa9 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ [![Check Taskfiles status](https://github.com/arduino/compile-sketches/actions/workflows/check-taskfiles.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/check-taskfiles.yml) [![Check ToC status](https://github.com/arduino/compile-sketches/actions/workflows/check-toc-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/check-toc-task.yml) [![Check Workflows status](https://github.com/arduino/compile-sketches/actions/workflows/check-workflows-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/check-workflows-task.yml) +[![Check YAML status](https://github.com/arduino/compile-sketches/actions/workflows/check-yaml-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/check-yaml-task.yml) [![Spell Check status](https://github.com/arduino/compile-sketches/actions/workflows/spell-check-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/spell-check-task.yml) [![Sync Labels status](https://github.com/arduino/compile-sketches/actions/workflows/sync-labels-npm.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/sync-labels-npm.yml) [![Test Python status](https://github.com/arduino/compile-sketches/actions/workflows/test-python-poetry-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/test-python-poetry-task.yml) diff --git a/Taskfile.yml b/Taskfile.yml index 922c172..3ee780b 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -19,6 +19,7 @@ tasks: - task: npm:validate - task: python:lint - task: python:test + - task: yaml:lint fix: desc: Make automated corrections to the project's files @@ -474,3 +475,13 @@ tasks: else echo "{{.RAW_PATH}}" fi + + # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-yaml-task/Taskfile.yml + yaml:lint: + desc: Check for problems with YAML files + deps: + - task: poetry:install-deps + vars: + POETRY_GROUPS: dev + cmds: + - poetry run yamllint --format {{default "colored" .YAMLLINT_FORMAT}} . diff --git a/poetry.lock b/poetry.lock index bbb6eb2..c6577dd 100644 --- a/poetry.lock +++ b/poetry.lock @@ -817,6 +817,23 @@ files = [ {file = "semver-2.13.0.tar.gz", hash = "sha256:fa0fe2722ee1c3f57eac478820c3a5ae2f624af8264cbdf9000c980ff7f75e3f"}, ] +[[package]] +name = "setuptools" +version = "67.6.0" +description = "Easily download, build, install, upgrade, and uninstall Python packages" +category = "dev" +optional = false +python-versions = ">=3.7" +files = [ + {file = "setuptools-67.6.0-py3-none-any.whl", hash = "sha256:b78aaa36f6b90a074c1fa651168723acbf45d14cb1196b6f02c0fd07f17623b2"}, + {file = "setuptools-67.6.0.tar.gz", hash = "sha256:2ee892cd5f29f3373097f5a814697e397cf3ce313616df0af11231e2ad118077"}, +] + +[package.extras] +docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "pygments-github-lexers (==0.0.5)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-favicon", "sphinx-hoverxref (<2)", "sphinx-inline-tabs", "sphinx-lint", "sphinx-notfound-page (==0.8.3)", "sphinx-reredirects", "sphinxcontrib-towncrier"] +testing = ["build[virtualenv]", "filelock (>=3.4.0)", "flake8 (<5)", "flake8-2020", "ini2toml[lite] (>=0.9)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pip (>=19.1)", "pip-run (>=8.8)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)", "pytest-perf", "pytest-timeout", "pytest-xdist", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel"] +testing-integration = ["build[virtualenv]", "filelock (>=3.4.0)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pytest", "pytest-enabler", "pytest-xdist", "tomli", "virtualenv (>=13.0.0)", "wheel"] + [[package]] name = "smmap" version = "5.0.0" @@ -931,7 +948,23 @@ files = [ {file = "wrapt-1.15.0.tar.gz", hash = "sha256:d06730c6aed78cee4126234cf2d071e01b44b915e725a6cb439a879ec9754a3a"}, ] +[[package]] +name = "yamllint" +version = "1.30.0" +description = "A linter for YAML files." +category = "dev" +optional = false +python-versions = ">=3.7" +files = [ + {file = "yamllint-1.30.0.tar.gz", hash = "sha256:4f58f323aedda16189a489d183ecc25c66d7a9cc0fe88f61b650fef167b13190"}, +] + +[package.dependencies] +pathspec = ">=0.5.3" +pyyaml = "*" +setuptools = "*" + [metadata] lock-version = "2.0" python-versions = "3.11.2" -content-hash = "90bcf233ee68f28be781628ee2739f788d357c921b75a07c5a71cec0359b3490" +content-hash = "f6d4dd084ff2e5f98adb604b0fd1d6ffeb5b7977666a8a2adecdc5540c4e0e78" diff --git a/pyproject.toml b/pyproject.toml index dd24ab2..21b1a7b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ pytest = "7.2.2" pytest-mock = "3.10.0" flake8 = "6.0.0" pep8-naming = "0.13.3" +yamllint = "1.30.0" [tool.poetry.group.external] # Provided only for use by boards platforms