Skip to content

Commit

Permalink
feat(GH-8): Add functionality to create octokit client
Browse files Browse the repository at this point in the history
  • Loading branch information
gavanlamb committed Jun 14, 2024
1 parent 93e9eb0 commit 6981a8e
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
132 changes: 132 additions & 0 deletions __tests__/client/octokit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
describe("createOctokitClient", () => {
beforeEach(() => {
jest.clearAllMocks();
jest.resetModules();
});

it("should handle auth failures gracefully", async () => {
const errorMessage = "Auth failed";

const debugMock = jest.fn();
jest.doMock("@actions/core", () => ({ debug: debugMock }));

const createActionAuthMock = jest.fn();
jest.doMock("@octokit/auth-action", () => ({ createActionAuth: createActionAuthMock }));

const OctokitMock = { plugin: jest.fn() };
jest.doMock("@octokit/core", () => ({ Octokit: OctokitMock }));

const pluginRetryMock = jest.fn();
jest.doMock("@octokit/plugin-retry", () => ({ retry: pluginRetryMock }));

const authMock = jest.fn();
createActionAuthMock.mockReturnValue(authMock);
authMock.mockRejectedValue(new Error(errorMessage));

const { createOctokitClient } = await import("../../src/clients/octokit");
const clientPromise = createOctokitClient();

expect(debugMock).toHaveBeenCalledWith('Going to create octokit client...');
await expect(clientPromise).rejects.toThrow(errorMessage);
expect(createActionAuthMock).toHaveBeenCalledTimes(1);
});

it("should call Octokit.plugin with retry", async () => {
const debugMock = jest.fn();
jest.doMock("@actions/core", () => ({ debug: debugMock }));

const createActionAuthMock = jest.fn();
jest.doMock("@octokit/auth-action", () => ({ createActionAuth: createActionAuthMock }));

const OctokitMock = { plugin: jest.fn() };
jest.doMock("@octokit/core", () => ({ Octokit: OctokitMock }));

const pluginRetryMock = jest.fn();
jest.doMock("@octokit/plugin-retry", () => ({ retry: pluginRetryMock }));

const authMock = jest.fn();
createActionAuthMock.mockReturnValue(authMock);
authMock.mockResolvedValue({ token: "test-token" });

const octokitInstanceMock = jest.fn();
OctokitMock.plugin.mockReturnValue(octokitInstanceMock);

const { createOctokitClient } = await import("../../src/clients/octokit");
await createOctokitClient();

expect(OctokitMock.plugin).toHaveBeenCalledWith(pluginRetryMock);
});

it("should pass correct config to the Octokit client", async () => {
const debugMock = jest.fn();
jest.doMock("@actions/core", () => ({ debug: debugMock }));

const createActionAuthMock = jest.fn();
jest.doMock("@octokit/auth-action", () => ({ createActionAuth: createActionAuthMock }));

const OctokitMock = { plugin: jest.fn() };
jest.doMock("@octokit/core", () => ({ Octokit: OctokitMock }));

const pluginRetryMock = jest.fn();
jest.doMock("@octokit/plugin-retry", () => ({ retry: pluginRetryMock }));

const authMock = jest.fn();
createActionAuthMock.mockReturnValue(authMock);
authMock.mockResolvedValue({ token: "test-token" });

const octokitInstanceMock = jest.fn();
OctokitMock.plugin.mockReturnValue(octokitInstanceMock);

const { createOctokitClient } = await import("../../src/clients/octokit");
await createOctokitClient();

expect(octokitInstanceMock).toHaveBeenCalledWith({
auth: "test-token",
request: {
retries: 3,
retryAfter: 1000,
}
});
});

it("should create the Octokit client successfully", async () => {
const debugMock = jest.fn();
jest.doMock("@actions/core", () => ({ debug: debugMock }));

const createActionAuthMock = jest.fn();
jest.doMock("@octokit/auth-action", () => ({ createActionAuth: createActionAuthMock }));

const OctokitMock = { plugin: jest.fn() };
jest.doMock("@octokit/core", () => ({ Octokit: OctokitMock }));

const pluginRetryMock = jest.fn();
jest.doMock("@octokit/plugin-retry", () => ({ retry: pluginRetryMock }));

const token = "test-token";
const authMock = jest.fn();
createActionAuthMock.mockReturnValue(authMock);
authMock.mockResolvedValue({ token });

const octokitInstanceMock = jest.fn();
OctokitMock.plugin.mockReturnValue(octokitInstanceMock);

const { createOctokitClient } = await import("../../src/clients/octokit");
await createOctokitClient();

expect(debugMock).toHaveBeenCalledWith('Going to create octokit client...');
expect(createActionAuthMock).toHaveBeenCalledTimes(1);
expect(authMock).toHaveBeenCalledTimes(1);
expect(debugMock).toHaveBeenCalledWith('Retrieved Octokit auth');
expect(debugMock).toHaveBeenCalledWith('Created Octokit auth');
expect(OctokitMock.plugin).toHaveBeenCalledWith(pluginRetryMock);
expect(debugMock).toHaveBeenCalledWith('Added retry policy to octokit client');
expect(octokitInstanceMock).toHaveBeenCalledWith({
auth: token,
request: {
retries: 3,
retryAfter: 1000,
}
});
expect(debugMock).toHaveBeenCalledWith('Created Octokit client');
});
});
36 changes: 36 additions & 0 deletions src/clients/octokit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createActionAuth } from "@octokit/auth-action";
import { Octokit } from "@octokit/core";
import { retry } from "@octokit/plugin-retry";
import { debug } from "@actions/core";

/**
* Create and get the Octokit client
* @returns {Promise<any>} Resolves when the action is complete. Include the Octokit client
*/
async function createOctokitClient(): Promise<any> {
debug('Going to create octokit client...');

const auth = createActionAuth();
debug('Retrieved Octokit auth');

const authentication = await auth();
debug('Created Octokit auth');

const octokitClient = Octokit.plugin(retry);
debug('Added retry policy to octokit client');

const client= new octokitClient({
auth: authentication.token,
request: {
retries: 3,
retryAfter: 1000,
}
});
debug('Created Octokit client');

return client;
}

export {
createOctokitClient
};

0 comments on commit 6981a8e

Please sign in to comment.