{-# LANGUAGE CPP #-}
-----------------------------------------------------------------------------
-- |
-- License     :  BSD-3-Clause
-- Maintainer  :  Oleg Grenrus <oleg.grenrus@iki.fi>
--
-- The repo commits API as described on
-- <http://developer.github.com/v3/repos/commits/>.
module GitHub.Endpoints.Repos.Commits (
    CommitQueryOption(..),
    commitsFor,
    commitsFor',
    commitsForR,
    commitsWithOptionsFor,
    commitsWithOptionsFor',
    commitsWithOptionsForR,
    commit,
    commit',
    commitR,
    diff,
    diff',
    diffR,
    module GitHub.Data,
    ) where

import GitHub.Data
import GitHub.Internal.Prelude
import GitHub.Request
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 sha :: Text
sha)      = ("sha", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 Text
sha)
renderCommitQueryOption (CommitQueryPath path :: Text
path)     = ("path", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 Text
path)
renderCommitQueryOption (CommitQueryAuthor author :: Text
author) = ("author", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 Text
author)
renderCommitQueryOption (CommitQuerySince date :: UTCTime
date)    = ("since", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 (Text -> ByteString) -> (String -> Text) -> String -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> ByteString) -> String -> ByteString
forall a b. (a -> b) -> a -> b
$ UTCTime -> String
formatISO8601 UTCTime
date)
renderCommitQueryOption (CommitQueryUntil date :: UTCTime
date)    = ("until", ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
TE.encodeUtf8 (Text -> ByteString) -> (String -> Text) -> String -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> ByteString) -> String -> ByteString
forall a b. (a -> b) -> a -> b
$ UTCTime -> String
formatISO8601 UTCTime
date)

-- | The commit history for a repo.
--
-- > commitsFor "mike-burns" "github"
commitsFor :: Name Owner -> Name Repo -> IO (Either Error (Vector Commit))
commitsFor :: Name Owner -> Name Repo -> IO (Either Error (Vector Commit))
commitsFor = Maybe Auth
-> Name Owner -> Name Repo -> IO (Either Error (Vector Commit))
commitsFor' Maybe Auth
forall a. Maybe a
Nothing

-- | The commit history for a repo.
-- With authentication.
--
-- > commitsFor' (Just $ BasicAuth "github-username" "github-password") "mike-burns" "github"
commitsFor' :: Maybe Auth -> Name Owner -> Name Repo -> IO (Either Error (Vector Commit))
commitsFor' :: Maybe Auth
-> Name Owner -> Name Repo -> IO (Either Error (Vector Commit))
commitsFor' auth :: Maybe Auth
auth user :: Name Owner
user repo :: Name Repo
repo =
    Maybe Auth
-> Name Owner
-> Name Repo
-> [CommitQueryOption]
-> IO (Either Error (Vector Commit))
commitsWithOptionsFor' Maybe Auth
auth Name Owner
user Name Repo
repo []

-- | 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 :: Name Owner -> Name Repo -> FetchCount -> Request k (Vector Commit)
commitsForR user :: Name Owner
user repo :: Name Repo
repo limit :: FetchCount
limit = Name Owner
-> Name Repo
-> FetchCount
-> [CommitQueryOption]
-> Request k (Vector Commit)
forall (k :: RW).
Name Owner
-> Name Repo
-> FetchCount
-> [CommitQueryOption]
-> Request k (Vector Commit)
commitsWithOptionsForR Name Owner
user Name Repo
repo FetchCount
limit []

commitsWithOptionsFor :: Name Owner -> Name Repo -> [CommitQueryOption] -> IO (Either Error (Vector Commit))
commitsWithOptionsFor :: Name Owner
-> Name Repo
-> [CommitQueryOption]
-> IO (Either Error (Vector Commit))
commitsWithOptionsFor = Maybe Auth
-> Name Owner
-> Name Repo
-> [CommitQueryOption]
-> IO (Either Error (Vector Commit))
commitsWithOptionsFor' Maybe Auth
forall a. Maybe a
Nothing

-- | The commit history for a repo, with commits filtered to satisfy a list of
-- query options.
-- With authentication.
--
-- > commitsWithOptionsFor' (Just $ BasicAuth "github-username" "github-password") "mike-burns" "github" [CommitQueryAuthor "djeik"]
commitsWithOptionsFor' :: Maybe Auth -> Name Owner -> Name Repo -> [CommitQueryOption] -> IO (Either Error (Vector Commit))
commitsWithOptionsFor' :: Maybe Auth
-> Name Owner
-> Name Repo
-> [CommitQueryOption]
-> IO (Either Error (Vector Commit))
commitsWithOptionsFor' auth :: Maybe Auth
auth user :: Name Owner
user repo :: Name Repo
repo opts :: [CommitQueryOption]
opts =
    Maybe Auth
-> GenRequest 'MtJSON 'RO (Vector Commit)
-> IO (Either Error (Vector Commit))
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 Commit)
 -> IO (Either Error (Vector Commit)))
-> GenRequest 'MtJSON 'RO (Vector Commit)
-> IO (Either Error (Vector Commit))
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo
-> FetchCount
-> [CommitQueryOption]
-> GenRequest 'MtJSON 'RO (Vector Commit)
forall (k :: RW).
Name Owner
-> Name Repo
-> FetchCount
-> [CommitQueryOption]
-> Request k (Vector Commit)
commitsWithOptionsForR Name Owner
user Name Repo
repo FetchCount
FetchAll [CommitQueryOption]
opts

-- | 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 :: Name Owner
-> Name Repo
-> FetchCount
-> [CommitQueryOption]
-> Request k (Vector Commit)
commitsWithOptionsForR user :: Name Owner
user repo :: Name Repo
repo limit :: FetchCount
limit opts :: [CommitQueryOption]
opts =
    Paths -> QueryString -> FetchCount -> Request k (Vector Commit)
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, "commits"] QueryString
qs FetchCount
limit
  where
    qs :: QueryString
qs = (CommitQueryOption -> (ByteString, Maybe ByteString))
-> [CommitQueryOption] -> QueryString
forall a b. (a -> b) -> [a] -> [b]
map CommitQueryOption -> (ByteString, Maybe ByteString)
renderCommitQueryOption [CommitQueryOption]
opts


-- | Details on a specific SHA1 for a repo.
--
-- > commit "mike-burns" "github" "9d1a9a361266c3c890b1108ad2fdf52f824b1b81"
commit :: Name Owner -> Name Repo -> Name Commit -> IO (Either Error Commit)
commit :: Name Owner -> Name Repo -> Name Commit -> IO (Either Error Commit)
commit = Maybe Auth
-> Name Owner
-> Name Repo
-> Name Commit
-> IO (Either Error Commit)
commit' Maybe Auth
forall a. Maybe a
Nothing

-- | Details on a specific SHA1 for a repo.
-- With authentication.
--
-- > commit (Just $ BasicAuth "github-username" "github-password") "mike-burns" "github" "9d1a9a361266c3c890b1108ad2fdf52f824b1b81"
commit' :: Maybe Auth -> Name Owner -> Name Repo -> Name Commit -> IO (Either Error Commit)
commit' :: Maybe Auth
-> Name Owner
-> Name Repo
-> Name Commit
-> IO (Either Error Commit)
commit' auth :: Maybe Auth
auth user :: Name Owner
user repo :: Name Repo
repo sha :: Name Commit
sha =
    Maybe Auth
-> GenRequest 'MtJSON 'RO Commit -> IO (Either Error Commit)
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 Commit -> IO (Either Error Commit))
-> GenRequest 'MtJSON 'RO Commit -> IO (Either Error Commit)
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo -> Name Commit -> GenRequest 'MtJSON 'RO Commit
forall (k :: RW).
Name Owner -> Name Repo -> Name Commit -> Request k Commit
commitR Name Owner
user Name Repo
repo Name Commit
sha

-- | 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 :: Name Owner -> Name Repo -> Name Commit -> Request k Commit
commitR user :: Name Owner
user repo :: Name Repo
repo sha :: Name Commit
sha =
    Paths -> QueryString -> Request k Commit
forall (mt :: RW) a. Paths -> QueryString -> Request mt 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, "commits", Name Commit -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Commit
sha] []

-- | The diff between two treeishes on a repo.
--
-- > diff "thoughtbot" "paperclip" "41f685f6e01396936bb8cd98e7cca517e2c7d96b" "HEAD"
diff :: Name Owner -> Name Repo -> Name Commit -> Name Commit -> IO (Either Error Diff)
diff :: Name Owner
-> Name Repo
-> Name Commit
-> Name Commit
-> IO (Either Error Diff)
diff = Maybe Auth
-> Name Owner
-> Name Repo
-> Name Commit
-> Name Commit
-> IO (Either Error Diff)
diff' Maybe Auth
forall a. Maybe a
Nothing

-- | The diff between two treeishes on a repo.
--
-- > diff "thoughtbot" "paperclip" "41f685f6e01396936bb8cd98e7cca517e2c7d96b" "HEAD"
diff' :: Maybe Auth -> Name Owner -> Name Repo -> Name Commit -> Name Commit -> IO (Either Error Diff)
diff' :: Maybe Auth
-> Name Owner
-> Name Repo
-> Name Commit
-> Name Commit
-> IO (Either Error Diff)
diff' auth :: Maybe Auth
auth user :: Name Owner
user repo :: Name Repo
repo base :: Name Commit
base headref :: Name Commit
headref =
    Maybe Auth -> GenRequest 'MtJSON 'RO Diff -> IO (Either Error Diff)
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 Diff -> IO (Either Error Diff))
-> GenRequest 'MtJSON 'RO Diff -> IO (Either Error Diff)
forall a b. (a -> b) -> a -> b
$ Name Owner
-> Name Repo
-> Name Commit
-> Name Commit
-> GenRequest 'MtJSON 'RO Diff
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

-- | 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 :: Name Owner
-> Name Repo -> Name Commit -> Name Commit -> Request k Diff
diffR user :: Name Owner
user repo :: Name Repo
repo base :: Name Commit
base headref :: Name Commit
headref =
    Paths -> QueryString -> Request k Diff
forall (mt :: RW) a. Paths -> QueryString -> Request mt 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, "compare", Name Commit -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Commit
base Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "..." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name Commit -> Text
forall a. IsPathPart a => a -> Text
toPathPart Name Commit
headref] []