From ec56779d37d7909cb1da8c6b825dfad4f440baf5 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 8 Dec 2016 23:48:44 -0600 Subject: [PATCH 1/2] Include relative to the current repo --- lib/configuration.js | 12 +++++++----- test/integration.js | 26 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/configuration.js b/lib/configuration.js index 475c1a6..18e1a29 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -4,16 +4,18 @@ const url = require('./util/github-url'); const log = require('./log'); module.exports = class Configuration { - static load(context, path) { - const options = context.toRepo(url(path)); + static load(context, path, source) { + const options = context.toRepo(Object.assign(source, url(path))); log.debug(options, 'Fetching config'); return context.github.repos.getContent(options).then(data => { - return new Configuration(context).parse(new Buffer(data.content, 'base64').toString()); + const config = new Configuration(context, options); + return config.parse(new Buffer(data.content, 'base64').toString()); }); } - constructor(context) { + constructor(context, source) { this.context = context; + this.source = source || {}; this.workflows = []; this.api = { @@ -30,7 +32,7 @@ module.exports = class Configuration { } include(path) { - const load = Configuration.load(this.context, path); + const load = Configuration.load(this.context, path, this.source); this.workflows.push({ execute() { diff --git a/test/integration.js b/test/integration.js index 60baf51..b59263b 100644 --- a/test/integration.js +++ b/test/integration.js @@ -71,8 +71,10 @@ describe('integration', () => { }); describe('include', () => { + let content; + beforeEach(() => { - const content = require('./fixtures/content/probot.json'); + content = require('./fixtures/content/probot.json'); content.content = new Buffer('on("issues").comment("Hello!");').toString('base64'); github.repos.getContent.andReturn(Promise.resolve(content)); @@ -95,6 +97,28 @@ describe('integration', () => { done(); }); }); + + it('includes files relative to included repository', () => { + github.repos.getContent.andCall(params => { + if (params.path === 'script-a.js') { + return Promise.resolve({ + content: new Buffer('include("script-b.js")').toString('base64') + }); + } else { + return Promise.resolve({content: ''}); + } + }); + + const config = configure('include("other/repo:script-a.js");'); + + return config.execute().then(() => { + expect(github.repos.getContent).toHaveBeenCalledWith({ + owner: 'other', + repo: 'repo', + path: 'script-b.js' + }); + }); + }); }); describe('contents', () => { From 9acfaba32b3921714631bda5499af4a2e4187a2c Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 8 Dec 2016 23:54:22 -0600 Subject: [PATCH 2/2] Include contents relative to repo source --- lib/configuration.js | 4 ++-- lib/util/github-url.js | 4 ++-- test/integration.js | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/configuration.js b/lib/configuration.js index 18e1a29..85bbe79 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -5,7 +5,7 @@ const log = require('./log'); module.exports = class Configuration { static load(context, path, source) { - const options = context.toRepo(Object.assign(source, url(path))); + const options = context.toRepo(url(path, source)); log.debug(options, 'Fetching config'); return context.github.repos.getContent(options).then(data => { const config = new Configuration(context, options); @@ -44,7 +44,7 @@ module.exports = class Configuration { } contents(path) { - const options = this.context.toRepo(url(path)); + const options = this.context.toRepo(url(path, this.source)); log.debug(options, 'Getting contents'); return this.context.github.repos.getContent(options).then(data => { return new Buffer(data.content, 'base64').toString(); diff --git a/lib/util/github-url.js b/lib/util/github-url.js index 0ef893b..8bd36be 100644 --- a/lib/util/github-url.js +++ b/lib/util/github-url.js @@ -1,7 +1,7 @@ const REGEX = /^(?:([\w-]+)\/([\w-]+):)?([^#]*)(?:#(.*))?$/; // Parses paths in the form of `owner/repo:path/to/file#ref` -module.exports = function (url) { +module.exports = function (url, source) { const [, owner, repo, path, ref] = url.match(REGEX); - return Object.assign({path}, owner && {owner, repo}, ref && {ref}); + return Object.assign({}, source, {path}, owner && {owner, repo}, ref && {ref}); }; diff --git a/test/integration.js b/test/integration.js index b59263b..90809aa 100644 --- a/test/integration.js +++ b/test/integration.js @@ -139,5 +139,29 @@ describe('integration', () => { }); }); }); + + it('gets contents relative to included repository', () => { + github.repos.getContent.andCall(params => { + if (params.path === 'script-a.js') { + return Promise.resolve({ + content: new Buffer(` + on("issues").comment(contents("content.md")); + `).toString('base64') + }); + } else { + return Promise.resolve({content: ''}); + } + }); + + const config = configure('include("other/repo:script-a.js");'); + + return config.execute().then(() => { + expect(github.repos.getContent).toHaveBeenCalledWith({ + owner: 'other', + repo: 'repo', + path: 'content.md' + }); + }); + }); }); });