Skip to content

Commit 81ed7c9

Browse files
authored
feat(extra): support multiple pipelines in git-hooks (#17)
1 parent 5fd92d7 commit 81ed7c9

3 files changed

Lines changed: 98 additions & 39 deletions

File tree

.conform.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ policies:
2121
- style
2222
- test
2323
scopes:
24+
- extra
2425
- github
2526
- gitlab
2627
- job

dev/flake-module.nix

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
};
8080

8181
perSystem =
82-
{ pkgs, ... }:
82+
{ config, pkgs, ... }:
8383
{
8484
pre-commit.check.enable = false;
8585
pre-commit.settings.hooks = {
@@ -117,6 +117,60 @@
117117
};
118118
};
119119

120+
checks.git-hooks =
121+
let
122+
inherit (config.pre-commit.settings) hooks;
123+
gh = hooks.first-ci-kit-gen-github-actions;
124+
gl = hooks.first-ci-kit-gen-gitlab-ci;
125+
126+
tests = {
127+
"github-actions: default pipelines" = {
128+
check = gh.settings.pipelines.default == ".github/workflows/ci.yaml";
129+
};
130+
131+
"gitlab-ci: default pipelines" = {
132+
check = gl.settings.pipelines.default == ".gitlab-ci.yml";
133+
};
134+
135+
"github-actions: hook name" = {
136+
check = gh.name == "generate-github-actions";
137+
};
138+
139+
"gitlab-ci: hook name" = {
140+
check = gl.name == "generate-gitlab-ci";
141+
};
142+
143+
"github-actions: pass_filenames is false" = {
144+
check = !gh.pass_filenames;
145+
};
146+
147+
"gitlab-ci: pass_filenames is false" = {
148+
check = !gl.pass_filenames;
149+
};
150+
};
151+
in
152+
pkgs.runCommand "test-git-hooks" { } (
153+
''
154+
set -e
155+
''
156+
+ lib.concatStrings (
157+
lib.mapAttrsToList (
158+
name:
159+
{ check }:
160+
if check then
161+
""
162+
else
163+
''
164+
echo "FAILED: ${name}"
165+
exit 1
166+
''
167+
) tests
168+
)
169+
+ ''
170+
echo "All git-hooks tests passed" > $out
171+
''
172+
);
173+
120174
packages.module-docs = pkgs.callPackage ../packages/module-docs {
121175
moduleRoot = ../module;
122176
};

extra/git-hooks.nix

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@ let
1212
}:
1313
let
1414
writePipelineGenerator =
15-
{ name, hook }:
15+
{ backend, pipelines }:
1616
pkgs.writeShellApplication {
17-
name = "generate-${name}";
17+
name = "generate-${backend}";
1818
runtimeInputs = [ pkgs.yq-go ];
19-
text = ''
20-
out="$(nix build --extra-experimental-features 'nix-command flakes' \
21-
--print-out-paths \
22-
.#ci-pipeline-${name}-${hook.settings.pipeline}
23-
)"
19+
text =
20+
let
21+
generatePipeline = name: outputPath: ''
22+
out="$(nix build --extra-experimental-features 'nix-command flakes' \
23+
--print-out-paths \
24+
.#ci-pipeline-${backend}-${name}
25+
)"
2426
25-
mkdir -p "$(dirname "${hook.settings.outputPath}")"
26-
yq --prettyPrint --output-format yaml "$out" > "${hook.settings.outputPath}"
27-
'';
27+
mkdir -p "$(dirname "${outputPath}")"
28+
yq --prettyPrint --output-format yaml "$out" > "${outputPath}"
29+
'';
30+
in
31+
lib.concatStringsSep "\n" (lib.mapAttrsToList generatePipeline pipelines);
2832
};
2933
in
3034
{
@@ -34,18 +38,18 @@ let
3438
type = types.submodule {
3539
imports = [ hookModule ];
3640
options.settings = {
37-
pipeline = lib.mkOption {
38-
type = types.str;
39-
description = "The pipeline to generate.";
40-
default = "default";
41-
example = "pr";
42-
};
43-
44-
outputPath = lib.mkOption {
45-
type = types.str;
46-
description = "The path of the output file generated.";
47-
default = ".github/workflows/ci.yaml";
48-
example = ".github/workflows/example.yaml";
41+
pipelines = lib.mkOption {
42+
type = types.attrsOf types.str;
43+
default = {
44+
default = ".github/workflows/ci.yaml";
45+
};
46+
description = "Pipeline name to output path mapping.";
47+
example = lib.literalExpression ''
48+
{
49+
default = ".github/workflows/ci.yaml";
50+
nightly = ".github/workflows/nightly.yaml";
51+
}
52+
'';
4953
};
5054
};
5155
};
@@ -56,18 +60,18 @@ let
5660
type = types.submodule {
5761
imports = [ hookModule ];
5862
options.settings = {
59-
pipeline = lib.mkOption {
60-
type = types.str;
61-
description = "The pipeline to generate.";
62-
default = "default";
63-
example = "pr";
64-
};
65-
66-
outputPath = lib.mkOption {
67-
type = types.str;
68-
description = "The path of the output file generated.";
69-
default = ".gitlab-ci.yml";
70-
example = ".gitlab/ci.yml";
63+
pipelines = lib.mkOption {
64+
type = types.attrsOf types.str;
65+
default = {
66+
default = ".gitlab-ci.yml";
67+
};
68+
description = "Pipeline name to output path mapping.";
69+
example = lib.literalExpression ''
70+
{
71+
default = ".gitlab-ci.yml";
72+
pr = ".gitlab/ci-pr.yml";
73+
}
74+
'';
7175
};
7276
};
7377
};
@@ -79,8 +83,8 @@ let
7983
name = "generate-github-actions";
8084
description = "generate GitHub Actions workflow";
8185
package = writePipelineGenerator {
82-
name = "github-actions";
83-
hook = config.hooks.first-ci-kit-gen-github-actions;
86+
backend = "github-actions";
87+
inherit (config.hooks.first-ci-kit-gen-github-actions.settings) pipelines;
8488
};
8589
entry = "${config.hooks.first-ci-kit-gen-github-actions.package}/bin/generate-github-actions";
8690
files = lib.mkDefault "\\.nix$";
@@ -91,8 +95,8 @@ let
9195
name = "generate-gitlab-ci";
9296
description = "generate GitLab CI pipeline";
9397
package = writePipelineGenerator {
94-
name = "gitlab-ci";
95-
hook = config.hooks.first-ci-kit-gen-gitlab-ci;
98+
backend = "gitlab-ci";
99+
inherit (config.hooks.first-ci-kit-gen-gitlab-ci.settings) pipelines;
96100
};
97101
entry = "${config.hooks.first-ci-kit-gen-gitlab-ci.package}/bin/generate-gitlab-ci";
98102
files = lib.mkDefault "\\.nix$";

0 commit comments

Comments
 (0)