mirror of
https://github.com/actions/github-script.git
synced 2026-04-09 07:30:05 +00:00
Extract createConfiguredGetOctokit factory that wraps getOctokit with: - retry and requestLog plugins (from action defaults) - retries count, proxy agent, orchestration ID user-agent - deep-merge for request options so user overrides don't clobber retries - plugin deduplication to prevent double-application This ensures secondary Octokit clients created via getOctokit() in github-script workflows inherit the same defaults as the primary github client.
186 lines
4.9 KiB
TypeScript
186 lines
4.9 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
import {createConfiguredGetOctokit} from '../src/create-configured-getoctokit'
|
|
|
|
describe('createConfiguredGetOctokit', () => {
|
|
const mockRetryPlugin = jest.fn()
|
|
const mockRequestLogPlugin = jest.fn()
|
|
|
|
function makeMockGetOctokit() {
|
|
return jest.fn().mockReturnValue('mock-client')
|
|
}
|
|
|
|
test('passes token and merged defaults to underlying getOctokit', () => {
|
|
const raw = makeMockGetOctokit()
|
|
const defaults = {
|
|
userAgent: 'actions/github-script actions_orchestration_id/abc',
|
|
retry: {enabled: true},
|
|
request: {retries: 3}
|
|
}
|
|
|
|
const wrapped = createConfiguredGetOctokit(
|
|
raw as any,
|
|
defaults,
|
|
mockRetryPlugin,
|
|
mockRequestLogPlugin
|
|
)
|
|
wrapped('my-token' as any)
|
|
|
|
expect(raw).toHaveBeenCalledWith(
|
|
'my-token',
|
|
expect.objectContaining({
|
|
userAgent: 'actions/github-script actions_orchestration_id/abc',
|
|
retry: {enabled: true},
|
|
request: {retries: 3}
|
|
}),
|
|
mockRetryPlugin,
|
|
mockRequestLogPlugin
|
|
)
|
|
})
|
|
|
|
test('user options override top-level defaults', () => {
|
|
const raw = makeMockGetOctokit()
|
|
const defaults = {
|
|
userAgent: 'default-agent',
|
|
previews: ['v3']
|
|
}
|
|
|
|
const wrapped = createConfiguredGetOctokit(raw as any, defaults)
|
|
wrapped('tok' as any, {userAgent: 'custom-agent'} as any)
|
|
|
|
expect(raw).toHaveBeenCalledWith(
|
|
'tok',
|
|
expect.objectContaining({userAgent: 'custom-agent', previews: ['v3']})
|
|
)
|
|
})
|
|
|
|
test('deep-merges request so partial overrides preserve retries', () => {
|
|
const raw = makeMockGetOctokit()
|
|
const defaults = {
|
|
request: {retries: 3, agent: 'proxy-agent', fetch: 'proxy-fetch'}
|
|
}
|
|
|
|
const wrapped = createConfiguredGetOctokit(raw as any, defaults)
|
|
wrapped('tok' as any, {request: {timeout: 5000}} as any)
|
|
|
|
expect(raw).toHaveBeenCalledWith(
|
|
'tok',
|
|
expect.objectContaining({
|
|
request: {
|
|
retries: 3,
|
|
agent: 'proxy-agent',
|
|
fetch: 'proxy-fetch',
|
|
timeout: 5000
|
|
}
|
|
})
|
|
)
|
|
})
|
|
|
|
test('user can override request.retries explicitly', () => {
|
|
const raw = makeMockGetOctokit()
|
|
const defaults = {request: {retries: 3}}
|
|
|
|
const wrapped = createConfiguredGetOctokit(raw as any, defaults)
|
|
wrapped('tok' as any, {request: {retries: 0}} as any)
|
|
|
|
expect(raw).toHaveBeenCalledWith(
|
|
'tok',
|
|
expect.objectContaining({request: {retries: 0}})
|
|
)
|
|
})
|
|
|
|
test('user plugins are appended after default plugins', () => {
|
|
const raw = makeMockGetOctokit()
|
|
const customPlugin = jest.fn()
|
|
|
|
const wrapped = createConfiguredGetOctokit(
|
|
raw as any,
|
|
{},
|
|
mockRetryPlugin,
|
|
mockRequestLogPlugin
|
|
)
|
|
wrapped('tok' as any, {} as any, customPlugin as any)
|
|
|
|
expect(raw).toHaveBeenCalledWith(
|
|
'tok',
|
|
expect.any(Object),
|
|
mockRetryPlugin,
|
|
mockRequestLogPlugin,
|
|
customPlugin
|
|
)
|
|
})
|
|
|
|
test('duplicate plugins are deduplicated', () => {
|
|
const raw = makeMockGetOctokit()
|
|
|
|
const wrapped = createConfiguredGetOctokit(
|
|
raw as any,
|
|
{},
|
|
mockRetryPlugin,
|
|
mockRequestLogPlugin
|
|
)
|
|
// User passes retry again — should not duplicate
|
|
wrapped('tok' as any, {} as any, mockRetryPlugin as any)
|
|
|
|
expect(raw).toHaveBeenCalledWith(
|
|
'tok',
|
|
expect.any(Object),
|
|
mockRetryPlugin,
|
|
mockRequestLogPlugin
|
|
)
|
|
})
|
|
|
|
test('applies defaults when no user options provided', () => {
|
|
const raw = makeMockGetOctokit()
|
|
const defaults = {
|
|
userAgent: 'actions/github-script',
|
|
retry: {enabled: true},
|
|
request: {retries: 3}
|
|
}
|
|
|
|
const wrapped = createConfiguredGetOctokit(
|
|
raw as any,
|
|
defaults,
|
|
mockRetryPlugin
|
|
)
|
|
wrapped('tok' as any)
|
|
|
|
expect(raw).toHaveBeenCalledWith(
|
|
'tok',
|
|
{
|
|
userAgent: 'actions/github-script',
|
|
retry: {enabled: true},
|
|
request: {retries: 3}
|
|
},
|
|
mockRetryPlugin
|
|
)
|
|
})
|
|
|
|
test('baseUrl: undefined from user does not clobber default', () => {
|
|
const raw = makeMockGetOctokit()
|
|
const defaults = {baseUrl: 'https://api.github.com'}
|
|
|
|
const wrapped = createConfiguredGetOctokit(raw as any, defaults)
|
|
wrapped('tok' as any, {baseUrl: undefined} as any)
|
|
|
|
// undefined spread still overwrites — this documents current behavior.
|
|
// The `baseUrl` key is present but value is undefined.
|
|
const calledOpts = raw.mock.calls[0][1]
|
|
expect(calledOpts).toHaveProperty('baseUrl')
|
|
})
|
|
|
|
test('each call creates an independent client', () => {
|
|
const raw = jest
|
|
.fn()
|
|
.mockReturnValueOnce('client-a')
|
|
.mockReturnValueOnce('client-b')
|
|
|
|
const wrapped = createConfiguredGetOctokit(raw as any, {})
|
|
const a = wrapped('token-a' as any)
|
|
const b = wrapped('token-b' as any)
|
|
|
|
expect(a).toBe('client-a')
|
|
expect(b).toBe('client-b')
|
|
expect(raw).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|