{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StrictData #-}
module GitLab.API.Events
(
currentUserEvents,
userEvents,
userEvents',
projectEvents,
projectEvents',
groupEvents,
groupEvents',
EventFilterAttrs (..),
defaultEventFilters,
)
where
import Control.Monad.Except
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Time.Clock
import Data.Time.Format.ISO8601
import GitLab.Types
import GitLab.WebRequests.GitLabWebCalls
import Network.HTTP.Client
data EventFilterAttrs = EventFilterAttrs
{
EventFilterAttrs -> Maybe EventActionName
eventFilter_action :: Maybe EventActionName,
EventFilterAttrs -> Maybe EventTargetType
eventFilter_target_type :: Maybe EventTargetType,
EventFilterAttrs -> Maybe UTCTime
eventFilter_before :: Maybe UTCTime,
EventFilterAttrs -> Maybe UTCTime
eventFilter_after :: Maybe UTCTime,
EventFilterAttrs -> Maybe SortBy
eventFilter_sort :: Maybe SortBy
}
defaultEventFilters :: EventFilterAttrs
defaultEventFilters :: EventFilterAttrs
defaultEventFilters =
EventFilterAttrs
{ eventFilter_action :: Maybe EventActionName
eventFilter_action = Maybe EventActionName
forall a. Maybe a
Nothing,
eventFilter_target_type :: Maybe EventTargetType
eventFilter_target_type = Maybe EventTargetType
forall a. Maybe a
Nothing,
eventFilter_before :: Maybe UTCTime
eventFilter_before = Maybe UTCTime
forall a. Maybe a
Nothing,
eventFilter_after :: Maybe UTCTime
eventFilter_after = Maybe UTCTime
forall a. Maybe a
Nothing,
eventFilter_sort :: Maybe SortBy
eventFilter_sort = Maybe SortBy
forall a. Maybe a
Nothing
}
currentUserEvents ::
EventFilterAttrs ->
GitLab [Event]
currentUserEvents :: EventFilterAttrs -> GitLab [Event]
currentUserEvents EventFilterAttrs
attrs = do
Either (Response ByteString) [Event]
result <- Text
-> [GitLabParam] -> GitLab (Either (Response ByteString) [Event])
forall a.
FromJSON a =>
Text -> [GitLabParam] -> GitLab (Either (Response ByteString) [a])
gitlabGetMany Text
"/events" (EventFilterAttrs -> [GitLabParam]
eventFilters EventFilterAttrs
attrs)
case Either (Response ByteString) [Event]
result of
Left Response ByteString
er -> GitLabError -> GitLab [Event]
forall a. GitLabError -> GitLabT (ExceptT GitLabError IO) a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> GitLabError
GitLabError (Text
"currentUserEvents error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Response ByteString -> Text
forall payload. Response payload -> Text
responseErrorText Response ByteString
er))
Right [Event]
xs -> [Event] -> GitLab [Event]
forall a. a -> GitLabT (ExceptT GitLabError IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [Event]
xs
userEvents ::
User ->
EventFilterAttrs ->
GitLab [Event]
userEvents :: User -> EventFilterAttrs -> GitLab [Event]
userEvents User
usr EventFilterAttrs
attrs = do
Either (Response ByteString) [Event]
result <- Int
-> EventFilterAttrs
-> GitLab (Either (Response ByteString) [Event])
userEvents' (User -> Int
user_id User
usr) EventFilterAttrs
attrs
case Either (Response ByteString) [Event]
result of
Left Response ByteString
er -> GitLabError -> GitLab [Event]
forall a. GitLabError -> GitLabT (ExceptT GitLabError IO) a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> GitLabError
GitLabError (Text
"userEvents error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Response ByteString -> Text
forall payload. Response payload -> Text
responseErrorText Response ByteString
er))
Right [Event]
xs -> [Event] -> GitLab [Event]
forall a. a -> GitLabT (ExceptT GitLabError IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [Event]
xs
userEvents' ::
Int ->
EventFilterAttrs ->
GitLab (Either (Response BSL.ByteString) [Event])
userEvents' :: Int
-> EventFilterAttrs
-> GitLab (Either (Response ByteString) [Event])
userEvents' Int
userId EventFilterAttrs
attrs =
Text
-> [GitLabParam] -> GitLab (Either (Response ByteString) [Event])
forall a.
FromJSON a =>
Text -> [GitLabParam] -> GitLab (Either (Response ByteString) [a])
gitlabGetMany Text
urlPath (EventFilterAttrs -> [GitLabParam]
eventFilters EventFilterAttrs
attrs)
where
urlPath :: Text
urlPath =
String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$
String
"/users/"
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
userId
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/events"
projectEvents ::
Project ->
EventFilterAttrs ->
GitLab [Event]
projectEvents :: Project -> EventFilterAttrs -> GitLab [Event]
projectEvents Project
proj EventFilterAttrs
attrs = do
Either (Response ByteString) [Event]
result <- Int
-> EventFilterAttrs
-> GitLab (Either (Response ByteString) [Event])
projectEvents' (Project -> Int
project_id Project
proj) EventFilterAttrs
attrs
case Either (Response ByteString) [Event]
result of
Left Response ByteString
er -> GitLabError -> GitLab [Event]
forall a. GitLabError -> GitLabT (ExceptT GitLabError IO) a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> GitLabError
GitLabError (Text
"projectEvents error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Response ByteString -> Text
forall payload. Response payload -> Text
responseErrorText Response ByteString
er))
Right [Event]
xs -> [Event] -> GitLab [Event]
forall a. a -> GitLabT (ExceptT GitLabError IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [Event]
xs
projectEvents' ::
Int ->
EventFilterAttrs ->
GitLab (Either (Response BSL.ByteString) [Event])
projectEvents' :: Int
-> EventFilterAttrs
-> GitLab (Either (Response ByteString) [Event])
projectEvents' Int
projectId EventFilterAttrs
attrs =
Text
-> [GitLabParam] -> GitLab (Either (Response ByteString) [Event])
forall a.
FromJSON a =>
Text -> [GitLabParam] -> GitLab (Either (Response ByteString) [a])
gitlabGetMany Text
urlPath (EventFilterAttrs -> [GitLabParam]
eventFilters EventFilterAttrs
attrs)
where
urlPath :: Text
urlPath =
String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$
String
"/projects/"
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
projectId
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/events"
groupEvents ::
Group ->
EventFilterAttrs ->
GitLab [Event]
groupEvents :: Group -> EventFilterAttrs -> GitLab [Event]
groupEvents Group
grp EventFilterAttrs
attrs = do
Either (Response ByteString) [Event]
result <- Int
-> EventFilterAttrs
-> GitLab (Either (Response ByteString) [Event])
groupEvents' (Group -> Int
group_id Group
grp) EventFilterAttrs
attrs
case Either (Response ByteString) [Event]
result of
Left Response ByteString
er -> GitLabError -> GitLab [Event]
forall a. GitLabError -> GitLabT (ExceptT GitLabError IO) a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> GitLabError
GitLabError (Text
"groupEvents error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Response ByteString -> Text
forall payload. Response payload -> Text
responseErrorText Response ByteString
er))
Right [Event]
xs -> [Event] -> GitLab [Event]
forall a. a -> GitLabT (ExceptT GitLabError IO) a
forall (m :: * -> *) a. Monad m => a -> m a
return [Event]
xs
groupEvents' ::
Int ->
EventFilterAttrs ->
GitLab (Either (Response BSL.ByteString) [Event])
groupEvents' :: Int
-> EventFilterAttrs
-> GitLab (Either (Response ByteString) [Event])
groupEvents' Int
groupId EventFilterAttrs
attrs =
Text
-> [GitLabParam] -> GitLab (Either (Response ByteString) [Event])
forall a.
FromJSON a =>
Text -> [GitLabParam] -> GitLab (Either (Response ByteString) [a])
gitlabGetMany Text
urlPath (EventFilterAttrs -> [GitLabParam]
eventFilters EventFilterAttrs
attrs)
where
urlPath :: Text
urlPath =
String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$
String
"/groups/"
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
groupId
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/events"
eventFilters :: EventFilterAttrs -> [GitLabParam]
eventFilters :: EventFilterAttrs -> [GitLabParam]
eventFilters EventFilterAttrs
attrs =
[Maybe GitLabParam] -> [GitLabParam]
forall a. [Maybe a] -> [a]
catMaybes
[ (\EventActionName
a -> GitLabParam -> Maybe GitLabParam
forall a. a -> Maybe a
Just (ByteString
"action", Text -> Maybe ByteString
encodeText (EventActionName -> Text
actionParam EventActionName
a))) (EventActionName -> Maybe GitLabParam)
-> Maybe EventActionName -> Maybe GitLabParam
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< EventFilterAttrs -> Maybe EventActionName
eventFilter_action EventFilterAttrs
attrs,
(\EventTargetType
t -> GitLabParam -> Maybe GitLabParam
forall a. a -> Maybe a
Just (ByteString
"target_type", Text -> Maybe ByteString
encodeText (EventTargetType -> Text
targetTypeParam EventTargetType
t))) (EventTargetType -> Maybe GitLabParam)
-> Maybe EventTargetType -> Maybe GitLabParam
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< EventFilterAttrs -> Maybe EventTargetType
eventFilter_target_type EventFilterAttrs
attrs,
(\UTCTime
d -> GitLabParam -> Maybe GitLabParam
forall a. a -> Maybe a
Just (ByteString
"before", Text -> Maybe ByteString
encodeText (UTCTime -> Text
showTime UTCTime
d))) (UTCTime -> Maybe GitLabParam)
-> Maybe UTCTime -> Maybe GitLabParam
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< EventFilterAttrs -> Maybe UTCTime
eventFilter_before EventFilterAttrs
attrs,
(\UTCTime
d -> GitLabParam -> Maybe GitLabParam
forall a. a -> Maybe a
Just (ByteString
"after", Text -> Maybe ByteString
encodeText (UTCTime -> Text
showTime UTCTime
d))) (UTCTime -> Maybe GitLabParam)
-> Maybe UTCTime -> Maybe GitLabParam
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< EventFilterAttrs -> Maybe UTCTime
eventFilter_after EventFilterAttrs
attrs,
(\SortBy
s -> GitLabParam -> Maybe GitLabParam
forall a. a -> Maybe a
Just (ByteString
"sort", Text -> Maybe ByteString
encodeText (String -> Text
T.pack (SortBy -> String
forall a. Show a => a -> String
show SortBy
s)))) (SortBy -> Maybe GitLabParam) -> Maybe SortBy -> Maybe GitLabParam
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< EventFilterAttrs -> Maybe SortBy
eventFilter_sort EventFilterAttrs
attrs
]
where
encodeText :: Text -> Maybe BS.ByteString
encodeText :: Text -> Maybe ByteString
encodeText = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> (Text -> ByteString) -> Text -> Maybe ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
showTime :: UTCTime -> Text
showTime :: UTCTime -> Text
showTime = String -> Text
T.pack (String -> Text) -> (UTCTime -> String) -> UTCTime -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> String
forall t. ISO8601 t => t -> String
iso8601Show
actionParam :: EventActionName -> Text
actionParam :: EventActionName -> Text
actionParam EventActionName
ANOpened = Text
"opened"
actionParam EventActionName
ANClosed = Text
"closed"
actionParam EventActionName
ANPushed = Text
"pushed"
actionParam EventActionName
ANCommentedOn = Text
"commented"
actionParam EventActionName
ANMerged = Text
"merged"
actionParam EventActionName
ANCreated = Text
"created"
actionParam EventActionName
ANUpdated = Text
"updated"
actionParam EventActionName
ANApproved = Text
"approved"
actionParam EventActionName
ANReopened = Text
"reopened"
actionParam EventActionName
ANLeft = Text
"left"
actionParam EventActionName
ANJoined = Text
"joined"
actionParam EventActionName
ANDestroyed = Text
"destroyed"
actionParam EventActionName
ANExpired = Text
"expired"
actionParam EventActionName
ANDeleted = Text
"deleted"
actionParam EventActionName
ANAccepted = Text
"accepted"
actionParam (ANOther Text
t) = Text
t
targetTypeParam :: EventTargetType -> Text
targetTypeParam :: EventTargetType -> Text
targetTypeParam EventTargetType
ETTIssue = Text
"issue"
targetTypeParam EventTargetType
ETTMilestone = Text
"milestone"
targetTypeParam EventTargetType
ETTMergeRequest = Text
"merge_request"
targetTypeParam EventTargetType
ETTNote = Text
"note"
targetTypeParam EventTargetType
ETTProject = Text
"project"
targetTypeParam EventTargetType
ETTSnippet = Text
"snippet"
targetTypeParam EventTargetType
ETTUser = Text
"user"
targetTypeParam EventTargetType
ETTWikiPage = Text
"wiki_page"
targetTypeParam EventTargetType
ETTDesign = Text
"design"
targetTypeParam (ETTOther Text
t) = Text
t