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

Work on MVP #16

Merged
merged 19 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
257 changes: 239 additions & 18 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"start": "node src/index.js",
"start-debug": "cross-env DEBUG=gh-canary:* npm run start",
"lint": "eslint src test",
"lint": "eslint .",
"test": "npm run lint && mocha test/**/*-test.js",
"test-debug": "cross-env DEBUG=gh-canary:* npm test",
"watch": "npm test -- -w",
Expand All @@ -31,7 +31,9 @@
"sinon": "^8.1.1"
},
"dependencies": {
"@octokit/rest": "^16.43.1",
"debug": "^4.1.1",
"dedent": "^0.7.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"got": "^10.4.0"
Expand Down
10 changes: 7 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const express = require('express');
const Server = require('./my-server');
const { DEFAULT_PORT, DEFAULT_HOST } = require('./config');
const router = require('./router');

class App {
constructor ({ port = DEFAULT_PORT, host = DEFAULT_HOST } = {}) {
this.express = express();
this.express.use(express.static('static'));
this.server = new Server(this._configExpress(express()), port, host);
}

this.server = new Server(this.express, port, host);
_configExpress (e) {
e.set('view engine', 'pug');
e.use(router);
return e;
}

async run () {
Expand Down
7 changes: 5 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require('dotenv').config();

module.exports = {
DEFAULT_PORT: process.env.PORT || 3000,
DEFAULT_HOST: process.env.HOST || '127.0.0.1'
DEFAULT_PORT: process.env.PORT || 3000,
DEFAULT_HOST: process.env.HOST || '127.0.0.1',
OWNER: process.env.OWNER || null,
REPO: process.env.REPO || null,
GITHUB_API_URL: process.env.GITHUB_API_URL || 'https://api.github.com'
};
1 change: 1 addition & 0 deletions src/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ const debug = require('debug');

module.exports = {
log: debug('gh-canary:log'),
warn: debug('gh-canary:warn'),
error: debug('gh-canary:error')
};
35 changes: 35 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const express = require('express');
const { Octokit } = require('@octokit/rest');
const constants = require('./config');
const pullRequestsPage = require('../views/templates');

const router = express.Router();
const octokit = Octokit({
wentwrong marked this conversation as resolved.
Show resolved Hide resolved
baseUrl: constants.GITHUB_API_URL
});

router.get('/', async (req, res) => {
res.send('Hello Node.js');
});

router.get('/get-prs', async (req, res) => {
const options = octokit.pulls.list.endpoint.merge({
owner: process.env.OWNER,
repo: process.env.REPO
});

const prs = [];
wentwrong marked this conversation as resolved.
Show resolved Hide resolved

for await (const page of octokit.paginate.iterator(options))
prs.push(page['data'].filter(pr => pr['author_association'] === 'NONE'));

res.send(
pullRequestsPage({
pullRequestList: prs.flat(),
owner: constants.OWNER,
repo: constants.REPO
})
);
});

module.exports = router;
9 changes: 0 additions & 9 deletions static/index.html

This file was deleted.

15 changes: 15 additions & 0 deletions views/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// NOTE: it's awful tmp solution
const dedent = require('dedent');

const pullRequestsPage = ({ pullRequestList, owner, repo }) => dedent `
<html>
<head><title>${owner}/${repo} pull requests</title></head>
<body>
${owner}/${repo} has ${ pullRequestList.length } non-collaborator pull requests.
<ul>
${pullRequestList.map(pr => `<li><a href="${pr.html_url}">${pr.title}</a> by ${pr.user.login}</li>`).join('\n')}
</ul>
</body>
</html>`;

module.exports = pullRequestsPage;