-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: node server |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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() | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as off-topic.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as off-topic.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as off-topic.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.