Skip to content

Commit

Permalink
Tweak responsibilities between Disabled and Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Oct 8, 2024
1 parent 4641f67 commit f728999
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/Restyled/Disabled.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ newtype Installation = Installation
deriving anyclass (FromJSON, ToJSON)

data PullRequest = PullRequest
{ base :: Commit
{ number :: Int
, base :: Commit
, head :: Commit
}
deriving stock (Generic)
Expand All @@ -50,6 +51,9 @@ data Repo = Repo
deriving stock (Generic)
deriving anyclass (FromJSON, ToJSON)

repoFullName :: Repo -> Text
repoFullName repo = repo.owner.login <> "/" <> repo.name

newtype Owner = Owner
{ login :: Text
}
Expand All @@ -76,10 +80,18 @@ emitDisabledStatus
-> m (Either String GitHub.Status)
emitDisabledStatus body = runExceptT $ do
webhook <- hoistEither $ eitherDecode body
logDebug $ "Webhook" :# ["contents" .= webhook]
guard $ shouldEmitDisabledStatus webhook

settings <- lift $ view settingsL
token <- generateToken settings webhook

logInfo
$ "Emitted disabled status"
:# [ "repository" .= repoFullName webhook.pull_request.base.repo
, "pullRequest" .= webhook.pull_request.number
, "commitSha" .= webhook.pull_request.head.sha
]

createStatus token webhook

generateToken
Expand Down
11 changes: 5 additions & 6 deletions src/Restyled/Handlers/Webhooks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import Restyled.Yesod

postWebhooksR :: Handler ()
postWebhooksR = do
qs <- view queuesL
body <- runConduit $ rawRequestBody .| sinkLbs

emitDisabledStatus body >>= \case
Left err -> do
logDebug $ "Disabled status not emitted" :# ["reason" .= err]
runRedis $ enqueue qs $ toStrict body -- proceed normally
Right status -> do
logInfo $ "Disabled status emitted" :# ["status" .= show @Text status]
Left {} -> do
-- Not emitted, proceed normally
qs <- view queuesL
runRedis $ enqueue qs $ toStrict body
Right {} -> pure ()

sendResponseStatus @_ @Text status201 ""

0 comments on commit f728999

Please sign in to comment.