Skip to content

Commit 529e357

Browse files
isomorphemeandreasabel
authored andcommitted
Add initial subset of Reactions endpoints
1 parent 709cc3a commit 529e357

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

github.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ library
117117
GitHub.Data.PublicSSHKeys
118118
GitHub.Data.PullRequests
119119
GitHub.Data.RateLimit
120+
GitHub.Data.Reactions
120121
GitHub.Data.Releases
121122
GitHub.Data.Repos
122123
GitHub.Data.Request
@@ -157,6 +158,7 @@ library
157158
GitHub.Endpoints.PullRequests.Comments
158159
GitHub.Endpoints.PullRequests.Reviews
159160
GitHub.Endpoints.RateLimit
161+
GitHub.Endpoints.Reactions
160162
GitHub.Endpoints.Repos
161163
GitHub.Endpoints.Repos.Collaborators
162164
GitHub.Endpoints.Repos.Comments

src/GitHub.hs

Lines changed: 10 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module GitHub.Data (
5555
module GitHub.Data.PullRequests,
5656
module GitHub.Data.RateLimit,
5757
module GitHub.Data.Releases,
58+
module GitHub.Data.Reactions,
5859
module GitHub.Data.Repos,
5960
module GitHub.Data.Request,
6061
module GitHub.Data.Reviews,
@@ -97,6 +98,7 @@ import GitHub.Data.PublicSSHKeys
9798
import GitHub.Data.PullRequests
9899
import GitHub.Data.RateLimit
99100
import GitHub.Data.Releases
101+
import GitHub.Data.Reactions
100102
import GitHub.Data.Repos
101103
import GitHub.Data.Request
102104
import GitHub.Data.Reviews

src/GitHub/Data/Reactions.hs

Lines changed: 78 additions & 0 deletions
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, Enum, Bounded, 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

Lines changed: 60 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy