PostHog / contributions-bot

A bot for automating the handling of open source contributions to PostHog.
MIT License
0 stars 2 forks source link

PostHog Contributions Bot

hero image

Credit where credit is due

Big thanks to All Contributors for the basic foundations of this bot.

What this is

This bot is a fork (albeit a largely unrecognizable one) of all-contributors/app, which also leverages a fork of all-contributors/all-contributors-cli piublished on npm as @posthog/all-contributors-cli.

Disclaimer

This bot is currently structured for PostHog use only, so uses a lot of hardcoded values. It also:

powered by bot Among a lot of other things, this bot powers our Contributors page at PostHog


The original All Contributors GitHub app (bot) allows users to tag the bot somewhere on a repo and request that a contributor be added to that repo's README.

This bot, however, does a lot more. Here are some notable modifications:

Functionality

Semantics

Code Quality


Environment Variables

Documentation for these is a work in progress.

APP_ID
WEBHOOK_SECRET
PRIVATE_KEY
WEBHOOK_PROXY_URL
GITHUB_TOKEN
DEBUG
PH_PROJECT_API_KEY
MAILGUN_API_KEY
MAILGUN_DOMAIN
MAILGUN_HOST
TARGET_OWNER
TARGET_REPO
USE_TEST_CODES
DEFAULT_BRANCH
ALLOWED_ORGS
DATABASE_URL

Database

Note: Schemas are provided as a copy-paste of the creation SQL for now, while I haven't made beautiful Markdown tables for them.

This bot uses a PostgreSQL database with the following 3 tables:

contributors

Hosts the data on contributors.

Schema (in the form of creation SQL):

CREATE TABLE contributors(
    username CHAR(40) NOT NULL,
    level INT NOT NULL,
    PRIMARY KEY(username)
);

gift_cards

Hosts gift cards manually generated from Shopify. Our plan doesn't give us access to the gift cards API that we could use to generate these on the fly.

Schema (in the form of creation SQL):

CREATE TABLE gift_cards(
    token CHAR(30) NOT NULL,
    value INT NOT NULL,
    level INT NOT NULL,
    has_been_used BOOL NOT NULL,
    used_at TIMESTAMP,
    username CHAR(40),
    PRIMARY KEY(token),
    CONSTRAINT fk_username
          FOREIGN KEY(username) 
      REFERENCES contributors(username)
);

test_gift_cards

Same as above, except this table is used when the USE_TEST_CODES environment variable is set to true.

This table on our production bot has been loaded with hundreds of randomly-generated test_gift_cards which aren't valid on Shopify.

Local Setup

While this bot doesn't have extensive Docs, these are the original instructions for running the All Contributors bot with slight modifications. This is not enough to run the PostHog bot! You also need to set the relevant environment variables - docs coming soon...

Important: If you have a production bot (from All Contributors or PostHog) running in your account you may get duplicated replies to comments, PRs, etc.

1. Create a GitHub App for testing

1.1 Go to your GitHub Developer Settings

1.2 Create a new GitHub app

Required fields are:

Important fields are:

2. Configure Your GitHub App for testing

You should now have an app created

3. Configure your local server to talk to the GitHub app

Create a file named .env with the following template:

APP_ID=
WEBHOOK_SECRET=development
PRIVATE_KEY=

Values

4. Setup a test GitHub repository/with issues PR

To verify if the bot should have seen this, go to your app settings. On the Advanced Tab, see if you have any delivered payloads. If you're not using a service like smee.io, copy the payload and save it locally in a file called test-webhook-payload.json. Also make note of the headers under the 'Headers' section.

5. Send your first hook

  1. Install the node modules for the bot: yarn
  2. Run the bot: yarn start
  3. If you're not using a payload delivery service, send a POST request to the bot using the headers you got from the previous step and the content from test-webhook-payload.json. If you're using curl, this will look something like this:
curl -vX POST http://localhost:3000/ -d @test-webhook-payload.json \
--header "Content-Type: application/json" \
--header "User-Agent: GitHub-Hookshot/4d63832" \
--header "X-GitHub-Delivery: 413857f0-8b61-11eb-92be-566b7aa5f6ee" \
--header "X-GitHub-Event: issue_comment" \
--header "X-GitHub-Hook-ID: 297478976" \
--header "X-GitHub-Hook-Installation-Target-ID: 105785" \
--header "X-GitHub-Hook-Installation-Target-Type: integration" \
--header "X-Hub-Signature: sha1=ed222e6750dc2954a422ed8dd371f9da66368104" \
--header "X-Hub-Signature-256: sha256=04d0943f20545ac8df974466c502e4b9743d3618149b03f4ea1a9e658bf31fd0"

If there are no errors in the bot console, check your github test issue to see the bot respond :tada:

Using smee.io

Alternatively, instead of having to mock the webhook payload using curl, you can add an additional environment variable called WEBHOOK_PROXY_URL and set it to a smee.io channel URL.

Once you've done that, set the Webhook URL for you app in GitHub to the same channel URL and, after a server restart, your bot will be able to directly respond to incoming webhooks.