Skip to content

Commit

Permalink
fix: resolve _extends in .github repo file (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurz committed Feb 4, 2022
1 parent 02e8122 commit bac29d8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/util/get-config-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ export async function getConfigFiles(
ref: branch,
});

const files = [requestedRepoFile];

// if no configuration file present in selected repository,
// try to load it from the `.github` repository
if (!requestedRepoFile.config) {
if (repo === ".github") {
return [requestedRepoFile];
return files;
}

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

return [requestedRepoFile, defaultRepoConfig];
files.push(defaultRepoConfig);
}

const file = files[files.length - 1];

// if the configuration has no `_extends` key, we are done here.
if (!requestedRepoFile.config._extends) {
return [requestedRepoFile];
if (!file.config || !file.config._extends) {
return files;
}

// parse the value of `_extends` into request parameters to
// retrieve the new configuration file
let extendConfigOptions = extendsToGetContentParams({
owner,
path,
url: requestedRepoFile.url,
extendsValue: requestedRepoFile.config._extends as string,
url: file.url,
extendsValue: file.config._extends as string,
});

// remove the `_extends` key from the configuration that is returned
delete requestedRepoFile.config._extends;
const files = [requestedRepoFile];
delete file.config._extends;

// now load the configuration linked from the `_extends` key. If that
// configuration also includes an `_extends` key, then load that configuration
Expand Down
43 changes: 43 additions & 0 deletions test/__snapshots__/get.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,49 @@ Object {
}
`;

exports[`octokit.config.get resolves _extends in .github repository file: result 1`] = `
Object {
"config": Object {
"config": "value from octocat/.github:.github/my-second-app.yml",
"otherConfig": "value from octocat/hello-world:.github/my-third-app.yml",
},
"files": Array [
Object {
"config": null,
"owner": "octocat",
"path": ".github/my-app.yml",
"repo": "hello-world",
"url": "https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-app.yml",
},
Object {
"config": Object {},
"owner": "octocat",
"path": ".github/my-app.yml",
"repo": ".github",
"url": "https://api.github.com/repos/octocat/.github/contents/.github%2Fmy-app.yml",
},
Object {
"config": Object {
"config": "value from octocat/.github:.github/my-second-app.yml",
},
"owner": "octocat",
"path": ".github/my-second-app.yml",
"repo": ".github",
"url": "https://api.github.com/repos/octocat/.github/contents/.github%2Fmy-second-app.yml",
},
Object {
"config": Object {
"otherConfig": "value from octocat/hello-world:.github/my-third-app.yml",
},
"owner": "octocat",
"path": ".github/my-third-app.yml",
"repo": "hello-world",
"url": "https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-third-app.yml",
},
],
}
`;

exports[`octokit.config.get merges defaults option: result 1`] = `
Object {
"config": Object {
Expand Down
42 changes: 42 additions & 0 deletions test/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,48 @@ describe("octokit.config.get", () => {
expect(mock.done()).toBe(true);
});

it("resolves _extends in .github repository file", async () => {
const mock = fetchMock
.sandbox()
.getOnce(
"https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-app.yml",
NOT_FOUND_RESPONSE
)
.getOnce(
"https://api.github.com/repos/octocat/.github/contents/.github%2Fmy-app.yml",
`_extends: '.github:.github/my-second-app.yml'`
)
.getOnce(
"https://api.github.com/repos/octocat/.github/contents/.github%2Fmy-second-app.yml",
stripIndent(`
config: value from octocat/.github:.github/my-second-app.yml
_extends: hello-world:.github/my-third-app.yml`)
)
.getOnce(
"https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-third-app.yml",
`otherConfig: 'value from octocat/hello-world:.github/my-third-app.yml'`
);

const octokit = new TestOctokit({
request: {
fetch: mock,
},
});

const result = await octokit.config.get({
owner: "octocat",
repo: "hello-world",
path: ".github/my-app.yml",
defaults: {
config: "default value",
otherConfig: "default value",
},
});

expect(result).toMatchSnapshot("result");
expect(mock.done()).toBe(true);
});

it("merges deeply using defaults function", async () => {
const mock = fetchMock.sandbox().getOnce(
"https://api.github.com/repos/octocat/hello-world/contents/.github%2Fmy-app.yml",
Expand Down

0 comments on commit bac29d8

Please sign in to comment.