Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit disable status for all public repositories #315

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ""
3 changes: 2 additions & 1 deletion templates/widgets/github-actions-warning.hamlet
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<p>
The Restyled GitHub App is deprecated and will be disabled in the coming
months. Please
<a href="https://github.com/restyled-io/actions#readme">run Restyled using GitHub Actions
<a href="https://docs.restyled.io/docs/migrating-to-github-actions/">
migrate to using Restyled via GitHub Actions
\. It works just as well or better, and is completely free, even for
private repositories.

Expand Down