{-# LANGUAGE OverloadedStrings #-}
module GitLab.API.Repositories
(
repositoryTree,
fileArchive,
fileArchiveBS,
contributors,
mergeBase,
)
where
import Control.Monad.IO.Class
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Data.Either
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import GitLab.Types
import GitLab.WebRequests.GitLabWebCalls
import Network.HTTP.Client
import Network.HTTP.Types.Status
repositoryTree ::
Project ->
GitLab [Repository]
repositoryTree :: Project -> GitLab [Repository]
repositoryTree Project
project =
forall b a. b -> Either a b -> b
fromRight (forall a. HasCallStack => [Char] -> a
error [Char]
"repositories error") forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> GitLab (Either (Response ByteString) [Repository])
repositories' (Project -> Int
project_id Project
project)
repositories' ::
Int ->
GitLab (Either (Response BSL.ByteString) [Repository])
repositories' :: Int -> GitLab (Either (Response ByteString) [Repository])
repositories' Int
projectId =
forall a.
FromJSON a =>
Text -> [GitLabParam] -> GitLab (Either (Response ByteString) [a])
gitlabGetMany Text
addr []
where
addr :: Text
addr =
Text
"/projects/"
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (forall a. Show a => a -> [Char]
show Int
projectId)
forall a. Semigroup a => a -> a -> a
<> Text
"/repository"
forall a. Semigroup a => a -> a -> a
<> Text
"/tree"
fileArchive ::
Project ->
ArchiveFormat ->
FilePath ->
GitLab (Either (Response BSL.ByteString) ())
fileArchive :: Project
-> ArchiveFormat
-> [Char]
-> GitLab (Either (Response ByteString) ())
fileArchive Project
project = Int
-> ArchiveFormat
-> [Char]
-> GitLab (Either (Response ByteString) ())
getFileArchive' (Project -> Int
project_id Project
project)
fileArchiveBS ::
Project ->
ArchiveFormat ->
GitLab (Either (Response BSL.ByteString) BSL.ByteString)
fileArchiveBS :: Project
-> ArchiveFormat
-> GitLab (Either (Response ByteString) ByteString)
fileArchiveBS 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 -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left Response ByteString
resp)
Right Maybe ByteString
Nothing -> forall a. HasCallStack => [Char] -> a
error [Char]
"could not download file"
Right (Just ByteString
bs) -> forall (m :: * -> *) a. Monad m => a -> m a
return (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 -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left Response ByteString
st)
Right Maybe ByteString
Nothing ->
forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. HasCallStack => [Char] -> a
error [Char]
"cannot download file"
Right (Just ByteString
archiveData) ->
forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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
Response ByteString
result <- Text -> [GitLabParam] -> GitLab (Response ByteString)
gitlabGetByteStringResponse Text
addr []
let (Status Int
n ByteString
_msg) = forall body. Response body -> Status
responseStatus Response ByteString
result
if Int
n forall a. Ord a => a -> a -> Bool
>= Int
200 Bool -> Bool -> Bool
&& Int
n forall a. Ord a => a -> a -> Bool
<= Int
226
then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right (forall a. a -> Maybe a
Just (forall body. Response body -> body
responseBody Response ByteString
result)))
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left Response ByteString
result)
where
addr :: Text
addr =
Text
"/projects/"
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (forall a. Show a => a -> [Char]
show Int
projectId)
forall a. Semigroup a => a -> a -> a
<> Text
"/repository"
forall a. Semigroup a => a -> a -> a
<> Text
"/archive"
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (forall a. Show a => a -> [Char]
show ArchiveFormat
format)
contributors ::
Project ->
Maybe OrderBy ->
Maybe SortBy ->
GitLab [Contributor]
contributors :: Project -> Maybe OrderBy -> Maybe SortBy -> GitLab [Contributor]
contributors Project
prj Maybe OrderBy
order Maybe SortBy
sort =
forall b a. b -> Either a b -> b
fromRight (forall a. HasCallStack => [Char] -> a
error [Char]
"contributors error")
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a.
FromJSON a =>
Text -> [GitLabParam] -> GitLab (Either (Response ByteString) [a])
gitlabGetMany Text
addr [GitLabParam]
params
where
addr :: Text
addr =
Text
"/projects/"
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (forall a. Show a => a -> [Char]
show (Project -> Int
project_id Project
prj))
forall a. Semigroup a => a -> a -> a
<> Text
"/repository"
forall a. Semigroup a => a -> a -> a
<> Text
"/contributors"
params :: [GitLabParam]
params :: [GitLabParam]
params =
forall a. [Maybe a] -> [a]
catMaybes
[ (\SortBy
x -> forall a. a -> Maybe a
Just (ByteString
"sort", forall a. Show a => a -> Maybe ByteString
showAttr SortBy
x)) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe SortBy
sort,
(\OrderBy
x -> forall a. a -> Maybe a
Just (ByteString
"order_by", forall a. Show a => a -> Maybe ByteString
showAttr OrderBy
x)) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe OrderBy
order
]
showAttr :: (Show a) => a -> Maybe BS.ByteString
showAttr :: forall a. Show a => a -> Maybe ByteString
showAttr = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> [Char]
show
mergeBase ::
Project ->
[Text] ->
GitLab (Either (Response BSL.ByteString) (Maybe Commit))
mergeBase :: Project
-> [Text] -> GitLab (Either (Response ByteString) (Maybe Commit))
mergeBase Project
prj [Text]
refs =
forall a.
FromJSON a =>
Text
-> [GitLabParam] -> GitLab (Either (Response ByteString) (Maybe a))
gitlabGetOne
Text
addr
[GitLabParam]
params
where
addr :: Text
addr =
Text
"/projects/"
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (forall a. Show a => a -> [Char]
show (Project -> Int
project_id Project
prj))
forall a. Semigroup a => a -> a -> a
<> Text
"/repository"
forall a. Semigroup a => a -> a -> a
<> Text
"/merge_base"
params :: [GitLabParam]
params :: [GitLabParam]
params =
forall a b. (a -> b) -> [a] -> [b]
map (\Text
ref -> (ByteString
"refs[]", forall a. a -> Maybe a
Just (Text -> ByteString
T.encodeUtf8 Text
ref))) [Text]
refs