summaryrefslogtreecommitdiff
path: root/src/Eval.hs
blob: b263a198064e1d660294882ace689ae7fe658eae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Eval (
    EvalInput(..),
    EvalError(..), textEvalError,

    evalJob,
    evalJobSet,
) where

import Control.Monad.Except

import Data.Bifunctor
import Data.Text (Text)
import Data.Text qualified as T

import Job.Types
import Repo

data EvalInput = EvalInput
    { eiContainingRepo :: Maybe Repo
    , eiOtherRepos :: [ ( RepoName, Repo ) ]
    }

data EvalError
    = OtherEvalError Text

textEvalError :: EvalError -> Text
textEvalError (OtherEvalError text) = text

evalJob :: EvalInput -> DeclaredJob -> Except EvalError Job
evalJob EvalInput {..} decl = do
    otherCheckout <- forM (jobOtherCheckout decl) $ \( DeclaredJobRepo name, revision, checkout ) -> do
        repo <- maybe (throwError $ OtherEvalError $ "repo `" <> textRepoName name <> "' not defined") return $
            lookup name eiOtherRepos
        return ( EvaluatedJobRepo repo, revision, checkout )
    return Job
        { jobName = jobName decl
        , jobContainingCheckout = jobContainingCheckout decl
        , jobOtherCheckout = otherCheckout
        , jobRecipe = jobRecipe decl
        , jobArtifacts = jobArtifacts decl
        , jobUses = jobUses decl
        }

evalJobSet :: EvalInput -> DeclaredJobSet -> JobSet
evalJobSet ei decl = do
    JobSet
        { jobsetCommit = jobsetCommit decl
        , jobsetJobsEither = join $
            fmap (sequence . map (runExceptStr . evalJob ei)) $
            jobsetJobsEither decl
        }
  where
    runExceptStr = first (T.unpack . textEvalError) . runExcept