Skip to content

Commit

Permalink
fix: GET /orgs/:org/teams/:team_slug (#63)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Travi <[email protected]>
  • Loading branch information
gr2m and travi authored May 21, 2020
1 parent 3e4dfa2 commit 47d5504
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ export function enterpriseCompatibility(octokit: Octokit) {
});
}

// TODO: implement fix for #62 here

// https://github.com/octokit/plugin-enterprise-compatibility.js/issues/60
if (/\/orgs\/[^/]+\/teams/.test(options.url)) {
try {
return await request(options);
} catch (error) {
if (error.status !== 404) {
throw error;
}

if (!error.headers || !error.headers["x-github-enterprise-version"]) {
throw error;
}

const deprecatedUrl = options.url.replace(
/\/orgs\/[^/]+\/teams\/[^/]+/,
"/teams/:team_id"
);

throw new RequestError(
`"${options.method} ${options.url}" is not supported in your GitHub Enterprise Server version. Please replace with octokit.request("${options.method} ${deprecatedUrl}", { team_id })`,
404,
{
request: options,
}
);
}
}

return request(options);
}
);
Expand Down
148 changes: 148 additions & 0 deletions test/octokit-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,151 @@ describe("GET /repos/:owner/:repo/git/refs/:ref (#21)", () => {
}
});
});

describe("GET /orgs/:org/teams/:team_slug*", () => {
it("Throws no error for github.com users", async () => {
const mock = fetchMock
.sandbox()
.getOnce("https://api.github.com/orgs/my-org/teams/my-team", {
status: 200,
body: { id: 123 },
});

const octokitPatched = new OctokitWithPlugin({
request: {
fetch: mock,
},
});

const { data } = await octokitPatched.request(
"GET /orgs/:org/teams/:team_slug",
{
org: "my-org",
team_slug: "my-team",
}
);

expect(data).toStrictEqual({ id: 123 });
});

it("Throws no error for github.com users", async () => {
const mock = fetchMock
.sandbox()
.getOnce("https://api.github.com/orgs/my-org/teams/my-team", {
status: 404,
body: { error: "not found" },
});

const octokitPatched = new OctokitWithPlugin({
request: {
fetch: mock,
},
});

try {
await octokitPatched.request("GET /orgs/:org/teams/:team_slug", {
org: "my-org",
team_slug: "my-team",
});
throw new Error("should not resolve");
} catch (error) {
expect(error.status).toEqual(404);
}
});

it("'GET /orgs/:org/teams/:team_slug': Throws a helpful error for GitHub Enterprise Server 2.20 users", async () => {
const mock = fetchMock
.sandbox()
.getOnce("https://ghes.acme-inc.test/api/v3/orgs/my-org/teams/my-team", {
status: 404,
body: { error: "Not found" },
headers: {
"X-GitHub-Enterprise-Version": "2.20.0",
},
});

const octokitPatched = new OctokitWithPlugin({
baseUrl: "https://ghes.acme-inc.test/api/v3",
request: {
fetch: mock,
},
});

try {
await octokitPatched.request("GET /orgs/:org/teams/:team_slug", {
org: "my-org",
team_slug: "my-team",
});
throw new Error("Should not resolve");
} catch (error) {
expect(error.status).toEqual(404);
expect(error.message).toEqual(
`"GET /orgs/:org/teams/:team_slug" is not supported in your GitHub Enterprise Server version. Please replace with octokit.request("GET /teams/:team_id", { team_id })`
);
}
});

it("'GET /orgs/:org/teams/:team_slug': 500 error", async () => {
const mock = fetchMock
.sandbox()
.getOnce("https://ghes.acme-inc.test/api/v3/orgs/my-org/teams/my-team", {
status: 500,
});

const octokitPatched = new OctokitWithPlugin({
baseUrl: "https://ghes.acme-inc.test/api/v3",
request: {
fetch: mock,
},
});

try {
await octokitPatched.request("GET /orgs/:org/teams/:team_slug", {
org: "my-org",
team_slug: "my-team",
});
throw new Error("Should not resolve");
} catch (error) {
expect(error.status).toEqual(500);
}
});

it("'GET /orgs/:org/teams/:team_slug/discussions/:discussion_number/comments': Throws a helpful error for GitHub Enterprise Server 2.20 users", async () => {
const mock = fetchMock
.sandbox()
.getOnce(
"https://ghes.acme-inc.test/api/v3/orgs/my-org/teams/my-team/discussions/123/comments",
{
status: 404,
body: { error: "Not found" },
headers: {
"X-GitHub-Enterprise-Version": "2.20.0",
},
}
);

const octokitPatched = new OctokitWithPlugin({
baseUrl: "https://ghes.acme-inc.test/api/v3",
request: {
fetch: mock,
},
});

try {
await octokitPatched.request(
"GET /orgs/:org/teams/:team_slug/discussions/:discussion_number/comments",
{
org: "my-org",
team_slug: "my-team",
discussion_number: 123,
}
);
throw new Error("Should not resolve");
} catch (error) {
expect(error.status).toEqual(404);
expect(error.message).toEqual(
`"GET /orgs/:org/teams/:team_slug/discussions/:discussion_number/comments" is not supported in your GitHub Enterprise Server version. Please replace with octokit.request("GET /teams/:team_id/discussions/:discussion_number/comments", { team_id })`
);
}
});
});

0 comments on commit 47d5504

Please sign in to comment.