diff --git a/server/graphql-engine.cabal b/server/graphql-engine.cabal index 1f97a242b98..9b01e802bc7 100644 --- a/server/graphql-engine.cabal +++ b/server/graphql-engine.cabal @@ -1077,6 +1077,7 @@ test-suite graphql-engine-tests Hasura.Server.MigrateSuite Hasura.Server.Migrate.VersionSpec Hasura.Server.TelemetrySpec + Hasura.Server.VersionSpec Hasura.Server.InitSpec Hasura.Server.Init.ArgSpec Hasura.SessionSpec diff --git a/server/src-lib/Hasura/Server/Telemetry.hs b/server/src-lib/Hasura/Server/Telemetry.hs index 12823dcb4b0..4ae1c4c786c 100644 --- a/server/src-lib/Hasura/Server/Telemetry.hs +++ b/server/src-lib/Hasura/Server/Telemetry.hs @@ -294,3 +294,4 @@ versionToTopic :: Version -> Topic versionToTopic = \case VersionDev _ -> Topic "server_metrics_v2_test" VersionRelease _ -> Topic "server_metrics_v2" + VersionCE _ -> Topic "server_metrics_v2" diff --git a/server/src-lib/Hasura/Server/Version.hs b/server/src-lib/Hasura/Server/Version.hs index b13d8805db1..18da3b9f8b5 100644 --- a/server/src-lib/Hasura/Server/Version.hs +++ b/server/src-lib/Hasura/Server/Version.hs @@ -4,6 +4,7 @@ module Hasura.Server.Version ( Version (..), currentVersion, consoleAssetsVersion, + versionToAssetsVersion, ) where @@ -20,16 +21,21 @@ import Language.Haskell.TH.Syntax import Text.Regex.TDFA ((=~~)) data Version - = VersionDev !Text - | VersionRelease !V.Version + = VersionDev Text + | VersionRelease V.Version + | VersionCE Text deriving (Show, Eq) instance ToText Version where toText = \case VersionDev txt -> txt VersionRelease version -> "v" <> V.toText version + VersionCE txt -> txt instance FromText Version where + -- Ensure that a -ce suffix is *not* interpreted as the release type of a + -- Data.SemVer-style semantic version + fromText txt | T.takeEnd 3 txt == "-ce" = VersionCE txt fromText txt = case V.fromText $ T.dropWhile (== 'v') txt of Left _ -> VersionDev txt Right version -> VersionRelease version @@ -61,15 +67,15 @@ currentVersion = runIO (readFile versionFileName `onException` error noFileErr) >>= stringE ) --- | A version-based string used to form the CDN URL for fetching console assets. -consoleAssetsVersion :: Text -consoleAssetsVersion = case currentVersion of +versionToAssetsVersion :: Version -> Text +versionToAssetsVersion = \case VersionDev txt -> "versioned/" <> txt VersionRelease v -> case getReleaseChannel v of Nothing -> "versioned/" <> vMajMin Just c -> "channel/" <> c <> "/" <> vMajMin where vMajMin = T.pack ("v" <> show (v ^. V.major) <> "." <> show (v ^. V.minor)) + VersionCE txt -> "channel/versioned/" <> txt where getReleaseChannel :: V.Version -> Maybe Text getReleaseChannel sv = case sv ^. V.release of @@ -89,3 +95,7 @@ consoleAssetsVersion = case currentVersion of where toTextualM _ Nothing = pure Nothing toTextualM f (Just a) = f a + +-- | A version-based string used to form the CDN URL for fetching console assets. +consoleAssetsVersion :: Text +consoleAssetsVersion = versionToAssetsVersion currentVersion diff --git a/server/src-test/Hasura/Server/VersionSpec.hs b/server/src-test/Hasura/Server/VersionSpec.hs new file mode 100644 index 00000000000..ea088fa3676 --- /dev/null +++ b/server/src-test/Hasura/Server/VersionSpec.hs @@ -0,0 +1,41 @@ +module Hasura.Server.VersionSpec (spec) where + +import Data.Text qualified as T +import Data.Text.Conversions (FromText (..)) +import Hasura.Prelude +import Hasura.Server.Version +import Test.Hspec + +versions :: [(Text, Text)] +versions = + [ ("v1.2.4", "channel/stable/v1.2"), + ("2.3.4", "channel/stable/v2.3"), + ("v1.0.0-alpha45", "channel/alpha/v1.0"), + ("v1.0.0-beta.01", "channel/beta/v1.0"), + ("v2.3.1-rc.11", "channel/rc/v2.3"), + ("v1.2.0-rj.1", "channel/rj/v1.2"), + -- Apparently this one was generated in a different way? + -- ("v2.0.0-pro.9", "channel/stable/v2.0"), + ("v2.0.0-alpha.pro.9", "channel/alpha/v2.0"), + ("v2.12.0-ce", "channel/versioned/v2.12.0-ce"), + ("v2.12.1-ce", "channel/versioned/v2.12.1-ce"), + ("v2.12.0-beta.1-ce", "channel/versioned/v2.12.0-beta.1-ce"), + ("v2.12.0-beta.2-ce", "channel/versioned/v2.12.0-beta.2-ce"), + ("v2.12.0", "channel/stable/v2.12"), + ("v2.12.1", "channel/stable/v2.12"), + ("v2.12.0-beta.1", "channel/beta/v2.12"), + ("v2.12.0-beta.2", "channel/beta/v2.12"), + ("v2.12.0-alpha.1", "channel/alpha/v2.12"), + ("v2.12.0-alpha.2", "channel/alpha/v2.12") + -- The Haskell server does not seem to be responsible for generating version strings for Cloud + -- ("v2.12.0-cloud.1", "channel/versioned/v2.12.0-cloud.1") + -- ("v2.0.0-cloud.9", "channel/stable/v2.0"), + -- ("v2.0.0-alpha.cloud.9", "channel/alpha/v2.0"), + ] + +spec :: Spec +spec = describe "console assets version" do + parallel $ + for_ versions \(input, output) -> + it ("versionToAssetsVersion returns expected output for " <> T.unpack input) $ + versionToAssetsVersion (fromText input) `shouldBe` output