Skip to content

Commit

Permalink
feat: /probot homepage (#279)
Browse files Browse the repository at this point in the history
* Add /probot route with tests

* Add index route, write tests

* Remove unused arg

* Pass tests

* Tweak HTML string and pkg require

* Add static directory

* Design welcome page

* Rename to probot.html

* Implement EJS

* Remove duplicate load

* Extract default homepage into plugin

* Fix issue after merge conflict

* Refactor app loading

* Remove autoloading of plugings

BREAKING CHANGE: `probot run` without any arguments will no longer autoload apps named `probot-*`.

* Move setup

* Look for `apps` key instead of `plugins`

* Update simulate command

* Simplify resolver

* Remove unused routes variable

* Move views to default path

* Call probot.setup to properly initialize default apps

* Simplify package loading

* Specify extension

* Add links to docs/slack

* Remove stats link

* Add helper to jest ignore

* Fix ejs views error

* Move a require to top of file

* Remove unnecessary (and breaking) test
  • Loading branch information
JasonEtco committed Oct 17, 2017
1 parent c37597d commit 5d29945
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 33 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const logger = bunyan.createLogger({
})

const defaultApps = [
require('./lib/plugins/stats')
require('./lib/plugins/stats'),
require('./lib/plugins/default')
]

// Log all unhandled rejections
Expand Down
16 changes: 16 additions & 0 deletions lib/plugins/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('path')

let pkg

try {
pkg = require(path.join(process.cwd(), 'package.json'))
} catch (e) {
pkg = {}
}

module.exports = robot => {
const app = robot.route()

app.get('/probot', (req, res) => res.render('probot.ejs', pkg))
app.get('/', (req, res, next) => res.redirect('/probot'))
}
4 changes: 4 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const express = require('express')
const path = require('path')

module.exports = function (webhook) {
const app = express()

app.use('/probot/static/', express.static(path.join(__dirname, '..', 'static')))
app.use(webhook)
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, '..', 'views'))
app.get('/ping', (req, res) => res.end('PONG'))

return app
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"testMatch": [ "**/test/**/*.js?(x)", "**/?(*.)(spec|test).js?(x)" ],
"modulePathIgnorePatterns": [
"<rootDir>/test/fixtures/",
"<rootDir>/test/setup.js"
"<rootDir>/test/setup.js",
"<rootDir>/test/plugins/helper.js"
],
"setupFiles": [
"<rootDir>/test/setup.js"
Expand All @@ -35,6 +36,7 @@
"cache-manager": "^2.4.0",
"commander": "^2.11.0",
"dotenv": "~4.0.0",
"ejs": "^2.5.7",
"express": "^4.15.4",
"github": "^10.1.0",
"github-app": "^3.2.0",
Expand Down
21 changes: 21 additions & 0 deletions static/primer.css

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions static/robot.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 10 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const createProbot = require('..')
const request = require('supertest')

describe('Probot', () => {
let probot
Expand All @@ -23,8 +24,6 @@ describe('Probot', () => {
})

describe('server', () => {
const request = require('supertest')

it('prefixes paths with route name', () => {
probot.load(robot => {
const app = robot.route('/my-plugin')
Expand All @@ -43,6 +42,15 @@ describe('Probot', () => {
return request(probot.server).get('/foo').expect(200, 'foo')
})

it('allows you to overwrite the root path', () => {
probot.load(robot => {
const app = robot.route()
app.get('/', (req, res) => res.end('foo'))
})

return request(probot.server).get('/').expect(200, 'foo')
})

it('isolates plugins from affecting eachother', async () => {
['foo', 'bar'].forEach(name => {
probot.load(robot => {
Expand Down Expand Up @@ -92,10 +100,6 @@ describe('Probot', () => {
// eslint-disable-next-line handle-callback-err
probot.server.use((err, req, res, next) => { })

// GET requests to `/` should 404 at express level, not 400 at webhook level
await request(probot.server).get('/')
.expect(404)

// POST requests to `/` should 400 b/c webhook signature will fail
await request(probot.server).post('/')
.expect(400)
Expand Down
30 changes: 30 additions & 0 deletions test/plugins/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const request = require('supertest')
const express = require('express')
const plugin = require('../../lib/plugins/default')
const helper = require('./helper')

describe('default plugin', function () {
let server, robot

beforeEach(async () => {
robot = helper.createRobot()

await plugin(robot)

server = express()

server.use(robot.router)
})

describe('GET /probot', () => {
it('returns a 200 response', () => {
return request(server).get('/probot').expect(200)
})
})

describe('GET /', () => {
it('redirects to /probot', () => {
return request(server).get('/').expect(302).expect('location', '/probot')
})
})
})
27 changes: 27 additions & 0 deletions test/plugins/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// FIXME: move this to a test helper that can be used by other apps

const cacheManager = require('cache-manager')
const GitHubApi = require('github')
const {createRobot} = require('../..')

const cache = cacheManager.caching({store: 'memory'})

const app = {
async asApp () {
return new GitHubApi()
},

async asInstallation () {
return new GitHubApi()
},

async createToken () {
return {data: {token: 'test'}}
}
}

module.exports = {
createRobot () {
return createRobot({app, cache})
}
}
27 changes: 3 additions & 24 deletions test/plugins/stats.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
const request = require('supertest')
const express = require('express')
const {createRobot} = require('../..')
const plugin = require('../../lib/plugins/stats')
const nock = require('nock')
const plugin = require('../../lib/plugins/stats')

const cacheManager = require('cache-manager')
const GitHubApi = require('github')

nock.disableNetConnect()
nock.enableNetConnect(/127\.0\.0\.1/)
const helper = require('./helper')

describe('stats', function () {
let robot, server
Expand All @@ -22,23 +17,7 @@ describe('stats', function () {
{private: false, stargazers_count: 2}
]})

// FIXME: move this and app setup below to a test harness
const cache = cacheManager.caching({store: 'memory'})

const app = {
async asApp () {
return new GitHubApi()
},

async asInstallation () {
return new GitHubApi()
},

async createToken () {
return {data: {token: 'test'}}
}
}
robot = createRobot({app, cache})
robot = helper.createRobot()

await plugin(robot)

Expand Down
2 changes: 1 addition & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('server', function () {
})

describe('GET /ping', () => {
it('returns a 200 repsonse', () => {
it('returns a 200 response', () => {
return request(server).get('/ping').expect(200, 'PONG')
})
})
Expand Down
5 changes: 5 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// This file gets loaded when mocha is run

process.env.LOG_LEVEL = 'fatal'

const nock = require('nock')

nock.disableNetConnect()
nock.enableNetConnect(/127\.0\.0\.1/)
31 changes: 31 additions & 0 deletions views/probot.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en" class="height-full">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><%= name %> | built with Probot</title>
<link rel="stylesheet" href="/probot/static/primer.css">
</head>
<body class="height-full bg-gray-light">
<div class="d-flex flex-column flex-justify-center flex-items-center text-center height-full">
<img src="/probot/static/robot.svg" alt="Probot Logo" width="100" class="mb-6">
<div class="box-shadow rounded-2 border p-6 bg-white">
<h1>Welcome to <%= name %> <span class="Label Label--outline v-align-middle ml-2 text-gray-light">v<%= version %></span></h1>
<%if (description) { %>
<p><%= description %></p>
<% } else { %>
<p>This bot was built using <a href="https://github.com/probot/probot">Probot</a>, a framework for building GitHub Apps.</p>
<% } %>
</div>

<div class="mt-4">
<h4 class="alt-h4 text-gray-light">Need help?</h4>
<div class="d-flex flex-justify-center mt-2">
<a href="https://probot.github.io/docs/" class="btn btn-outline mr-2">Documentation</a>
<a href="https://probot-slackin.herokuapp.com/" class="btn btn-outline">Chat on Slack</a>
</div>
</div>
</div>
</body>
</html>

0 comments on commit 5d29945

Please sign in to comment.