{-# LANGUAGE OverloadedStrings #-}
module Gerrit
(
GerritClient,
withClient,
getClient,
getClientWithManager,
getVersion,
getChange,
queryChanges,
postReview,
getAccountId,
getAccount,
getProjects,
GerritVersion (..),
GerritQuery (..),
GerritChange (..),
GerritChangeStatus (..),
ReviewResult (..),
GerritAccount (..),
GerritAccountQuery (..),
GerritProjectQuery (..),
changeUrl,
serverUrl,
)
where
import Control.Exception (try)
import Data.List.NonEmpty (NonEmpty)
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Gerrit.Client
import Gerrit.Data.Account
import Gerrit.Data.Change
import Gerrit.Data.Project
import Gerrit.Data.Review
import Network.HTTP.Client (HttpException)
serverUrl :: GerritClient -> T.Text
serverUrl :: GerritClient -> Text
serverUrl = GerritClient -> Text
baseUrl
changeUrl :: GerritClient -> GerritChange -> T.Text
changeUrl :: GerritClient -> GerritChange -> Text
changeUrl GerritClient
client GerritChange
change = GerritClient -> Text
baseUrl GerritClient
client forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (forall a. Show a => a -> String
show (GerritChange -> Int
number GerritChange
change))
getVersion :: GerritClient -> IO GerritVersion
getVersion :: GerritClient -> IO GerritVersion
getVersion = forall a. FromJSON a => Text -> GerritClient -> IO a
gerritGet Text
"config/server/version"
getProjects ::
Int ->
GerritProjectQuery ->
Maybe Int ->
GerritClient ->
IO GerritProjectsMessage
getProjects :: Int
-> GerritProjectQuery
-> Maybe Int
-> GerritClient
-> IO GerritProjectsMessage
getProjects Int
count GerritProjectQuery
query Maybe Int
startM = forall a. FromJSON a => Text -> GerritClient -> IO a
gerritGet (Text
"projects/?" forall a. Semigroup a => a -> a -> a
<> Int -> GerritProjectQuery -> Maybe Int -> Text
projectQS Int
count GerritProjectQuery
query Maybe Int
startM)
queryChanges ::
Int ->
[GerritQuery] ->
Maybe Int ->
GerritClient ->
IO [GerritChange]
queryChanges :: Int
-> [GerritQuery] -> Maybe Int -> GerritClient -> IO [GerritChange]
queryChanges Int
count [GerritQuery]
queries Maybe Int
startM = forall a. FromJSON a => Text -> GerritClient -> IO a
gerritGet (Text
"changes/?" forall a. Semigroup a => a -> a -> a
<> Int -> [GerritQuery] -> Maybe Int -> Text
changeQS Int
count [GerritQuery]
queries Maybe Int
startM)
getChange :: Int -> GerritClient -> IO (Maybe GerritChange)
getChange :: Int -> GerritClient -> IO (Maybe GerritChange)
getChange Int
changeNumber GerritClient
client = do
Either HttpException GerritChange
res <-
forall e a. Exception e => IO a -> IO (Either e a)
try
( forall a. FromJSON a => Text -> GerritClient -> IO a
gerritGet (Text
"changes/" forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (forall a. Show a => a -> String
show Int
changeNumber) forall a. Semigroup a => a -> a -> a
<> Text
"?" forall a. Semigroup a => a -> a -> a
<> Text
defaultQueryChangeOptions) GerritClient
client
) ::
IO (Either HttpException GerritChange)
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ case Either HttpException GerritChange
res of
Right GerritChange
change -> forall a. a -> Maybe a
Just GerritChange
change
Left HttpException
_err -> forall a. Maybe a
Nothing
postReview ::
GerritChange ->
Text ->
Text ->
Int ->
GerritClient ->
IO ReviewResult
postReview :: GerritChange
-> Text -> Text -> Int -> GerritClient -> IO ReviewResult
postReview GerritChange
change Text
message Text
label Int
value' = forall a b.
(ToJSON a, FromJSON b) =>
Text -> a -> GerritClient -> IO b
gerritPost Text
urlPath ReviewInput
review
where
urlPath :: Text
urlPath = Text
"changes/" forall a. Semigroup a => a -> a -> a
<> Text
changeId forall a. Semigroup a => a -> a -> a
<> Text
"/revisions/" forall a. Semigroup a => a -> a -> a
<> Text
revHash forall a. Semigroup a => a -> a -> a
<> Text
"/review"
changeId :: Text
changeId = GerritChange -> Text
Gerrit.Data.Change.id GerritChange
change
revHash :: Text
revHash = forall a. a -> Maybe a -> a
fromMaybe Text
"" (GerritChange -> Maybe Text
Gerrit.Data.Change.current_revision GerritChange
change)
review :: ReviewInput
review =
ReviewInput
{ riMessage :: Maybe Text
riMessage = forall a. a -> Maybe a
Just Text
message,
riLabels :: Maybe (Map Text Int)
riLabels = forall a. a -> Maybe a
Just (forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Text
label, Int
value')])
}
getAccountId :: Int -> NonEmpty GerritAccountQuery -> GerritClient -> IO [GerritAccountId]
getAccountId :: Int
-> NonEmpty GerritAccountQuery
-> GerritClient
-> IO [GerritAccountId]
getAccountId Int
count NonEmpty GerritAccountQuery
queries = forall a. FromJSON a => Text -> GerritClient -> IO a
gerritGet (Text
"accounts/?" forall a. Semigroup a => a -> a -> a
<> Int -> NonEmpty GerritAccountQuery -> Text
accountQs Int
count NonEmpty GerritAccountQuery
queries)
getAccount :: Int -> NonEmpty GerritAccountQuery -> GerritClient -> IO [GerritAccount]
getAccount :: Int
-> NonEmpty GerritAccountQuery
-> GerritClient
-> IO [GerritAccount]
getAccount Int
count NonEmpty GerritAccountQuery
queries = forall a. FromJSON a => Text -> GerritClient -> IO a
gerritGet (Text
"accounts/?" forall a. Semigroup a => a -> a -> a
<> Int -> NonEmpty GerritAccountQuery -> Text
accountQs Int
count NonEmpty GerritAccountQuery
queries forall a. Semigroup a => a -> a -> a
<> Text
"&o=DETAILS")