Skip to content

Instantly share code, notes, and snippets.

@dustinlarimer
Last active October 6, 2017 00:23
Show Gist options
  • Save dustinlarimer/1b380bb8d7e8a7ca634ba10d6e2fadb6 to your computer and use it in GitHub Desktop.
Save dustinlarimer/1b380bb8d7e8a7ca634ba10d6e2fadb6 to your computer and use it in GitHub Desktop.
Github Analytics with Keen IO

Github Analytics with Keen IO

This script will create a series of webhooks to stream event data from your Github organization(s) to a Keen project.

Project ID & API Key

Login to Keen IO to create a project and grab the Project ID and Write Key from your project's Access page. Copy these into the keen.projectId and keen.writeKey fields of the config.json file included with this gist.

Github Access Token

Create a new Github access token with the admin:org_hook option selected. Copy this token into the github.token field of the config.json file included with this gist.

Next, add the organization names you want to configure to github.accounts field. This script will create all of the necessary webhooks for each organization listed there. All event data will be recorded to the same Keen project from the previous step, but can be segmented or filterd by an organization.login property, which exists for all event types.

Important: you must be an admin for all of the Github organizations listed for your access token to work properly.

Installation

Clone this gist and run the following command:

npm install octonode

Run the Script

Run the following command to execute the script:

node index.js
{
"keen": {
"projectId": "YOUR_KEEN_PROJECT_ID",
"writeKey": "YOUR_KEEN_WRITE_KEY"
},
"github": {
"accounts": [ "your-organization" ],
"token": "YOUR_GITHUB_ACCESS_TOKEN",
"events": [
"commit_comment",
"create",
"delete",
"deployment",
"deployment_status",
"fork",
"gollum",
"installation",
"installation_repositories",
"issue_comment",
"issues",
"label",
"marketplace_purchase",
"member",
"membership",
"milestone",
"organization",
"org_block",
"page_build",
"project_card",
"project_column",
"project",
"public",
"pull_request_review_comment",
"pull_request_review",
"pull_request",
"push",
"repository",
"release",
"status",
"team",
"team_add",
"watch"
]
}
}
const config = require('./config.json');
const github = require('octonode');
config.github.accounts.forEach((org) => {
config.github.events.forEach((eventType) => {
let url = `https://api.keen.io/3.0/projects/${config.keen.projectId}`;
url += `/events/${eventType}?api_key=${config.keen.writeKey}`;
github
.client(config.github.token)
.org(org)
.hook({
name: 'web',
active: true,
events: [ eventType ],
config: {
url: url,
content_type: 'json'
}
}, console.log);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment