-- |
-- The Github Repos API, as documented at
-- <http://developer.github.com/v3/repos/>

module GitHub.Endpoints.Repos (
    -- * Querying repositories
    currentUserReposR,
    userReposR,
    organizationReposR,
    repositoryR,
    contributorsR,
    languagesForR,
    tagsForR,
    branchesForR,

    -- ** Create
    createRepoR,
    createOrganizationRepoR,
    forkExistingRepoR,

    -- ** Edit
    editRepoR,

    -- ** Delete
    deleteRepoR,

    -- * Data
    module GitHub.Data,
    ) where

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

repoPublicityQueryString :: RepoPublicity -> QueryString
repoPublicityQueryString :: RepoPublicity -> QueryString
repoPublicityQueryString RepoPublicity
RepoPublicityAll     = [(ByteString
"type", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"all")]
repoPublicityQueryString RepoPublicity
RepoPublicityOwner   = [(ByteString
"type", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"owner")]
repoPublicityQueryString RepoPublicity
RepoPublicityMember  = [(ByteString
"type", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"member")]
repoPublicityQueryString RepoPublicity
RepoPublicityPublic  = [(ByteString
"type", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"public")]
repoPublicityQueryString RepoPublicity
RepoPublicityPrivate = [(ByteString
"type", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"private")]

-- | List your repositories.
-- See <https://docs.github.com/en/rest/reference/repos#list-repositories-for-the-authenticated-user>
currentUserReposR :: RepoPublicity -> FetchCount -> Request k (Vector Repo)
currentUserReposR :: forall (k :: RW).
RepoPublicity -> FetchCount -> Request k (Vector Repo)
currentUserReposR RepoPublicity
publicity =
    Paths -> QueryString -> FetchCount -> Request k (Vector Repo)
forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery  [Text
"user", Text
"repos"] QueryString
qs
  where
    qs :: QueryString
qs = RepoPublicity -> QueryString
repoPublicityQueryString RepoPublicity
publicity

-- | List user repositories.
-- See <https://docs.github.com/en/rest/reference/repos#list-repositories-for-a-user>
userReposR :: Name Owner -> RepoPublicity -> FetchCount -> Request k(Vector Repo)
userReposR :: forall (k :: RW).
Name Owner
-> RepoPublicity -> FetchCount -> Request k (Vector Repo)
userReposR Name Owner
user RepoPublicity
publicity =
    Paths -> QueryString -> FetchCount -> Request k (Vector Repo)
forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery  [Text
"users", Name Owner -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, Text
"repos"] QueryString
qs
  where
    qs :: QueryString
qs = RepoPublicity -> QueryString
repoPublicityQueryString RepoPublicity
publicity

-- | List organization repositories.
-- See <https://docs.github.com/en/rest/reference/repos#list-organization-repositories>
organizationReposR
    :: Name Organization
    -> RepoPublicity
    -> FetchCount
    -> Request k (Vector Repo)
organizationReposR :: forall (k :: RW).
Name Organization
-> RepoPublicity -> FetchCount -> Request k (Vector Repo)
organizationReposR Name Organization
org RepoPublicity
publicity =
    Paths -> QueryString -> FetchCount -> Request k (Vector Repo)
forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery [Text
"orgs", Name Organization -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Organization
org, Text
"repos"] QueryString
qs
  where
    qs :: QueryString
qs = RepoPublicity -> QueryString
repoPublicityQueryString RepoPublicity
publicity

-- | Query single repository.
-- See <https://developer.github.com/v3/repos/#get>
repositoryR :: Name Owner -> Name Repo -> Request k Repo
repositoryR :: forall (k :: RW). Name Owner -> Name Repo -> Request k Repo
repositoryR Name Owner
user Name Repo
repo =
    Paths -> QueryString -> Request k Repo
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] []

-- | Create a new repository.
-- See <https://developer.github.com/v3/repos/#create>
createRepoR :: NewRepo -> Request 'RW Repo
createRepoR :: NewRepo -> Request 'RW Repo
createRepoR NewRepo
nrepo =
    CommandMethod -> Paths -> ByteString -> Request 'RW Repo
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Post [Text
"user", Text
"repos"] (NewRepo -> ByteString
forall a. ToJSON a => a -> ByteString
encode NewRepo
nrepo)

-- | Fork an existing repository.
-- See <https://developer.github.com/v3/repos/forks/#create-a-fork>
-- TODO: The third paramater (an optional Organisation) is not used yet.
forkExistingRepoR :: Name Owner -> Name Repo -> Maybe (Name Owner) -> Request 'RW Repo
forkExistingRepoR :: Name Owner -> Name Repo -> Maybe (Name Owner) -> Request 'RW Repo
forkExistingRepoR Name Owner
owner Name Repo
repo Maybe (Name Owner)
_morg =
    CommandMethod -> Paths -> ByteString -> Request 'RW Repo
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
owner, Name Repo -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"forks" ] ByteString
forall a. Monoid a => a
mempty

-- | Create a new repository for an organization.
-- See <https://developer.github.com/v3/repos/#create>
createOrganizationRepoR :: Name Organization -> NewRepo -> Request 'RW Repo
createOrganizationRepoR :: Name Organization -> NewRepo -> Request 'RW Repo
createOrganizationRepoR Name Organization
org NewRepo
nrepo =
    CommandMethod -> Paths -> ByteString -> Request 'RW Repo
forall a. CommandMethod -> Paths -> ByteString -> Request 'RW a
command CommandMethod
Post [Text
"orgs", Name Organization -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Organization
org, Text
"repos"] (NewRepo -> ByteString
forall a. ToJSON a => a -> ByteString
encode NewRepo
nrepo)

-- | Edit an existing repository.
-- See <https://developer.github.com/v3/repos/#edit>
editRepoR :: Name Owner -> Name Repo -> EditRepo -> Request 'RW Repo
editRepoR :: Name Owner -> Name Repo -> EditRepo -> Request 'RW Repo
editRepoR Name Owner
user Name Repo
repo EditRepo
body =
    CommandMethod -> Paths -> ByteString -> Request 'RW Repo
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] (EditRepo -> ByteString
forall a. ToJSON a => a -> ByteString
encode EditRepo
b)
  where
    -- if no name is given, use curent name
    b :: EditRepo
b = EditRepo
body {editName = editName body <|> Just repo}

-- | List contributors.
-- See <https://developer.github.com/v3/repos/#list-contributors>
contributorsR
    :: Name Owner
    -> Name Repo
    -> Bool              -- ^ Include anonymous
    -> FetchCount
    -> Request k (Vector Contributor)
contributorsR :: forall (k :: RW).
Name Owner
-> Name Repo
-> Bool
-> FetchCount
-> Request k (Vector Contributor)
contributorsR Name Owner
user Name Repo
repo Bool
anon =
    Paths
-> QueryString -> FetchCount -> Request k (Vector Contributor)
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
"contributors"] QueryString
qs
  where
    qs :: QueryString
qs | Bool
anon      = [(ByteString
"anon", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
"true")]
       | Bool
otherwise = []

-- | List languages.
-- See <https://developer.github.com/v3/repos/#list-languages>
languagesForR :: Name Owner -> Name Repo -> Request k Languages
languagesForR :: forall (k :: RW). Name Owner -> Name Repo -> Request k Languages
languagesForR Name Owner
user Name Repo
repo =
    Paths -> QueryString -> Request k Languages
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
"languages"] []

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

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

-- | Delete a repository,.
-- See <https://developer.github.com/v3/repos/#delete-a-repository>
deleteRepoR :: Name Owner -> Name Repo -> GenRequest 'MtUnit 'RW ()
deleteRepoR :: Name Owner -> Name Repo -> GenRequest 'MtUnit 'RW ()
deleteRepoR Name Owner
user Name Repo
repo =
    CommandMethod -> Paths -> ByteString -> GenRequest 'MtUnit 'RW ()
forall (mt :: MediaType (*)) a.
CommandMethod -> Paths -> ByteString -> GenRequest mt 'RW a
Command CommandMethod
Delete [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] ByteString
forall a. Monoid a => a
mempty