README
¶
Contribute Bot
Contribute Bot is a small service (written using the Go CDK!) that performs automated checks on issues and pull requests to help keep contributions organized and easy to triage for maintainers.
Contribute Bot has two servers: a webhook endpoint and an event listener. The webhook endpoint publishes events to a Cloud Pub/Sub topic that are eventually processed by the event listener. GitHub has a 10 second webhook response time limit combined with a 5000 request/hour API rate limit, so this adds buffering with the assumption that incoming events are bursty.
Configuration
Contribute Bot will look for a configuration file at the root of the repository
called .contributebot on the repository's default branch. This allows changes
to the configuration to be version-controlled and reviewed using the project's
normal process.
The configuration file is in JSON format and has the following keys:
issue_title_pattern-
An RE2 regular expression of an
acceptable issue title. Any issue that does not match the pattern will
receive a response. The default pattern is
^([a-z0-9./-]+|[A-Z_]+): .*$. issue_title_response- The text of the comment that will be added to an issue that does not match the title pattern. This can use GitHub-flavored Markdown.
pull_request_title_pattern-
An RE2 regular expression of an
acceptable pull request title. Any issue that does not match the pattern will
receive a response. The default pattern is
^([a-z0-9./-]+|[A-Z_]+): .*$. pull_request_title_response- The text of the comment that will be added to a pull request that does not match the title pattern. This can use GitHub-flavored Markdown.
require_pull_request_fork_branch-
If
true, then pull requests coming from branches on the same repository will be automatically closed. Defaults totrue.
DevOps Setup
To set up your own instance of Contribute Bot for local testing or deployment:
- Create a new GCP project.
- Set your project using
gcloud config set project PROJECTID, wherePROJECTIDis the project's ID. - Download default application credentials with
gcloud auth application-default login. - Enable App Engine with
gcloud app create. - Copy the
proddirectory to a directory calleddev. - In
dev/main.tf, remove thebackend "gcs"block and change the project IDs to your new GCP project. - Run
terraform initfrom the newdevdirectory. - Run
terraform applyto set up the infrastructure. - Deploy the webhook, creating a random webhook secret.
- Create the GitHub application, setting the webhook URL to
https://PROJECTID.appspot.com/webhook, wherePROJECTIDis your GCP project ID.- Set the
Webhook secretto the random webhook secret you created above. - Make sure to give Read & Write access to Issues, Pull Requests, Checks, Repository Contents and Read-only access to Repository metadata and Repository administration.
- Subscribe to pull request, issue, check run, and push events.
- Set the
- Download a GitHub application secret key and copy the contents into a new
Terraform variable file in the
devdirectory, setting thegithub_app_keyvariable. It's useful to use a "here doc". Then runterraform applyagain to update the secret material. Your variable file should look something like this:
contributebot/dev$ cat terraform.tfvars
github_app_key = <<EOF
-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----
EOF
Developing
To run Contribute Bot locally for testing:
- Create a GitHub repository for testing.
- Install the GitHub application on your test repository (
Settings > Developer Settings > Github Apps, thenEdityour app and selectInstall App). - Download a GitHub application secret key for your test application.
- Run
contributebot, setting the flags for your test GCP project and GitHub application. You can find the App ID underAbouton the Github page for your app. Example:
go run . --project=your-project-name --github_app=42 --github_key=/foo.pem
Deploying
To production
To deploy an updated Contribute Bot to production, follow these steps.
# If you're working on production Contribute Bot, Cloud Build will
# automatically build a new version of the Docker image when commits are
# made to internal/contributebot. Find a new image at
# https://console.cloud.google.com/cloud-build/builds?project=go-cloud-contribute-bot
# Otherwise, fire off a manual Cloud Build.
gcloud builds submit --config cloudbuild.yaml ../.. --project=go-cloud-contribute-bot
# Edit prod/k8s/contributebot.yaml and replace the image with the one
# you just created.
# Apply to cluster. Replace project and zone with the actual values.
gcloud container clusters get-credentials \
--project=go-cloud-contribute-bot \
--zone=us-central1-c \
contributebot-cluster
kubectl apply -f prod/k8s
# Check that the deployment was successful:
kubectl describe pods --selector=app=contributebot-worker
# Send a PR with the updated .yaml file.
Somewhere else
If you want to deploy to your own cluster, modify k8s/contributebot.yaml to
replace go-cloud-contribute-bot with your own project ID, and 15206 with
your own Github App ID. Run the commands above, using your own project ID
in the command line arguments instead of go-cloud-contribute-bot.