-----------------------------------------------------------------------------
-- |
-- License     :  BSD-3-Clause
-- Maintainer  :  Oleg Grenrus <oleg.grenrus@iki.fi>
--
-- The pull request review comments API as described at
-- <http://developer.github.com/v3/pulls/comments/>.
module GitHub.Endpoints.PullRequests.Comments (
    pullRequestCommentsIO,
    pullRequestCommentsR,
    pullRequestComment,
    pullRequestCommentR,
    createPullComment,
    createPullCommentR,
    module GitHub.Data,
    ) where

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

-- | All the comments on a pull request with the given ID.
--
-- > pullRequestComments "thoughtbot" "factory_girl" (Id 256)
pullRequestCommentsIO :: Name Owner -> Name Repo -> IssueNumber -> IO (Either Error (Vector Comment))
pullRequestCommentsIO :: Name Owner
-> Name Repo -> IssueNumber -> IO (Either Error (Vector Comment))
pullRequestCommentsIO user :: Name Owner
user repo :: Name Repo
repo prid :: IssueNumber
prid =
    GenRequest 'MtJSON 'RO (Vector Comment)
-> IO (Either Error (Vector Comment))
forall (mt :: MediaType *) a.
ParseResponse mt a =>
GenRequest mt 'RO a -> IO (Either Error a)
executeRequest' (GenRequest 'MtJSON 'RO (Vector Comment)
 -> IO (Either Error (Vector Comment)))
-> GenRequest 'MtJSON 'RO (Vector Comment)
-> IO (Either Error (Vector Comment))
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo
-> IssueNumber
-> FetchCount
-> GenRequest 'MtJSON 'RO (Vector Comment)
forall (k :: RW).
Name Owner
-> Name Repo
-> IssueNumber
-> FetchCount
-> Request k (Vector Comment)
pullRequestCommentsR Name Owner
user Name Repo
repo IssueNumber
prid FetchCount
FetchAll

-- | List comments on a pull request.
-- See <https://developer.github.com/v3/pulls/comments/#list-comments-on-a-pull-request>
pullRequestCommentsR :: Name Owner -> Name Repo -> IssueNumber -> FetchCount -> Request k (Vector Comment)
pullRequestCommentsR :: Name Owner
-> Name Repo
-> IssueNumber
-> FetchCount
-> Request k (Vector Comment)
pullRequestCommentsR user :: Name Owner
user repo :: Name Repo
repo prid :: IssueNumber
prid =
    Paths -> QueryString -> FetchCount -> Request k (Vector Comment)
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, "pulls", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
prid, "comments"] []

-- | One comment on a pull request, by the comment's ID.
--
-- > pullRequestComment "thoughtbot" "factory_girl" (Id 301819)
pullRequestComment :: Name Owner -> Name Repo -> Id Comment -> IO (Either Error Comment)
pullRequestComment :: Name Owner -> Name Repo -> Id Comment -> IO (Either Error Comment)
pullRequestComment user :: Name Owner
user repo :: Name Repo
repo cid :: Id Comment
cid =
    GenRequest 'MtJSON 'RO Comment -> IO (Either Error Comment)
forall (mt :: MediaType *) a.
ParseResponse mt a =>
GenRequest mt 'RO a -> IO (Either Error a)
executeRequest' (GenRequest 'MtJSON 'RO Comment -> IO (Either Error Comment))
-> GenRequest 'MtJSON 'RO Comment -> IO (Either Error Comment)
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo -> Id Comment -> GenRequest 'MtJSON 'RO Comment
forall (k :: RW).
Name Owner -> Name Repo -> Id Comment -> Request k Comment
pullRequestCommentR Name Owner
user Name Repo
repo Id Comment
cid

-- | Query a single comment.
-- See <https://developer.github.com/v3/pulls/comments/#get-a-single-comment>
pullRequestCommentR :: Name Owner -> Name Repo -> Id Comment -> Request k Comment
pullRequestCommentR :: Name Owner -> Name Repo -> Id Comment -> Request k Comment
pullRequestCommentR user :: Name Owner
user repo :: Name Repo
repo cid :: Id Comment
cid =
    Paths -> QueryString -> Request k Comment
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, "pulls", "comments", Id Comment -> Text
forall a. IsPathPart a => a -> Text
toPathPart Id Comment
cid] []

-- | Create a new comment.
--
-- > createPullComment (BasicAuth "github-username" "github-password") user repo issue commit path position
-- >  "some words"
createPullComment :: Auth -> Name Owner -> Name Repo -> IssueNumber -> Text -> Text -> Int -> Text
            -> IO (Either Error Comment)
createPullComment :: Auth
-> Name Owner
-> Name Repo
-> IssueNumber
-> Text
-> Text
-> Int
-> Text
-> IO (Either Error Comment)
createPullComment auth :: Auth
auth user :: Name Owner
user repo :: Name Repo
repo iss :: IssueNumber
iss commit :: Text
commit path :: Text
path position :: Int
position 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
-> Text
-> Int
-> Text
-> GenRequest 'MtJSON 'RW Comment
createPullCommentR Name Owner
user Name Repo
repo IssueNumber
iss Text
commit Text
path Int
position Text
body

-- | Create a comment.
--
-- See <https://developer.github.com/v3/pulls/comments/#create-a-comment>
createPullCommentR :: Name Owner -> Name Repo -> IssueNumber -> Text -> Text -> Int -> Text -> Request 'RW Comment
createPullCommentR :: Name Owner
-> Name Repo
-> IssueNumber
-> Text
-> Text
-> Int
-> Text
-> GenRequest 'MtJSON 'RW Comment
createPullCommentR user :: Name Owner
user repo :: Name Repo
repo iss :: IssueNumber
iss commit :: Text
commit path :: Text
path position :: Int
position body :: Text
body =
    CommandMethod
-> Paths -> ByteString -> GenRequest 'MtJSON 'RW Comment
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Post Paths
parts (NewPullComment -> ByteString
forall a. ToJSON a => a -> ByteString
encode (NewPullComment -> ByteString) -> NewPullComment -> ByteString
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Int -> Text -> NewPullComment
NewPullComment Text
commit Text
path Int
position 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, "pulls", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
iss, "comments"]