Skip to content

Commit 7b54326

Browse files
DSchauTheAlexLichter
authored andcommitted
fix: work with all netlify build contexts (#15)
1 parent 40fd1a6 commit 7b54326

File tree

4 files changed

+75
-102
lines changed

4 files changed

+75
-102
lines changed

__test__/index.test.js

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,95 @@
1-
import nock from 'nock'
1+
/* eslint import/first: "off" */
2+
jest.mock('@octokit/rest', () => {
3+
class Octokit {
4+
constructor() {
5+
this.repos = {
6+
getCombinedStatusForRef: Octokit.getCombinedStatusForRef
7+
}
8+
}
9+
}
10+
11+
Octokit.getCombinedStatusForRef = jest.fn()
12+
13+
return Octokit
14+
})
15+
import Octokit from '@octokit/rest'
216
import pWaitFor from 'p-wait-for'
317
import consola from 'consola'
418
import combinedStatusResponse from './combined-status-response'
519
import combinedStatusFailedResponse from './combined-status-failed-response'
620

7-
nock.disableNetConnect()
8-
921
beforeEach(() => {
22+
Octokit.getCombinedStatusForRef.mockReset()
1023
consola.mockTypes(() => jest.fn())
11-
nock.cleanAll()
24+
25+
process.env.TRAVIS_REPO_SLUG = 'manniL/lichter.io'
26+
process.env.TRAVIS_PULL_REQUEST_SHA = '50ad1b7dccafa9b08ee3fe70b18df5cce3b6c4b0'
27+
process.env.GITHUB_API_TOKEN = '111'
1228
})
1329

14-
process.env.TRAVIS_REPO_SLUG = 'manniL/lichter.io'
15-
process.env.TRAVIS_PULL_REQUEST_SHA = '50ad1b7dccafa9b08ee3fe70b18df5cce3b6c4b0'
16-
process.env.GITHUB_API_TOKEN = '111'
30+
const requireMain = () => {
31+
jest.isolateModules(() => {
32+
require('../main')
33+
})
34+
}
1735

1836
test('it throws when deploy preview failed', async () => {
19-
nock('https://api.github.com')
20-
.persist()
21-
.get(`/repos/${process.env.TRAVIS_REPO_SLUG}/commits/${process.env.TRAVIS_PULL_REQUEST_SHA}/status`)
22-
.reply(200, combinedStatusFailedResponse)
23-
2437
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {})
38+
39+
Octokit.getCombinedStatusForRef.mockResolvedValue({
40+
data: combinedStatusFailedResponse
41+
})
42+
2543
try {
26-
require('../main')
27-
await pWaitFor(() => false, { timeout: 15000 })
44+
requireMain()
45+
await pWaitFor(() => consola.errors.mock.calls.length > 0)
2846
} catch (e) {}
2947

3048
expect(exit).toHaveBeenCalledWith(1)
3149

3250
// Disable mocks
3351
exit.mockRestore()
34-
}, 30000)
52+
})
3553

3654
test('it calls console.log with deployed url', async () => {
37-
nock('https://api.github.com')
38-
.persist()
39-
.get(`/repos/${process.env.TRAVIS_REPO_SLUG}/commits/${process.env.TRAVIS_PULL_REQUEST_SHA}/status`)
40-
.reply(200, combinedStatusResponse)
55+
Octokit.getCombinedStatusForRef.mockResolvedValue({
56+
data: combinedStatusResponse
57+
})
4158

42-
require('../main')
59+
requireMain()
4360
await pWaitFor(() => consola.log.mock.calls.length > 0)
4461

45-
const consolaMessages = consola.log.mock.calls.map(c => c[0])
46-
expect(consolaMessages).toContain(combinedStatusResponse.statuses[0].target_url)
47-
}, 30000)
62+
expect(consola.log).toHaveBeenCalledWith(expect.stringContaining(
63+
combinedStatusResponse.statuses[0].target_url
64+
))
65+
})
66+
67+
test('it works with deploy/netlify status', async () => {
68+
const targetUrl = `https://deploy-preview-26--something-different.netlify.com`
69+
const combinedStatusResponseWithChangedContext = {
70+
...combinedStatusResponse,
71+
repository: {
72+
...combinedStatusResponse.repository,
73+
full_name: `dschau/some-repo`
74+
},
75+
statuses: [
76+
{
77+
...combinedStatusResponse.statuses[0],
78+
context: 'deploy/netlify',
79+
target_url: targetUrl
80+
}
81+
]
82+
}
83+
process.env.TRAVIS_REPO_SLUG = combinedStatusResponseWithChangedContext.repository.full_name
84+
85+
Octokit.getCombinedStatusForRef.mockResolvedValue({
86+
data: combinedStatusResponseWithChangedContext
87+
})
88+
89+
requireMain()
90+
await pWaitFor(() => consola.log.mock.calls.length > 0)
91+
92+
expect(consola.log).toHaveBeenCalledWith(
93+
expect.stringContaining(targetUrl)
94+
)
95+
})

main.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ const octokit = new Octokit({
2020
const [owner, repo] = process.env.TRAVIS_REPO_SLUG.split('/')
2121
const ref = process.env.TRAVIS_PULL_REQUEST_SHA
2222

23-
const hasDeployPreview = context => /^netlify\/.*\/deploy-preview$/.test(context)
23+
const hasDeployPreview = context => [/^netlify\/.*\/deploy-preview$/, /^deploy\/netlify$/].some(expr => expr.test(context))
2424
const successPreview = state => state === 'success'
2525
const failedPreview = state => state === 'failure'
2626

2727
const getSuccessfulDeployment = async () => {
2828
const { data: { statuses } } = await octokit.repos.getCombinedStatusForRef({ owner, ref, repo })
29-
29+
3030
if (statuses.find(({ context, state }) => hasDeployPreview(context) && failedPreview(state))) {
3131
consola.error('Deploy preview failed')
3232
// Fail CI
@@ -37,7 +37,6 @@ const getSuccessfulDeployment = async () => {
3737
}
3838

3939
const deployed = async () => Boolean(await getSuccessfulDeployment())
40-
4140
;(async () => {
4241
await pWaitFor(deployed, { interval: 15000 })
4342

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"repository": "Developmint/wait-for-netlify-preview",
88
"scripts": {
99
"lint": "eslint index.js __test__",
10-
"test": "yarn run lint && jest --detectOpenHandles",
10+
"pretest": "yarn run lint",
11+
"test": "jest --detectOpenHandles",
1112
"release": "standard-version && git push --follow-tags && npm publish",
1213
"commitlint": "commitlint -E HUSKY_GIT_PARAMS",
1314
"coverage": "codecov"
@@ -55,7 +56,6 @@
5556
"eslint-plugin-standard": "^4.0.0",
5657
"eslint-plugin-vue": "^5.1.0",
5758
"jest": "^24.1.0",
58-
"nock": "^10.0.6",
5959
"standard-version": "^4.4.0"
6060
},
6161
"husky": {

yarn.lock

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,6 @@ assert-plus@1.0.0, assert-plus@^1.0.0:
928928
resolved "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525"
929929
integrity sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=
930930

931-
assertion-error@^1.1.0:
932-
version "1.1.0"
933-
resolved "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b"
934-
integrity sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==
935-
936931
assign-symbols@^1.0.0:
937932
version "1.0.0"
938933
resolved "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz#59667f41fadd4f20ccbc2bb96b8d4f7f78ec0367"
@@ -1281,18 +1276,6 @@ caseless@~0.12.0:
12811276
resolved "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
12821277
integrity sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=
12831278

1284-
chai@^4.1.2:
1285-
version "4.2.0"
1286-
resolved "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5"
1287-
integrity sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==
1288-
dependencies:
1289-
assertion-error "^1.1.0"
1290-
check-error "^1.0.2"
1291-
deep-eql "^3.0.1"
1292-
get-func-name "^2.0.0"
1293-
pathval "^1.1.0"
1294-
type-detect "^4.0.5"
1295-
12961279
chalk@^1.1.3:
12971280
version "1.1.3"
12981281
resolved "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
@@ -1318,11 +1301,6 @@ chardet@^0.7.0:
13181301
resolved "https://registry.npmjs.org/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
13191302
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==
13201303

1321-
check-error@^1.0.2:
1322-
version "1.0.2"
1323-
resolved "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
1324-
integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=
1325-
13261304
chownr@^1.1.1:
13271305
version "1.1.1"
13281306
resolved "https://registry.npmjs.org/chownr/-/chownr-1.1.1.tgz#54726b8b8fff4df053c42187e801fb4412df1494"
@@ -2026,18 +2004,6 @@ decode-uri-component@^0.2.0:
20262004
resolved "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545"
20272005
integrity sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=
20282006

2029-
deep-eql@^3.0.1:
2030-
version "3.0.1"
2031-
resolved "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
2032-
integrity sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==
2033-
dependencies:
2034-
type-detect "^4.0.0"
2035-
2036-
deep-equal@^1.0.0:
2037-
version "1.0.1"
2038-
resolved "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
2039-
integrity sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=
2040-
20412007
deep-extend@^0.6.0:
20422008
version "0.6.0"
20432009
resolved "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz#c4fa7c95404a17a9c3e8ca7e1537312b736330ac"
@@ -2838,11 +2804,6 @@ get-caller-file@^1.0.1:
28382804
resolved "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz#f978fa4c90d1dfe7ff2d6beda2a515e713bdcf4a"
28392805
integrity sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==
28402806

2841-
get-func-name@^2.0.0:
2842-
version "2.0.0"
2843-
resolved "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41"
2844-
integrity sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=
2845-
28462807
get-pkg-repo@^1.0.0:
28472808
version "1.4.0"
28482809
resolved "https://registry.npmjs.org/get-pkg-repo/-/get-pkg-repo-1.4.0.tgz#c73b489c06d80cc5536c2c853f9e05232056972d"
@@ -4584,21 +4545,6 @@ nice-try@^1.0.4:
45844545
resolved "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
45854546
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
45864547

4587-
nock@^10.0.6:
4588-
version "10.0.6"
4589-
resolved "https://registry.npmjs.org/nock/-/nock-10.0.6.tgz#e6d90ee7a68b8cfc2ab7f6127e7d99aa7d13d111"
4590-
integrity sha512-b47OWj1qf/LqSQYnmokNWM8D88KvUl2y7jT0567NB3ZBAZFz2bWp2PC81Xn7u8F2/vJxzkzNZybnemeFa7AZ2w==
4591-
dependencies:
4592-
chai "^4.1.2"
4593-
debug "^4.1.0"
4594-
deep-equal "^1.0.0"
4595-
json-stringify-safe "^5.0.1"
4596-
lodash "^4.17.5"
4597-
mkdirp "^0.5.0"
4598-
propagate "^1.0.0"
4599-
qs "^6.5.1"
4600-
semver "^5.5.0"
4601-
46024548
node-fetch@^2.3.0:
46034549
version "2.3.0"
46044550
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
@@ -5074,11 +5020,6 @@ path-type@^3.0.0:
50745020
dependencies:
50755021
pify "^3.0.0"
50765022

5077-
pathval@^1.1.0:
5078-
version "1.1.0"
5079-
resolved "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"
5080-
integrity sha1-uULm1L3mUwBe9rcTYd74cn0GReA=
5081-
50825023
performance-now@^2.1.0:
50835024
version "2.1.0"
50845025
resolved "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
@@ -5540,11 +5481,6 @@ prompts@^2.0.1:
55405481
kleur "^3.0.0"
55415482
sisteransi "^1.0.0"
55425483

5543-
propagate@^1.0.0:
5544-
version "1.0.0"
5545-
resolved "https://registry.npmjs.org/propagate/-/propagate-1.0.0.tgz#00c2daeedda20e87e3782b344adba1cddd6ad709"
5546-
integrity sha1-AMLa7t2iDofjeCs0Stuhzd1q1wk=
5547-
55485484
pseudomap@^1.0.2:
55495485
version "1.0.2"
55505486
resolved "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
@@ -5578,11 +5514,6 @@ q@^1.1.2, q@^1.4.1, q@^1.5.1:
55785514
resolved "https://registry.npmjs.org/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
55795515
integrity sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=
55805516

5581-
qs@^6.5.1:
5582-
version "6.6.0"
5583-
resolved "https://registry.npmjs.org/qs/-/qs-6.6.0.tgz#a99c0f69a8d26bf7ef012f871cdabb0aee4424c2"
5584-
integrity sha512-KIJqT9jQJDQx5h5uAVPimw6yVg2SekOKu959OCtktD3FjzbpvaPr8i4zzg07DOMz+igA4W/aNM7OV8H37pFYfA==
5585-
55865517
qs@~6.5.2:
55875518
version "6.5.2"
55885519
resolved "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
@@ -6746,11 +6677,6 @@ type-check@~0.3.2:
67466677
dependencies:
67476678
prelude-ls "~1.1.2"
67486679

6749-
type-detect@^4.0.0, type-detect@^4.0.5:
6750-
version "4.0.8"
6751-
resolved "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
6752-
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==
6753-
67546680
typedarray@^0.0.6:
67556681
version "0.0.6"
67566682
resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

0 commit comments

Comments
 (0)