Skip to content

Commit d79924e

Browse files
committed
configuration.js loading flow
A bit more coherent and "dry" Also add more tests to be sure to keep config.source coherent and avoid mismatches
1 parent f9c7fad commit d79924e

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

lib/configuration.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ const url = require('./util/github-url')
55
module.exports = class Configuration {
66
static load (context, path, source) {
77
const options = context.repo(url(path, source))
8-
return context.github.repos.getContent(options).then(res => {
9-
const config = new Configuration(context, options)
10-
return config.parse(Buffer.from(res.data.content, 'base64').toString())
11-
})
8+
const config = new Configuration(context, options)
9+
return config.contents().then(buffer => config.parse(buffer))
1210
}
1311

1412
constructor (context, source) {
@@ -37,15 +35,15 @@ module.exports = class Configuration {
3735
return load.then(config => config.execute(this.context))
3836
}
3937
})
40-
41-
return undefined
4238
}
4339

4440
contents (path) {
45-
const options = this.context.repo(url(path, this.source))
46-
return this.context.github.repos.getContent(options).then(res => {
47-
return Buffer.from(res.data.content, 'base64').toString()
48-
})
41+
const options = path
42+
? this.context.repo(url(path, this.source))
43+
: this.source
44+
return this.context.github.repos.getContent(options).then(res =>
45+
Buffer.from(res.data.content, 'base64').toString()
46+
)
4947
}
5048

5149
parse (content) {

test/index.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,28 @@ describe('app', () => {
114114
if (params.path === 'script-b.js') {
115115
return ''
116116
}
117-
return 'include("other/repo:script-a.js");'
117+
return `
118+
include("other/repo:script-a.js");
119+
include("another/repo:script-a.js");
120+
include("script-b.js");
121+
`
118122
})
123+
expect(github.repos.getContent).toHaveBeenCalledTimes(1 + 3 + 2)
119124
expect(github.repos.getContent).toHaveBeenCalledWith({
120125
owner: 'other',
121126
repo: 'repo',
122127
path: 'script-b.js'
123128
})
129+
expect(github.repos.getContent).toHaveBeenCalledWith({
130+
owner: 'another',
131+
repo: 'repo',
132+
path: 'script-b.js'
133+
})
134+
expect(github.repos.getContent).toHaveBeenCalledWith({
135+
owner: 'bkeepers-inc',
136+
repo: 'test',
137+
path: 'script-b.js'
138+
})
124139
})
125140
})
126141

@@ -156,5 +171,28 @@ describe('app', () => {
156171
path: 'content.md'
157172
})
158173
})
174+
175+
it('gets contents relative to included repository', async () => {
176+
await configure(params => {
177+
if (params.path === 'content.md' || params.path === 'label.md') {
178+
return ''
179+
}
180+
return `
181+
on("issues")
182+
.comment(contents("other/repo:content.md"))
183+
.comment(contents("label.md"));
184+
`
185+
})
186+
expect(github.repos.getContent).toHaveBeenCalledWith({
187+
owner: 'other',
188+
repo: 'repo',
189+
path: 'content.md'
190+
})
191+
expect(github.repos.getContent).toHaveBeenCalledWith({
192+
owner: 'bkeepers-inc',
193+
repo: 'test',
194+
path: 'label.md'
195+
})
196+
})
159197
})
160198
})

0 commit comments

Comments
 (0)