-----------------------------------------------------------------------------
-- |
-- License     :  BSD-3-Clause
-- Maintainer  :  Oleg Grenrus <oleg.grenrus@iki.fi>
--
-- The pull requests API as documented at
-- <http://developer.github.com/v3/pulls/>.
module GitHub.Endpoints.PullRequests (
    pullRequestsForR,
    pullRequestR,
    pullRequestDiffR,
    pullRequestPatchR,
    createPullRequestR,
    updatePullRequestR,
    pullRequestCommitsR,
    pullRequestFilesR,
    isPullRequestMergedR,
    mergePullRequestR,
    module GitHub.Data
    ) where

import GitHub.Data
import GitHub.Internal.Prelude
import Prelude ()
import Data.ByteString.Lazy (ByteString)

-- | List pull requests.
-- See <https://developer.github.com/v3/pulls/#list-pull-requests>
pullRequestsForR
    :: Name Owner
    -> Name Repo
    -> PullRequestMod
    -> FetchCount
    -> Request k (Vector SimplePullRequest)
pullRequestsForR :: forall (k :: RW).
Name Owner
-> Name Repo
-> PullRequestMod
-> FetchCount
-> Request k (Vector SimplePullRequest)
pullRequestsForR Name Owner
user Name Repo
repo PullRequestMod
opts = Paths
-> QueryString
-> FetchCount
-> Request k (Vector SimplePullRequest)
forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery
    [Text
"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, Text
"pulls"]
    (PullRequestMod -> QueryString
prModToQueryString PullRequestMod
opts)

-- | Query a single pull request to obtain the diff
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
pullRequestDiffR :: Name Owner -> Name Repo -> IssueNumber -> GenRequest 'MtDiff rw ByteString
pullRequestDiffR :: forall (rw :: RW).
Name Owner
-> Name Repo -> IssueNumber -> GenRequest 'MtDiff rw ByteString
pullRequestDiffR Name Owner
user Name Repo
repo IssueNumber
prid =
    Paths -> QueryString -> GenRequest 'MtDiff rw ByteString
forall (mt :: MediaType (*)) (rw :: RW) a.
Paths -> QueryString -> GenRequest mt rw a
Query [Text
"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, Text
"pulls", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
prid] []

-- | Query a single pull request to obtain the patch
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
pullRequestPatchR :: Name Owner -> Name Repo -> IssueNumber -> GenRequest 'MtPatch rw ByteString
pullRequestPatchR :: forall (rw :: RW).
Name Owner
-> Name Repo -> IssueNumber -> GenRequest 'MtPatch rw ByteString
pullRequestPatchR Name Owner
user Name Repo
repo IssueNumber
prid =
    Paths -> QueryString -> GenRequest 'MtPatch rw ByteString
forall (mt :: MediaType (*)) (rw :: RW) a.
Paths -> QueryString -> GenRequest mt rw a
Query [Text
"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, Text
"pulls", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
prid] []

-- | Query a single pull request.
-- See <https://developer.github.com/v3/pulls/#get-a-single-pull-request>
pullRequestR :: Name Owner -> Name Repo -> IssueNumber -> Request k PullRequest
pullRequestR :: forall (k :: RW).
Name Owner -> Name Repo -> IssueNumber -> Request k PullRequest
pullRequestR Name Owner
user Name Repo
repo IssueNumber
prid =
    Paths -> QueryString -> Request k PullRequest
forall (mt :: RW) a. Paths -> QueryString -> Request mt a
query [Text
"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, Text
"pulls", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
prid] []

-- | Create a pull request.
-- See <https://developer.github.com/v3/pulls/#create-a-pull-request>
createPullRequestR :: Name Owner
                   -> Name Repo
                   -> CreatePullRequest
                   -> Request 'RW PullRequest
createPullRequestR :: Name Owner
-> Name Repo -> CreatePullRequest -> Request 'RW PullRequest
createPullRequestR Name Owner
user Name Repo
repo CreatePullRequest
cpr =
    CommandMethod -> Paths -> ByteString -> Request 'RW PullRequest
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Post [Text
"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, Text
"pulls"] (CreatePullRequest -> ByteString
forall a. ToJSON a => a -> ByteString
encode CreatePullRequest
cpr)

-- | Update a pull request.
-- See <https://developer.github.com/v3/pulls/#update-a-pull-request>
updatePullRequestR :: Name Owner
                   -> Name Repo
                   -> IssueNumber
                   -> EditPullRequest
                   -> Request 'RW PullRequest
updatePullRequestR :: Name Owner
-> Name Repo
-> IssueNumber
-> EditPullRequest
-> Request 'RW PullRequest
updatePullRequestR Name Owner
user Name Repo
repo IssueNumber
prid EditPullRequest
epr =
    CommandMethod -> Paths -> ByteString -> Request 'RW PullRequest
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Patch [Text
"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, Text
"pulls", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
prid] (EditPullRequest -> ByteString
forall a. ToJSON a => a -> ByteString
encode EditPullRequest
epr)

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

-- | List pull requests files.
-- See <https://developer.github.com/v3/pulls/#list-pull-requests-files>
pullRequestFilesR :: Name Owner -> Name Repo -> IssueNumber -> FetchCount -> Request k (Vector File)
pullRequestFilesR :: forall (k :: RW).
Name Owner
-> Name Repo
-> IssueNumber
-> FetchCount
-> Request k (Vector File)
pullRequestFilesR Name Owner
user Name Repo
repo IssueNumber
prid =
    Paths -> QueryString -> FetchCount -> Request k (Vector File)
forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery [Text
"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, Text
"pulls", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
prid, Text
"files"] []

-- | Query if a pull request has been merged.
-- See <https://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged>
isPullRequestMergedR :: Name Owner -> Name Repo -> IssueNumber -> GenRequest 'MtStatus rw Bool
isPullRequestMergedR :: forall (rw :: RW).
Name Owner
-> Name Repo -> IssueNumber -> GenRequest 'MtStatus rw Bool
isPullRequestMergedR Name Owner
user Name Repo
repo IssueNumber
prid =
    Paths -> QueryString -> GenRequest 'MtStatus rw Bool
forall (mt :: MediaType (*)) (rw :: RW) a.
Paths -> QueryString -> GenRequest mt rw a
Query [Text
"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, Text
"pulls", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
prid, Text
"merge"] []

-- | Merge a pull request (Merge Button).
-- https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button
mergePullRequestR :: Name Owner -> Name Repo -> IssueNumber -> Maybe Text -> GenRequest 'MtStatus 'RW MergeResult
mergePullRequestR :: Name Owner
-> Name Repo
-> IssueNumber
-> Maybe Text
-> GenRequest 'MtStatus 'RW MergeResult
mergePullRequestR Name Owner
user Name Repo
repo IssueNumber
prid Maybe Text
commitMessage =
    CommandMethod
-> Paths -> ByteString -> GenRequest 'MtStatus 'RW MergeResult
forall (mt :: MediaType (*)) a.
CommandMethod -> Paths -> ByteString -> GenRequest mt 'RW a
Command CommandMethod
Put Paths
paths (Value -> ByteString
forall a. ToJSON a => a -> ByteString
encode (Value -> ByteString) -> Value -> ByteString
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Value
buildCommitMessageMap Maybe Text
commitMessage)
  where
    paths :: Paths
paths = [Text
"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, Text
"pulls", IssueNumber -> Text
forall a. IsPathPart a => a -> Text
toPathPart IssueNumber
prid, Text
"merge"]

    buildCommitMessageMap :: Maybe Text -> Value
    buildCommitMessageMap :: Maybe Text -> Value
buildCommitMessageMap (Just Text
msg) = [Pair] -> Value
object [Key
"commit_message" Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
msg ]
    buildCommitMessageMap Maybe Text
Nothing    = [Pair] -> Value
object []