Skip to content

Commit 33a41a6

Browse files
committed
Ensure impure hooks still run in CI
1 parent d58033c commit 33a41a6

File tree

6 files changed

+56
-36
lines changed

6 files changed

+56
-36
lines changed

.last-exported-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Last exported commit from parent repo: e283eef4787456402415cacd138093dbb080f812
1+
Last exported commit from parent repo: 071219753e1fc8d28d4731d6f0588802ff0d7a3b

src/Bootstrap.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ makeBuildPlan MakeBuildPlanArgs {..} = do
506506
)
507507
~: nixShellCompatFor mbpRunConfig
508508
~: nixPreCommitHookConfig
509-
~: gitlabCIConfigFor mbpContinuousIntegrationConfig mbpRunConfig mbpProjectType mbpPreCommitHooksConfig
509+
~: gitlabCIConfigFor mbpContinuousIntegrationConfig mbpRunConfig mbpProjectType nixPreCommitHookConfig
510510
~: devContainerDockerComposeFor mbpDevContainerConfig mbpProjectName
511511
~: devContainerDockerfileFor mbpDevContainerConfig
512512
~: devContainerJsonFor mbpDevContainerConfig mbpProjectName mbpProjectType

src/Bootstrap/Data/Bootstrappable/GitlabCIConfig.hs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import Bootstrap.Data.Bootstrappable
1313
bootstrapReason
1414
),
1515
)
16+
import Bootstrap.Data.Bootstrappable.NixPreCommitHookConfig
17+
( NixPreCommitHookConfig (nixPreCommitHookConfigImpureHookCommand),
18+
)
1619
import Bootstrap.Data.ContinuousIntegration
1720
( ContinuousIntegrationConfig (ContinuousIntegrationConfig),
1821
)
19-
import Bootstrap.Data.PreCommitHook
20-
( PreCommitHooksConfig (unPreCommitHooksConfig),
21-
)
2222
import Bootstrap.Data.ProjectType
2323
( ElmMode (ElmModeBare, ElmModeNode),
2424
ElmOptions (ElmOptions, elmOptionElmMode),
@@ -32,7 +32,7 @@ import Bootstrap.Data.ProjectType
3232
data GitlabCIConfig = GitlabCIConfig
3333
{ gitlabCIConfigUseFlakes :: Bool,
3434
gitlabCIConfigProjectType :: ProjectType,
35-
gitlabCIConfigPreCommitHooksConfig :: PreCommitHooksConfig
35+
gitlabCIConfigPreCommitHooksConfig :: Maybe NixPreCommitHookConfig
3636
}
3737

3838
instance Bootstrappable GitlabCIConfig where
@@ -49,12 +49,18 @@ instance Bootstrappable GitlabCIConfig where
4949
" - nix-env -iA nixpkgs.bash nixpkgs.openssh",
5050
" - echo \"experimental-features = nix-command flakes\" >> /etc/nix/nix.conf",
5151
"",
52-
"check-dev-environment:",
52+
case nixPreCommitHookConfigImpureHookCommand <$> gitlabCIConfigPreCommitHooksConfig of
53+
Just c | c /= "echo ok" -> "check-dev-environment-and-run-impure-hooks:"
54+
_ -> "check-dev-environment:",
5355
" stage: build",
5456
" script:",
55-
commandInShell "echo ok"
57+
commandInShell $
58+
maybe
59+
"echo ok"
60+
nixPreCommitHookConfigImpureHookCommand
61+
gitlabCIConfigPreCommitHooksConfig
5662
]
57-
<> ( if unPreCommitHooksConfig gitlabCIConfigPreCommitHooksConfig
63+
<> ( if isJust gitlabCIConfigPreCommitHooksConfig
5864
then
5965
[ "",
6066
"pre-commit-check:",
@@ -123,7 +129,7 @@ gitlabCIConfigFor ::
123129
ContinuousIntegrationConfig ->
124130
RunConfig ->
125131
ProjectType ->
126-
PreCommitHooksConfig ->
132+
Maybe NixPreCommitHookConfig ->
127133
Maybe GitlabCIConfig
128134
gitlabCIConfigFor (ContinuousIntegrationConfig False) _ _ _ = Nothing
129135
gitlabCIConfigFor (ContinuousIntegrationConfig True) RunConfig {rcUseFlakes} t p =

src/Bootstrap/Data/Bootstrappable/NixPreCommitHookConfig.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
-- | Copyright : (c) Crown Copyright GCHQ
55
module Bootstrap.Data.Bootstrappable.NixPreCommitHookConfig
6-
( NixPreCommitHookConfig (nixPreCommitHookConfigRequiresNixpkgs),
6+
( NixPreCommitHookConfig
7+
( nixPreCommitHookConfigRequiresNixpkgs,
8+
nixPreCommitHookConfigImpureHookCommand
9+
),
710
PreCommitHook,
811
nixPreCommitHookConfigFor,
912
)
@@ -52,7 +55,9 @@ import Relude.Extra.Tuple (dup)
5255
data NixPreCommitHookConfig = NixPreCommitHookConfig
5356
{ nixPreCommitHookConfigHooks :: [PreCommitHook],
5457
nixPreCommitHookConfigRequiresNixpkgs :: Bool,
55-
nixPreCommitHookConfigUsingFlakeLib :: Bool
58+
nixPreCommitHookConfigUsingFlakeLib :: Bool,
59+
-- | A command used to run any impure hooks manually, defaults to "echo ok"
60+
nixPreCommitHookConfigImpureHookCommand :: Text
5661
}
5762

5863
instance Bootstrappable NixPreCommitHookConfig where
@@ -146,6 +151,12 @@ nixPreCommitHookConfigFor RunConfig {rcUseFlakes} projectType =
146151
nixPreCommitHookConfigRequiresNixpkgs =
147152
any preCommitHookToolIsFromNixpkgs (tool <$> nixPreCommitHookConfigHooks)
148153
|| hpack `elem` nixPreCommitHookConfigHooks
154+
nixPreCommitHookConfigImpureHookCommand =
155+
if not (all preCommitHookIsPure nixPreCommitHookConfigHooks)
156+
then case projectType of
157+
Elm _ -> "elm-review"
158+
_ -> "echo ok"
159+
else "echo ok"
149160
in NixPreCommitHookConfig {nixPreCommitHookConfigUsingFlakeLib = rcUseFlakes, ..}
150161

151162
data PreCommitHook = PreCommitHook

test/Bootstrap/Data/Bootstrappable/GitlabCIConfigSpec.hs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ module Bootstrap.Data.Bootstrappable.GitlabCIConfigSpec (spec) where
55

66
import Bootstrap.Data.Bootstrappable (Bootstrappable (bootstrapContent))
77
import Bootstrap.Data.Bootstrappable.GitlabCIConfig (gitlabCIConfigFor)
8+
import Bootstrap.Data.Bootstrappable.NixPreCommitHookConfig
9+
( nixPreCommitHookConfigFor,
10+
)
811
import Bootstrap.Data.ContinuousIntegration
912
( ContinuousIntegrationConfig (ContinuousIntegrationConfig),
1013
)
11-
import Bootstrap.Data.PreCommitHook (PreCommitHooksConfig (PreCommitHooksConfig))
1214
import Bootstrap.Data.ProjectType
1315
( ElmMode (ElmModeBare, ElmModeNode),
1416
ElmOptions (ElmOptions),
@@ -26,8 +28,7 @@ spec :: Spec
2628
spec = describe "gitlab-ci.yml rendering" do
2729
let ciConfig = ContinuousIntegrationConfig True
2830
it "renders an Elm/Parcel gitlab-ci config without pre-commit checks correctly" do
29-
let preCommitHooksConfig = PreCommitHooksConfig False
30-
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Elm $ ElmOptions (ElmModeNode PNPm) False) preCommitHooksConfig)
31+
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Elm $ ElmOptions (ElmModeNode PNPm) False) Nothing)
3132
>>= ( `shouldBe`
3233
Right
3334
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
@@ -52,8 +53,7 @@ build-site:
5253
|]
5354
)
5455
it "renders an Elm/Parcel gitlab-ci config without pre-commit or flakes checks correctly" do
55-
let preCommitHooksConfig = PreCommitHooksConfig False
56-
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Elm $ ElmOptions (ElmModeNode PNPm) False) preCommitHooksConfig)
56+
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Elm $ ElmOptions (ElmModeNode PNPm) False) Nothing)
5757
>>= ( `shouldBe`
5858
Right
5959
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
@@ -78,8 +78,9 @@ build-site:
7878
|]
7979
)
8080
it "renders a bare Elm gitlab-ci config with pre-commit checks correctly" do
81-
let preCommitHooksConfig = PreCommitHooksConfig True
82-
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Elm $ ElmOptions ElmModeBare True) preCommitHooksConfig)
81+
let projectType = Elm $ ElmOptions ElmModeBare True
82+
nixPreCommitHookConfig = Just $ nixPreCommitHookConfigFor rcDefault projectType
83+
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault projectType nixPreCommitHookConfig)
8384
>>= ( `shouldBe`
8485
Right
8586
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
@@ -91,10 +92,10 @@ default:
9192
- nix-env -iA nixpkgs.bash nixpkgs.openssh
9293
- echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf
9394

94-
check-dev-environment:
95+
check-dev-environment-and-run-impure-hooks:
9596
stage: build
9697
script:
97-
- nix-shell --run 'echo ok'
98+
- nix-shell --run 'elm-review'
9899

99100
pre-commit-check:
100101
stage: build
@@ -107,8 +108,9 @@ build-site:
107108
|]
108109
)
109110
it "renders a bare Elm gitlab-ci config with pre-commit checks and flakes correctly" do
110-
let preCommitHooksConfig = PreCommitHooksConfig True
111-
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Elm $ ElmOptions ElmModeBare True) preCommitHooksConfig)
111+
let projectType = Elm $ ElmOptions ElmModeBare True
112+
nixPreCommitHookConfig = Just $ nixPreCommitHookConfigFor rcDefault projectType
113+
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes projectType nixPreCommitHookConfig)
112114
>>= ( `shouldBe`
113115
Right
114116
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
@@ -120,10 +122,10 @@ default:
120122
- nix-env -iA nixpkgs.bash nixpkgs.openssh
121123
- echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf
122124

123-
check-dev-environment:
125+
check-dev-environment-and-run-impure-hooks:
124126
stage: build
125127
script:
126-
- nix develop -c echo ok
128+
- nix develop -c elm-review
127129

128130
pre-commit-check:
129131
stage: build
@@ -136,8 +138,7 @@ build-site:
136138
|]
137139
)
138140
it "renders a Go gitlab-ci with a flake build and without pre-commit checks correctly" do
139-
let preCommitHooksConfig = PreCommitHooksConfig False
140-
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Go $ SetUpGoBuild True) preCommitHooksConfig)
141+
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Go $ SetUpGoBuild True) Nothing)
141142
>>= ( `shouldBe`
142143
Right
143144
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
@@ -160,8 +161,9 @@ build:
160161
|]
161162
)
162163
it "renders a Go gitlab-ci with a flake build and with pre-commit checks correctly" do
163-
let preCommitHooksConfig = PreCommitHooksConfig True
164-
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Go $ SetUpGoBuild True) preCommitHooksConfig)
164+
let projectType = Go $ SetUpGoBuild True
165+
nixPreCommitHookConfig = Just $ nixPreCommitHookConfigFor rcDefault projectType
166+
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes projectType nixPreCommitHookConfig)
165167
>>= ( `shouldBe`
166168
Right
167169
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
@@ -188,8 +190,7 @@ build:
188190
|]
189191
)
190192
it "renders a Go gitlab-ci with a build and without pre-commit checks correctly" do
191-
let preCommitHooksConfig = PreCommitHooksConfig False
192-
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Go $ SetUpGoBuild True) preCommitHooksConfig)
193+
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Go $ SetUpGoBuild True) Nothing)
193194
>>= ( `shouldBe`
194195
Right
195196
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
@@ -212,8 +213,9 @@ build:
212213
|]
213214
)
214215
it "renders a Go gitlab-ci with a build and with pre-commit checks correctly" do
215-
let preCommitHooksConfig = PreCommitHooksConfig True
216-
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Go $ SetUpGoBuild True) preCommitHooksConfig)
216+
let projectType = Go $ SetUpGoBuild True
217+
nixPreCommitHookConfig = Just $ nixPreCommitHookConfigFor rcDefault projectType
218+
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault projectType nixPreCommitHookConfig)
217219
>>= ( `shouldBe`
218220
Right
219221
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
@@ -240,8 +242,9 @@ build:
240242
|]
241243
)
242244
it "renders a gitlab-ci without a build and with pre-commit checks correctly" do
243-
let preCommitHooksConfig = PreCommitHooksConfig True
244-
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Go $ SetUpGoBuild False) preCommitHooksConfig)
245+
let projectType = Go $ SetUpGoBuild False
246+
nixPreCommitHookConfig = Just $ nixPreCommitHookConfigFor rcDefault projectType
247+
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault projectType nixPreCommitHookConfig)
245248
>>= ( `shouldBe`
246249
Right
247250
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa

test/Bootstrap/Data/BuildPlanSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ spec = describe "toReasonTree" do
7474
~: gitignoreFor rcDefault projectType preCommitHooksConfig
7575
~: Readme projectName projectType devContainerConfig Nothing False
7676
~: nixPreCommitHookConfig
77-
~: gitlabCIConfigFor ciConfig rcDefault projectType preCommitHooksConfig
77+
~: gitlabCIConfigFor ciConfig rcDefault projectType (Just nixPreCommitHookConfig)
7878
~: devContainerDockerComposeFor devContainerConfig projectName
7979
~: devContainerDockerfileFor devContainerConfig
8080
~: devContainerJsonFor devContainerConfig projectName projectType

0 commit comments

Comments
 (0)