Skip to content

emit github events #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node server
39 changes: 39 additions & 0 deletions lib/github-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const crypto = require('crypto')
const debug = require('debug')('github')

const secret = process.env.GITHUB_WEBHOOK_SECRET || 'hush-hush'

const sign = (secret, data) => {
const buffer = new Buffer(data, 'utf8')
return 'sha1=' + crypto.createHmac('sha1', secret).update(buffer).digest('hex')
}

module.exports = (app) => {
app.post('/hooks/github', (req, res) => {
const event = req.headers['x-github-event']
if (!event) {
res.writeHead(400, 'Event Header Missing')
return res.end()
}

const signature = req.headers['x-hub-signature']
if (!signature || signature !== sign(secret, req.raw)) {
res.writeHead(401, 'Invalid Signature')
return res.end()
}

res.end()

const data = req.body
data.action = data.action ? event + '.' + data.action : event

var source = data.repository ? data.repository.full_name : data.organization.login
console.log('event@%s: %s', source, data.action)

if (debug.enabled) {
debug(JSON.stringify(data, null, 2))
}

app.emit(data.action, data)
})
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"license": "MIT",
"dependencies": {
"body-parser": "^1.15.0",
"debug": "^2.2.0",
"dotenv": "^2.0.0",
"express": "^4.13.4",
"github": "^0.2.4",
"travis-ci": "^2.1.0"
Expand Down
27 changes: 27 additions & 0 deletions scripts/display-travis-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const debug = require('debug')('display_travis_status')
const pollTravis = require('../lib/pollTravis')
const enabledRepos = ['citgm', 'readable-stream', 'nodejs.org']

module.exports = function (app) {
app.on('pull_request.opened', (event) => {
const owner = event.repository.owner.login
const repo = event.repository.name
if (!~enabledRepos.indexOf(repo)) return

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.


debug(`/${owner}/${repo}/pull/${event.number} opened`)
pollTravis.pollThenComment(owner, repo, event.number)
})

// to trigger polling manually
app.get('/pr/:owner/:repo/:id', (req, res) => {
const owner = req.params.owner
const repo = req.params.repo
const id = req.params.id
if (~enabledRepos.indexOf(repo)) {
pollTravis.pollThenComment(owner, repo, parseInt(id, 10))
}
res.end()
})
}
42 changes: 13 additions & 29 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
'use strict'

require('dotenv').load({ silent: true })

const express = require('express')
const bodyParser = require('body-parser')

const pollTravis = require('./lib/pollTravis')
const captureRaw = (req, res, buffer) => { req.raw = buffer }

const app = express()

const port = process.env.PORT || 3000

app.use(bodyParser.json())

app.all('/hooks/github', (req, res) => {
if (wasPullRequestOpened(req)) {
const repo = req.body.repository

console.log(`* ${repo.owner.login}/${repo.name}/#${req.body.number} Opened, starting build checks!`)
pollTravis.pollThenComment(repo.owner.login, repo.name, parseInt(req.body.number))
}

res.end()
})

// to trigger polling manually
app.get('/pr/:owner/:repo/:prId', (req, res) => {
pollTravis.pollThenComment(req.params.owner, req.params.repo, parseInt(req.params.prId))
res.end()
app.use(bodyParser.json({ verify: captureRaw }))
require('./lib/github-events.js')(app)

// load all the files in the scripts folder
require('fs').readdirSync('./scripts').forEach((file) => {
file = './scripts/' + file
console.log('loading:', file)
require(file)(app)
})

app.listen(process.env.PORT || 3000, () => {
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log('Example app listening on port', port)
})

function wasPullRequestOpened (req) {
const githubEvent = req.headers['x-github-event'] || ''
const githubAction = req.body.action || ''
return githubEvent === 'pull_request' && githubAction === 'opened'
}