Skip to content

Commit 3f5325a

Browse files
feat: exit process when the deploy failed
Resolves #7
1 parent 4994fc6 commit 3f5325a

File tree

3 files changed

+152
-9
lines changed

3 files changed

+152
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"state": "failure",
3+
"statuses": [
4+
{
5+
"url": "https://api.github.com/repos/Developmint/developmint.de/statuses/be5ead9c3e2f8e5694384230eba8866370a4f07c",
6+
"avatar_url": "https://avatars3.githubusercontent.com/in/13473?v=4",
7+
"id": 6198390575,
8+
"node_id": "MDEzOlN0YXR1c0NvbnRleHQ2MTk4MzkwNTc1",
9+
"state": "failure",
10+
"description": "Deploy preview failed.",
11+
"target_url": "https://app.netlify.com/sites/developmint/deploys/5c59ab760e1c470008a5a296",
12+
"context": "netlify/developmint/deploy-preview",
13+
"created_at": "2019-02-05T15:28:32Z",
14+
"updated_at": "2019-02-05T15:28:32Z"
15+
},
16+
{
17+
"url": "https://api.github.com/repos/Developmint/developmint.de/statuses/be5ead9c3e2f8e5694384230eba8866370a4f07c",
18+
"avatar_url": "https://avatars2.githubusercontent.com/oa/1508?v=4",
19+
"id": 6198403512,
20+
"node_id": "MDEzOlN0YXR1c0NvbnRleHQ2MTk4NDAzNTEy",
21+
"state": "success",
22+
"description": "The Travis CI build passed",
23+
"target_url": "https://travis-ci.org/Developmint/developmint.de/builds/489096130?utm_source=github_status&utm_medium=notification",
24+
"context": "continuous-integration/travis-ci/push",
25+
"created_at": "2019-02-05T15:30:15Z",
26+
"updated_at": "2019-02-05T15:30:15Z"
27+
},
28+
{
29+
"url": "https://api.github.com/repos/Developmint/developmint.de/statuses/be5ead9c3e2f8e5694384230eba8866370a4f07c",
30+
"avatar_url": "https://avatars2.githubusercontent.com/oa/1508?v=4",
31+
"id": 6198802558,
32+
"node_id": "MDEzOlN0YXR1c0NvbnRleHQ2MTk4ODAyNTU4",
33+
"state": "error",
34+
"description": "The Travis CI build could not complete due to an error",
35+
"target_url": "https://travis-ci.org/Developmint/developmint.de/builds/489096145?utm_source=github_status&utm_medium=notification",
36+
"context": "continuous-integration/travis-ci/pr",
37+
"created_at": "2019-02-05T16:21:07Z",
38+
"updated_at": "2019-02-05T16:21:07Z"
39+
}
40+
],
41+
"sha": "be5ead9c3e2f8e5694384230eba8866370a4f07c",
42+
"total_count": 3,
43+
"repository": {
44+
"id": 125930547,
45+
"node_id": "MDEwOlJlcG9zaXRvcnkxMjU5MzA1NDc=",
46+
"name": "developmint.de",
47+
"full_name": "Developmint/developmint.de",
48+
"private": false,
49+
"owner": {
50+
"login": "Developmint",
51+
"id": 29969219,
52+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjI5OTY5MjE5",
53+
"avatar_url": "https://avatars0.githubusercontent.com/u/29969219?v=4",
54+
"gravatar_id": "",
55+
"url": "https://api.github.com/users/Developmint",
56+
"html_url": "https://github.com/Developmint",
57+
"followers_url": "https://api.github.com/users/Developmint/followers",
58+
"following_url": "https://api.github.com/users/Developmint/following{/other_user}",
59+
"gists_url": "https://api.github.com/users/Developmint/gists{/gist_id}",
60+
"starred_url": "https://api.github.com/users/Developmint/starred{/owner}{/repo}",
61+
"subscriptions_url": "https://api.github.com/users/Developmint/subscriptions",
62+
"organizations_url": "https://api.github.com/users/Developmint/orgs",
63+
"repos_url": "https://api.github.com/users/Developmint/repos",
64+
"events_url": "https://api.github.com/users/Developmint/events{/privacy}",
65+
"received_events_url": "https://api.github.com/users/Developmint/received_events",
66+
"type": "Organization",
67+
"site_admin": false
68+
},
69+
"html_url": "https://github.com/Developmint/developmint.de",
70+
"description": "Open source company page built with Nuxt.js and TailwindCSS",
71+
"fork": false,
72+
"url": "https://api.github.com/repos/Developmint/developmint.de",
73+
"forks_url": "https://api.github.com/repos/Developmint/developmint.de/forks",
74+
"keys_url": "https://api.github.com/repos/Developmint/developmint.de/keys{/key_id}",
75+
"collaborators_url": "https://api.github.com/repos/Developmint/developmint.de/collaborators{/collaborator}",
76+
"teams_url": "https://api.github.com/repos/Developmint/developmint.de/teams",
77+
"hooks_url": "https://api.github.com/repos/Developmint/developmint.de/hooks",
78+
"issue_events_url": "https://api.github.com/repos/Developmint/developmint.de/issues/events{/number}",
79+
"events_url": "https://api.github.com/repos/Developmint/developmint.de/events",
80+
"assignees_url": "https://api.github.com/repos/Developmint/developmint.de/assignees{/user}",
81+
"branches_url": "https://api.github.com/repos/Developmint/developmint.de/branches{/branch}",
82+
"tags_url": "https://api.github.com/repos/Developmint/developmint.de/tags",
83+
"blobs_url": "https://api.github.com/repos/Developmint/developmint.de/git/blobs{/sha}",
84+
"git_tags_url": "https://api.github.com/repos/Developmint/developmint.de/git/tags{/sha}",
85+
"git_refs_url": "https://api.github.com/repos/Developmint/developmint.de/git/refs{/sha}",
86+
"trees_url": "https://api.github.com/repos/Developmint/developmint.de/git/trees{/sha}",
87+
"statuses_url": "https://api.github.com/repos/Developmint/developmint.de/statuses/{sha}",
88+
"languages_url": "https://api.github.com/repos/Developmint/developmint.de/languages",
89+
"stargazers_url": "https://api.github.com/repos/Developmint/developmint.de/stargazers",
90+
"contributors_url": "https://api.github.com/repos/Developmint/developmint.de/contributors",
91+
"subscribers_url": "https://api.github.com/repos/Developmint/developmint.de/subscribers",
92+
"subscription_url": "https://api.github.com/repos/Developmint/developmint.de/subscription",
93+
"commits_url": "https://api.github.com/repos/Developmint/developmint.de/commits{/sha}",
94+
"git_commits_url": "https://api.github.com/repos/Developmint/developmint.de/git/commits{/sha}",
95+
"comments_url": "https://api.github.com/repos/Developmint/developmint.de/comments{/number}",
96+
"issue_comment_url": "https://api.github.com/repos/Developmint/developmint.de/issues/comments{/number}",
97+
"contents_url": "https://api.github.com/repos/Developmint/developmint.de/contents/{+path}",
98+
"compare_url": "https://api.github.com/repos/Developmint/developmint.de/compare/{base}...{head}",
99+
"merges_url": "https://api.github.com/repos/Developmint/developmint.de/merges",
100+
"archive_url": "https://api.github.com/repos/Developmint/developmint.de/{archive_format}{/ref}",
101+
"downloads_url": "https://api.github.com/repos/Developmint/developmint.de/downloads",
102+
"issues_url": "https://api.github.com/repos/Developmint/developmint.de/issues{/number}",
103+
"pulls_url": "https://api.github.com/repos/Developmint/developmint.de/pulls{/number}",
104+
"milestones_url": "https://api.github.com/repos/Developmint/developmint.de/milestones{/number}",
105+
"notifications_url": "https://api.github.com/repos/Developmint/developmint.de/notifications{?since,all,participating}",
106+
"labels_url": "https://api.github.com/repos/Developmint/developmint.de/labels{/name}",
107+
"releases_url": "https://api.github.com/repos/Developmint/developmint.de/releases{/id}",
108+
"deployments_url": "https://api.github.com/repos/Developmint/developmint.de/deployments"
109+
},
110+
"commit_url": "https://api.github.com/repos/Developmint/developmint.de/commits/be5ead9c3e2f8e5694384230eba8866370a4f07c",
111+
"url": "https://api.github.com/repos/Developmint/developmint.de/commits/be5ead9c3e2f8e5694384230eba8866370a4f07c/status"
112+
}

__test__/index.test.js

+29-8
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,43 @@ import nock from 'nock'
22
import pWaitFor from 'p-wait-for'
33
import consola from 'consola'
44
import combinedStatusResponse from './combined-status-response'
5+
import combinedStatusFailedResponse from './combined-status-failed-response'
56

6-
process.env.TRAVIS_REPO_SLUG = 'manniL/lichter.io'
7-
process.env.TRAVIS_PULL_REQUEST_SHA = '50ad1b7dccafa9b08ee3fe70b18df5cce3b6c4b0'
8-
process.env.GITHUB_API_TOKEN = '111'
7+
nock.disableNetConnect()
98

109
beforeEach(() => {
1110
consola.mockTypes(() => jest.fn())
11+
nock.cleanAll()
1212
})
1313

14-
nock('https://api.github.com')
15-
.persist()
16-
.get(`/repos/${process.env.TRAVIS_REPO_SLUG}/commits/${process.env.TRAVIS_PULL_REQUEST_SHA}/status`)
17-
.query({ access_token: '111' })
18-
.reply(200, combinedStatusResponse)
14+
process.env.TRAVIS_REPO_SLUG = 'manniL/lichter.io'
15+
process.env.TRAVIS_PULL_REQUEST_SHA = '50ad1b7dccafa9b08ee3fe70b18df5cce3b6c4b0'
16+
process.env.GITHUB_API_TOKEN = '111'
17+
18+
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+
24+
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {})
25+
try {
26+
require('../main')
27+
await pWaitFor(() => false, { timeout: 15000 })
28+
} catch (e) {}
29+
30+
expect(exit).toHaveBeenCalledWith(1)
31+
32+
// Disable mocks
33+
exit.mockRestore()
34+
}, 30000)
1935

2036
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)
41+
2142
require('../main')
2243
await pWaitFor(() => consola.log.mock.calls.length > 0)
2344

main.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ 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)
24+
const successPreview = state => state === 'success'
25+
const failedPreview = state => state === 'failure'
26+
2327
const getSuccessfulDeployment = async () => {
2428
const { data: { statuses } } = await octokit.repos.getCombinedStatusForRef({ owner, ref, repo })
2529

26-
return statuses.find(({ context, state }) => /^netlify\/.*\/deploy-preview$/.test(context) && state === 'success')
30+
if (statuses.find(({ context, state }) => hasDeployPreview(context) && failedPreview(state))) {
31+
consola.error('Deploy preview failed')
32+
// Fail CI
33+
process.exit(1)
34+
}
35+
36+
return statuses.find(({ context, state }) => hasDeployPreview(context) && successPreview(state))
2737
}
2838

2939
const deployed = async () => Boolean(await getSuccessfulDeployment())

0 commit comments

Comments
 (0)