Skip to content

Commit 7a72215

Browse files
committed
Add create status endpoint
1 parent 71f5b2a commit 7a72215

File tree

5 files changed

+113
-3
lines changed

5 files changed

+113
-3
lines changed

github.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Library
8686
GitHub.Data.Request
8787
GitHub.Data.Reviews
8888
GitHub.Data.Search
89+
GitHub.Data.Statuses
8990
GitHub.Data.Teams
9091
GitHub.Data.URL
9192
GitHub.Data.Webhooks
@@ -118,6 +119,7 @@ Library
118119
GitHub.Endpoints.Repos.DeployKeys
119120
GitHub.Endpoints.Repos.Forks
120121
GitHub.Endpoints.Repos.Releases
122+
GitHub.Endpoints.Repos.Statuses
121123
GitHub.Endpoints.Repos.Webhooks
122124
GitHub.Endpoints.Search
123125
GitHub.Endpoints.Users

spec/GitHub/CommitsSpec.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import qualified GitHub
66

77
import GitHub.Auth (Auth (..))
88
import GitHub.Endpoints.Repos.Commits (Commit, commitSha, commitsFor',
9-
commitsForR, diffR, mkName)
9+
commitsForR, diffR, mkCommitName)
1010
import GitHub.Request (executeRequest)
1111

1212
import Control.Monad (forM_)
1313
import Data.Either.Compat (isRight)
1414
import Data.List (nub, sort)
15-
import Data.Proxy (Proxy (..))
1615
import Data.String (fromString)
1716
import System.Environment (lookupEnv)
1817
import Test.Hspec (Spec, describe, it, pendingWith, shouldBe,
@@ -59,6 +58,5 @@ spec = do
5958
d `shouldSatisfy` isRight
6059

6160
it "issue #155" $ withAuth $ \auth -> do
62-
let mkCommitName = mkName (Proxy :: Proxy Commit)
6361
d <- executeRequest auth $ diffR "nomeata" "codespeed" (mkCommitName "ghc") (mkCommitName "tobami:master")
6462
d `shouldSatisfy` isRight

src/GitHub/Data.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module GitHub.Data (
1616
mkTeamName,
1717
mkOrganizationName,
1818
mkRepoName,
19+
mkCommitName,
1920
fromUserName,
2021
fromOrganizationName,
2122
-- ** Id
@@ -48,6 +49,7 @@ module GitHub.Data (
4849
module GitHub.Data.Request,
4950
module GitHub.Data.Reviews,
5051
module GitHub.Data.Search,
52+
module GitHub.Data.Statuses,
5153
module GitHub.Data.Teams,
5254
module GitHub.Data.URL,
5355
module GitHub.Data.Webhooks
@@ -76,6 +78,7 @@ import GitHub.Data.Repos
7678
import GitHub.Data.Request
7779
import GitHub.Data.Reviews
7880
import GitHub.Data.Search
81+
import GitHub.Data.Statuses
7982
import GitHub.Data.Teams
8083
import GitHub.Data.URL
8184
import GitHub.Data.Webhooks
@@ -110,6 +113,9 @@ mkRepoId = Id
110113
mkRepoName :: Text -> Name Repo
111114
mkRepoName = N
112115

116+
mkCommitName :: Text -> Name Commit
117+
mkCommitName = N
118+
113119
fromOrganizationName :: Name Organization -> Name Owner
114120
fromOrganizationName = N . untagName
115121

src/GitHub/Data/Statuses.hs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{-# LANGUAGE DeriveDataTypeable #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE NoImplicitPrelude #-}
4+
{-# LANGUAGE OverloadedStrings #-}
5+
module GitHub.Data.Statuses where
6+
7+
import GitHub.Data.Definitions
8+
import GitHub.Data.Id (Id)
9+
import GitHub.Data.URL (URL)
10+
import GitHub.Internal.Prelude
11+
import Prelude ()
12+
13+
data StatusState
14+
= StatusPending
15+
| StatusSuccess
16+
| StatusError
17+
| StatusFailure
18+
deriving (Show, Data, Enum, Bounded, Typeable, Eq, Ord, Generic)
19+
20+
instance NFData StatusState where rnf = genericRnf
21+
instance Binary StatusState
22+
23+
instance FromJSON StatusState where
24+
parseJSON (String "pending") = pure StatusPending
25+
parseJSON (String "success") = pure StatusSuccess
26+
parseJSON (String "error") = pure StatusError
27+
parseJSON (String "failure") = pure StatusFailure
28+
parseJSON _ = fail "Could not build a StatusState"
29+
30+
instance ToJSON StatusState where
31+
toJSON StatusPending = String "pending"
32+
toJSON StatusSuccess = String "success"
33+
toJSON StatusError = String "error"
34+
toJSON StatusFailure = String "failure"
35+
36+
data Status = Status
37+
{ statusCreatedAt :: !UTCTime
38+
, statusUpdatedAt :: !UTCTime
39+
, statusState :: !StatusState
40+
, statusTargetUrl :: !(Maybe URL)
41+
, statusDescription :: !(Maybe Text)
42+
, statusId :: !(Id Status)
43+
, statusUrl :: !URL
44+
, statusContext :: !(Maybe Text)
45+
, statusCreator :: !SimpleUser
46+
}
47+
deriving (Show, Data, Typeable, Eq, Ord, Generic)
48+
49+
instance FromJSON Status where
50+
parseJSON = withObject "Status" $ \o -> Status
51+
<$> o .: "created_at"
52+
<*> o .: "updated_at"
53+
<*> o .: "state"
54+
<*> o .:? "target_url"
55+
<*> o .:? "description"
56+
<*> o .: "id"
57+
<*> o .: "url"
58+
<*> o .:? "context"
59+
<*> o .: "creator"
60+
61+
data NewStatus = NewStatus
62+
{ newStatusState :: !StatusState
63+
, newStatusTargetUrl :: !(Maybe URL)
64+
, newStatusDescription :: !(Maybe Text)
65+
, newStatusContext :: !(Maybe Text)
66+
}
67+
deriving (Show, Data, Typeable, Eq, Ord, Generic)
68+
69+
instance NFData NewStatus where rnf = genericRnf
70+
instance Binary NewStatus
71+
72+
instance ToJSON NewStatus where
73+
toJSON (NewStatus s t d c) = object $ filter notNull $
74+
[ "state" .= s
75+
, "target_url" .= t
76+
, "description" .= d
77+
, "context" .= c
78+
]
79+
where
80+
notNull (_, Null) = False
81+
notNull (_, _) = True
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--
2+
-- The repo statuses API as described on
3+
-- <https://developer.github.com/v3/repos/statuses/>.
4+
module GitHub.Endpoints.Repos.Statuses (
5+
createStatus,
6+
createStatusR,
7+
module GitHub.Data
8+
) where
9+
10+
import GitHub.Data
11+
import GitHub.Internal.Prelude
12+
import GitHub.Request
13+
import Prelude ()
14+
15+
createStatus :: Auth -> Name Owner -> Name Repo -> Name Commit -> NewStatus -> IO (Either Error Status)
16+
createStatus auth owner repo sha ns =
17+
executeRequest auth $ createStatusR owner repo sha ns
18+
19+
createStatusR :: Name Owner -> Name Repo -> Name Commit -> NewStatus -> Request 'RW Status
20+
createStatusR owner repo sha =
21+
command Post parts . encode
22+
where
23+
parts = ["repos", toPathPart owner, toPathPart repo, "statuses", toPathPart sha]

0 commit comments

Comments
 (0)