generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GH-8): Add functionality to create octokit client
- Loading branch information
Showing
2 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |