Skip to content

Commit

Permalink
Ensure impure hooks still run in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
sd234678 committed Sep 26, 2023
1 parent adf09dd commit b6fd8a5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .last-exported-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Last exported commit from parent repo: e283eef4787456402415cacd138093dbb080f812
Last exported commit from parent repo: 071219753e1fc8d28d4731d6f0588802ff0d7a3b
2 changes: 1 addition & 1 deletion src/Bootstrap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ makeBuildPlan MakeBuildPlanArgs {..} = do
)
~: nixShellCompatFor mbpRunConfig
~: nixPreCommitHookConfig
~: gitlabCIConfigFor mbpContinuousIntegrationConfig mbpRunConfig mbpProjectType mbpPreCommitHooksConfig
~: gitlabCIConfigFor mbpContinuousIntegrationConfig mbpRunConfig mbpProjectType nixPreCommitHookConfig
~: devContainerDockerComposeFor mbpDevContainerConfig mbpProjectName
~: devContainerDockerfileFor mbpDevContainerConfig
~: devContainerJsonFor mbpDevContainerConfig mbpProjectName mbpProjectType
Expand Down
22 changes: 14 additions & 8 deletions src/Bootstrap/Data/Bootstrappable/GitlabCIConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import Bootstrap.Data.Bootstrappable
bootstrapReason
),
)
import Bootstrap.Data.Bootstrappable.NixPreCommitHookConfig
( NixPreCommitHookConfig (nixPreCommitHookConfigImpureHookCommand),
)
import Bootstrap.Data.ContinuousIntegration
( ContinuousIntegrationConfig (ContinuousIntegrationConfig),
)
import Bootstrap.Data.PreCommitHook
( PreCommitHooksConfig (unPreCommitHooksConfig),
)
import Bootstrap.Data.ProjectType
( ElmMode (ElmModeBare, ElmModeNode),
ElmOptions (ElmOptions, elmOptionElmMode),
Expand All @@ -32,7 +32,7 @@ import Bootstrap.Data.ProjectType
data GitlabCIConfig = GitlabCIConfig
{ gitlabCIConfigUseFlakes :: Bool,
gitlabCIConfigProjectType :: ProjectType,
gitlabCIConfigPreCommitHooksConfig :: PreCommitHooksConfig
gitlabCIConfigPreCommitHooksConfig :: Maybe NixPreCommitHookConfig
}

instance Bootstrappable GitlabCIConfig where
Expand All @@ -49,12 +49,18 @@ instance Bootstrappable GitlabCIConfig where
" - nix-env -iA nixpkgs.bash nixpkgs.openssh",
" - echo \"experimental-features = nix-command flakes\" >> /etc/nix/nix.conf",
"",
"check-dev-environment:",
case nixPreCommitHookConfigImpureHookCommand <$> gitlabCIConfigPreCommitHooksConfig of
Just c | c /= "echo ok" -> "check-dev-environment-and-run-impure-hooks:"
_ -> "check-dev-environment:",
" stage: build",
" script:",
commandInShell "echo ok"
commandInShell $
maybe
"echo ok"
nixPreCommitHookConfigImpureHookCommand
gitlabCIConfigPreCommitHooksConfig
]
<> ( if unPreCommitHooksConfig gitlabCIConfigPreCommitHooksConfig
<> ( if isJust gitlabCIConfigPreCommitHooksConfig
then
[ "",
"pre-commit-check:",
Expand Down Expand Up @@ -123,7 +129,7 @@ gitlabCIConfigFor ::
ContinuousIntegrationConfig ->
RunConfig ->
ProjectType ->
PreCommitHooksConfig ->
Maybe NixPreCommitHookConfig ->
Maybe GitlabCIConfig
gitlabCIConfigFor (ContinuousIntegrationConfig False) _ _ _ = Nothing
gitlabCIConfigFor (ContinuousIntegrationConfig True) RunConfig {rcUseFlakes} t p =
Expand Down
15 changes: 13 additions & 2 deletions src/Bootstrap/Data/Bootstrappable/NixPreCommitHookConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

-- | Copyright : (c) Crown Copyright GCHQ
module Bootstrap.Data.Bootstrappable.NixPreCommitHookConfig
( NixPreCommitHookConfig (nixPreCommitHookConfigRequiresNixpkgs),
( NixPreCommitHookConfig
( nixPreCommitHookConfigRequiresNixpkgs,
nixPreCommitHookConfigImpureHookCommand
),
PreCommitHook,
nixPreCommitHookConfigFor,
)
Expand Down Expand Up @@ -52,7 +55,9 @@ import Relude.Extra.Tuple (dup)
data NixPreCommitHookConfig = NixPreCommitHookConfig
{ nixPreCommitHookConfigHooks :: [PreCommitHook],
nixPreCommitHookConfigRequiresNixpkgs :: Bool,
nixPreCommitHookConfigUsingFlakeLib :: Bool
nixPreCommitHookConfigUsingFlakeLib :: Bool,
-- | A command used to run any impure hooks manually, defaults to "echo ok"
nixPreCommitHookConfigImpureHookCommand :: Text
}

instance Bootstrappable NixPreCommitHookConfig where
Expand Down Expand Up @@ -146,6 +151,12 @@ nixPreCommitHookConfigFor RunConfig {rcUseFlakes} projectType =
nixPreCommitHookConfigRequiresNixpkgs =
any preCommitHookToolIsFromNixpkgs (tool <$> nixPreCommitHookConfigHooks)
|| hpack `elem` nixPreCommitHookConfigHooks
nixPreCommitHookConfigImpureHookCommand =
if not (all preCommitHookIsPure nixPreCommitHookConfigHooks)
then case projectType of
Elm _ -> "elm-review"
_ -> "echo ok"
else "echo ok"
in NixPreCommitHookConfig {nixPreCommitHookConfigUsingFlakeLib = rcUseFlakes, ..}

data PreCommitHook = PreCommitHook
Expand Down
49 changes: 26 additions & 23 deletions test/Bootstrap/Data/Bootstrappable/GitlabCIConfigSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ module Bootstrap.Data.Bootstrappable.GitlabCIConfigSpec (spec) where

import Bootstrap.Data.Bootstrappable (Bootstrappable (bootstrapContent))
import Bootstrap.Data.Bootstrappable.GitlabCIConfig (gitlabCIConfigFor)
import Bootstrap.Data.Bootstrappable.NixPreCommitHookConfig
( nixPreCommitHookConfigFor,
)
import Bootstrap.Data.ContinuousIntegration
( ContinuousIntegrationConfig (ContinuousIntegrationConfig),
)
import Bootstrap.Data.PreCommitHook (PreCommitHooksConfig (PreCommitHooksConfig))
import Bootstrap.Data.ProjectType
( ElmMode (ElmModeBare, ElmModeNode),
ElmOptions (ElmOptions),
Expand All @@ -26,8 +28,7 @@ spec :: Spec
spec = describe "gitlab-ci.yml rendering" do
let ciConfig = ContinuousIntegrationConfig True
it "renders an Elm/Parcel gitlab-ci config without pre-commit checks correctly" do
let preCommitHooksConfig = PreCommitHooksConfig False
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Elm $ ElmOptions (ElmModeNode PNPm) False) preCommitHooksConfig)
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Elm $ ElmOptions (ElmModeNode PNPm) False) Nothing)
>>= ( `shouldBe`
Right
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
Expand All @@ -52,8 +53,7 @@ build-site:
|]
)
it "renders an Elm/Parcel gitlab-ci config without pre-commit or flakes checks correctly" do
let preCommitHooksConfig = PreCommitHooksConfig False
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Elm $ ElmOptions (ElmModeNode PNPm) False) preCommitHooksConfig)
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Elm $ ElmOptions (ElmModeNode PNPm) False) Nothing)
>>= ( `shouldBe`
Right
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
Expand All @@ -78,8 +78,9 @@ build-site:
|]
)
it "renders a bare Elm gitlab-ci config with pre-commit checks correctly" do
let preCommitHooksConfig = PreCommitHooksConfig True
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Elm $ ElmOptions ElmModeBare True) preCommitHooksConfig)
let projectType = Elm $ ElmOptions ElmModeBare True
nixPreCommitHookConfig = Just $ nixPreCommitHookConfigFor rcDefault projectType
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault projectType nixPreCommitHookConfig)
>>= ( `shouldBe`
Right
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
Expand All @@ -91,10 +92,10 @@ default:
- nix-env -iA nixpkgs.bash nixpkgs.openssh
- echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf

check-dev-environment:
check-dev-environment-and-run-impure-hooks:
stage: build
script:
- nix-shell --run 'echo ok'
- nix-shell --run 'elm-review'

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

check-dev-environment:
check-dev-environment-and-run-impure-hooks:
stage: build
script:
- nix develop -c echo ok
- nix develop -c elm-review

pre-commit-check:
stage: build
Expand All @@ -136,8 +138,7 @@ build-site:
|]
)
it "renders a Go gitlab-ci with a flake build and without pre-commit checks correctly" do
let preCommitHooksConfig = PreCommitHooksConfig False
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Go $ SetUpGoBuild True) preCommitHooksConfig)
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Go $ SetUpGoBuild True) Nothing)
>>= ( `shouldBe`
Right
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
Expand All @@ -160,8 +161,9 @@ build:
|]
)
it "renders a Go gitlab-ci with a flake build and with pre-commit checks correctly" do
let preCommitHooksConfig = PreCommitHooksConfig True
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes (Go $ SetUpGoBuild True) preCommitHooksConfig)
let projectType = Go $ SetUpGoBuild True
nixPreCommitHookConfig = Just $ nixPreCommitHookConfigFor rcDefault projectType
bootstrapContent (gitlabCIConfigFor ciConfig rcWithFlakes projectType nixPreCommitHookConfig)
>>= ( `shouldBe`
Right
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
Expand All @@ -188,8 +190,7 @@ build:
|]
)
it "renders a Go gitlab-ci with a build and without pre-commit checks correctly" do
let preCommitHooksConfig = PreCommitHooksConfig False
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Go $ SetUpGoBuild True) preCommitHooksConfig)
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Go $ SetUpGoBuild True) Nothing)
>>= ( `shouldBe`
Right
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
Expand All @@ -212,8 +213,9 @@ build:
|]
)
it "renders a Go gitlab-ci with a build and with pre-commit checks correctly" do
let preCommitHooksConfig = PreCommitHooksConfig True
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Go $ SetUpGoBuild True) preCommitHooksConfig)
let projectType = Go $ SetUpGoBuild True
nixPreCommitHookConfig = Just $ nixPreCommitHookConfigFor rcDefault projectType
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault projectType nixPreCommitHookConfig)
>>= ( `shouldBe`
Right
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
Expand All @@ -240,8 +242,9 @@ build:
|]
)
it "renders a gitlab-ci without a build and with pre-commit checks correctly" do
let preCommitHooksConfig = PreCommitHooksConfig True
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault (Go $ SetUpGoBuild False) preCommitHooksConfig)
let projectType = Go $ SetUpGoBuild False
nixPreCommitHookConfig = Just $ nixPreCommitHookConfigFor rcDefault projectType
bootstrapContent (gitlabCIConfigFor ciConfig rcDefault projectType nixPreCommitHookConfig)
>>= ( `shouldBe`
Right
[r|image: nixos/nix@sha256:473a2b527958665554806aea24d0131bacec46d23af09fef4598eeab331850fa
Expand Down
2 changes: 1 addition & 1 deletion test/Bootstrap/Data/BuildPlanSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ spec = describe "toReasonTree" do
~: gitignoreFor rcDefault projectType preCommitHooksConfig
~: Readme projectName projectType devContainerConfig Nothing False
~: nixPreCommitHookConfig
~: gitlabCIConfigFor ciConfig rcDefault projectType preCommitHooksConfig
~: gitlabCIConfigFor ciConfig rcDefault projectType (Just nixPreCommitHookConfig)
~: devContainerDockerComposeFor devContainerConfig projectName
~: devContainerDockerfileFor devContainerConfig
~: devContainerJsonFor devContainerConfig projectName projectType
Expand Down

0 comments on commit b6fd8a5

Please sign in to comment.