-----------------------------------------------------------------------------
-- |
-- License     :  BSD-3-Clause
-- Maintainer  :  Oleg Grenrus <oleg.grenrus@iki.fi>
--
-- The Github issue comments API from
-- <http://developer.github.com/v3/issues/comments/>.
module GitHub.Endpoints.Issues.Comments (
    comment,
    commentR,
    comments,
    commentsR,
    comments',
    createComment,
    createCommentR,
    deleteComment,
    deleteCommentR,
    editComment,
    editCommentR,
    module GitHub.Data,
    ) where

import GitHub.Data
import GitHub.Internal.Prelude
import GitHub.Request
import Prelude ()

-- | A specific comment, by ID.
--
-- > comment "thoughtbot" "paperclip" 1468184
comment :: Name Owner -> Name Repo -> Id Comment -> IO (Either Error IssueComment)
comment :: Name Owner
-> Name Repo -> Id Comment -> IO (Either Error IssueComment)
comment user :: Name Owner
user repo :: Name Repo
repo cid :: Id Comment
cid =
    GenRequest 'MtJSON 'RO IssueComment
-> IO (Either Error IssueComment)
forall (mt :: MediaType *) a.
ParseResponse mt a =>
GenRequest mt 'RO a -> IO (Either Error a)
executeRequest' (GenRequest 'MtJSON 'RO IssueComment
 -> IO (Either Error IssueComment))
-> GenRequest 'MtJSON 'RO IssueComment
-> IO (Either Error IssueComment)
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo -> Id Comment -> GenRequest 'MtJSON 'RO IssueComment
forall (k :: RW).
Name Owner -> Name Repo -> Id Comment -> Request k IssueComment
commentR Name Owner
user Name Repo
repo Id Comment
cid

-- | Query a single comment.
-- See <https://developer.github.com/v3/issues/comments/#get-a-single-comment>
commentR :: Name Owner -> Name Repo -> Id Comment -> Request k IssueComment
commentR :: Name Owner -> Name Repo -> Id Comment -> Request k IssueComment
commentR user :: Name Owner
user repo :: Name Repo
repo cid :: Id Comment
cid =
    Paths -> QueryString -> Request k IssueComment
forall (mt :: RW) a. Paths -> QueryString -> Request mt a
query ["repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, "issues", "comments", Id Comment -> Text
forall a. IsPathPart a => a -> Text
toPathPart Id Comment
cid] []

-- | All comments on an issue, by the issue's number.
--
-- > comments "thoughtbot" "paperclip" 635
comments :: Name Owner -> Name Repo -> IssueNumber -> IO (Either Error (Vector IssueComment))
comments :: Name Owner
-> Name Repo
-> IssueNumber
-> IO (Either Error (Vector IssueComment))
comments = Maybe Auth
-> Name Owner
-> Name Repo
-> IssueNumber
-> IO (Either Error (Vector IssueComment))
comments' Maybe Auth
forall a. Maybe a
Nothing

-- | All comments on an issue, by the issue's number, using authentication.
--
-- > comments' (Just $ BasicAuth "github-username" "github-password") "thoughtbot" "paperclip" 635
comments' :: Maybe Auth -> Name Owner -> Name Repo -> IssueNumber -> IO (Either Error (Vector IssueComment))
comments' :: Maybe Auth
-> Name Owner
-> Name Repo
-> IssueNumber
-> IO (Either Error (Vector IssueComment))
comments' auth :: Maybe Auth
auth user :: Name Owner
user repo :: Name Repo
repo iid :: IssueNumber
iid =
    Maybe Auth
-> GenRequest 'MtJSON 'RO (Vector IssueComment)
-> IO (Either Error (Vector IssueComment))
forall am (mt :: MediaType *) a.
(AuthMethod am, ParseResponse mt a) =>
Maybe am -> GenRequest mt 'RO a -> IO (Either Error a)
executeRequestMaybe Maybe Auth
auth (GenRequest 'MtJSON 'RO (Vector IssueComment)
 -> IO (Either Error (Vector IssueComment)))
-> GenRequest 'MtJSON 'RO (Vector IssueComment)
-> IO (Either Error (Vector IssueComment))
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo
-> IssueNumber
-> FetchCount
-> GenRequest 'MtJSON 'RO (Vector IssueComment)
forall (k :: RW).
Name Owner
-> Name Repo
-> IssueNumber
-> FetchCount
-> Request k (Vector IssueComment)
commentsR Name Owner
user Name Repo
repo IssueNumber
iid FetchCount
FetchAll

-- | List comments on an issue.
-- See <https://developer.github.com/v3/issues/comments/#list-comments-on-an-issue>
commentsR :: Name Owner -> Name Repo -> IssueNumber -> FetchCount -> Request k (Vector IssueComment)
commentsR :: Name Owner
-> Name Repo
-> IssueNumber
-> FetchCount
-> Request k (Vector IssueComment)
commentsR user :: Name Owner
user repo :: Name Repo
repo iid :: IssueNumber
iid =
    Paths
-> QueryString -> FetchCount -> Request k (Vector IssueComment)
forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery ["repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, "issues", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
iid, "comments"] []

-- | Create a new comment.
--
-- > createComment (BasicAuth "github-username" "github-password") user repo issue
-- >  "some words"
createComment :: Auth -> Name Owner -> Name Repo -> IssueNumber -> Text
            -> IO (Either Error Comment)
createComment :: Auth
-> Name Owner
-> Name Repo
-> IssueNumber
-> Text
-> IO (Either Error Comment)
createComment auth :: Auth
auth user :: Name Owner
user repo :: Name Repo
repo iss :: IssueNumber
iss body :: Text
body =
    Auth -> GenRequest 'MtJSON 'RW Comment -> IO (Either Error Comment)
forall am (mt :: MediaType *) a (rw :: RW).
(AuthMethod am, ParseResponse mt a) =>
am -> GenRequest mt rw a -> IO (Either Error a)
executeRequest Auth
auth (GenRequest 'MtJSON 'RW Comment -> IO (Either Error Comment))
-> GenRequest 'MtJSON 'RW Comment -> IO (Either Error Comment)
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo
-> IssueNumber
-> Text
-> GenRequest 'MtJSON 'RW Comment
createCommentR Name Owner
user Name Repo
repo IssueNumber
iss Text
body

-- | Create a comment.
-- See <https://developer.github.com/v3/issues/comments/#create-a-comment>
createCommentR :: Name Owner -> Name Repo -> IssueNumber -> Text -> Request 'RW Comment
createCommentR :: Name Owner
-> Name Repo
-> IssueNumber
-> Text
-> GenRequest 'MtJSON 'RW Comment
createCommentR user :: Name Owner
user repo :: Name Repo
repo iss :: IssueNumber
iss body :: Text
body =
    CommandMethod
-> Paths -> ByteString -> GenRequest 'MtJSON 'RW Comment
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Post Paths
parts (NewComment -> ByteString
forall a. ToJSON a => a -> ByteString
encode (NewComment -> ByteString) -> NewComment -> ByteString
forall a b. (a -> b) -> a -> b
$ Text -> NewComment
NewComment Text
body)
  where
    parts :: Paths
parts = ["repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, "issues", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
iss, "comments"]

-- | Edit a comment.
--
-- > editComment (BasicAuth "github-username" "github-password") user repo commentid
-- >  "new words"
editComment :: Auth -> Name Owner -> Name Repo -> Id Comment -> Text
            -> IO (Either Error Comment)
editComment :: Auth
-> Name Owner
-> Name Repo
-> Id Comment
-> Text
-> IO (Either Error Comment)
editComment auth :: Auth
auth user :: Name Owner
user repo :: Name Repo
repo commid :: Id Comment
commid body :: Text
body =
    Auth -> GenRequest 'MtJSON 'RW Comment -> IO (Either Error Comment)
forall am (mt :: MediaType *) a (rw :: RW).
(AuthMethod am, ParseResponse mt a) =>
am -> GenRequest mt rw a -> IO (Either Error a)
executeRequest Auth
auth (GenRequest 'MtJSON 'RW Comment -> IO (Either Error Comment))
-> GenRequest 'MtJSON 'RW Comment -> IO (Either Error Comment)
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo
-> Id Comment
-> Text
-> GenRequest 'MtJSON 'RW Comment
editCommentR Name Owner
user Name Repo
repo Id Comment
commid Text
body

-- | Edit a comment.
-- See <https://developer.github.com/v3/issues/comments/#edit-a-comment>
editCommentR :: Name Owner -> Name Repo -> Id Comment -> Text -> Request 'RW Comment
editCommentR :: Name Owner
-> Name Repo
-> Id Comment
-> Text
-> GenRequest 'MtJSON 'RW Comment
editCommentR user :: Name Owner
user repo :: Name Repo
repo commid :: Id Comment
commid body :: Text
body =
    CommandMethod
-> Paths -> ByteString -> GenRequest 'MtJSON 'RW Comment
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Patch Paths
parts (EditComment -> ByteString
forall a. ToJSON a => a -> ByteString
encode (EditComment -> ByteString) -> EditComment -> ByteString
forall a b. (a -> b) -> a -> b
$ Text -> EditComment
EditComment Text
body)
  where
    parts :: Paths
parts = ["repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, "issues", "comments", Id Comment -> Text
forall a. IsPathPart a => a -> Text
toPathPart Id Comment
commid]

-- | Delete a comment.
--
-- > deleteComment (BasicAuth "github-username" "github-password") user repo commentid
deleteComment :: Auth -> Name Owner -> Name Repo -> Id Comment
              -> IO (Either Error ())
deleteComment :: Auth
-> Name Owner -> Name Repo -> Id Comment -> IO (Either Error ())
deleteComment auth :: Auth
auth user :: Name Owner
user repo :: Name Repo
repo commid :: Id Comment
commid =
    Auth -> GenRequest 'MtUnit 'RW () -> IO (Either Error ())
forall am (mt :: MediaType *) a (rw :: RW).
(AuthMethod am, ParseResponse mt a) =>
am -> GenRequest mt rw a -> IO (Either Error a)
executeRequest Auth
auth (GenRequest 'MtUnit 'RW () -> IO (Either Error ()))
-> GenRequest 'MtUnit 'RW () -> IO (Either Error ())
forall a b. (a -> b) -> a -> b
$ Name Owner -> Name Repo -> Id Comment -> GenRequest 'MtUnit 'RW ()
deleteCommentR Name Owner
user Name Repo
repo Id Comment
commid

-- | Delete a comment.
-- See <https://developer.github.com/v3/issues/comments/#delete-a-comment>
deleteCommentR :: Name Owner -> Name Repo -> Id Comment -> GenRequest 'MtUnit 'RW ()
deleteCommentR :: Name Owner -> Name Repo -> Id Comment -> GenRequest 'MtUnit 'RW ()
deleteCommentR user :: Name Owner
user repo :: Name Repo
repo commid :: Id Comment
commid =
    CommandMethod -> Paths -> ByteString -> GenRequest 'MtUnit 'RW ()
forall (mt :: MediaType *) a.
CommandMethod -> Paths -> ByteString -> GenRequest mt 'RW a
Command CommandMethod
Delete Paths
parts ByteString
forall a. Monoid a => a
mempty
  where
    parts :: Paths
parts = ["repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, "issues", "comments", Id Comment -> Text
forall a. IsPathPart a => a -> Text
toPathPart Id Comment
commid]