Skip to content

Commit

Permalink
Include restriction configuration in restylerEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Jun 1, 2023
1 parent 322df62 commit 0ac5957
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
30 changes: 21 additions & 9 deletions src/Restyled/Api/Repo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Restyled.Prelude

import Blammo.Logging.LogSettings (getLogSettingsLevels)
import Blammo.Logging.LogSettings.LogLevels (newLogLevels, showLogLevels)
import Restyled.Bytes
import Restyled.CpuShares
import Restyled.Marketplace
import Restyled.Models.DB
import Restyled.RestylerImage
Expand All @@ -32,18 +34,23 @@ apiRepo (Entity _ Repo {..}) AppSettings {..} mAllows = ApiRepo
, isEnabled = repoEnabled
, installationId = repoInstallationId
, restylerImage = fromMaybe appRestylerImage repoRestylerImage
, restylerEnv =
[ "REPO_DISABLED=" <> if repoEnabled then "" else "x"
, "PLAN_RESTRICTION=" <> fromMaybe "" mPlanRestriction
, "PLAN_UPGRADE_URL=https://github.com/marketplace/restyled-io"
, "LOG_FORMAT=json"
, "LOG_LEVEL=" <> pack (showLogLevels logLevels)
, restylerEnv = catMaybes
[ Just $ "REPO_DISABLED=" <> if repoEnabled then "" else "x"
, Just "LOG_FORMAT=json"
, Just $ "LOG_LEVEL=" <> pack (showLogLevels logLevels)
, Just "PLAN_UPGRADE_URL=https://github.com/marketplace/restyled-io"
, ("PLAN_RESTRICTION=" <>)
. toPlanRestriction repoOwner
<$> (getLimitation =<< mAllows)
, ("RESTYLER_CPU_SHARES=" <>)
. cpuSharesToText
<$> (marketplacePlanCpuShares =<< getPlan =<< mAllows)
, ("RESTYLER_MEMORY=" <>)
. bytesToText
<$> (marketplacePlanMemory =<< getPlan =<< mAllows)
]
}
where
mPlanRestriction =
fmap (toPlanRestriction repoOwner) $ getLimitation =<< mAllows

logLevels = if repoDebugEnabled
then newLogLevels LevelDebug []
else getLogSettingsLevels appLogSettings
Expand All @@ -68,6 +75,11 @@ toPlanRestriction ownerName = \case
<> ") expired at "
<> pack (show asOf)

getPlan :: MarketplacePlanAllows -> Maybe MarketplacePlan
getPlan = \case
MarketplacePlanAllows x -> x
MarketplacePlanForbids{} -> Nothing

getLimitation :: MarketplacePlanAllows -> Maybe MarketplacePlanLimitation
getLimitation = \case
MarketplacePlanAllows{} -> Nothing
Expand Down
4 changes: 2 additions & 2 deletions test/Restyled/Api/UpsertRepoSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ spec = withApp $ do
Right repo <- runValidateT
$ upsertRepo body { ApiUpsertRepo.isPrivate = False }

lookupRestylerEnv "PLAN_RESTRICTION" repo `shouldBe` Just ""
lookupRestylerEnv "PLAN_RESTRICTION" repo `shouldBe` Nothing

it "handles private plans" $ graph $ do
plan <- node @MarketplacePlan () $ edit $ setPlanLimited 1
Expand All @@ -84,7 +84,7 @@ spec = withApp $ do
, ApiUpsertRepo.isPrivate = True
}

lookupRestylerEnv "PLAN_RESTRICTION" repo1 `shouldBe` Just ""
lookupRestylerEnv "PLAN_RESTRICTION" repo1 `shouldBe` Nothing
lookupRestylerEnv "PLAN_RESTRICTION" repo2
`shouldBe` Just
"You have reached the maximum number of private repositories for the Marketplace plan for the owner of this repository (foo)"
Expand Down

0 comments on commit 0ac5957

Please sign in to comment.