Skip to content

Commit f455f45

Browse files
committed
Merge pull request haskell-github#65 from mulby/support-webhook-events
Support webhook events
2 parents b36415c + bf64204 commit f455f45

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

Github/Data.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,29 @@ instance FromJSON PullRequestCommit where
402402
<*> o .: "repo"
403403
parseJSON _ = fail "Could not build a PullRequestCommit"
404404

405+
instance FromJSON PullRequestEvent where
406+
parseJSON (Object o) =
407+
PullRequestEvent <$> o .: "action"
408+
<*> o .: "number"
409+
<*> o .: "pull_request"
410+
<*> o .: "repository"
411+
<*> o .: "sender"
412+
parseJSON _ = fail "Could not build a PullRequestEvent"
413+
414+
instance FromJSON PullRequestEventType where
415+
parseJSON (String "opened") = pure PullRequestOpened
416+
parseJSON (String "closed") = pure PullRequestClosed
417+
parseJSON (String "synchronize") = pure PullRequestSynchronized
418+
parseJSON (String "reopened") = pure PullRequestReopened
419+
parseJSON _ = fail "Could not build a PullRequestEventType"
420+
421+
instance FromJSON PingEvent where
422+
parseJSON (Object o) =
423+
PingEvent <$> o .: "zen"
424+
<*> o .: "hook"
425+
<*> o .: "hook_id"
426+
parseJSON _ = fail "Could not build a PingEvent"
427+
405428
instance FromJSON SearchReposResult where
406429
parseJSON (Object o) =
407430
SearchReposResult <$> o .: "total_count"

Github/Data/Definitions.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,24 @@ data RepoWebhookResponse = RepoWebhookResponse {
517517
,repoWebhookResponseStatus :: String
518518
,repoWebhookResponseMessage :: Maybe String
519519
} deriving (Show, Data, Typeable, Eq, Ord)
520+
521+
data PullRequestEvent = PullRequestEvent {
522+
pullRequestEventAction :: PullRequestEventType
523+
,pullRequestEventNumber :: Int
524+
,pullRequestEventPullRequest :: DetailedPullRequest
525+
,pullRequestRepository :: Repo
526+
,pullRequestSender :: GithubOwner
527+
} deriving (Show, Data, Typeable, Eq, Ord)
528+
529+
data PullRequestEventType =
530+
PullRequestOpened
531+
| PullRequestClosed
532+
| PullRequestSynchronized
533+
| PullRequestReopened
534+
deriving (Show, Data, Typeable, Eq, Ord)
535+
536+
data PingEvent = PingEvent {
537+
pingEventZen :: String
538+
,pingEventHook :: RepoWebhook
539+
,pingEventHookId :: Int
540+
} deriving (Show, Data, Typeable, Eq, Ord)

Github/Events.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Github.Events (
2+
parseEvent
3+
) where
4+
5+
import qualified Data.ByteString.Lazy.Char8 as LBS
6+
7+
import Data.Aeson (FromJSON)
8+
9+
import Github.Data.Definitions (Error(..))
10+
import Github.Private (parseJson)
11+
12+
parseEvent :: (FromJSON b, Show b) => LBS.ByteString -> Either Error b
13+
parseEvent = parseJson

github.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Library
123123
Exposed-modules: Github.Auth,
124124
Github.Data,
125125
Github.Data.Definitions,
126+
Github.Events,
126127
Github.Gists,
127128
Github.Gists.Comments,
128129
Github.GitData.Commits,

0 commit comments

Comments
 (0)