{-# LANGUAGE CPP, PatternGuards #-}
-- This is a quick hack for uploading build reports to Hackage.

module Distribution.Client.BuildReports.Upload
    ( BuildLog
    , BuildReportId
    , uploadReports
    ) where

import Distribution.Client.Compat.Prelude
import Prelude ()

{-
import Network.Browser
         ( BrowserAction, request, setAllowRedirects )
import Network.HTTP
         ( Header(..), HeaderName(..)
         , Request(..), RequestMethod(..), Response(..) )
import Network.TCP (HandleStream)
-}
import Network.URI (URI, uriPath) --parseRelativeReference, relativeTo)

import System.FilePath.Posix
         ( (</>) )
import qualified Distribution.Client.BuildReports.Anonymous as BuildReport
import Distribution.Client.BuildReports.Anonymous (BuildReport, showBuildReport)
import Distribution.Simple.Utils (die')
import Distribution.Client.HttpUtils
import Distribution.Client.Setup
         ( RepoContext(..) )

type BuildReportId = URI
type BuildLog = String

uploadReports :: Verbosity -> RepoContext -> (String, String) -> URI -> [(BuildReport, Maybe BuildLog)] -> IO ()
uploadReports :: Verbosity
-> RepoContext
-> (String, String)
-> URI
-> [(BuildReport, Maybe String)]
-> IO ()
uploadReports Verbosity
verbosity RepoContext
repoCtxt (String, String)
auth URI
uri [(BuildReport, Maybe String)]
reports = do
  forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [(BuildReport, Maybe String)]
reports forall a b. (a -> b) -> a -> b
$ \(BuildReport
report, Maybe String
mbBuildLog) -> do
     URI
buildId <- Verbosity
-> RepoContext -> (String, String) -> URI -> BuildReport -> IO URI
postBuildReport Verbosity
verbosity RepoContext
repoCtxt (String, String)
auth URI
uri BuildReport
report
     case Maybe String
mbBuildLog of
       Just String
buildLog -> Verbosity
-> RepoContext -> (String, String) -> URI -> String -> IO ()
putBuildLog Verbosity
verbosity RepoContext
repoCtxt (String, String)
auth URI
buildId String
buildLog
       Maybe String
Nothing       -> forall (m :: * -> *) a. Monad m => a -> m a
return ()

postBuildReport :: Verbosity -> RepoContext -> (String, String) -> URI -> BuildReport -> IO BuildReportId
postBuildReport :: Verbosity
-> RepoContext -> (String, String) -> URI -> BuildReport -> IO URI
postBuildReport Verbosity
verbosity RepoContext
repoCtxt (String, String)
auth URI
uri BuildReport
buildReport = do
  let fullURI :: URI
fullURI = URI
uri { uriPath :: String
uriPath = String
"/package" String -> String -> String
</> forall a. Pretty a => a -> String
prettyShow (BuildReport -> PackageIdentifier
BuildReport.package BuildReport
buildReport) String -> String -> String
</> String
"reports" }
  HttpTransport
transport <- RepoContext -> IO HttpTransport
repoContextGetTransport RepoContext
repoCtxt
  (HttpCode, String)
res <- HttpTransport
-> Verbosity
-> URI
-> String
-> Maybe (String, String)
-> IO (HttpCode, String)
postHttp HttpTransport
transport Verbosity
verbosity URI
fullURI (BuildReport -> String
showBuildReport BuildReport
buildReport) (forall a. a -> Maybe a
Just (String, String)
auth)
  case (HttpCode, String)
res of
    (HttpCode
303, String
redir) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => a
undefined String
redir --TODO parse redir
    (HttpCode, String)
_ -> forall a. Verbosity -> String -> IO a
die' Verbosity
verbosity String
"unrecognized response" -- give response

{-
  setAllowRedirects False
  (_, response) <- request Request {
    rqURI     = uri { uriPath = "/package" </> prettyShow (BuildReport.package buildReport) </> "reports" },
    rqMethod  = POST,
    rqHeaders = [Header HdrContentType   ("text/plain"),
                 Header HdrContentLength (show (length body)),
                 Header HdrAccept        ("text/plain")],
    rqBody    = body
  }
  case rspCode response of
    (3,0,3) | [Just buildId] <- [ do rel <- parseRelativeReference location
#if defined(VERSION_network_uri)
                                     return $ relativeTo rel uri
#elif defined(VERSION_network)
#if MIN_VERSION_network(2,4,0)
                                     return $ relativeTo rel uri
#else
                                     relativeTo rel uri
#endif
#endif
                                  | Header HdrLocation location <- rspHeaders response ]
              -> return $ buildId
    _         -> error "Unrecognised response from server."
  where body  = BuildReport.show buildReport
-}


-- TODO force this to be a PUT?

putBuildLog :: Verbosity -> RepoContext -> (String, String)
            -> BuildReportId -> BuildLog
            -> IO ()
putBuildLog :: Verbosity
-> RepoContext -> (String, String) -> URI -> String -> IO ()
putBuildLog Verbosity
verbosity RepoContext
repoCtxt (String, String)
auth URI
reportId String
buildLog = do
  let fullURI :: URI
fullURI = URI
reportId {uriPath :: String
uriPath = URI -> String
uriPath URI
reportId String -> String -> String
</> String
"log"}
  HttpTransport
transport <- RepoContext -> IO HttpTransport
repoContextGetTransport RepoContext
repoCtxt
  (HttpCode, String)
res <- HttpTransport
-> Verbosity
-> URI
-> String
-> Maybe (String, String)
-> IO (HttpCode, String)
postHttp HttpTransport
transport Verbosity
verbosity URI
fullURI String
buildLog (forall a. a -> Maybe a
Just (String, String)
auth)
  case (HttpCode, String)
res of
    (HttpCode
200, String
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
    (HttpCode, String)
_ -> forall a. Verbosity -> String -> IO a
die' Verbosity
verbosity String
"unrecognized response" -- give response