Move config method to context

This commit is contained in:
Brandon Keepers 2017-07-27 09:37:31 -05:00
parent f21679a303
commit 794064f242
No known key found for this signature in database
GPG Key ID: F9533396D5FACBF6
4 changed files with 96 additions and 93 deletions

View File

@ -1,3 +1,6 @@
const path = require('path');
const yaml = require('js-yaml');
/**
* Helpers for extracting information from the webhook event, which can be
* passed to GitHub API calls.
@ -58,6 +61,21 @@ class Context {
get isBot() {
return this.payload.sender.type === 'Bot';
}
/**
* Reads the plugin configuration from the given file name in the `.github` directory.
*
* @param {github} github Authenticated GitHub API client
* @param {string} owner Owner of the repository
* @param {string} repo Name of the repository
* @param {string} fileName Name of the configuration file within the `.github` directory
* @return {Promise<Object>} Configuration object read from the file
*/
async config(fileName) {
const params = this.repo({path: path.join('.github', fileName)});
const data = await this.github.repos.getContent(params);
return yaml.load(Buffer.from(data.content, 'base64').toString()) || {};
}
}
module.exports = Context;

View File

@ -1,5 +1,3 @@
const path = require('path');
const yaml = require('js-yaml');
const {EventEmitter} = require('promise-events');
const GitHubApi = require('github');
const Bottleneck = require('bottleneck');
@ -120,20 +118,6 @@ class Robot {
return probotEnhancedClient(github);
}
/**
* Reads the plugin configuration from the given file name in the `.github` directory.
*
* @param {github} github Authenticated GitHub API client
* @param {string} owner Owner of the repository
* @param {string} repo Name of the repository
* @param {string} fileName Name of the configuration file within the `.github` directory
* @return {Promise<Object>} Configuration object read from the file
*/
async getPluginConfig(github, owner, repo, fileName) {
const data = await github.repos.getContent({owner, repo, path: path.join('.github', fileName)});
return yaml.load(Buffer.from(data.content, 'base64').toString()) || {};
}
}
function probotEnhancedClient(github) {

View File

@ -1,6 +1,14 @@
const fs = require('fs');
const path = require('path');
const expect = require('expect');
const Context = require('../lib/context');
function readConfig(fileName) {
const configPath = path.join(__dirname, 'fixtures', 'config', fileName);
const content = fs.readFileSync(configPath, {encoding: 'utf8'});
return {content: Buffer.from(content).toString('base64')};
}
describe('Context', function () {
let event;
let context;
@ -67,4 +75,74 @@ describe('Context', function () {
});
});
});
describe('config', function () {
let github;
beforeEach(function () {
github = {
repos: {
getContent: expect.createSpy()
}
};
context = new Context(event, github);
});
it('gets a valid configuration', async function () {
github.repos.getContent.andReturn(Promise.resolve(readConfig('basic.yml')));
const config = await context.config('test-file.yml');
expect(github.repos.getContent).toHaveBeenCalledWith({
owner: 'bkeepers',
repo: 'probot',
path: '.github/test-file.yml'
});
expect(config).toEqual({
foo: 5,
bar: 7,
baz: 11
});
});
it('throws when the file is missing', async function () {
github.repos.getContent.andReturn(Promise.reject(new Error('An error occurred')));
let e;
let contents;
try {
contents = await context.config('test-file.yml');
} catch (err) {
e = err;
}
expect(contents).toNotExist();
expect(e).toExist();
expect(e.message).toEqual('An error occurred');
});
it('throws when the configuration file is malformed', async function () {
github.repos.getContent.andReturn(Promise.resolve(readConfig('malformed.yml')));
let e;
let contents;
try {
contents = await context.config('test-file.yml');
} catch (err) {
e = err;
}
expect(contents).toNotExist();
expect(e).toExist();
expect(e.message).toMatch(/^end of the stream or a document separator/);
});
it('returns an empty object when the file is empty', async function () {
github.repos.getContent.andReturn(readConfig('empty.yml'));
const contents = await context.config('test-file.yml');
expect(contents).toEqual({});
});
});
});

View File

@ -1,15 +1,7 @@
const fs = require('fs');
const path = require('path');
const expect = require('expect');
const Context = require('../lib/context');
const createRobot = require('../lib/robot');
function readConfig(fileName) {
const configPath = path.join(__dirname, 'fixtures', 'config', fileName);
const content = fs.readFileSync(configPath, {encoding: 'utf8'});
return {content: Buffer.from(content).toString('base64')};
}
describe('Robot', function () {
let robot;
let event;
@ -139,73 +131,4 @@ describe('Robot', function () {
expect(robot.log.error).toHaveBeenCalledWith(error);
});
});
describe('getPluginConfig', function () {
let github;
beforeEach(function () {
github = {
repos: {
getContent: spy
}
};
});
it('gets a valid configuration', async function () {
spy.andReturn(Promise.resolve(readConfig('basic.yml')));
const config = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml');
expect(spy).toHaveBeenCalled();
expect(spy.calls[0].arguments[0]).toEqual({
owner: 'owner',
repo: 'repo',
path: '.github/test-file.yml'
});
expect(config).toEqual({
foo: 5,
bar: 7,
baz: 11
});
});
it('throws when the file is missing', async function () {
spy.andReturn(Promise.reject(new Error('An error occurred')));
let e;
let contents;
try {
contents = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml');
} catch (err) {
e = err;
}
expect(contents).toNotExist();
expect(e).toExist();
expect(e.message).toEqual('An error occurred');
});
it('throws when the configuration file is malformed', async function () {
spy.andReturn(Promise.resolve(readConfig('malformed.yml')));
let e;
let contents;
try {
contents = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml');
} catch (err) {
e = err;
}
expect(contents).toNotExist();
expect(e).toExist();
expect(e.message).toMatch(/^end of the stream or a document separator/);
});
it('returns an empty object when the file is empty', async function () {
spy.andReturn(readConfig('empty.yml'));
const contents = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml');
expect(contents).toEqual({});
});
});
});