{-# LANGUAGE OverloadedStrings #-}
module GitLab.API.Repositories where
import Control.Monad.IO.Unlift
import qualified Data.ByteString.Lazy as BSL
import Data.Either
import qualified Data.Text as T
import GitLab.Types
import GitLab.WebRequests.GitLabWebCalls
import Network.HTTP.Client
repositories ::
Project ->
GitLab [Repository]
repositories :: Project -> GitLab [Repository]
repositories Project
project =
[Repository]
-> Either (Response ByteString) [Repository] -> [Repository]
forall b a. b -> Either a b -> b
fromRight ([Char] -> [Repository]
forall a. HasCallStack => [Char] -> a
error [Char]
"repositories error") (Either (Response ByteString) [Repository] -> [Repository])
-> ReaderT
GitLabState IO (Either (Response ByteString) [Repository])
-> GitLab [Repository]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> ReaderT
GitLabState IO (Either (Response ByteString) [Repository])
repositories' (Project -> Int
project_id Project
project)
repositories' ::
Int ->
GitLab (Either (Response BSL.ByteString) [Repository])
repositories' :: Int
-> ReaderT
GitLabState IO (Either (Response ByteString) [Repository])
repositories' Int
projectId =
Text
-> [GitLabParam]
-> ReaderT
GitLabState IO (Either (Response ByteString) [Repository])
forall a.
FromJSON a =>
Text -> [GitLabParam] -> GitLab (Either (Response ByteString) [a])
gitlabGetMany Text
addr []
where
addr :: Text
addr =
Text
"/projects/"
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
projectId)
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/repository"
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/tree"
getFileArchive ::
Project ->
ArchiveFormat ->
FilePath ->
GitLab (Either (Response BSL.ByteString) ())
getFileArchive :: Project
-> ArchiveFormat
-> [Char]
-> GitLab (Either (Response ByteString) ())
getFileArchive Project
project = Int
-> ArchiveFormat
-> [Char]
-> GitLab (Either (Response ByteString) ())
getFileArchive' (Project -> Int
project_id Project
project)
getFileArchiveBS ::
Project ->
ArchiveFormat ->
GitLab (Either (Response BSL.ByteString) BSL.ByteString)
getFileArchiveBS :: Project
-> ArchiveFormat
-> GitLab (Either (Response ByteString) ByteString)
getFileArchiveBS Project
project ArchiveFormat
format = do
Either (Response ByteString) (Maybe ByteString)
result <- Int
-> ArchiveFormat
-> GitLab (Either (Response ByteString) (Maybe ByteString))
getFileArchiveBS' (Project -> Int
project_id Project
project) ArchiveFormat
format
case Either (Response ByteString) (Maybe ByteString)
result of
Left Response ByteString
resp -> Either (Response ByteString) ByteString
-> GitLab (Either (Response ByteString) ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (Response ByteString -> Either (Response ByteString) ByteString
forall a b. a -> Either a b
Left Response ByteString
resp)
Right Maybe ByteString
Nothing -> [Char] -> GitLab (Either (Response ByteString) ByteString)
forall a. HasCallStack => [Char] -> a
error [Char]
"could not download file"
Right (Just ByteString
bs) -> Either (Response ByteString) ByteString
-> GitLab (Either (Response ByteString) ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Either (Response ByteString) ByteString
forall a b. b -> Either a b
Right ByteString
bs)
getFileArchive' ::
Int ->
ArchiveFormat ->
FilePath ->
GitLab (Either (Response BSL.ByteString) ())
getFileArchive' :: Int
-> ArchiveFormat
-> [Char]
-> GitLab (Either (Response ByteString) ())
getFileArchive' Int
projectId ArchiveFormat
format [Char]
fPath = do
Either (Response ByteString) (Maybe ByteString)
attempt <- Int
-> ArchiveFormat
-> GitLab (Either (Response ByteString) (Maybe ByteString))
getFileArchiveBS' Int
projectId ArchiveFormat
format
case Either (Response ByteString) (Maybe ByteString)
attempt of
Left Response ByteString
st -> Either (Response ByteString) ()
-> GitLab (Either (Response ByteString) ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Response ByteString -> Either (Response ByteString) ()
forall a b. a -> Either a b
Left Response ByteString
st)
Right Maybe ByteString
Nothing ->
() -> Either (Response ByteString) ()
forall a b. b -> Either a b
Right (() -> Either (Response ByteString) ())
-> ReaderT GitLabState IO ()
-> GitLab (Either (Response ByteString) ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> ReaderT GitLabState IO ()
forall a. HasCallStack => [Char] -> a
error [Char]
"cannot download file"
Right (Just ByteString
archiveData) ->
() -> Either (Response ByteString) ()
forall a b. b -> Either a b
Right (() -> Either (Response ByteString) ())
-> ReaderT GitLabState IO ()
-> GitLab (Either (Response ByteString) ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO () -> ReaderT GitLabState IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ([Char] -> ByteString -> IO ()
BSL.writeFile [Char]
fPath ByteString
archiveData)
getFileArchiveBS' ::
Int ->
ArchiveFormat ->
GitLab (Either (Response BSL.ByteString) (Maybe BSL.ByteString))
getFileArchiveBS' :: Int
-> ArchiveFormat
-> GitLab (Either (Response ByteString) (Maybe ByteString))
getFileArchiveBS' Int
projectId ArchiveFormat
format = do
Either (Response ByteString) (Maybe Bool)
result <- Text
-> [GitLabParam]
-> GitLab (Either (Response ByteString) (Maybe Bool))
forall a.
FromJSON a =>
Text
-> [GitLabParam] -> GitLab (Either (Response ByteString) (Maybe a))
gitlabGetOne Text
addr [] :: GitLab (Either (Response BSL.ByteString) (Maybe Bool))
case Either (Response ByteString) (Maybe Bool)
result of
Left Response ByteString
response -> Either (Response ByteString) (Maybe ByteString)
-> GitLab (Either (Response ByteString) (Maybe ByteString))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe ByteString -> Either (Response ByteString) (Maybe ByteString)
forall a b. b -> Either a b
Right (ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (Response ByteString -> ByteString
forall body. Response body -> body
responseBody Response ByteString
response)))
Right Maybe Bool
_b -> [Char] -> GitLab (Either (Response ByteString) (Maybe ByteString))
forall a. HasCallStack => [Char] -> a
error [Char]
"impossible"
where
addr :: Text
addr =
Text
"/projects/"
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
projectId)
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/repository"
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/archive"
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (ArchiveFormat -> [Char]
forall a. Show a => a -> [Char]
show ArchiveFormat
format)