Skip to content
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

Created smoketest verifying the bot starts #38

Merged
merged 1 commit into from
May 10, 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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ language: node_js
node_js:
- 4
- 5
- 6
21 changes: 21 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

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

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

const captureRaw = (req, res, buffer) => { req.raw = buffer }

const app = express()
app.use(bodyParser.json({ verify: captureRaw }))
require('./lib/github-events')(app)

// load all the files in the scripts folder
glob.sync(process.argv[2] || './scripts/**/*.js').forEach((file) => {
console.log('loading:', file)
require(file)(app)
})

module.exports = app
2 changes: 1 addition & 1 deletion lib/github-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const githubClient = new GitHub({

githubClient.authenticate({
type: 'oauth',
token: process.env.GITHUB_TOKEN
token: process.env.GITHUB_TOKEN || 'invalid-placeholder-token'
})

module.exports = githubClient
19 changes: 2 additions & 17 deletions lib/github-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,10 @@ module.exports = (app) => {

res.end()

emit(data)
app.emitGhEvent(data)
})

if (process.env.SSE_RELAY) {
const EventSource = require('eventsource')
var es = new EventSource(process.env.SSE_RELAY)
es.onmessage = (e) => {
try {
const data = JSON.parse(e.data)
if (!data.action) return

emit(data)
} catch (e) {
console.error(e)
}
}
}

function emit (data) {
app.emitGhEvent = function emitGhEvent (data) {
const repo = data.repository
const org = repo.owner.login || data.organization.login

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"devDependencies": {
"eventsource": "^0.2.1",
"nodemon": "^1.9.1",
"request": "^2.72.0",
"standard": "^6.0.7",
"tap": "^5.7.1"
}
Expand Down
7 changes: 7 additions & 0 deletions scripts/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = function (app) {
app.get('/ping', (req, res) => {
res.end('pong')
})
}
37 changes: 19 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
'use strict'

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

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

const captureRaw = (req, res, buffer) => { req.raw = buffer }

const app = express()
app.use(bodyParser.json({ verify: captureRaw }))
require('./lib/github-events.js')(app)

// load all the files in the scripts folder
glob.sync(process.argv[2] || './scripts/**/*.js').forEach((file) => {
console.log('loading:', file)
require(file)(app)
})
const app = require('./app')

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

// may open up an SSE relay channel helpful for local debugging
// of github repository events, wo/having to deploy changes
if (process.env.SSE_RELAY) {
const EventSource = require('eventsource')
const es = new EventSource(process.env.SSE_RELAY)
es.onmessage = (e) => {
try {
const data = JSON.parse(e.data)
if (!data.action) return

app.emitGhEvent(data)
} catch (e) {
console.error('Error while receiving SSE relay message', e)
}
}
}
21 changes: 21 additions & 0 deletions test/ping.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const tap = require('tap')
const request = require('request')

const app = require('../app')

tap.test('GET /ping responds with status 200 / "pong"', (t) => {
const server = app.listen()
const port = server.address().port
const url = `http://localhost:${port}/ping`

t.plan(3)
t.tearDown(() => server.close())

request(url, (err, res, body) => {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(res.body, 'pong')
})
})