diff --git a/lib/configuration.js b/lib/configuration.js index 475c1a6..85bbe79 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(url(path, source)); 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() { @@ -42,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 60baf51..90809aa 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', () => { @@ -115,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' + }); + }); + }); }); });