Custom Webhook Governance
Implementing Webhook Based Custom Governance Controllers
Some enterprises require bespoke governance, either because they are not standardized on ServiceNow or because they use it in a unique, tailor-built way. Plural allows you to bridge that gap with a simple webhook-based protocol. Before implementing, it's worth briefly looking through the docs on PR governance to familiarize yourself with the model.
Defining a PrGovernance CRD
The webhook-based Governance CRD is relatively straightforward. Here's an example:
apiVersion: deployments.plural.sh/v1alpha1
kind: PrGovernance
metadata:
name: webhook
spec:
type: WEBHOOK
connectionRef:
name: governance # reference to the ScmConnection that is used for Pr approval
configuration:
webhook:
url: https://governance-webhook.acme.comFrom there, to tie it to a pull request, you'll need:
- An SCM webhook for the repository pointing to Plural (this can be created in Self Service -> SCM Management in your Plural Console instance).
- The
Plural governance: webhooktag in your PR description, so that we identify it as requiring governance.
Webhook Controller Implementation
The webhook expects three REST-like APIs to be present:
- POST /v1/open - called when a PR is created
- POST /v1/confirm - called while a PR is open; allows auto-approval if it returns 200
- POST /v1/close - called once a PR is merged or closed, allowing your implementation to clean up any needed resources
Throughout the request flow, we will persist the results from prior calls as state, and provide a JSON-encoded request body in the following format:
{
"state": any | null,
"pr": {
"id": string,
"title": string,
"body": string,
"ref": string,
"url": string,
"status": "open" | "closed" | "merged",
"labels": string[]
}
}The webhook controller will do the following once it is tied to a PR:
- On PR open, call
POST /v1/open - While the PR is open, periodically call
POST /v1/confirmwith the body from the prior call to/v1/openprovided in thestatefield above. - If
/v1/confirmreturns 200, the pull request is approved. - Once the pull request is merged or closed, a call to
POST /v1/closeis made.
Here's a state diagram for the visually inclined as well: