diff --git a/src/Niv/GitHub.hs b/src/Niv/GitHub.hs index 439e2ff..d52488d 100644 --- a/src/Niv/GitHub.hs +++ b/src/Niv/GitHub.hs @@ -80,7 +80,10 @@ data GithubRepo = GithubRepo githubRepo :: T.Text -> T.Text -> IO GithubRepo githubRepo owner repo = do request <- defaultRequest ["repos", owner, repo] - resp <- HTTP.httpJSONEither request -- >>= \case + -- we don't use httpJSONEither because it adds an "Accept: + -- application/json" header that GitHub chokes on + resp0 <- HTTP.httpBS request + let resp = fmap Aeson.eitherDecodeStrict resp0 case (HTTP.getResponseStatusCode resp, HTTP.getResponseBody resp) of (200, Right (Aeson.Object m)) -> do let lookupText k = case HMS.lookup k m of @@ -95,12 +98,12 @@ githubRepo owner repo = do error $ "expected object, got " <> show v (200, Left e) -> do error $ "github didn't return JSON: " <> show e - _ -> abortCouldNotFetchGitHubRepo (tshow resp) (owner, repo) + _ -> abortCouldNotFetchGitHubRepo (tshow (request,resp0)) (owner, repo) -- | TODO: Error instead of T.Text? abortCouldNotFetchGitHubRepo :: T.Text -> (T.Text, T.Text) -> IO a abortCouldNotFetchGitHubRepo e (T.unpack -> owner, T.unpack -> repo) = do - putStrLn $ unlines [ line1, line2, line3 ] + putStrLn $ unlines [ line1, line2, T.unpack line3 ] exitFailure where line1 = "WARNING: Could not read from GitHub repo: " <> owner <> "/" <> repo @@ -118,7 +121,7 @@ If not, try re-adding it: Make sure the repository exists. |] - line3 = unwords [ "(Error was:", show e, ")" ] + line3 = T.unwords [ "(Error was:", e, ")" ] defaultRequest :: [T.Text] -> IO HTTP.Request defaultRequest (map T.encodeUtf8 -> parts) = do @@ -129,6 +132,7 @@ defaultRequest (map T.encodeUtf8 -> parts) = do HTTP.addRequestHeader "authorization" ("token " <> BS8.pack token) ) $ HTTP.setRequestPath path $ + HTTP.addRequestHeader "user-agent" "niv" $ HTTP.addRequestHeader "accept" "application/vnd.github.v3+json" $ HTTP.setRequestSecure githubSecure $ HTTP.setRequestHost (T.encodeUtf8 githubApiHost) $ @@ -185,7 +189,7 @@ githubApiPort :: Int githubApiPort = unsafePerformIO $ do lookupEnv "GITHUB_API_PORT" >>= \case Just (readMaybe -> Just x) -> pure x - _ -> pure 80 + _ -> pure $ if githubSecure then 443 else 80 githubApiHost :: T.Text githubApiHost = unsafePerformIO $ do diff --git a/tests/default.nix b/tests/default.nix index 6defc1c..ba31e81 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -18,15 +18,6 @@ let niv_HEAD = "a489b65a5c3a29983701069d1ce395b23d9bde64"; niv_HEAD- = "abc51449406ba3279c466b4d356b4ae8522ceb58"; nixpkgs-channels_HEAD = "571b40d3f50466d3e91c1e609d372de96d782793"; - nivForTest = niv.overrideDerivation(old: { - # TODO: Remove this patch by adding an argument to the github - # subcommand to support GitHub entreprise. - prePatch = '' - sed 's|githubHost = "github.com"|githubHost = "localhost:3333"|' -i src/Niv/GitHub.hs - sed 's|githuApiHost = "github.com"|githuApiHost = "localhost:3333"|' -i src/Niv/GitHub.hs - sed 's|githubSecure = True|githubSecure = False|' -i src/Niv/GitHub.hs - ''; - }); in pkgs.runCommand "test" { buildInputs = [ pkgs.haskellPackages.wai-app-static