{-# LANGUAGE CPP #-}

-- |
-- The repo commits API as described on
-- <http://developer.github.com/v3/repos/commits/>.

module GitHub.Endpoints.Repos.Commits (
    CommitQueryOption(..),
    commitsForR,
    commitsWithOptionsForR,
    commitR,
    diffR,
    module GitHub.Data,
    ) where

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

import qualified Data.ByteString    as BS
import qualified Data.Text          as T
import qualified Data.Text.Encoding as TE

renderCommitQueryOption :: CommitQueryOption -> (BS.ByteString, Maybe BS.ByteString)
renderCommitQueryOption :: CommitQueryOption -> (ByteString, Maybe ByteString)
renderCommitQueryOption (CommitQuerySha Text
sha)      = (ByteString
"sha", forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 Text
sha)
renderCommitQueryOption (CommitQueryPath Text
path)     = (ByteString
"path", forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 Text
path)
renderCommitQueryOption (CommitQueryAuthor Text
author) = (ByteString
"author", forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 Text
author)
renderCommitQueryOption (CommitQuerySince UTCTime
date)    = (ByteString
"since", forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ UTCTime -> String
formatISO8601 UTCTime
date)
renderCommitQueryOption (CommitQueryUntil UTCTime
date)    = (ByteString
"until", forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ UTCTime -> String
formatISO8601 UTCTime
date)

-- | List commits on a repository.
-- See <https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository>
commitsForR :: Name Owner -> Name Repo -> FetchCount -> Request k (Vector Commit)
commitsForR :: forall (k :: RW).
Name Owner -> Name Repo -> FetchCount -> Request k (Vector Commit)
commitsForR Name Owner
user Name Repo
repo FetchCount
limit = forall (k :: RW).
Name Owner
-> Name Repo
-> FetchCount
-> [CommitQueryOption]
-> Request k (Vector Commit)
commitsWithOptionsForR Name Owner
user Name Repo
repo FetchCount
limit []

-- | List commits on a repository.
-- See <https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository>
commitsWithOptionsForR :: Name Owner -> Name Repo -> FetchCount -> [CommitQueryOption] -> Request k (Vector Commit)
commitsWithOptionsForR :: forall (k :: RW).
Name Owner
-> Name Repo
-> FetchCount
-> [CommitQueryOption]
-> Request k (Vector Commit)
commitsWithOptionsForR Name Owner
user Name Repo
repo FetchCount
limit [CommitQueryOption]
opts =
    forall a (mt :: RW).
FromJSON a =>
Paths -> QueryString -> FetchCount -> Request mt (Vector a)
pagedQuery [Text
"repos", forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"commits"] QueryString
qs FetchCount
limit
  where
    qs :: QueryString
qs = forall a b. (a -> b) -> [a] -> [b]
map CommitQueryOption -> (ByteString, Maybe ByteString)
renderCommitQueryOption [CommitQueryOption]
opts

-- | Query a single commit.
-- See <https://developer.github.com/v3/repos/commits/#get-a-single-commit>
commitR :: Name Owner -> Name Repo -> Name Commit -> Request k Commit
commitR :: forall (k :: RW).
Name Owner -> Name Repo -> Name Commit -> Request k Commit
commitR Name Owner
user Name Repo
repo Name Commit
sha =
    forall (mt :: RW) a. Paths -> QueryString -> Request mt a
query [Text
"repos", forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"commits", forall a. IsPathPart a => a -> Text
toPathPart Name Commit
sha] []

-- | Compare two commits.
-- See <https://developer.github.com/v3/repos/commits/#compare-two-commits>
diffR :: Name Owner -> Name Repo -> Name Commit -> Name Commit -> Request k Diff
diffR :: forall (k :: RW).
Name Owner
-> Name Repo -> Name Commit -> Name Commit -> Request k Diff
diffR Name Owner
user Name Repo
repo Name Commit
base Name Commit
headref =
    forall (mt :: RW) a. Paths -> QueryString -> Request mt a
query [Text
"repos", forall a. IsPathPart a => a -> Text
toPathPart Name Owner
user, forall a. IsPathPart a => a -> Text
toPathPart Name Repo
repo, Text
"compare", forall a. IsPathPart a => a -> Text
toPathPart Name Commit
base forall a. Semigroup a => a -> a -> a
<> Text
"..." forall a. Semigroup a => a -> a -> a
<> forall a. IsPathPart a => a -> Text
toPathPart Name Commit
headref] []