Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions checks/git-hooks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ let
inherit (flakeboxLib.config.git.pre-commit.hooks) shellcheck;
};

yamlLintConfig =
(mkLib pkgs {
config = {
shellcheck.enable = false;
yamllint.enable = true;
};
}).config;
actionLintConfig =
(mkLib pkgs {
config = {
shellcheck.enable = false;
actionlint.enable = true;
};
}).config;
yamlLintHooks = mkHookFixture { inherit (yamlLintConfig.git.pre-commit.hooks) yamllint; };
actionLintHooks = mkHookFixture { inherit (actionLintConfig.git.pre-commit.hooks) actionlint; };

newlineLib = mkLib pkgs {
config.git.pre-commit = {
trailing_newline = true;
Expand Down Expand Up @@ -184,6 +201,15 @@ let
emptyHooks = mkHookFixture { };
in
assert pkgs.lib.elem timer flakeboxLib.config.env.shellPackages;
assert !(flakeboxLib.config.git.pre-commit.hooks ? yamllint);
assert !(flakeboxLib.config.git.pre-commit.hooks ? actionlint);
assert !(pkgs.lib.elem pkgs.yamllint flakeboxLib.config.env.shellPackages);
assert !(pkgs.lib.elem pkgs.actionlint flakeboxLib.config.env.shellPackages);
assert !(yamlLintConfig.git.pre-commit.hooks ? actionlint);
assert !(actionLintConfig.git.pre-commit.hooks ? yamllint);
assert pkgs.lib.elem pkgs.yamllint yamlLintConfig.env.shellPackages;
assert pkgs.lib.elem pkgs.actionlint actionLintConfig.env.shellPackages;
assert pkgs.lib.elem pkgs.shellcheck actionLintConfig.env.shellPackages;
assert flakeboxLib.config.git.enable;
assert gitDisabledLib.config.git.pre-commit.enable;
assert gitDisabledLib.config.git.commit-msg.enable;
Expand Down Expand Up @@ -1209,6 +1235,86 @@ pkgs.runCommand "git-hooks-tests"
[ "$(grep -c '^invocation$' "$FLAKEBOX_SHELLCHECK_LOG")" -eq 2 ]
cd ..

# Exercise the real tools: malformed tracked input fails, but formatting,
# ignored paths, and empty selections do not.
(
export PATH="${
pkgs.lib.makeBinPath [
pkgs.yamllint
pkgs.actionlint
pkgs.shellcheck
]
}:$PATH"
mkdir yaml-repo
cd yaml-repo
git init -q
mkdir -p .github/workflows/nested
run_yamllint() {
NO_STASH=1 bash ${yamlLintHooks}/misc/git-hooks/pre-commit
}
run_actionlint() {
NO_STASH=1 bash ${actionLintHooks}/misc/git-hooks/pre-commit
}

printf 'broken: [\n' > .github/workflows/untracked.yml
ln -s untracked.yml .github/workflows/symlink.yml
git add .github/workflows/symlink.yml
run_yamllint
run_actionlint

yaml_path=$'-odd file[0]\n.yaml'
printf 'key: value \n' > "$yaml_path"
git add -- "$yaml_path"
run_yamllint

printf 'key: value\nkey: duplicate\n' > "$yaml_path"
if run_yamllint > duplicate.log 2>&1; then
echo 'yamllint accepted a duplicate key' >&2
exit 1
fi
grep -F 'key-duplicates' duplicate.log
FLAKEBOX_GIT_LS_IGNORE='^-odd' run_yamllint
FLAKEBOX_GIT_LS_TEXT_IGNORE='^-odd' run_yamllint

printf 'broken: [\n' > "$yaml_path"
if run_yamllint > syntax.log 2>&1; then
echo 'yamllint accepted invalid YAML' >&2
exit 1
fi
grep -F 'syntax' syntax.log

workflow_path=$'.github/workflows/odd file[0]\n.yml'
printf 'not a workflow\n' > .github/workflows/nested/not-a-workflow.yaml
cat > "$workflow_path" <<'EOF'
on: push
jobs:
check:
runs-on: ubuntu-latest
steps:
- run: echo ok
EOF
git add -- "$workflow_path" .github/workflows/nested/not-a-workflow.yaml
run_actionlint

sed -i 's/ubuntu-latest/unknown-runner/' "$workflow_path"
if run_actionlint > workflow.log 2>&1; then
echo 'actionlint accepted an unknown runner label' >&2
exit 1
fi
grep -F 'label "unknown-runner" is unknown' workflow.log
FLAKEBOX_GIT_LS_IGNORE='^\.github/workflows/odd' run_actionlint
FLAKEBOX_GIT_LS_TEXT_IGNORE='^\.github/workflows/odd' run_actionlint

printf 'self-hosted-runner: {labels: [unknown-runner]}\n' > .github/actionlint.yaml
run_actionlint
sed -i 's/echo ok/echo $UNQUOTED/' "$workflow_path"
if run_actionlint > inline-shell.log 2>&1; then
echo 'actionlint did not run ShellCheck on the inline script' >&2
exit 1
fi
grep -F 'SC2086' inline-shell.log
)

# The newline hook sees all tracked files and uses raw path records, not
# Git's quoted display or shell word splitting.
mkdir newline-repo
Expand Down
23 changes: 23 additions & 0 deletions docs/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ Customize the generated `.cargo-crap.toml` by setting
with `cargo-crap.enable = false`.


## Check YAML and GitHub Actions workflows

Enable either or both checks in your Flakebox configuration:

```nix
yamllint.enable = true;
actionlint.enable = true;
```

- `yamllint` checks tracked `.yaml` and `.yml` files for syntax and duplicate
keys only. It does not apply formatting rules or load a project's
`yamllint` configuration.
- `actionlint` checks tracked `.yaml` and `.yml` files directly under
`.github/workflows/`. It uses ShellCheck for inline shell scripts even if
Flakebox's standalone `shellcheck` integration is disabled.

Both checks are off by default. They use the pre-commit hook's tracked text
file inventory, respect `FLAKEBOX_GIT_LS_IGNORE` and
`FLAKEBOX_GIT_LS_TEXT_IGNORE`, and skip symbolic links and files Git doesn't
track. Run `flakebox install` to regenerate the hook, then run
`bash misc/git-hooks/pre-commit` to check files manually.


## Enforce structural rules with ast-grep

Flakebox installs [ast-grep](https://ast-grep.github.io/) and runs
Expand Down
40 changes: 40 additions & 0 deletions lib/modules/actionlint.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
lib,
config,
pkgs,
...
}:
{
options.actionlint.enable = lib.mkEnableOption "GitHub Actions workflow checks, including ShellCheck for inline shell scripts, in the pre-commit hook";

config = lib.mkIf config.actionlint.enable {
env.shellPackages = [
pkgs.actionlint
pkgs.shellcheck
];

git.pre-commit.hooks.actionlint = ''
local workflows=() path shellcheck_path
while LC_ALL=C IFS= read -r -d "" path; do
case "$path" in
.github/workflows/*/*) ;;
.github/workflows/*.yaml | .github/workflows/*.yml)
if [[ -f "$path" ]]; then
workflows+=("$path")
fi
;;
esac
done < "$FLAKEBOX_GIT_LS_TEXT_FILE"

if (( ''${#workflows[@]} == 0 )); then
return 0
fi
shellcheck_path="$(command -v shellcheck)" || {
echo "flakebox: actionlint requires shellcheck on PATH" >&2
return 1
}
printf '%s\0' "''${workflows[@]}" |
xargs -0 actionlint -shellcheck "$shellcheck_path" -pyflakes= --
'';
};
}
23 changes: 23 additions & 0 deletions lib/modules/yamllint.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
lib,
config,
pkgs,
...
}:
{
options.yamllint.enable = lib.mkEnableOption "YAML syntax and duplicate-key checks for tracked YAML files in the pre-commit hook";

config = lib.mkIf config.yamllint.enable {
env.shellPackages = [ pkgs.yamllint ];

git.pre-commit.hooks.yamllint = ''
local path
while LC_ALL=C IFS= read -r -d "" path; do
if [[ "$path" == *.yaml || "$path" == *.yml ]] && [[ -f "$path" ]]; then
printf '%s\0' "$path"
fi
done < "$FLAKEBOX_GIT_LS_TEXT_FILE" |
xargs -0 -r yamllint -d '{rules: {key-duplicates: enable}}' --
'';
};
}
Loading