Skip to content

Commit 2ee2e43

Browse files
committed
Add initial subset of Reactions endpoints
1 parent 6b9716c commit 2ee2e43

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

github.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ library
120120
GitHub.Data.PublicSSHKeys
121121
GitHub.Data.PullRequests
122122
GitHub.Data.RateLimit
123+
GitHub.Data.Reactions
123124
GitHub.Data.Releases
124125
GitHub.Data.Repos
125126
GitHub.Data.Request
@@ -160,6 +161,7 @@ library
160161
GitHub.Endpoints.PullRequests.Comments
161162
GitHub.Endpoints.PullRequests.Reviews
162163
GitHub.Endpoints.RateLimit
164+
GitHub.Endpoints.Reactions
163165
GitHub.Endpoints.Repos
164166
GitHub.Endpoints.Repos.Collaborators
165167
GitHub.Endpoints.Repos.Comments

src/GitHub.hs

+10
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ module GitHub (
285285
commitR,
286286
diffR,
287287

288+
-- ** Reactions
289+
-- | See <https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28>
290+
issueReactionsR,
291+
createIssueReactionR,
292+
deleteIssueReactionR,
293+
commentReactionsR,
294+
createCommentReactionR,
295+
deleteCommentReactionR,
296+
288297
-- ** Contents
289298
-- | See <https://developer.github.com/v3/repos/contents/>
290299
contentsForR,
@@ -514,6 +523,7 @@ import GitHub.Endpoints.Organizations.Teams
514523
import GitHub.Endpoints.PullRequests
515524
import GitHub.Endpoints.PullRequests.Comments
516525
import GitHub.Endpoints.PullRequests.Reviews
526+
import GitHub.Endpoints.Reactions
517527
import GitHub.Endpoints.RateLimit
518528
import GitHub.Endpoints.Repos
519529
import GitHub.Endpoints.Repos.Collaborators

src/GitHub/Data.hs

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module GitHub.Data (
5757
module GitHub.Data.PullRequests,
5858
module GitHub.Data.RateLimit,
5959
module GitHub.Data.Releases,
60+
module GitHub.Data.Reactions,
6061
module GitHub.Data.Repos,
6162
module GitHub.Data.Request,
6263
module GitHub.Data.Reviews,
@@ -99,6 +100,7 @@ import GitHub.Data.PublicSSHKeys
99100
import GitHub.Data.PullRequests
100101
import GitHub.Data.RateLimit
101102
import GitHub.Data.Releases
103+
import GitHub.Data.Reactions
102104
import GitHub.Data.Repos
103105
import GitHub.Data.Request
104106
import GitHub.Data.Reviews

src/GitHub/Data/Reactions.hs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{-# LANGUAGE InstanceSigs #-}
2+
module GitHub.Data.Reactions where
3+
4+
import qualified Data.Text as T
5+
import GitHub.Data.Id (Id)
6+
import GitHub.Data.Definitions (SimpleUser)
7+
import GitHub.Internal.Prelude
8+
import Prelude ()
9+
10+
data Reaction = Reaction
11+
{ reactionId :: Id Reaction
12+
, reactionUser :: !(Maybe SimpleUser)
13+
, reactionContent :: !ReactionContent
14+
, reactionCreatedAt :: !UTCTime
15+
}
16+
deriving (Show, Data, Typeable, Eq, Ord, Generic)
17+
18+
instance NFData Reaction where rnf = genericRnf
19+
instance Binary Reaction
20+
21+
data NewReaction = NewReaction
22+
{ newReactionContent :: !ReactionContent
23+
}
24+
deriving (Show, Data, Typeable, Eq, Ord, Generic)
25+
26+
instance NFData NewReaction where rnf = genericRnf
27+
instance Binary NewReaction
28+
29+
-- |
30+
-- <https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#about-reactions>
31+
data ReactionContent
32+
= PlusOne
33+
| MinusOne
34+
| Laugh
35+
| Confused
36+
| Heart
37+
| Hooray
38+
| Rocket
39+
| Eyes
40+
deriving (Show, Data, Typeable, Eq, Ord, Generic)
41+
42+
instance NFData ReactionContent where rnf = genericRnf
43+
instance Binary ReactionContent
44+
45+
-- JSON instances
46+
47+
instance FromJSON Reaction where
48+
parseJSON = withObject "Reaction" $ \o ->
49+
Reaction
50+
<$> o .: "id"
51+
<*> o .:? "user"
52+
<*> o .: "content"
53+
<*> o .: "created_at"
54+
55+
instance ToJSON NewReaction where
56+
toJSON (NewReaction content) = object ["content" .= content]
57+
58+
instance FromJSON ReactionContent where
59+
parseJSON = withText "ReactionContent" $ \case
60+
"+1" -> pure PlusOne
61+
"-1" -> pure MinusOne
62+
"laugh" -> pure Laugh
63+
"confused" -> pure Confused
64+
"heart" -> pure Heart
65+
"hooray" -> pure Hooray
66+
"rocket" -> pure Rocket
67+
"eyes" -> pure Eyes
68+
t -> fail $ "Unknown ReactionContent: " <> T.unpack t
69+
70+
instance ToJSON ReactionContent where
71+
toJSON PlusOne = String "+1"
72+
toJSON MinusOne = String "-1"
73+
toJSON Laugh = String "laugh"
74+
toJSON Confused = String "confused"
75+
toJSON Heart = String "heart"
76+
toJSON Hooray = String "hooray"
77+
toJSON Rocket = String "rocket"
78+
toJSON Eyes = String "eyes"

src/GitHub/Endpoints/Reactions.hs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- |
2+
-- The Reactions API as described at
3+
-- <https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28>.
4+
module GitHub.Endpoints.Reactions (
5+
issueReactionsR,
6+
createIssueReactionR,
7+
deleteIssueReactionR,
8+
commentReactionsR,
9+
createCommentReactionR,
10+
deleteCommentReactionR,
11+
module GitHub.Data,
12+
) where
13+
14+
import GitHub.Data
15+
import GitHub.Internal.Prelude
16+
import Prelude ()
17+
18+
-- | List reactions for an issue.
19+
-- See <https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#list-reactions-for-an-issue>
20+
issueReactionsR :: Name Owner -> Name Repo -> Id Issue -> FetchCount -> Request k (Vector Reaction)
21+
issueReactionsR owner repo iid =
22+
pagedQuery ["repos", toPathPart owner, toPathPart repo, "issues", toPathPart iid, "reactions"] []
23+
24+
-- | Create reaction for an issue comment.
25+
-- See <https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#create-reaction-for-an-issue>
26+
createIssueReactionR :: Name Owner -> Name Repo -> Id Issue -> ReactionContent -> Request 'RW Reaction
27+
createIssueReactionR owner repo iid content =
28+
command Post parts (encode $ NewReaction content)
29+
where
30+
parts = ["repos", toPathPart owner, toPathPart repo, "issues", toPathPart iid, "reactions"]
31+
32+
-- | Delete an issue comment reaction.
33+
-- See <https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#delete-an-issue-reaction>
34+
deleteIssueReactionR :: Name Owner -> Name Repo -> Id Issue -> Id Reaction -> GenRequest 'MtUnit 'RW ()
35+
deleteIssueReactionR owner repo iid rid =
36+
Command Delete parts mempty
37+
where
38+
parts = ["repos", toPathPart owner, toPathPart repo, "issues", toPathPart iid, "reactions", toPathPart rid]
39+
40+
-- | List reactions for an issue comment.
41+
-- See <https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#list-reactions-for-an-issue-comment>
42+
commentReactionsR :: Name Owner -> Name Repo -> Id Comment -> FetchCount -> Request k (Vector Reaction)
43+
commentReactionsR owner repo cid =
44+
pagedQuery ["repos", toPathPart owner, toPathPart repo, "issues", "comments", toPathPart cid, "reactions"] []
45+
46+
-- | Create reaction for an issue comment.
47+
-- See https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#create-reaction-for-an-issue-comment
48+
createCommentReactionR :: Name Owner -> Name Repo -> Id Comment -> ReactionContent -> Request 'RW Reaction
49+
createCommentReactionR owner repo cid content =
50+
command Post parts (encode $ NewReaction content)
51+
where
52+
parts = ["repos", toPathPart owner, toPathPart repo, "issues", "comments", toPathPart cid, "reactions"]
53+
54+
-- | Delete an issue comment reaction.
55+
-- See <https://docs.github.com/en/rest/reactions/reactions?apiVersion=2022-11-28#delete-an-issue-comment-reaction>
56+
deleteCommentReactionR :: Name Owner -> Name Repo -> Id Comment -> Id Reaction -> GenRequest 'MtUnit 'RW ()
57+
deleteCommentReactionR owner repo cid rid =
58+
Command Delete parts mempty
59+
where
60+
parts = ["repos", toPathPart owner, toPathPart repo, "issues", "comments", toPathPart cid, "reactions", toPathPart rid]

0 commit comments

Comments
 (0)