Skip to content

Commit 30575d7

Browse files
committed
Merge #272: Use reactions for confirmation when appropriate
Approved-by: isomorpheme Priority: Normal Auto-deploy: false
2 parents 7aa6981 + 802f041 commit 30575d7

9 files changed

Lines changed: 651 additions & 428 deletions

File tree

nix/haskell-overlay.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
{ sources ? import ./sources.nix, pkgs }:
22
self: super: {
33
hoff = self.callPackage ../hoff.nix { };
4+
5+
github =
6+
pkgs.haskell.lib.compose.appendPatches
7+
[
8+
# https://github.com/haskell-github/github/pull/509
9+
(pkgs.fetchpatch {
10+
name = "github.patch";
11+
url = "https://github.com/haskell-github/github/commit/623105d3987c4bb4e67d48e5ae36a3af97480be9.patch";
12+
sha256 = "sha256-3zRYnrxg9G+druD8o5iejCnTclxd2eg1V7BAO6USjzo=";
13+
})
14+
]
15+
super.github;
416
}

src/EventLoop.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ eventFromCommentPayload payload =
7171
let number = PullRequestId payload.number
7272
author = payload.author -- TODO: Wrapper type
7373
body = payload.body
74-
commentAdded = Logic.CommentAdded number author body
74+
commentAdded = Logic.CommentAdded number author payload.id body
7575
in case payload.action of
7676
Left Github.CommentCreated -> Just commentAdded
7777
Right Github.ReviewSubmitted -> Just commentAdded

src/Github.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import GHC.Natural (Natural)
3737

3838
import Git (Sha (..), Branch (..), BaseBranch (..), Context)
3939
import Project (ProjectInfo (..))
40-
import Types (Body, Username)
40+
import Types (Body, Username, CommentId (..))
4141
import Data.Maybe (fromMaybe)
4242

4343
data PullRequestAction
@@ -82,11 +82,15 @@ data PullRequestPayload = PullRequestPayload {
8282

8383
data CommentPayload = CommentPayload {
8484
action :: Either CommentAction ReviewAction, -- Corresponds to "action".
85-
owner :: Text, -- Corresponds to "repository.owner.login".
86-
repository :: Text, -- Corresponds to "repository.name".
87-
number :: Int, -- Corresponds to "issue.number" or "pull_request.number".
88-
author :: Username, -- Corresponds to "sender.login".
89-
body :: Text -- Corresponds to "comment.body" or "review.body".
85+
owner :: Text, -- Corresponds to "repository.owner.login".
86+
repository :: Text, -- Corresponds to "repository.name".
87+
number :: Int, -- Corresponds to "issue.number" or "pull_request.number".
88+
author :: Username, -- Corresponds to "sender.login".
89+
id :: Maybe CommentId, -- Corresponds to "comment.id".
90+
-- Can be absent if we actually received a review,
91+
-- because those have separate IDs from ordinary issue
92+
-- comments.
93+
body :: Text -- Corresponds to "comment.body" or "review.body".
9094
} deriving (Eq, Show)
9195

9296
data CommitStatusPayload = CommitStatusPayload {
@@ -169,6 +173,10 @@ instance FromJSON CommentPayload where
169173
<*> (getNested v ["issue", "number"]
170174
<|> getNested v ["pull_request", "number"])
171175
<*> getNested v ["sender", "login"]
176+
<*> (getNested v ["comment", "id"]
177+
-- If we couldn't get a comment ID, we likely got a review, which does have an ID,
178+
-- but we can't treat that as a comment ID for API requests.
179+
<|> pure Nothing)
172180
<*> (getNested v ["comment", "body"]
173181
<|> fromMaybe "" <$> getNested v ["review", "body"])
174182
parseJSON nonObject = typeMismatch "(issue_comment | pull_request_review) payload" nonObject

src/GithubApi.hs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ module GithubApi
1717
(
1818
GithubOperation (..),
1919
PullRequest (..),
20+
ReactionContent(..),
2021
getOpenPullRequests,
2122
getPullRequest,
2223
hasPushAccess,
2324
leaveComment,
25+
addReaction,
2426
runGithub,
2527
runGithubReadOnly,
2628
)
@@ -32,13 +34,16 @@ import Effectful (Dispatch (Dynamic), DispatchOf, Eff, Effect, IOE, (:>))
3234
import Effectful.Dispatch.Dynamic (interpret, send, interpose)
3335
import Data.IntSet (IntSet)
3436
import Data.Text (Text)
37+
import GitHub.Data.Reactions (ReactionContent(..))
3538

3639
import qualified Data.IntSet as IntSet
3740
import qualified Data.Vector as Vector
41+
import qualified GitHub.Data.Id as Github3
3842
import qualified GitHub.Data.Name as Github3
3943
import qualified GitHub.Data.Options as Github3
4044
import qualified GitHub.Endpoints.Issues.Comments as Github3
4145
import qualified GitHub.Endpoints.PullRequests as Github3
46+
import qualified GitHub.Endpoints.Reactions as Github3
4247
import qualified GitHub.Endpoints.Repos.Collaborators as Github3
4348
import qualified GitHub.Request as Github3
4449
import qualified Network.HTTP.Client as Http
@@ -48,7 +53,7 @@ import Format (format)
4853
import Git (BaseBranch (..), Branch (..), Sha (..))
4954
import MonadLoggerEffect (MonadLoggerEffect)
5055
import Project (ProjectInfo)
51-
import Types (PullRequestId (..), Username (..))
56+
import Types (PullRequestId (..), Username (..), CommentId (..), ReactableId (..))
5257

5358
import qualified Project
5459

@@ -64,6 +69,7 @@ data PullRequest = PullRequest
6469

6570
data GithubOperation :: Effect where
6671
LeaveComment :: PullRequestId -> Text -> GithubOperation m ()
72+
AddReaction :: ReactableId -> ReactionContent -> GithubOperation m ()
6773
HasPushAccess :: Username -> GithubOperation m Bool
6874
GetPullRequest :: PullRequestId -> GithubOperation m (Maybe PullRequest)
6975
GetOpenPullRequests :: GithubOperation m (Maybe IntSet)
@@ -73,6 +79,9 @@ type instance DispatchOf GithubOperation = 'Dynamic
7379
leaveComment :: GithubOperation :> es => PullRequestId -> Text -> Eff es ()
7480
leaveComment pr remoteBranch = send $ LeaveComment pr remoteBranch
7581

82+
addReaction :: GithubOperation :> es => ReactableId -> ReactionContent -> Eff es ()
83+
addReaction id' reaction = send $ AddReaction id' reaction
84+
7685
hasPushAccess :: GithubOperation :> es => Username -> Eff es Bool
7786
hasPushAccess username = send $ HasPushAccess username
7887

@@ -119,6 +128,26 @@ runGithub auth projectInfo =
119128
Right _ -> logInfoN $ format "Posted comment on {}#{}: {}"
120129
(Project.repository projectInfo, pr, body)
121130

131+
AddReaction reactableId reaction -> do
132+
let
133+
createReactionR project owner =
134+
case reactableId of
135+
OnIssueComment (CommentId commentId) -> Github3.createCommentReactionR project owner (Github3.Id commentId)
136+
OnPullRequest (PullRequestId prId) -> Github3.createIssueReactionR project owner (Github3.Id prId)
137+
138+
result <- liftIO $ Github3.github auth $ createReactionR
139+
(Github3.N $ Project.owner projectInfo)
140+
(Github3.N $ Project.repository projectInfo)
141+
reaction
142+
143+
case result of
144+
Left err -> logWarnN $ format "Failed to add reaction: {}" [show err]
145+
Right _ ->
146+
logInfoN $
147+
format
148+
"Added reaction in {} on {}: {}"
149+
(Project.repository projectInfo, reactableId, show reaction)
150+
122151
HasPushAccess (Username username) -> do
123152
result <- liftIO $ Github3.github auth $ Github3.collaboratorPermissionOnR
124153
(Github3.N $ Project.owner projectInfo)
@@ -196,3 +225,5 @@ runGithubReadOnly auth projectInfo = runGithub auth projectInfo . augmentedGithu
196225
-- These operations have side effects, we fake them.
197226
LeaveComment pr body ->
198227
logInfoN $ format "Would have posted comment on {}: {}" (show pr, body)
228+
AddReaction reactableId reaction ->
229+
logInfoN $ format "Would have added reaction on {}: {}" (reactableId, show reaction)

0 commit comments

Comments
 (0)