This is a very basic flask server using Flask-Hookserver that responds to custom github webhooks.
When a pull request hook is received, the app does the following things:
- Check the action on the PR webook. Only continues if it is a review request or assignment
- Look up all slack users
- Attempt to match the github username to a slack username or displayname
- If there is no match, it will retrieve the github user's full name via the API and attempt to match it to a slack full name.
- If there is no matched username, it will use a generic phrase, and post the message in the default channel
- If a matched slack username is found, a message will be sent to the matched user only.
- The message will contain:
- Github Repo & PR #
- A link to the PR
- PR Title and description
- The author of the PR
- A random octocat (because why not?)
First, create a custom slack bot. Save the API Key.
Next, create a Github Personal Access Token. This used for basic auth needed to lookup a user's full name over the api.
Next, create a custom github webhook on your repo or organization.
The webhook should be defined at the highest level for which you would like notifications.
If you want notifications on a single repo, set it up on that repo.
If you would like it for all repos on an organization, create the webhook on the organization.
The pull request event is currently the only event that matters.
Make note of the secret. You will need to use this as an environment variable.
Point the URL to where you will be hosting your code.
Make sure to include /hooks
. e.g. https://my-github.example.com/hooks
Finally, host the code on a server that github can reach. You will need the following environment variables:
SLACK_BOT_TOKEN='<YOUR SLACKBOT TOKEN>'
GITHUB_WEBHOOKS_KEY='<YOUR WEBHOOK SECRET>'
GITHUB_API_TOKEN='<YOUR GITHUB API TOKEN>'
GITHUB_API_USER='<YOUR GITHUB API USERNAME>'
DEFAULT_NOTIFICATION_CHANNEL='#<YOUR DEFAULT SLACK CHANNEL>' # Fallback channel where messages will appear when no user found
IGNORED_USERS='<IGNOREDUSERNAME>,<ANOTHER_IGNORED_USERNAME>' # Actions initiated by these users will be ignored
You can use the prebuilt Docker image to run the server. Be sure to inject the appropriate env vars when starting up the container.
There are plenty of ways to improve this little app. See the TODO labeled issues for a list of some of them. Pull requests are welcome