-- | The deployments API, as described at <https://developer.github.com/v3/repos/deployments/>
module GitHub.Endpoints.Repos.Deployments
    ( deploymentsWithOptionsForR
    , createDeploymentR

    , deploymentStatusesForR
    , createDeploymentStatusR

    , module GitHub.Data
    ) where

import Control.Arrow (second)

import GitHub.Data
import GitHub.Internal.Prelude

-- | List deployments.
-- See <https://developer.github.com/v3/repos/deployments/#list-deployments>
deploymentsWithOptionsForR
    :: FromJSON a
    => Name Owner
    -> Name Repo
    -> FetchCount
    -> [DeploymentQueryOption]
    -> Request 'RA (Vector (Deployment a))
deploymentsWithOptionsForR :: Name Owner
-> Name Repo
-> FetchCount
-> [DeploymentQueryOption]
-> Request 'RA (Vector (Deployment a))
deploymentsWithOptionsForR Name Owner
owner Name Repo
repo FetchCount
limit [DeploymentQueryOption]
opts =
    Paths
-> QueryString -> FetchCount -> Request 'RA (Vector (Deployment a))
forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery (Name Owner -> Name Repo -> Paths
deployPaths Name Owner
owner Name Repo
repo)
        ((DeploymentQueryOption -> (ByteString, Maybe ByteString))
-> [DeploymentQueryOption] -> QueryString
forall a b. (a -> b) -> [a] -> [b]
map ((ByteString -> Maybe ByteString)
-> (ByteString, ByteString) -> (ByteString, Maybe ByteString)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ((ByteString, ByteString) -> (ByteString, Maybe ByteString))
-> (DeploymentQueryOption -> (ByteString, ByteString))
-> DeploymentQueryOption
-> (ByteString, Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DeploymentQueryOption -> (ByteString, ByteString)
renderDeploymentQueryOption) [DeploymentQueryOption]
opts)
        FetchCount
limit

-- | Create a deployment.
-- See <https://developer.github.com/v3/repos/deployments/#create-a-deployment>
createDeploymentR
    :: ( ToJSON   a
       , FromJSON a
       )
    => Name Owner
    -> Name Repo
    -> CreateDeployment a
    -> Request 'RW (Deployment a)
createDeploymentR :: Name Owner
-> Name Repo -> CreateDeployment a -> Request 'RW (Deployment a)
createDeploymentR Name Owner
owner Name Repo
repo =
    CommandMethod -> Paths -> ByteString -> Request 'RW (Deployment a)
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Post (Name Owner -> Name Repo -> Paths
deployPaths Name Owner
owner Name Repo
repo) (ByteString -> Request 'RW (Deployment a))
-> (CreateDeployment a -> ByteString)
-> CreateDeployment a
-> Request 'RW (Deployment a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CreateDeployment a -> ByteString
forall a. ToJSON a => a -> ByteString
encode

-- | List deployment statuses.
-- See <https://developer.github.com/v3/repos/deployments/#list-deployment-statuses>
deploymentStatusesForR
    :: Name Owner
    -> Name Repo
    -> Id (Deployment a)
    -> FetchCount
    -> Request 'RA (Vector DeploymentStatus)
deploymentStatusesForR :: Name Owner
-> Name Repo
-> Id (Deployment a)
-> FetchCount
-> Request 'RA (Vector DeploymentStatus)
deploymentStatusesForR Name Owner
owner Name Repo
repo Id (Deployment a)
deploy =
    Paths
-> QueryString
-> FetchCount
-> Request 'RA (Vector DeploymentStatus)
forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery (Name Owner -> Name Repo -> Id (Deployment a) -> Paths
forall a. Name Owner -> Name Repo -> Id (Deployment a) -> Paths
statusesPaths Name Owner
owner Name Repo
repo Id (Deployment a)
deploy) []

-- | Create a deployment status.
-- See <https://developer.github.com/v3/repos/deployments/#list-deployment-statuses>
createDeploymentStatusR
    :: Name Owner
    -> Name Repo
    -> Id (Deployment a)
    -> CreateDeploymentStatus
    -> Request 'RW DeploymentStatus
createDeploymentStatusR :: Name Owner
-> Name Repo
-> Id (Deployment a)
-> CreateDeploymentStatus
-> Request 'RW DeploymentStatus
createDeploymentStatusR Name Owner
owner Name Repo
repo Id (Deployment a)
deploy =
    CommandMethod
-> Paths -> ByteString -> Request 'RW DeploymentStatus
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Post (Name Owner -> Name Repo -> Id (Deployment a) -> Paths
forall a. Name Owner -> Name Repo -> Id (Deployment a) -> Paths
statusesPaths Name Owner
owner Name Repo
repo Id (Deployment a)
deploy) (ByteString -> Request 'RW DeploymentStatus)
-> (CreateDeploymentStatus -> ByteString)
-> CreateDeploymentStatus
-> Request 'RW DeploymentStatus
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CreateDeploymentStatus -> ByteString
forall a. ToJSON a => a -> ByteString
encode

statusesPaths :: Name Owner -> Name Repo -> Id (Deployment a) -> Paths
statusesPaths :: Name Owner -> Name Repo -> Id (Deployment a) -> Paths
statusesPaths Name Owner
owner Name Repo
repo Id (Deployment a)
deploy =
    Name Owner -> Name Repo -> Paths
deployPaths Name Owner
owner Name Repo
repo Paths -> Paths -> Paths
forall a. [a] -> [a] -> [a]
++ [Id (Deployment a) -> Text
forall a. IsPathPart a => a -> Text
toPathPart Id (Deployment a)
deploy, Text
"statuses"]

deployPaths :: Name Owner -> Name Repo -> Paths
deployPaths :: Name Owner -> Name Repo -> Paths
deployPaths Name Owner
owner Name Repo
repo =
    [Text
"repos", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
owner, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"deployments"]