-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
executable file
·46 lines (36 loc) · 1.39 KB
/
main.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
import Octokit from '@octokit/rest'
import pWaitFor from 'p-wait-for'
import consola from 'consola'
const SimpleReporter = class {
constructor({ stream } = {}) {
this.stream = stream || process.stdout
}
log(logObj) {
this.stream.write(`${logObj.args[0]}\n`)
}
}
consola.setReporters(new SimpleReporter())
const octokit = new Octokit({
auth: `token ${process.env.GITHUB_API_TOKEN}`
})
const [owner, repo] = process.env.TRAVIS_REPO_SLUG.split('/')
const ref = process.env.TRAVIS_PULL_REQUEST_SHA
const hasDeployPreview = context => [/^netlify\/.*\/deploy-preview$/, /^deploy\/netlify$/].some(expr => expr.test(context))
const successPreview = state => state === 'success'
const failedPreview = state => state === 'failure'
const getSuccessfulDeployment = async () => {
const { data: { statuses } } = await octokit.repos.getCombinedStatusForRef({ owner, ref, repo })
if (statuses.find(({ context, state }) => hasDeployPreview(context) && failedPreview(state))) {
consola.error('Deploy preview failed')
// Fail CI
process.exit(1)
}
return statuses.find(({ context, state }) => hasDeployPreview(context) && successPreview(state))
}
const deployed = async () => Boolean(await getSuccessfulDeployment())
;(async () => {
await pWaitFor(deployed, { interval: 15000 })
const { target_url: targetUrl } = await getSuccessfulDeployment()
consola.log(targetUrl)
return targetUrl
})()