-
Notifications
You must be signed in to change notification settings - Fork 11
Add a hook to run shellcheck on all "runs" steps in melange pipelines #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f77be7f
Add a hook to run shellcheck on all "runs" steps in melange pipelines
dannf 80f9123
Rename .pre-commit-config.yaml -> example.pre-commit-config.yaml
dannf c44e2d2
Add pre-commit config for this repo
dannf 4e9e18f
pre-commit: Use ruff
dannf 9e4e950
pre-commit: Add additional python hooks
dannf 21ae0b9
pre-commit: Add add-trailing-comma hook
dannf 4c67b09
pre-commit: Add mypy hook
dannf a8f19d0
shellcheck_run_steps: Add --shellcheck parameter
dannf 584d6fd
shellcheck_run_steps: default to using shellcheck docker image
dannf d449bb0
shellcheck_run_steps: just call shellcheck once per yaml
dannf 43bce5e
shellcheck_run_steps: Add typing info for pipelines var
dannf bbcdb5a
shellcheck_run_steps: prefix tmpfile with main package name
dannf bf10ac6
shellcheck_run_steps: use --foo=bar syntax for melange params
dannf e3d6bed
github: run pre-commit check
dannf 48fd522
setup.py: ruff
dannf d9ade74
More verbosely define stages
AmberArcadia 54a94fa
Add shellcheck-run-steps to example config
AmberArcadia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| name: Lint | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: ['main'] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| lint: | ||
| name: Lint | ||
| runs-on: ubuntu-latest | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # ratchet:actions/checkout@v4.2.2 | ||
| - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0 | ||
| - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # In general, this file (when sourced from https://github.com/chainguard-dev/pre-commit-hooks) | ||
| # should be able to be used as-is in other repos such as wolfi-dev/os, enterprise-packages, and extra-packages, | ||
| # and can be assumed to be the "source of truth" for our pre-commit rules. | ||
| # See https://eng.inky.wtf/docs/technical/git/pre-commit for info on how to edit this file. | ||
| repos: | ||
| - repo: https://github.com/chainguard-dev/pre-commit-hooks | ||
| rev: e4f3bba353cc583ce73f660dcf217e245fd681d3 | ||
| hooks: | ||
| - id: check-for-epoch-bump | ||
| - id: shellcheck-run-steps | ||
| - repo: https://github.com/chainguard-dev/yam | ||
| rev: 498642e77997ba79709f43a7ee2c84b12b2145bb # v0.2.12 | ||
| hooks: | ||
| - id: yam | ||
| # TODO: Swap with CG repo: - repo: https://github.com/wolfi-dev/wolfictl | ||
| - repo: https://github.com/amberarcadia/wolfictl | ||
| rev: 85b1301c4d17fcd0c8f0ce455724941cae815d68 | ||
| hooks: | ||
| - id: wolfictl-lint | ||
| - repo: https://github.com/golangci/misspell | ||
| rev: 528d713e620bdf4b41849db93cb489c4fef9f5c5 # v0.6.0 | ||
| hooks: | ||
| - id: misspell | ||
| - repo: https://github.com/pre-commit/pre-commit-hooks | ||
| rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # v5.0.0 | ||
| hooks: | ||
| - id: check-yaml | ||
| - id: forbid-submodules | ||
| - id: check-added-large-files | ||
| - id: check-case-conflict | ||
| - id: check-merge-conflict | ||
| - id: check-symlinks | ||
| - id: detect-private-key | ||
| exclude: ^ruby-3\.0/0001-ruby-3\.0\.6-openssl-patch\.patch$ |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import contextlib | ||
| import os | ||
| import subprocess | ||
| import tempfile | ||
| from collections.abc import Generator | ||
| from collections.abc import Mapping | ||
| from collections.abc import Sequence | ||
| from typing import Any | ||
| from typing import NamedTuple | ||
|
|
||
| import ruamel.yaml | ||
|
|
||
| yaml = ruamel.yaml.YAML(typ="safe") | ||
|
|
||
|
|
||
| def _exhaust(gen: Generator[str]) -> None: | ||
| for _ in gen: | ||
| pass | ||
|
|
||
|
|
||
| def _parse_unsafe(*args: Any, **kwargs: Any) -> None: | ||
| _exhaust(yaml.parse(*args, **kwargs)) | ||
|
|
||
|
|
||
| def _load_all(*args: Any, **kwargs: Any) -> None: | ||
| _exhaust(yaml.load_all(*args, **kwargs)) | ||
|
|
||
|
|
||
| class Key(NamedTuple): | ||
| multi: bool | ||
| unsafe: bool | ||
|
|
||
|
|
||
| def do_shellcheck( | ||
| melange_cfg: Mapping[str, Any], | ||
| shellcheck: list[str], | ||
| ) -> None: | ||
| if melange_cfg == {}: | ||
| return | ||
|
|
||
| pkgs = [melange_cfg] | ||
| pkgs.extend(melange_cfg.get("subpackages", [])) | ||
| pipelines: list[Mapping[str, Any]] = [] | ||
| for pkg in pkgs: | ||
| pipelines.extend(pkg.get("pipeline", [])) | ||
| if "test" in pkg.keys(): | ||
| test_pipeline = pkg["test"].get("pipeline", []) | ||
| pipelines.extend(test_pipeline) | ||
| name = melange_cfg["package"]["name"] | ||
| all_run_files = [] | ||
| with contextlib.ExitStack() as stack: | ||
| for step in pipelines: | ||
| if "runs" not in step.keys(): | ||
| continue | ||
| all_run_files.extend( | ||
| [ | ||
| stack.enter_context( | ||
| tempfile.NamedTemporaryFile( | ||
| mode="w", | ||
| prefix=name, | ||
| dir=os.getcwd(), | ||
| delete_on_close=False, | ||
| ), | ||
| ), | ||
| ], | ||
| ) | ||
| for shfile in all_run_files: | ||
| shfile.write(step["runs"]) | ||
| shfile.close() | ||
| subprocess.check_call( | ||
| ["/usr/bin/shellcheck"] | ||
| + ["--shell=busybox", "--"] | ||
| + [os.path.basename(f.name) for f in all_run_files], | ||
| cwd=os.getcwd(), | ||
| ) | ||
|
|
||
|
|
||
| def main(argv: Sequence[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("filenames", nargs="*", help="Filenames to check.") | ||
| parser.add_argument( | ||
| "--shellcheck", | ||
| default=[ | ||
| "docker", | ||
| "run", | ||
| f"--volume={os.getcwd()}:/mnt", | ||
| "--rm", | ||
| "-it", | ||
| "koalaman/shellcheck:latest", | ||
| ], | ||
| nargs="*", | ||
| help="shellcheck command", | ||
| ) | ||
| args = parser.parse_args(argv) | ||
|
|
||
| melange_cfg = {} | ||
| for filename in args.filenames: | ||
| with tempfile.NamedTemporaryFile( | ||
| "w", | ||
| delete_on_close=False, | ||
| ) as compiled_out: | ||
| subprocess.check_call( | ||
| [ | ||
| "melange", | ||
| "compile", | ||
| "--arch=x86_64", | ||
| "--pipeline-dir=./pipelines", | ||
| filename, | ||
| ], | ||
| stdout=compiled_out, | ||
| ) | ||
| compiled_out.close() | ||
| try: | ||
| with open(compiled_out.name) as compiled_in: | ||
| melange_cfg = yaml.load(compiled_in) | ||
| do_shellcheck(melange_cfg, args.shellcheck) | ||
| except ruamel.yaml.YAMLError as exc: | ||
| print(exc) | ||
| return 1 | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
|
dannf marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| line-length = 80 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| [metadata] | ||
| name = pre_commit_hooks | ||
| version = 0.0.1 | ||
| description = chainguard hooks for pre-commit | ||
| long_description = file: README.md | ||
| long_description_content_type = text/markdown | ||
| url = https://github.com/chainguard-dev/pre-commit-hooks | ||
| license = MIT | ||
| license_files = LICENSE | ||
| classifiers = | ||
| Programming Language :: Python :: 3 | ||
| Programming Language :: Python :: 3 :: Only | ||
| Programming Language :: Python :: Implementation :: CPython | ||
| Programming Language :: Python :: Implementation :: PyPy | ||
|
|
||
| [options] | ||
| packages = find: | ||
| install_requires = | ||
| ruamel.yaml>=0.15 | ||
| python_requires = >=3.9 | ||
|
|
||
| [options.entry_points] | ||
| console_scripts = | ||
| shellcheck-run-steps = pre_commit_hooks.shellcheck_run_steps:main | ||
|
|
||
| [bdist_wheel] | ||
| universal = True | ||
|
|
||
| [coverage:run] | ||
| plugins = covdefaults | ||
|
|
||
| [mypy] | ||
| check_untyped_defs = true | ||
| disallow_any_generics = true | ||
| disallow_incomplete_defs = true | ||
| disallow_untyped_defs = true | ||
| warn_redundant_casts = true | ||
| warn_unused_ignores = true | ||
|
|
||
| [mypy-testing.*] | ||
| disallow_untyped_defs = false | ||
|
|
||
| [mypy-tests.*] | ||
| disallow_untyped_defs = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from setuptools import setup | ||
|
|
||
| setup() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.