Skip to content

Commit bac29d8

Browse files
authored
fix: resolve _extends in .github repo file (#167)
1 parent 02e8122 commit bac29d8

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

Diff for: src/util/get-config-files.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ export async function getConfigFiles(
3232
ref: branch,
3333
});
3434

35+
const files = [requestedRepoFile];
36+
3537
// if no configuration file present in selected repository,
3638
// try to load it from the `.github` repository
3739
if (!requestedRepoFile.config) {
3840
if (repo === ".github") {
39-
return [requestedRepoFile];
41+
return files;
4042
}
4143

4244
const defaultRepoConfig = await getConfigFile(octokit, {
@@ -45,26 +47,27 @@ export async function getConfigFiles(
4547
path,
4648
});
4749

48-
return [requestedRepoFile, defaultRepoConfig];
50+
files.push(defaultRepoConfig);
4951
}
5052

53+
const file = files[files.length - 1];
54+
5155
// if the configuration has no `_extends` key, we are done here.
52-
if (!requestedRepoFile.config._extends) {
53-
return [requestedRepoFile];
56+
if (!file.config || !file.config._extends) {
57+
return files;
5458
}
5559

5660
// parse the value of `_extends` into request parameters to
5761
// retrieve the new configuration file
5862
let extendConfigOptions = extendsToGetContentParams({
5963
owner,
6064
path,
61-
url: requestedRepoFile.url,
62-
extendsValue: requestedRepoFile.config._extends as string,
65+
url: file.url,
66+
extendsValue: file.config._extends as string,
6367
});
6468

6569
// remove the `_extends` key from the configuration that is returned
66-
delete requestedRepoFile.config._extends;
67-
const files = [requestedRepoFile];
70+
delete file.config._extends;
6871

6972
// now load the configuration linked from the `_extends` key. If that
7073
// configuration also includes an `_extends` key, then load that configuration

Diff for: test/__snapshots__/get.test.ts.snap

+43
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,49 @@ Object {
400400
}
401401
`;
402402

403+
exports[`octokit.config.get resolves _extends in .github repository file: result 1`] = `
404+
Object {
405+
"config": Object {
406+
"config": "value from octocat/.github:.github/my-second-app.yml",
407+
"otherConfig": "value from octocat/hello-world:.github/my-third-app.yml",
408+
},
409+
"files": Array [
410+
Object {
411+
"config": null,
412+
"owner": "octocat",
413+
"path": ".github/my-app.yml",
414+
"repo": "hello-world",
415+
"url": "https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-app.yml",
416+
},
417+
Object {
418+
"config": Object {},
419+
"owner": "octocat",
420+
"path": ".github/my-app.yml",
421+
"repo": ".github",
422+
"url": "https://api.github.com/repos/octocat/.github/contents/.github%2Fmy-app.yml",
423+
},
424+
Object {
425+
"config": Object {
426+
"config": "value from octocat/.github:.github/my-second-app.yml",
427+
},
428+
"owner": "octocat",
429+
"path": ".github/my-second-app.yml",
430+
"repo": ".github",
431+
"url": "https://api.github.com/repos/octocat/.github/contents/.github%2Fmy-second-app.yml",
432+
},
433+
Object {
434+
"config": Object {
435+
"otherConfig": "value from octocat/hello-world:.github/my-third-app.yml",
436+
},
437+
"owner": "octocat",
438+
"path": ".github/my-third-app.yml",
439+
"repo": "hello-world",
440+
"url": "https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-third-app.yml",
441+
},
442+
],
443+
}
444+
`;
445+
403446
exports[`octokit.config.get merges defaults option: result 1`] = `
404447
Object {
405448
"config": Object {

Diff for: test/get.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,48 @@ describe("octokit.config.get", () => {
180180
expect(mock.done()).toBe(true);
181181
});
182182

183+
it("resolves _extends in .github repository file", async () => {
184+
const mock = fetchMock
185+
.sandbox()
186+
.getOnce(
187+
"https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-app.yml",
188+
NOT_FOUND_RESPONSE
189+
)
190+
.getOnce(
191+
"https://api.github.com/repos/octocat/.github/contents/.github%2Fmy-app.yml",
192+
`_extends: '.github:.github/my-second-app.yml'`
193+
)
194+
.getOnce(
195+
"https://api.github.com/repos/octocat/.github/contents/.github%2Fmy-second-app.yml",
196+
stripIndent(`
197+
config: value from octocat/.github:.github/my-second-app.yml
198+
_extends: hello-world:.github/my-third-app.yml`)
199+
)
200+
.getOnce(
201+
"https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-third-app.yml",
202+
`otherConfig: 'value from octocat/hello-world:.github/my-third-app.yml'`
203+
);
204+
205+
const octokit = new TestOctokit({
206+
request: {
207+
fetch: mock,
208+
},
209+
});
210+
211+
const result = await octokit.config.get({
212+
owner: "octocat",
213+
repo: "hello-world",
214+
path: ".github/my-app.yml",
215+
defaults: {
216+
config: "default value",
217+
otherConfig: "default value",
218+
},
219+
});
220+
221+
expect(result).toMatchSnapshot("result");
222+
expect(mock.done()).toBe(true);
223+
});
224+
183225
it("merges deeply using defaults function", async () => {
184226
const mock = fetchMock.sandbox().getOnce(
185227
"https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-app.yml",

0 commit comments

Comments
 (0)