Skip to content

Commit

Permalink
Use updated @actions/github API
Browse files Browse the repository at this point in the history
  • Loading branch information
wms committed Oct 13, 2022
1 parent b76a0bb commit afb5c6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
16 changes: 9 additions & 7 deletions __tests__/poll.test.ts
@@ -1,8 +1,10 @@
import {poll} from '../src/poll'

const client = {
checks: {
listForRef: jest.fn()
rest: {
checks: {
listForRef: jest.fn()
}
}
}

Expand All @@ -19,7 +21,7 @@ const run = () =>
})

test('returns conclusion of completed check', async () => {
client.checks.listForRef.mockResolvedValue({
client.rest.checks.listForRef.mockResolvedValue({
data: {
check_runs: [
{
Expand All @@ -38,7 +40,7 @@ test('returns conclusion of completed check', async () => {
const result = await run()

expect(result).toBe('success')
expect(client.checks.listForRef).toHaveBeenCalledWith({
expect(client.rest.checks.listForRef).toHaveBeenCalledWith({
owner: 'testOrg',
repo: 'testRepo',
ref: 'abcd',
Expand All @@ -47,7 +49,7 @@ test('returns conclusion of completed check', async () => {
})

test('polls until check is completed', async () => {
client.checks.listForRef
client.rest.checks.listForRef
.mockResolvedValueOnce({
data: {
check_runs: [
Expand Down Expand Up @@ -83,11 +85,11 @@ test('polls until check is completed', async () => {
const result = await run()

expect(result).toBe('failure')
expect(client.checks.listForRef).toHaveBeenCalledTimes(3)
expect(client.rest.checks.listForRef).toHaveBeenCalledTimes(3)
})

test(`returns 'timed_out' if exceeding deadline`, async () => {
client.checks.listForRef.mockResolvedValue({
client.rest.checks.listForRef.mockResolvedValue({
data: {
check_runs: [
{
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
@@ -1,13 +1,13 @@
import * as core from '@actions/core'
import {context, GitHub} from '@actions/github'
import github, {context} from '@actions/github'
import {poll} from './poll'

async function run(): Promise<void> {
try {
const token = core.getInput('token', {required: true})

const result = await poll({
client: new GitHub(token),
client: github.getOctokit(token),
log: msg => core.info(msg),

checkName: core.getInput('checkName', {required: true}),
Expand All @@ -21,7 +21,7 @@ async function run(): Promise<void> {

core.setOutput('conclusion', result)
} catch (error) {
core.setFailed(error.message)
core.setFailed(error instanceof Error ? error : JSON.stringify(error))
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/poll.ts
@@ -1,8 +1,8 @@
import {GitHub} from '@actions/github'
import {GitHub} from '@actions/github/lib/utils'
import {wait} from './wait'

export interface Options {
client: GitHub
client: InstanceType<typeof GitHub>
log: (message: string) => void

checkName: string
Expand Down Expand Up @@ -32,7 +32,7 @@ export const poll = async (options: Options): Promise<string> => {
log(
`Retrieving check runs named ${checkName} on ${owner}/${repo}@${ref}...`
)
const result = await client.checks.listForRef({
const result = await client.rest.checks.listForRef({
// eslint-disable-next-line @typescript-eslint/camelcase
check_name: checkName,
owner,
Expand All @@ -51,7 +51,8 @@ export const poll = async (options: Options): Promise<string> => {
log(
`Found a completed check with id ${completedCheck.id} and conclusion ${completedCheck.conclusion}`
)
return completedCheck.conclusion
// conclusion is only `null` if status is not `completed`.
return completedCheck.conclusion!
}

log(
Expand Down

0 comments on commit afb5c6f

Please sign in to comment.