Skip to content
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
14 changes: 14 additions & 0 deletions apps/api/src/vcs-sync-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ export const runScheduledSync = Effect.gen(function* () {
Effect.withSpan("VcsScheduledSync.tick"),
)

// The refused-push cron: enqueue a rule suite scan per GitHub installation that collects them.
export const runRuleSuiteScan = Effect.gen(function* () {
const scheduler = yield* VcsScheduledSyncService
const enqueued = yield* scheduler.runRuleSuiteScan
yield* Effect.annotateCurrentSpan({ "vcs.rule_suites.enqueued": enqueued })
}).pipe(
Effect.tapCause((cause) =>
Effect.logError("[VCS] rule suite scan tick failed").pipe(
Effect.annotateLogs({ error: summarizeCause(cause) }),
),
),
Effect.withSpan("VcsRuleSuiteScan.tick"),
)

// No DLQ exists, so on the final delivery (attempt > max_retries) we persist a terminal status
// instead of silently dropping.

Expand Down
12 changes: 11 additions & 1 deletion apps/api/src/worker/crons.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* The api Worker's schedules. Dispatched by `Cloudflare.Workers.cron`:
* every 12h — VCS sync backstop, enqueues a refresh per installation
* every 15m — reads the pushes GitHub rulesets refused into the audit log
* hourly — scrape_target_checks retention (was inline on the
* scrape-results write path; a busy target writes ~75k
* rows/day, so the 10k cap binds within hours), PlanetScale
Expand All @@ -15,8 +16,9 @@ import { vcsSyncModule } from "./modules"

const VCS_SYNC_CRON = "0 */12 * * *"
const SCRAPE_RETENTION_CRON = "0 * * * *"
const RULE_SUITE_SCAN_CRON = "*/15 * * * *"

/** Attaches the two schedules at plan time and their listeners at runtime. Needs `CronEventSourceLive`. */
/** Attaches the schedules at plan time and their listeners at runtime. Needs `CronEventSourceLive`. */
export const registerCrons = (ports: ApiPortsLayer) =>
Effect.gen(function* () {
yield* Cloudflare.Workers.cron(VCS_SYNC_CRON, () =>
Expand All @@ -27,6 +29,14 @@ export const registerCrons = (ports: ApiPortsLayer) =>
),
).pipe(settleFire(VCS_SYNC_CRON)),
)
yield* Cloudflare.Workers.cron(RULE_SUITE_SCAN_CRON, () =>
Effect.flatMap(vcsSyncModule, ({ VcsScheduledLive, runRuleSuiteScan, vcsSyncTelemetry }) =>
runEvent(
runRuleSuiteScan,
VcsScheduledLive.pipe(Layer.provideMerge(vcsSyncTelemetry), Layer.provideMerge(ports)),
),
).pipe(settleFire(RULE_SUITE_SCAN_CRON)),
)
yield* Cloudflare.Workers.cron(SCRAPE_RETENTION_CRON, () =>
Effect.gen(function* () {
const [
Expand Down
3 changes: 2 additions & 1 deletion apps/landing/src/content/docs/integrations/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Events are grouped into categories, each with its own switch under **Audit log**
| Deploy keys and access tokens | Yes | Deploy keys added or removed; fine-grained token requests (`deploy_key.*`, `personal_access_token_request.*`) | Deployments: Read for deploy keys; Personal access token requests (organization): Read for token requests |
| Code security settings | Yes | Secret scanning, push protection, Dependabot or code scanning turned on or off (`security_and_analysis.changed`) | Administration: Read |
| Force pushes | Yes | A push that rewrote a branch's or tag's history, with the commits before and after (`push.forced`, see below) | None |
| Refused pushes | Yes | Pushes a ruleset refused, such as a force push or an unsigned commit to a protected branch (`rule_suite.failed`), read every 15 minutes | Administration: Read |
| Branch and tag deletions | Yes | `delete.branch`, `delete.tag` | None |
| Branch and tag creations | No | `create.branch`, `create.tag` | None |
| Pull request merges and approvals | No | `pull_request.merged`, `pull_request_review.approved`, `pull_request_review.dismissed` | None |
Expand All @@ -97,7 +98,7 @@ Events are grouped into categories, each with its own switch under **Audit log**
| Security alerts | No | Secret scanning, code scanning and Dependabot alerts and repository security advisories (`secret_scanning_alert.created`, ...) | Secret scanning alerts, Code scanning alerts, Dependabot alerts, Repository security advisories: Read |

- **Force pushes:** a force push to the repository's default branch is `push.forced.default_branch`. One to a branch with an open pull request that someone else opened is `push.forced.non_author`, and its entry names that pull request; Maple asks GitHub for the branch's pull requests when the push arrives, and if GitHub does not answer the entry stays `push.forced`. A rebase someone does for the author with **Update branch**, or a bot that pushes to a person's pull request, counts too. Only branches in the same repository are checked: a push to a fork's branch never reaches the upstream repository's webhooks. Every other force push is `push.forced`. The **Detections** page has a template for each of the first two.
- **Refused pushes:** a push that branch protection or a ruleset refuses never reaches the repository, so GitHub sends no webhook for it and it is not in the audit log.
- **Refused pushes:** a push a ruleset refuses never reaches the repository, so GitHub sends no webhook for it. Maple reads each repository's rule insights every 15 minutes instead and writes every refusal as `rule_suite.failed` with the outcome **Denied**, naming who pushed, the branch, and the rules that failed (for example `non_fast_forward` for a force push, `required_signatures` for an unsigned commit). It needs **Administration: Read**, and only rulesets are covered: a push that classic branch protection refuses leaves no record GitHub shares. GitHub keeps rule insights for public repositories, and for private ones on paid plans. The **Detections** page has a template for refused pushes.
- **Permissions:** until an owner accepts a permission the Maple app asks for (organization **Settings > GitHub Apps > Review request**), the events that need it do not arrive. Maple never changes repository settings.
- **Time:** GitHub's webhooks carry no event time, so an entry's time is when GitHub sent it, normally seconds before Maple receives it.
- **Not covered:** sign-ins, Actions settings, and other events GitHub records only in its own audit log, which GitHub offers on Enterprise Cloud. Ordinary pushes, pull request activity other than merges and approvals, and CI runs stay out.
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/page/integrations/github.scene.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ describe("github card", () => {
given(model),
expectView(role("switch", { name: "Force pushes" })).toHaveAttr("aria-checked", "true"),
expectView(role("switch", { name: "Security alerts" })).toHaveAttr("aria-checked", "false"),
expectView(text("7 of 13 categories")).toExist(),
expectView(role("switch", { name: "Refused pushes" })).toHaveAttr("aria-checked", "true"),
expectView(text("8 of 14 categories")).toExist(),
)
const toggled = pageUpdate(model, Message.ToggledAuditCategory({ id: "securityAlerts" }))
expect(Arr.map(toggled.commands ?? [], ({ name, args }) => [name, args])).toEqual([
Expand Down
11 changes: 11 additions & 0 deletions apps/web/src/page/security/kit/detection-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type DetectionTemplateId =
| "sign_in"
| "force_push_default_branch"
| "force_push_non_author"
| "refused_push"

export type DetectionTemplate = Readonly<{
id: DetectionTemplateId
Expand Down Expand Up @@ -90,6 +91,16 @@ export const DETECTION_TEMPLATES: ReadonlyArray<DetectionTemplate> = [
windowMinutes: 5,
severity: "warning",
},
{
id: "refused_push",
title: "Push refused by a ruleset",
summary:
"Fires when a GitHub ruleset refuses a push, such as a force push or an unsigned commit to a protected branch.",
filters: { provider: "github", action: "rule_suite.failed" },
threshold: 1,
windowMinutes: 30,
severity: "critical",
},
]

/** The template a URL names, if this build has it. */
Expand Down
20 changes: 10 additions & 10 deletions docs/github-app-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ On repositories where a Maple admin has switched on the pull request review, Map
1. Find **Permissions → Repository permissions**.
2. Set the following, leaving every other permission at **No access**:

| Repository permission | Access level |
| --------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Contents** | **Read-only**, or **Read and write** to allow `@maple fix` commits |
| **Pull requests** | **Read and write** (reads the diff and posts the review as a comment; approves a pull request only where an admin turned on **Approve low-risk pull requests**, and never requests changes) |
| **Checks** | **Read and write** (posts the `Maple / observability` check run on the head commit; without it the review is still posted as a comment) |
| **Metadata** | **Read-only** (GitHub pre-selects this and it is mandatory) |
| **Actions** | **Read-only** (downloads each finished job's log for CI logs; without it CI traces still arrive but carry no logs) |
| **Administration** | **Read-only** (delivers branch protection, ruleset and code security changes to the Security audit log; without it the audit log still gets members, teams and repositories) |
| **Deployments** | **Read-only** (delivers deploy key and deployment changes to the Security audit log; optional) |
| **Secret scanning alerts**, **Code scanning alerts**, **Dependabot alerts**, **Repository security advisories** | **Read-only** (the Security alerts audit category; optional) |
| Repository permission | Access level |
| --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Contents** | **Read-only**, or **Read and write** to allow `@maple fix` commits |
| **Pull requests** | **Read and write** (reads the diff and posts the review as a comment; approves a pull request only where an admin turned on **Approve low-risk pull requests**, and never requests changes) |
| **Checks** | **Read and write** (posts the `Maple / observability` check run on the head commit; without it the review is still posted as a comment) |
| **Metadata** | **Read-only** (GitHub pre-selects this and it is mandatory) |
| **Actions** | **Read-only** (downloads each finished job's log for CI logs; without it CI traces still arrive but carry no logs) |
| **Administration** | **Read-only** (delivers branch protection, ruleset and code security changes to the Security audit log, and lets Maple read rule insights for pushes a ruleset refused; without it the audit log still gets members, teams and repositories) |
| **Deployments** | **Read-only** (delivers deploy key and deployment changes to the Security audit log; optional) |
| **Secret scanning alerts**, **Code scanning alerts**, **Dependabot alerts**, **Repository security advisories** | **Read-only** (the Security alerts audit category; optional) |

> **Upgrading an existing App?** Adding a permission to an App that is already installed makes GitHub ask every installation's admin to accept the new permissions. Until they do, Maple still records each review but cannot post it: the review's row keeps a publish error and nothing appears on the pull request. CI logs behave the same way: until the installation accepts **Actions: Read**, GitHub refuses every job log download with `403 Resource not accessible by integration` and those repositories get traces without logs. To fix it, accept the permission request on GitHub (Settings → Applications → the App → Review request).

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AuditTrailEvent } from "@maple/domain/audit-trail"
import type { Effect, Option, Stream } from "effect"
import type {
BranchUpsertInput,
Expand Down Expand Up @@ -205,6 +206,28 @@ export interface VcsProviderClient {
| VcsRateLimitedError
>

/**
* Pushes the repository's rulesets refused after `sinceMs`, as audit events, oldest first.
* Reads at most a few pages, newest first: `truncated` says older refusals were left unread.
* `newestMs` is the newest refusal read, the cursor's next value.
*/
readonly fetchRefusedPushes: (
installation: VcsInstallation,
repo: VcsRepositoryRef & { readonly fullName: string },
opts: { readonly sinceMs: number; readonly nowMs: number },
) => Effect.Effect<
{
readonly events: ReadonlyArray<AuditTrailEvent>
readonly newestMs: number | undefined
readonly truncated: boolean
},
| VcsProviderError
| VcsInstallationGoneError
| VcsRepoUnavailableError
| VcsRepositoryBlockedError
| VcsRateLimitedError
>

/**
* One pull request by number, normalized. `Option.none` means "no such PR in
* this repo" (404 — expected, not a failure), matching `fetchCommit`.
Expand Down Expand Up @@ -604,6 +627,14 @@ export interface VcsProviderClient {
VcsProviderError | VcsInstallationGoneError | VcsRepoUnavailableError | VcsRepositoryBlockedError
>

/** Whether the installation has granted the provider access to rulesets' rule insights. */
readonly canReadRuleSuites: (
installation: VcsInstallation,
) => Effect.Effect<
boolean,
VcsProviderError | VcsInstallationGoneError | VcsRepoUnavailableError | VcsRepositoryBlockedError
>

/** Whether the installation has granted the provider access to CI job logs. */
readonly canReadWorkflowLogs: (
installation: VcsInstallation,
Expand Down
Loading
Loading