Skip to content

Commit 3704bf6

Browse files
authored
feat: set correct accept header when using media type previews (#93)
1 parent 2bf9a2e commit 3704bf6

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

src/util/get-config-file.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,29 @@ export async function getConfigFile(
3131
}
3232

3333
// https://docs.github.com/en/rest/reference/repos#get-repository-content
34-
const requestOptions = await octokit.request.endpoint(
35-
"GET /repos/{owner}/{repo}/contents/{path}",
36-
{
37-
owner,
38-
repo,
39-
path,
40-
mediaType: {
41-
format: "raw",
42-
},
43-
// this can be just `ref` once https://github.com/octokit/endpoint.js/issues/206 is resolved
44-
...(ref ? { ref } : {}),
45-
}
46-
);
34+
const endpoint = {
35+
method: "GET",
36+
url: "/repos/{owner}/{repo}/contents/{path}",
37+
owner,
38+
repo,
39+
path,
40+
mediaType: {
41+
format: "raw",
42+
},
43+
// this can be just `ref` once https://github.com/octokit/endpoint.js/issues/206 is resolved
44+
...(ref ? { ref } : {}),
45+
};
46+
const { url } = await octokit.request.endpoint(endpoint);
4747
const emptyConfigResult = {
4848
owner,
4949
repo,
5050
path,
51-
url: requestOptions.url,
51+
url,
5252
config: null,
5353
};
5454

5555
try {
56-
const { data, headers } = await octokit.request(requestOptions);
56+
const { data, headers } = await octokit.request(endpoint);
5757

5858
// If path is a submodule, or a folder, then a JSON string is returned with
5959
// the "Content-Type" header set to "application/json; charset=utf-8".
@@ -65,14 +65,14 @@ export async function getConfigFile(
6565
// so we are fine
6666
if (headers["content-type"] === "application/json; charset=utf-8") {
6767
throw new Error(
68-
`[@probot/octokit-plugin-config] ${requestOptions.url} exists, but is either a directory or a submodule. Ignoring.`
68+
`[@probot/octokit-plugin-config] ${url} exists, but is either a directory or a submodule. Ignoring.`
6969
);
7070
}
7171

7272
if (fileExtension === "json") {
7373
if (typeof data === "string") {
7474
throw new Error(
75-
`[@probot/octokit-plugin-config] Configuration could not be parsed from ${requestOptions.url} (invalid JSON)`
75+
`[@probot/octokit-plugin-config] Configuration could not be parsed from ${url} (invalid JSON)`
7676
);
7777
}
7878

@@ -86,7 +86,7 @@ export async function getConfigFile(
8686

8787
if (typeof config === "string") {
8888
throw new Error(
89-
`[@probot/octokit-plugin-config] Configuration could not be parsed from ${requestOptions.url} (YAML is not an object)`
89+
`[@probot/octokit-plugin-config] Configuration could not be parsed from ${url} (YAML is not an object)`
9090
);
9191
}
9292

@@ -105,7 +105,7 @@ export async function getConfigFile(
105105
: "invalid YAML";
106106

107107
throw new Error(
108-
`[@probot/octokit-plugin-config] Configuration could not be parsed from ${requestOptions.url} (${reason})`
108+
`[@probot/octokit-plugin-config] Configuration could not be parsed from ${url} (${reason})`
109109
);
110110
}
111111

test/issues.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Octokit } from "@octokit/core";
2+
import fetchMock from "fetch-mock";
3+
import stripIndent from "strip-indent";
4+
5+
import { config } from "../src";
6+
7+
const TestOctokit = Octokit.plugin(config);
8+
9+
describe("issues", () => {
10+
it("#91 sets incorrect accept header when media type previews are set", async () => {
11+
const mock = fetchMock.sandbox().getOnce((url, { headers }) => {
12+
// @ts-ignore TypeScript says we can't do this but turns out we can so there you go
13+
expect(headers["accept"]).toEqual(
14+
"application/vnd.github.luke-cage-preview.raw"
15+
);
16+
return true;
17+
}, "foo: bar");
18+
const octokit = new TestOctokit({
19+
previews: ["luke-cage"],
20+
request: {
21+
fetch: mock,
22+
},
23+
});
24+
25+
await octokit.config.get({
26+
owner: "ScottChapman",
27+
repo: "probot-config-plugin-bug",
28+
path: "config.yaml",
29+
});
30+
31+
expect(mock.done()).toBe(true);
32+
});
33+
});

0 commit comments

Comments
 (0)