This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
87 lines (72 loc) · 2.26 KB
/
index.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const core = require('@actions/core')
const github = require('@actions/github')
const options = {
token: core.getInput('github-token'),
environment: core.getInput('environment'),
timeout: core.getInput('timeout'),
interval: core.getInput('interval')
}
waitForDeployment(options)
.then(res => {
core.setOutput('id', res.deployment.id)
core.setOutput('url', res.url)
})
.catch(error => {
core.setFailed(error.message)
})
async function waitForDeployment (options) {
const {
token,
interval,
environment
} = options
const timeout = parseInt(options.timeout) || 30
const { sha } = github.context
const octokit = github.getOctokit(token)
const start = Date.now()
const params = {
...github.context.repo,
environment,
sha
}
core.info(`Deployment params: ${JSON.stringify(params, null, 2)}`)
// throw new Error('DERP')
while (true) {
const { data: deployments } = await octokit.repos.listDeployments(params)
core.info(`Found ${deployments.length} deployments...`)
for (const deployment of deployments) {
core.info(`\tgetting statuses for deployment ${deployment.id}...`)
const { data: statuses } = await octokit.request('GET /repos/:owner/:repo/deployments/:deployment/statuses', {
...github.context.repo,
deployment: deployment.id
})
core.info(`\tfound ${statuses.length} statuses`)
const [success] = statuses
.filter(status => status.state === 'success')
if (success) {
core.info(`\tsuccess! ${JSON.stringify(success, null, 2)}`)
let url = success.target_url
const { payload = {} } = deployment
if (payload.web_url) {
url = payload.web_url
}
return {
deployment,
status: success,
url
}
} else {
core.info(`No statuses with state === "success": "${statuses.map(status => status.state).join('", "')}"`)
}
await sleep(interval)
}
const elapsed = (Date.now() - start) / 1000
if (elapsed >= timeout) {
throw new Error(`Timing out after ${timeout} seconds (${elapsed} elapsed)`)
}
}
}
function sleep (seconds) {
const ms = parseInt(seconds) * 1000 || 1
return new Promise(resolve => setTimeout(resolve, ms))
}