generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 44
/
poll.ts
70 lines (60 loc) · 1.63 KB
/
poll.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {GitHub} from '@actions/github/lib/utils'
import {wait} from './wait'
export interface Options {
client: InstanceType<typeof GitHub>
log: (message: string) => void
checkName: string
timeoutSeconds: number
intervalSeconds: number
owner: string
repo: string
ref: string
}
export const poll = async (options: Options): Promise<string> => {
const {
client,
log,
checkName,
timeoutSeconds,
intervalSeconds,
owner,
repo,
ref
} = options
let now = new Date().getTime()
const deadline = now + timeoutSeconds * 1000
while (now <= deadline) {
log(
`Retrieving check runs named ${checkName} on ${owner}/${repo}@${ref}...`
)
const result = await client.rest.checks.listForRef({
// eslint-disable-next-line @typescript-eslint/camelcase
check_name: checkName,
owner,
repo,
ref
})
log(
`Retrieved ${result.data.check_runs.length} check runs named ${checkName}`
)
const completedCheck = result.data.check_runs.find(
checkRun => checkRun.status === 'completed'
)
if (completedCheck) {
log(
`Found a completed check with id ${completedCheck.id} and conclusion ${completedCheck.conclusion}`
)
// conclusion is only `null` if status is not `completed`.
return completedCheck.conclusion!
}
log(
`No completed checks named ${checkName}, waiting for ${intervalSeconds} seconds...`
)
await wait(intervalSeconds * 1000)
now = new Date().getTime()
}
log(
`No completed checks after ${timeoutSeconds} seconds, exiting with conclusion 'timed_out'`
)
return 'timed_out'
}