-----------------------------------------------------------------------------
-- |
-- License     :  BSD-3-Clause
-- Maintainer  :  Oleg Grenrus <oleg.grenrus@iki.fi>
--
-- The repo collaborators API as described on
-- <http://developer.github.com/v3/repos/collaborators/>.
module GitHub.Endpoints.Repos.Collaborators (
    collaboratorsOn,
    collaboratorsOn',
    collaboratorsOnR,
    isCollaboratorOn,
    isCollaboratorOnR,
    addCollaborator,
    addCollaboratorR,
    module GitHub.Data,
    ) where

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

-- | All the users who have collaborated on a repo.
--
-- > collaboratorsOn "thoughtbot" "paperclip"
collaboratorsOn :: Name Owner -> Name Repo -> IO (Either Error (Vector SimpleUser))
collaboratorsOn :: Name Owner -> Name Repo -> IO (Either Error (Vector SimpleUser))
collaboratorsOn = Maybe Auth
-> Name Owner -> Name Repo -> IO (Either Error (Vector SimpleUser))
collaboratorsOn' Maybe Auth
forall a. Maybe a
Nothing

-- | All the users who have collaborated on a repo.
-- With authentication.
collaboratorsOn' :: Maybe Auth -> Name Owner -> Name Repo -> IO (Either Error (Vector SimpleUser))
collaboratorsOn' :: Maybe Auth
-> Name Owner -> Name Repo -> IO (Either Error (Vector SimpleUser))
collaboratorsOn' auth :: Maybe Auth
auth user :: Name Owner
user repo :: Name Repo
repo =
    Maybe Auth
-> GenRequest 'MtJSON 'RO (Vector SimpleUser)
-> IO (Either Error (Vector SimpleUser))
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 SimpleUser)
 -> IO (Either Error (Vector SimpleUser)))
-> GenRequest 'MtJSON 'RO (Vector SimpleUser)
-> IO (Either Error (Vector SimpleUser))
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo
-> FetchCount
-> GenRequest 'MtJSON 'RO (Vector SimpleUser)
forall (k :: RW).
Name Owner
-> Name Repo -> FetchCount -> Request k (Vector SimpleUser)
collaboratorsOnR Name Owner
user Name Repo
repo FetchCount
FetchAll

-- | List collaborators.
-- See <https://developer.github.com/v3/repos/collaborators/#list-collaborators>
collaboratorsOnR :: Name Owner -> Name Repo -> FetchCount -> Request k (Vector SimpleUser)
collaboratorsOnR :: Name Owner
-> Name Repo -> FetchCount -> Request k (Vector SimpleUser)
collaboratorsOnR user :: Name Owner
user repo :: Name Repo
repo =
    Paths -> QueryString -> FetchCount -> Request k (Vector SimpleUser)
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, "collaborators"] []

-- | Whether the user is collaborating on a repo. Takes the user in question,
-- the user who owns the repo, and the repo name.
--
-- > isCollaboratorOn Nothing "mike-burns" "thoughtbot" "paperclip"
-- > isCollaboratorOn Nothing "johnson" "thoughtbot" "paperclip"
isCollaboratorOn
    :: Maybe Auth
    -> Name Owner  -- ^ Repository owner
    -> Name Repo         -- ^ Repository name
    -> Name User         -- ^ Collaborator?
    -> IO (Either Error Bool)
isCollaboratorOn :: Maybe Auth
-> Name Owner -> Name Repo -> Name User -> IO (Either Error Bool)
isCollaboratorOn auth :: Maybe Auth
auth user :: Name Owner
user repo :: Name Repo
repo coll :: Name User
coll =
    Maybe Auth
-> GenRequest 'MtStatus 'RO Bool -> IO (Either Error Bool)
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 'MtStatus 'RO Bool -> IO (Either Error Bool))
-> GenRequest 'MtStatus 'RO Bool -> IO (Either Error Bool)
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo -> Name User -> GenRequest 'MtStatus 'RO Bool
forall (rw :: RW).
Name Owner
-> Name Repo -> Name User -> GenRequest 'MtStatus rw Bool
isCollaboratorOnR Name Owner
user Name Repo
repo Name User
coll

-- | Check if a user is a collaborator.
-- See <https://developer.github.com/v3/repos/collaborators/#check-if-a-user-is-a-collaborator>
isCollaboratorOnR
    :: Name Owner  -- ^ Repository owner
    -> Name Repo         -- ^ Repository name
    -> Name User         -- ^ Collaborator?
    -> GenRequest 'MtStatus rw Bool
isCollaboratorOnR :: Name Owner
-> Name Repo -> Name User -> GenRequest 'MtStatus rw Bool
isCollaboratorOnR user :: Name Owner
user repo :: Name Repo
repo coll :: Name User
coll =
    Paths -> QueryString -> GenRequest 'MtStatus rw Bool
forall (mt :: MediaType *) (rw :: RW) a.
Paths -> QueryString -> GenRequest mt rw 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, "collaborators", Name User -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name User
coll] []

addCollaborator
    :: Auth
    -> Name Owner        -- ^ Repository owner
    -> Name Repo         -- ^ Repository name
    -> Name User         -- ^ Collaborator to add
    -> IO (Either Error (Maybe RepoInvitation))
addCollaborator :: Auth
-> Name Owner
-> Name Repo
-> Name User
-> IO (Either Error (Maybe RepoInvitation))
addCollaborator auth :: Auth
auth owner :: Name Owner
owner repo :: Name Repo
repo coll :: Name User
coll =
    Auth
-> GenRequest 'MtJSON 'RW (Maybe RepoInvitation)
-> IO (Either Error (Maybe RepoInvitation))
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 (Maybe RepoInvitation)
 -> IO (Either Error (Maybe RepoInvitation)))
-> GenRequest 'MtJSON 'RW (Maybe RepoInvitation)
-> IO (Either Error (Maybe RepoInvitation))
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo
-> Name User
-> GenRequest 'MtJSON 'RW (Maybe RepoInvitation)
addCollaboratorR Name Owner
owner Name Repo
repo Name User
coll

-- | Invite a user as a collaborator.
-- See <https://developer.github.com/v3/repos/collaborators/#add-user-as-a-collaborator>
addCollaboratorR
    :: Name Owner        -- ^ Repository owner
    -> Name Repo         -- ^ Repository name
    -> Name User         -- ^ Collaborator to add
    -> GenRequest 'MtJSON 'RW (Maybe RepoInvitation)
addCollaboratorR :: Name Owner
-> Name Repo
-> Name User
-> GenRequest 'MtJSON 'RW (Maybe RepoInvitation)
addCollaboratorR owner :: Name Owner
owner repo :: Name Repo
repo coll :: Name User
coll =
    CommandMethod
-> Paths
-> ByteString
-> GenRequest 'MtJSON 'RW (Maybe RepoInvitation)
forall (mt :: MediaType *) a.
CommandMethod -> Paths -> ByteString -> GenRequest mt 'RW a
Command CommandMethod
Put ["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, "collaborators", Name User -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name User
coll] ByteString
forall a. Monoid a => a
mempty