Skip to content

Commit

Permalink
Revert "Revert emitting disabled status for all public repositories"
Browse files Browse the repository at this point in the history
This reverts commit 0eaddf8.
  • Loading branch information
pbrisbin committed Oct 8, 2024
1 parent d53709d commit 059634f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
30 changes: 21 additions & 9 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 @@ -43,12 +44,16 @@ data Commit = Commit
deriving anyclass (FromJSON, ToJSON)

data Repo = Repo
{ owner :: Owner
{ private :: Bool
, owner :: Owner
, name :: Text
}
deriving stock (Generic)
deriving anyclass (FromJSON, ToJSON)

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

newtype Owner = Owner
{ login :: Text
}
Expand All @@ -60,26 +65,33 @@ newtype Owner = Owner
-- Rough plan:
--
-- - [x] Our own testing repositories
-- - [ ] All public repositories
-- - [x] All public repositories
-- - [ ] All except those orgs who are still paying
-- - [ ] Every
shouldEmitDisabledStatus :: Webhook -> Bool
shouldEmitDisabledStatus webhook =
and
[ webhook.pull_request.base.repo.owner.login == "restyled-io"
, webhook.pull_request.base.repo.name == "demo"
]
shouldEmitDisabledStatus webhook
| webhook.pull_request.base.repo.owner.login == "restyled-io" = True
| not webhook.pull_request.base.repo.private = True
| otherwise = False

emitDisabledStatus
:: (MonadIO m, MonadLogger m, MonadReader env m, HasSettings env)
=> BSL.ByteString
-> 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 059634f

Please sign in to comment.