Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Mybox/Display.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Mybox.Display (
bannerModified,
bannerFailed,
MDisplay,
displayLogText,
displayWarning,
displayVersion,
) where

Expand All @@ -34,8 +34,8 @@ runDisplay h act = do
let run = if isANSI then runANSIDisplay else runSimpleDisplay
Print.run h $ run $ inject @_ @(AppDisplay : Print : _) act

displayLogText :: AppDisplay :> es => Text -> Eff es ()
displayLogText = displayLog . MLog
displayWarning :: AppDisplay :> es => Text -> Eff es ()
displayWarning = displayLog . MWarning

displayVersion :: AppDisplay :> es => Version -> Eff es ()
displayVersion = displayLog . MVersion
4 changes: 2 additions & 2 deletions src/Mybox/Display/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import Mybox.Prelude
data MDisplay

data instance Log MDisplay
= MLog Text
= MWarning Text
| MVersion Version

instance TerminalShow (Log MDisplay) where
terminalShow _ (MLog log) = [[tiMk log]]
terminalShow _ (MWarning log) = [[(tiMk "Warning"){foreground = Just Yellow}, tiColon, tiMk log]]
terminalShow _ (MVersion version) =
[
[ tiMk "mybox"
Expand Down
2 changes: 1 addition & 1 deletion src/Mybox/Driver/Ops.hs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ drvGithubToken :: Driver :> es => Eff es (Maybe Text)
drvGithubToken = drvEnv "GITHUB_TOKEN" `orMaybeM` (getGhAuth <$> drvRunOutputExit ("gh" :| ["auth", "token"]))
where
getGhAuth result
| result.exit == ExitSuccess = Just result.output
| result.exit == ExitSuccess && not (Text.null result.output) = Just result.output
| otherwise = Nothing

drvUrlEtag :: Driver :> es => Text -> Eff es Text
Expand Down
22 changes: 15 additions & 7 deletions src/Mybox/Package/Forge/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Control.Monad.Writer
import Data.Text qualified as Text

import Mybox.Aeson
import Mybox.Display
import Mybox.Driver
import Mybox.Filters
import Mybox.Package.Archive
Expand Down Expand Up @@ -57,7 +58,7 @@ data APIEndpoint = APIEndpoint
, authToken :: Maybe Text
}

apiEndpoint :: Driver :> es => ForgePackage -> Eff es APIEndpoint
apiEndpoint :: (AppDisplay :> es, Driver :> es) => ForgePackage -> Eff es APIEndpoint
apiEndpoint p = do
let parts = Text.splitOn "/" p.repo
case parts of
Expand All @@ -76,13 +77,20 @@ genericApiEndpoint host owner repo = do
let baseUrl = start <> "/api/v1/repos/" <> owner <> "/" <> repo
pure APIEndpoint{..}

githubApiEndpoint :: Driver :> es => Text -> Text -> Eff es APIEndpoint
githubApiEndpoint :: (AppDisplay :> es, Driver :> es) => Text -> Text -> Eff es APIEndpoint
githubApiEndpoint owner repo = do
authToken <- drvGithubToken
when (isNothing authToken)
$ displayWarning
$ "No GitHub token for "
<> owner
<> "/"
<> repo
<> ". Unauthenticated API requests may be rate limited. Set GITHUB_TOKEN or run gh auth login."
let baseUrl = "https://api.github.com/repos/" <> owner <> "/" <> repo
pure APIEndpoint{..}

api :: Driver :> es => ForgePackage -> Text -> Eff es (Either (Int, Text) Text)
api :: (AppDisplay :> es, Driver :> es) => ForgePackage -> Text -> Eff es (Either (Int, Text) Text)
api p url = do
endpoint <- apiEndpoint p
let headers = case endpoint.authToken of
Expand Down Expand Up @@ -117,13 +125,13 @@ handleAPIError = \case
Left (status, err) -> error $ "Releases API returned " <> show status <> ": " <> Text.unpack err
Right output -> pure output

releases :: Driver :> es => ForgePackage -> Eff es [Release]
releases :: (AppDisplay :> es, Driver :> es) => ForgePackage -> Eff es [Release]
releases p =
api p "/releases"
>>= handleAPIError
>>= jsonDecode "Releases"

latestRelease :: Driver :> es => ForgePackage -> Eff es (Maybe Release)
latestRelease :: (AppDisplay :> es, Driver :> es) => ForgePackage -> Eff es (Maybe Release)
latestRelease p =
api p "/releases/latest" >>= \case
Left (404, _) -> pure Nothing
Expand All @@ -136,7 +144,7 @@ wantRelease p r =
-- the full list, so that the latest release can be checked on its own.
&& all ($ r.tag_name) (toFilters p.releaseFilter)

release :: forall es. Driver :> es => ForgePackage -> Eff es Release
release :: forall es. (AppDisplay :> es, Driver :> es) => ForgePackage -> Eff es Release
release p =
findRelease (latestRelease p)
`fromMaybeOrMM` findRelease (releases p)
Expand Down Expand Up @@ -164,7 +172,7 @@ ghFilters p = do
os <- drvOS
pure $ toFilters p.filters <> environmentFilters arch os

artifact :: Driver :> es => ForgePackage -> Eff es ReleaseArtifact
artifact :: (AppDisplay :> es, Driver :> es) => ForgePackage -> Eff es ReleaseArtifact
artifact p = do
r <- release p
fs <- ghFilters p
Expand Down
8 changes: 6 additions & 2 deletions test/Mybox/Display/ANSISpec.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Mybox.Display.ANSISpec where

import Data.Text qualified as Text
import Data.Version

import Mybox.Display
import Mybox.Display.ANSI
Expand All @@ -13,8 +14,11 @@ spec :: Spec
spec = do
describe "runANSIDisplay" $ do
let run = fmap snd . runTmux . runANSIDisplay @MDisplay
it "displays logs" $ do
run (displayLogText "hello") >>= (`shouldBe` "hello")
it "displays version" $ do
run (displayVersion $ makeVersion [1, 2, 3]) >>= (`shouldBe` "mybox 1.2.3")
it "displays warnings with a yellow label" $ do
output <- run $ displayWarning "missing token"
output `shouldBe` colourString "<yellow>Warning<reset>: missing token"
it "displays banner" $ do
output <- run $ displayBanner $ bannerChecking "check" <> bannerInstalling "install"

Expand Down
20 changes: 10 additions & 10 deletions test/Mybox/Display/SimpleSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ run act = let ((), logs) = runPureEff $ runSimpleDisplayPure @MDisplay act in lo
spec :: Spec
spec = do
describe "runSimpleDisplay" $ do
it "displays logs" $ do
it "displays warnings" $ do
let logs = run $ do
displayLogText "hello"
displayLogText "world"
logs `shouldBe` "hello\nworld\n"
displayWarning "missing token"
displayWarning "rate limit"
logs `shouldBe` "Warning: missing token\nWarning: rate limit\n"
it "displays version" $ do
let logs = run $ displayVersion $ makeVersion [1, 2, 3]
logs `shouldBe` "mybox 1.2.3\n"
it "displays and updates the banner" $ do
let logs = run $ do
displayLogText "hello"
displayBannerWhile (bannerInstalling "one") $ displayLogText "inside"
displayWarning "before"
displayBannerWhile (bannerInstalling "one") $ displayWarning "inside"

displayLogText "world"
displayWarning "after"
displayBanner $
bannerChecking "two" <> bannerInstalling "three" <> bannerInstalling "four"

logs
`shouldBe` unlines
[ "hello"
[ "Warning: before"
, "installing one"
, "inside"
, "world"
, "Warning: inside"
, "Warning: after"
, "installing four, three"
, "checking two"
]
4 changes: 2 additions & 2 deletions test/Mybox/DisplaySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ spec :: Spec
spec = sequential $ do
describe "runDisplay" $
it "selects simple display" $ do
((), output) <- writeHandle $ \h -> runDisplay h $ displayLogText "hello"
output `shouldBe` "hello\n"
((), output) <- writeHandle $ \h -> runDisplay h $ displayWarning "missing token"
output `shouldBe` "Warning: missing token\n"

describe "runDisplay in CI" $
it "selects simple display" $ do
Expand Down
13 changes: 13 additions & 0 deletions test/Mybox/Package/ForgeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module Mybox.Package.ForgeSpec where
import Data.Text qualified as Text

import Mybox.DesktopFile
import Mybox.Display
import Mybox.Display.SpecUtils
import Mybox.Driver
import Mybox.Driver.Test
import Mybox.Filters
import Mybox.Package.Archive
import Mybox.Package.Forge.Internal hiding (id)
Expand Down Expand Up @@ -92,6 +95,16 @@ spec = do
, (Nothing, "{\"repo\": \"example/example\", \"release\": {\"prefix\": [\"v\"], \"exclude\": [\"rc\"]}}")
]

it "warns when no GitHub token is available" $ do
let withoutToken args
| args == shellRaw "echo $GITHUB_TOKEN" = Just ""
| args == "gh" :| ["auth", "token"] = Just ""
| otherwise = Nothing
(endpoint, output) <-
runSimpleDisplayPure @MDisplay $ pureDriver withoutToken $ apiEndpoint $ mkForgePackage "owner/repo"
endpoint.authToken `shouldBe` Nothing
output `shouldContain` "Warning: No GitHub token for owner/repo"

packageSpecGen "neovim" $ neovimPackage False
packageSpecGen "neovim with root" $ neovimPackage True

Expand Down
Loading