-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | The decisions Pilot makes before it does anything: whether the scheduled loop exports at
all and how often, which upstreams one compile reads, whether a failed EPSS feed stops its
publication, and whether a one-shot run uploads.

Each is a pure function over the resolved configuration, so "Ecluse.Pilot" dispatches on their
results instead of branching inside @IO@ ("Ecluse.Composition.MirrorRole" is the same shape).
-}
module Ecluse.Pilot.Plan (
    -- * The scheduled export loop
    ExportLoopPlan (..),
    ExportTarget (..),
    exportLoopPlan,
    exportCadenceMicros,
    idleCadenceMicros,

    -- * The upstreams a compile reads
    configuredSources,
    quietTimeFor,
    epssAttemptLine,

    -- * The publication guard
    shrinkPolicyOf,
    pilotPostureLines,

    -- * The one-shot run
    PilotCompileOptions (..),
    UploadRequest (..),
    compileEpssRequirement,
    unmountedCompileWarning,
    UploadPlan (..),
    uploadPlan,
    PilotUploadUnconfigured (..),
    uploadTarget,
) where

import Data.Map.Strict qualified as Map
import System.FilePath (takeFileName)

import Ecluse.Config (
    AdvisoriesSettings (advCompileInterval, advEpssFeedUrl, advEpssLossCap, advEpssQuietTime, advMaxShrink, advOsvExportBaseUrl, advQuietTime, advSourcePathRecording, advUrl),
    AdvisoryStoreUrl,
    Mount,
    MountMap,
    advisoryObjectKey,
    advisoryStoreBucket,
    compileMountEpssRequirement,
    unUrl,
 )
import Ecluse.Config.AdvisoryStore (advisoryStoreCapability)
import Ecluse.Core.Clock (secondsToMicros)
import Ecluse.Core.Ecosystem (Ecosystem, parseEcosystem)
import Ecluse.Core.Osv.Advisory (osvExportUrl)
import Ecluse.Core.Osv.Compile (CompileSources, mkCompileSources)
import Ecluse.Core.Osv.Ecosystem (OsvEcosystem (osvExportDirectory))
import Ecluse.Core.Osv.Provenance (QuietTime (..), defaultQuietTime)
import Ecluse.Core.Osv.Publication (Rebaseline, ShrinkPolicy (..), renderBaselineCapability, renderPercent)
import Ecluse.Core.Osv.Schema (EpssRequirement (EpssRequired))
import Ecluse.Core.Security.Authority (dialledAuthorityLabel)

-- | What the scheduled export loop does with the advisory settings and the mounted ecosystems.
data ExportLoopPlan
    = -- | No advisory store is configured, so the loop idles and exports nothing.
      ExportIdle
    | -- | Compile and upload one artifact per target to this store, each on its own cadence.
      ExportTo AdvisoryStoreUrl (NonEmpty ExportTarget)
    deriving stock (ExportLoopPlan -> ExportLoopPlan -> Bool
(ExportLoopPlan -> ExportLoopPlan -> Bool)
-> (ExportLoopPlan -> ExportLoopPlan -> Bool) -> Eq ExportLoopPlan
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExportLoopPlan -> ExportLoopPlan -> Bool
== :: ExportLoopPlan -> ExportLoopPlan -> Bool
$c/= :: ExportLoopPlan -> ExportLoopPlan -> Bool
/= :: ExportLoopPlan -> ExportLoopPlan -> Bool
Eq, Int -> ExportLoopPlan -> ShowS
[ExportLoopPlan] -> ShowS
ExportLoopPlan -> String
(Int -> ExportLoopPlan -> ShowS)
-> (ExportLoopPlan -> String)
-> ([ExportLoopPlan] -> ShowS)
-> Show ExportLoopPlan
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExportLoopPlan -> ShowS
showsPrec :: Int -> ExportLoopPlan -> ShowS
$cshow :: ExportLoopPlan -> String
show :: ExportLoopPlan -> String
$cshowList :: [ExportLoopPlan] -> ShowS
showList :: [ExportLoopPlan] -> ShowS
Show)

-- | One mounted ecosystem the scheduled loop compiles, with what a failed EPSS feed means for it.
data ExportTarget = ExportTarget
    { ExportTarget -> Ecosystem
etEcosystem :: Ecosystem
    , ExportTarget -> EpssRequirement
etEpss :: EpssRequirement
    -- ^ The mount's resolved requirement, the one its advisory consumers enforce.
    }
    deriving stock (ExportTarget -> ExportTarget -> Bool
(ExportTarget -> ExportTarget -> Bool)
-> (ExportTarget -> ExportTarget -> Bool) -> Eq ExportTarget
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExportTarget -> ExportTarget -> Bool
== :: ExportTarget -> ExportTarget -> Bool
$c/= :: ExportTarget -> ExportTarget -> Bool
/= :: ExportTarget -> ExportTarget -> Bool
Eq, Int -> ExportTarget -> ShowS
[ExportTarget] -> ShowS
ExportTarget -> String
(Int -> ExportTarget -> ShowS)
-> (ExportTarget -> String)
-> ([ExportTarget] -> ShowS)
-> Show ExportTarget
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExportTarget -> ShowS
showsPrec :: Int -> ExportTarget -> ShowS
$cshow :: ExportTarget -> String
show :: ExportTarget -> String
$cshowList :: [ExportTarget] -> ShowS
showList :: [ExportTarget] -> ShowS
Show)

-- | A configured store turns exporting on, one artifact per mounted ecosystem. 'Nothing' is a store with no ecosystem, which the boot refuses.
exportLoopPlan :: AdvisoriesSettings -> [ExportTarget] -> Maybe ExportLoopPlan
exportLoopPlan :: AdvisoriesSettings -> [ExportTarget] -> Maybe ExportLoopPlan
exportLoopPlan AdvisoriesSettings
advisories [ExportTarget]
targets = case AdvisoriesSettings -> Maybe AdvisoryStoreUrl
advUrl AdvisoriesSettings
advisories of
    Maybe AdvisoryStoreUrl
Nothing -> ExportLoopPlan -> Maybe ExportLoopPlan
forall a. a -> Maybe a
Just ExportLoopPlan
ExportIdle
    Just AdvisoryStoreUrl
store -> AdvisoryStoreUrl -> NonEmpty ExportTarget -> ExportLoopPlan
ExportTo AdvisoryStoreUrl
store (NonEmpty ExportTarget -> ExportLoopPlan)
-> Maybe (NonEmpty ExportTarget) -> Maybe ExportLoopPlan
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ExportTarget] -> Maybe (NonEmpty ExportTarget)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [ExportTarget]
targets

-- | The delay between export cycles. The decoder bounds @compileInterval@ to @maxBound \`div\` 1000000@ seconds, so it cannot wrap negative.
exportCadenceMicros :: AdvisoriesSettings -> Int
exportCadenceMicros :: AdvisoriesSettings -> Int
exportCadenceMicros = NominalDiffTime -> Int
secondsToMicros (NominalDiffTime -> Int)
-> (AdvisoriesSettings -> NominalDiffTime)
-> AdvisoriesSettings
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AdvisoriesSettings -> NominalDiffTime
advCompileInterval

-- | The idle loop's sleep. An added store takes effect only on the next boot, so nothing observes the wakeup.
idleCadenceMicros :: Int
idleCadenceMicros :: Int
idleCadenceMicros = NominalDiffTime -> Int
secondsToMicros (NominalDiffTime
24 NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
* NominalDiffTime
60 NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
* NominalDiffTime
60)

-- | The upstreams a compile reads, and how its artifact records them. A moved or mirrored feed never needs a new binary.
configuredSources :: AdvisoriesSettings -> OsvEcosystem -> CompileSources
configuredSources :: AdvisoriesSettings -> OsvEcosystem -> CompileSources
configuredSources AdvisoriesSettings
advisories OsvEcosystem
eco =
    SourcePathRecording -> String -> String -> CompileSources
mkCompileSources
        (AdvisoriesSettings -> SourcePathRecording
advSourcePathRecording AdvisoriesSettings
advisories)
        (Text -> Text -> String
osvExportUrl (Url -> Text
unUrl (AdvisoriesSettings -> Url
advOsvExportBaseUrl AdvisoriesSettings
advisories)) (OsvEcosystem -> Text
osvExportDirectory OsvEcosystem
eco))
        (Text -> String
forall a. ToString a => a -> String
toString (Url -> Text
unUrl (AdvisoriesSettings -> Url
advEpssFeedUrl AdvisoriesSettings
advisories)))

-- | The egress every compile attempts whatever the rules, named by the host and port it dials.
epssAttemptLine :: AdvisoriesSettings -> Text
epssAttemptLine :: AdvisoriesSettings -> Text
epssAttemptLine AdvisoriesSettings
advisories =
    Text
"pilot: every compile attempts the EPSS feed at "
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
dialledAuthorityLabel (Url -> Text
unUrl (AdvisoriesSettings -> Url
advEpssFeedUrl AdvisoriesSettings
advisories))
        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", whatever the rules, so its egress must be allowed"

-- | The shrink limits every publication is held to, scheduled or one-shot.
shrinkPolicyOf :: AdvisoriesSettings -> ShrinkPolicy
shrinkPolicyOf :: AdvisoriesSettings -> ShrinkPolicy
shrinkPolicyOf AdvisoriesSettings
advisories = ShrinkPolicy{spMaxShrink :: Percent
spMaxShrink = AdvisoriesSettings -> Percent
advMaxShrink AdvisoriesSettings
advisories, spEpssLossCap :: Percent
spEpssLossCap = AdvisoriesSettings -> Percent
advEpssLossCap AdvisoriesSettings
advisories}

-- | What every Pilot boot reports: the egress it attempts, and, with a store to publish to, the guard each publication passes.
pilotPostureLines :: AdvisoriesSettings -> [Text]
pilotPostureLines :: AdvisoriesSettings -> [Text]
pilotPostureLines AdvisoriesSettings
advisories = AdvisoriesSettings -> Text
epssAttemptLine AdvisoriesSettings
advisories Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
-> (AdvisoryStoreUrl -> [Text]) -> Maybe AdvisoryStoreUrl -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (Text -> [Text]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> [Text])
-> (AdvisoryStoreUrl -> Text) -> AdvisoryStoreUrl -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BaselineCapability -> Text
guardLine (BaselineCapability -> Text)
-> (AdvisoryStoreUrl -> BaselineCapability)
-> AdvisoryStoreUrl
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AdvisoryStoreUrl -> BaselineCapability
advisoryStoreCapability) (AdvisoriesSettings -> Maybe AdvisoryStoreUrl
advUrl AdvisoriesSettings
advisories)
  where
    guardLine :: BaselineCapability -> Text
guardLine BaselineCapability
capability =
        Text
"pilot: a publication must not be older than the stored artifact, by source, nor shrink more than "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Percent -> Text
renderPercent (AdvisoriesSettings -> Percent
advMaxShrink AdvisoriesSettings
advisories)
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" under its "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> BaselineCapability -> Text
renderBaselineCapability BaselineCapability
capability
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", with up to "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Percent -> Text
renderPercent (AdvisoriesSettings -> Percent
advEpssLossCap AdvisoriesSettings
advisories)
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" more after an optional EPSS outage"

-- | The quiet-time thresholds of one compile. An unconfigured ecosystem, or one this build does not serve, takes 'defaultQuietTime'.
quietTimeFor :: AdvisoriesSettings -> Maybe Ecosystem -> QuietTime
quietTimeFor :: AdvisoriesSettings -> Maybe Ecosystem -> QuietTime
quietTimeFor AdvisoriesSettings
advisories Maybe Ecosystem
mEco =
    QuietTime
        { qtOsv :: NominalDiffTime
qtOsv = NominalDiffTime
-> (Ecosystem -> NominalDiffTime)
-> Maybe Ecosystem
-> NominalDiffTime
forall b a. b -> (a -> b) -> Maybe a -> b
maybe NominalDiffTime
defaultQuietTime Ecosystem -> NominalDiffTime
configured Maybe Ecosystem
mEco
        , qtEpss :: NominalDiffTime
qtEpss = AdvisoriesSettings -> NominalDiffTime
advEpssQuietTime AdvisoriesSettings
advisories
        }
  where
    configured :: Ecosystem -> NominalDiffTime
configured Ecosystem
eco = NominalDiffTime
-> Ecosystem -> Map Ecosystem NominalDiffTime -> NominalDiffTime
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault NominalDiffTime
defaultQuietTime Ecosystem
eco (AdvisoriesSettings -> Map Ecosystem NominalDiffTime
advQuietTime AdvisoriesSettings
advisories)

-- | Options for the one-shot @ecluse pilot compile@ mode.
data PilotCompileOptions = PilotCompileOptions
    { PilotCompileOptions -> Text
pcoEcosystem :: Text
    , PilotCompileOptions -> String
pcoOutDir :: FilePath
    , PilotCompileOptions -> UploadRequest
pcoUpload :: UploadRequest
    }
    deriving stock (PilotCompileOptions -> PilotCompileOptions -> Bool
(PilotCompileOptions -> PilotCompileOptions -> Bool)
-> (PilotCompileOptions -> PilotCompileOptions -> Bool)
-> Eq PilotCompileOptions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PilotCompileOptions -> PilotCompileOptions -> Bool
== :: PilotCompileOptions -> PilotCompileOptions -> Bool
$c/= :: PilotCompileOptions -> PilotCompileOptions -> Bool
/= :: PilotCompileOptions -> PilotCompileOptions -> Bool
Eq, Int -> PilotCompileOptions -> ShowS
[PilotCompileOptions] -> ShowS
PilotCompileOptions -> String
(Int -> PilotCompileOptions -> ShowS)
-> (PilotCompileOptions -> String)
-> ([PilotCompileOptions] -> ShowS)
-> Show PilotCompileOptions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PilotCompileOptions -> ShowS
showsPrec :: Int -> PilotCompileOptions -> ShowS
$cshow :: PilotCompileOptions -> String
show :: PilotCompileOptions -> String
$cshowList :: [PilotCompileOptions] -> ShowS
showList :: [PilotCompileOptions] -> ShowS
Show)

-- | Whether a one-shot run publishes its artifact. A rebaseline exists only beside an upload.
data UploadRequest
    = NoUpload
    | -- | Publish under the configured guard.
      Upload
    | -- | Publish once over the named revision, with this run's own shrink allowance, and restart the baseline.
      UploadRebaselined Rebaseline
    deriving stock (UploadRequest -> UploadRequest -> Bool
(UploadRequest -> UploadRequest -> Bool)
-> (UploadRequest -> UploadRequest -> Bool) -> Eq UploadRequest
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UploadRequest -> UploadRequest -> Bool
== :: UploadRequest -> UploadRequest -> Bool
$c/= :: UploadRequest -> UploadRequest -> Bool
/= :: UploadRequest -> UploadRequest -> Bool
Eq, Int -> UploadRequest -> ShowS
[UploadRequest] -> ShowS
UploadRequest -> String
(Int -> UploadRequest -> ShowS)
-> (UploadRequest -> String)
-> ([UploadRequest] -> ShowS)
-> Show UploadRequest
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UploadRequest -> ShowS
showsPrec :: Int -> UploadRequest -> ShowS
$cshow :: UploadRequest -> String
show :: UploadRequest -> String
$cshowList :: [UploadRequest] -> ShowS
showList :: [UploadRequest] -> ShowS
Show)

-- | The compiled mount's own requirement. A compile for no mount may serve a policy that needs EPSS.
compileEpssRequirement :: AdvisoriesSettings -> MountMap -> PilotCompileOptions -> EpssRequirement
compileEpssRequirement :: AdvisoriesSettings
-> MountMap -> PilotCompileOptions -> EpssRequirement
compileEpssRequirement AdvisoriesSettings
advisories MountMap
mounts PilotCompileOptions
opts = EpssRequirement
-> (Mount -> EpssRequirement) -> Maybe Mount -> EpssRequirement
forall b a. b -> (a -> b) -> Maybe a -> b
maybe EpssRequirement
EpssRequired (AdvisoriesSettings -> Mount -> EpssRequirement
compileMountEpssRequirement AdvisoriesSettings
advisories) (MountMap -> PilotCompileOptions -> Maybe Mount
compiledMount MountMap
mounts PilotCompileOptions
opts)

-- | The warning a one-shot compile logs for an ecosystem the loaded configuration does not mount.
unmountedCompileWarning :: MountMap -> PilotCompileOptions -> Maybe Text
unmountedCompileWarning :: MountMap -> PilotCompileOptions -> Maybe Text
unmountedCompileWarning MountMap
mounts PilotCompileOptions
opts = case MountMap -> PilotCompileOptions -> Maybe Mount
compiledMount MountMap
mounts PilotCompileOptions
opts of
    Just Mount
_ -> Maybe Text
forall a. Maybe a
Nothing
    Maybe Mount
Nothing ->
        Text -> Maybe Text
forall a. a -> Maybe a
Just
            ( Text
"The loaded configuration mounts no "
                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> PilotCompileOptions -> Text
pcoEcosystem PilotCompileOptions
opts
                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" ecosystem. This compile prepares its full dataset, so it requires EPSS enrichment and publishes nothing without it"
            )

compiledMount :: MountMap -> PilotCompileOptions -> Maybe Mount
compiledMount :: MountMap -> PilotCompileOptions -> Maybe Mount
compiledMount MountMap
mounts PilotCompileOptions
opts = (Ecosystem -> MountMap -> Maybe Mount
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` MountMap
mounts) (Ecosystem -> Maybe Mount) -> Maybe Ecosystem -> Maybe Mount
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Text -> Maybe Ecosystem
parseEcosystem (PilotCompileOptions -> Text
pcoEcosystem PilotCompileOptions
opts)

-- | An upload requested without an advisory store: a wiring fault, so it throws rather than return a value to re-raise.
data PilotUploadUnconfigured = PilotUploadUnconfigured
    deriving stock (PilotUploadUnconfigured -> PilotUploadUnconfigured -> Bool
(PilotUploadUnconfigured -> PilotUploadUnconfigured -> Bool)
-> (PilotUploadUnconfigured -> PilotUploadUnconfigured -> Bool)
-> Eq PilotUploadUnconfigured
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PilotUploadUnconfigured -> PilotUploadUnconfigured -> Bool
== :: PilotUploadUnconfigured -> PilotUploadUnconfigured -> Bool
$c/= :: PilotUploadUnconfigured -> PilotUploadUnconfigured -> Bool
/= :: PilotUploadUnconfigured -> PilotUploadUnconfigured -> Bool
Eq, Int -> PilotUploadUnconfigured -> ShowS
[PilotUploadUnconfigured] -> ShowS
PilotUploadUnconfigured -> String
(Int -> PilotUploadUnconfigured -> ShowS)
-> (PilotUploadUnconfigured -> String)
-> ([PilotUploadUnconfigured] -> ShowS)
-> Show PilotUploadUnconfigured
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PilotUploadUnconfigured -> ShowS
showsPrec :: Int -> PilotUploadUnconfigured -> ShowS
$cshow :: PilotUploadUnconfigured -> String
show :: PilotUploadUnconfigured -> String
$cshowList :: [PilotUploadUnconfigured] -> ShowS
showList :: [PilotUploadUnconfigured] -> ShowS
Show)

instance Exception PilotUploadUnconfigured

-- | Whether a one-shot run uploads its artifact, and where.
data UploadPlan
    = -- | The run did not ask to upload, so the artifact stays local.
      UploadSkipped
    | -- | Upload the compiled artifact to this store, under an operator's rebaseline if one was given.
      UploadTo AdvisoryStoreUrl (Maybe Rebaseline)
    deriving stock (UploadPlan -> UploadPlan -> Bool
(UploadPlan -> UploadPlan -> Bool)
-> (UploadPlan -> UploadPlan -> Bool) -> Eq UploadPlan
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UploadPlan -> UploadPlan -> Bool
== :: UploadPlan -> UploadPlan -> Bool
$c/= :: UploadPlan -> UploadPlan -> Bool
/= :: UploadPlan -> UploadPlan -> Bool
Eq, Int -> UploadPlan -> ShowS
[UploadPlan] -> ShowS
UploadPlan -> String
(Int -> UploadPlan -> ShowS)
-> (UploadPlan -> String)
-> ([UploadPlan] -> ShowS)
-> Show UploadPlan
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UploadPlan -> ShowS
showsPrec :: Int -> UploadPlan -> ShowS
$cshow :: UploadPlan -> String
show :: UploadPlan -> String
$cshowList :: [UploadPlan] -> ShowS
showList :: [UploadPlan] -> ShowS
Show)

-- | Plan a one-shot run's upload. One asked for with no store is refused, so a scripted run cannot succeed having published nothing.
uploadPlan :: PilotCompileOptions -> Maybe AdvisoryStoreUrl -> Either PilotUploadUnconfigured UploadPlan
uploadPlan :: PilotCompileOptions
-> Maybe AdvisoryStoreUrl
-> Either PilotUploadUnconfigured UploadPlan
uploadPlan PilotCompileOptions
opts Maybe AdvisoryStoreUrl
mStore = case PilotCompileOptions -> UploadRequest
pcoUpload PilotCompileOptions
opts of
    UploadRequest
NoUpload -> UploadPlan -> Either PilotUploadUnconfigured UploadPlan
forall a b. b -> Either a b
Right UploadPlan
UploadSkipped
    UploadRequest
Upload -> Maybe Rebaseline -> Either PilotUploadUnconfigured UploadPlan
uploadingTo Maybe Rebaseline
forall a. Maybe a
Nothing
    UploadRebaselined Rebaseline
approval -> Maybe Rebaseline -> Either PilotUploadUnconfigured UploadPlan
uploadingTo (Rebaseline -> Maybe Rebaseline
forall a. a -> Maybe a
Just Rebaseline
approval)
  where
    uploadingTo :: Maybe Rebaseline -> Either PilotUploadUnconfigured UploadPlan
uploadingTo Maybe Rebaseline
approval = Either PilotUploadUnconfigured UploadPlan
-> (AdvisoryStoreUrl -> Either PilotUploadUnconfigured UploadPlan)
-> Maybe AdvisoryStoreUrl
-> Either PilotUploadUnconfigured UploadPlan
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (PilotUploadUnconfigured
-> Either PilotUploadUnconfigured UploadPlan
forall a b. a -> Either a b
Left PilotUploadUnconfigured
PilotUploadUnconfigured) (\AdvisoryStoreUrl
store -> UploadPlan -> Either PilotUploadUnconfigured UploadPlan
forall a b. b -> Either a b
Right (AdvisoryStoreUrl -> Maybe Rebaseline -> UploadPlan
UploadTo AdvisoryStoreUrl
store Maybe Rebaseline
approval)) Maybe AdvisoryStoreUrl
mStore

-- | Where one compiled artifact lands: the store's bucket and its file's key under the prefix, which the proxy's sync reads.
uploadTarget :: AdvisoryStoreUrl -> FilePath -> (Text, Text)
uploadTarget :: AdvisoryStoreUrl -> String -> (Text, Text)
uploadTarget AdvisoryStoreUrl
store String
dbPath =
    (AdvisoryStoreUrl -> Text
advisoryStoreBucket AdvisoryStoreUrl
store, AdvisoryStoreUrl -> String -> Text
advisoryObjectKey AdvisoryStoreUrl
store (ShowS
takeFileName String
dbPath))