Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 37 additions & 32 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

177 changes: 97 additions & 80 deletions src/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getWorkflowRuns
} from '.'
import {ActionConfig, DispatchMethod, ExponentialBackoff} from '../action'
import {RequestError} from '@octokit/request-error'

jest.mock('@actions/core')

Expand Down Expand Up @@ -42,6 +43,9 @@ const mockOctokit = {
throw new Error('Should be mocked')
}
}
},
paginate: async (_method: any, _params: any, _mapFn: any): Promise<any> => {
throw new Error('Should be mocked')
}
}

Expand All @@ -50,9 +54,12 @@ describe('API', () => {
placeholder: 'placeholder'
}

let startEpoch: number
let mockActionConfig: ActionConfig

beforeEach(() => {
startEpoch = Date.now()

mockActionConfig = {
dispatchMethod: DispatchMethod.WorkflowDispatch,
eventType: '',
Expand Down Expand Up @@ -360,69 +367,65 @@ describe('API', () => {
})

it('should return the workflow runs for a valid configuration', async () => {
const mockData = {
workflow_runs: [
{
id: 0,
name: 'Apple',
html_url: 'http://github.com/0'
},
{
id: 1,
html_url: 'http://github.com/1'
}
]
}
const mockData = [
{
id: 0,
name: 'Apple',
html_url: 'http://github.com/0'
},
{
id: 1,
html_url: 'http://github.com/1'
}
]

jest
.spyOn(mockOctokit.rest.actions, 'listWorkflowRuns')
.mockReturnValue(
Promise.resolve({
data: mockData,
status: 200
})
)

const workflowRuns = await getWorkflowRuns()
expect(workflowRuns.length).toStrictEqual(mockData.workflow_runs.length)
.spyOn(mockOctokit, 'paginate')
.mockImplementation(async (method, _params, _mapFn) => {
if (method === mockOctokit.rest.actions.listWorkflowRuns) {
return mockData
}
throw new Error(`Unexpected paginate call, got ${method}`)
})

const workflowRuns = await getWorkflowRuns(startEpoch)
expect(workflowRuns.length).toStrictEqual(mockData.length)
})

it('should return the workflow runs for a tags ref', async () => {
mockActionConfig.ref = 'refs/tags/v1.0.0'
init(mockActionConfig)

const mockData = {
workflow_runs: [
{
id: 0,
name: 'Apple',
html_url: 'http://github.com/0'
},
{
id: 1,
html_url: 'http://github.com/1'
}
]
}
const mockData = [
{
id: 0,
name: 'Apple',
html_url: 'http://github.com/0'
},
{
id: 1,
html_url: 'http://github.com/1'
}
]

jest
.spyOn(mockOctokit.rest.actions, 'listWorkflowRuns')
.mockReturnValue(
Promise.resolve({
data: mockData,
status: 200
})
)

const workflowRuns = await getWorkflowRuns()
expect(workflowRuns.length).toStrictEqual(mockData.workflow_runs.length)
.spyOn(mockOctokit, 'paginate')
.mockImplementation(async (method, _params, _mapFn) => {
if (method === mockOctokit.rest.actions.listWorkflowRuns) {
return mockData
}
throw new Error(`Unexpected paginate call, got ${method}`)
})

const workflowRuns = await getWorkflowRuns(startEpoch)
expect(workflowRuns.length).toStrictEqual(mockData.length)
})

it('should throw if getWorkflowRuns is invoked without a workflow configured', async () => {
mockActionConfig.workflow = ''
init(mockActionConfig)

await expect(getWorkflowRuns()).rejects.toThrow(
await expect(getWorkflowRuns(startEpoch)).rejects.toThrow(
`An input to 'workflow' was not provided`
)
})
Expand All @@ -447,31 +450,29 @@ describe('API', () => {
})

it('should return the workflow runs for a valid configuration', async () => {
const mockData = {
workflow_runs: [
{
id: 0,
name: 'Apple',
html_url: 'http://github.com/0'
},
{
id: 1,
html_url: 'http://github.com/1'
}
]
}
const mockData = [
{
id: 0,
name: 'Apple',
html_url: 'http://github.com/0'
},
{
id: 1,
html_url: 'http://github.com/1'
}
]

jest
.spyOn(mockOctokit.rest.actions, 'listWorkflowRunsForRepo')
.mockReturnValue(
Promise.resolve({
data: mockData,
status: 200
})
)

const workflowRuns = await getWorkflowRuns()
expect(workflowRuns.length).toStrictEqual(mockData.workflow_runs.length)
.spyOn(mockOctokit, 'paginate')
.mockImplementation(async (method, _params, _mapFn) => {
if (method === mockOctokit.rest.actions.listWorkflowRunsForRepo) {
return mockData
}
throw new Error(`Unexpected paginate call, got ${method}`)
})

const workflowRuns = await getWorkflowRuns(startEpoch)
expect(workflowRuns.length).toStrictEqual(mockData.length)
})
})

Expand All @@ -480,16 +481,32 @@ describe('API', () => {
const errorStatus = 404

jest
.spyOn(mockOctokit.rest.actions, 'listWorkflowRuns')
.mockReturnValue(
Promise.resolve({
data: undefined,
status: errorStatus
})
)

await expect(getWorkflowRuns()).rejects.toThrowError(
`Failed to get workflow runs, expected 200 but received ${errorStatus}`
.spyOn(mockOctokit, 'paginate')
.mockImplementation(async (method, _params, _mapFn) => {
if (method === mockOctokit.rest.actions.listWorkflowRuns) {
// throw Object.assign(new Error('Request failed'), {
// status: errorStatus
// })

throw new RequestError('Request failed', errorStatus, {
request: {
method: 'GET',
url: 'https://api.github.com/foo',
headers: {}
},
response: {
status: 500,
url: 'https://api.github.com/foo',
headers: {},
data: []
}
})
}
throw new Error(`Unexpected paginate call, got ${method}`)
})

await expect(getWorkflowRuns(startEpoch)).rejects.toThrowError(
`Expected 200 but received ${errorStatus}`
)
})
})
Expand Down
14 changes: 10 additions & 4 deletions src/api/api.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type {GitHub} from '@actions/github/lib/utils'
// eslint-disable-next-line import/no-unresolved
import {GetResponseTypeFromEndpointMethod} from '@octokit/types'

import {
GetResponseDataTypeFromEndpointMethod,
RequestError
// eslint-disable-next-line import/no-unresolved
} from '@octokit/types'

export type Octokit = InstanceType<typeof GitHub>
let octokit: Octokit
Expand All @@ -11,6 +15,8 @@ export interface WorkflowRun {
htmlUrl: string
}

export type WorkflowRunResponse = GetResponseTypeFromEndpointMethod<
typeof octokit.rest.actions.listWorkflowRuns
Comment on lines -14 to -15
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect the typing here can probably be improved, but I'm not really sure what to put here.

export type WorkflowRunResponse = GetResponseDataTypeFromEndpointMethod<
typeof octokit.paginate
>

export type OctokitRequestError = RequestError
Loading