Skip to content

Commit 5da4f8c

Browse files
committed
fix: avoid crashing when getting results for a missing branch
Bullet point in #46
1 parent ce845d4 commit 5da4f8c

12 files changed

+256
-10
lines changed

lib/result.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ const debug = logger('wiby:result')
99

1010
// enum containing possible pipeline checks statuses
1111
const pipelineStatusesEnum = module.exports.pipelineStatusesEnum = Object.freeze({
12+
// statuses returned by github
1213
FAILED: 'failure',
1314
QUEUED: 'queued',
1415
PENDING: 'pending',
15-
SUCCEED: 'success'
16+
SUCCEED: 'success',
17+
18+
// custom statuses
19+
MISSING: 'test branch missing'
1620
})
1721

1822
const PENDING_RESULT_EXIT_CODE = 64
@@ -36,6 +40,16 @@ module.exports = async function ({ dependents }) {
3640

3741
const parentBranchName = await context.getParentBranchName()
3842
const branch = await context.getTestingBranchName(parentBranchName)
43+
const exists = await github.getBranch(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
44+
if (!exists) {
45+
output.results.push({
46+
dependent: `${dependentPkgInfo.owner}/${dependentPkgInfo.name}`,
47+
status: pipelineStatusesEnum.MISSING,
48+
runs: []
49+
})
50+
allDependentsChecks.push([undefined, pipelineStatusesEnum.MISSING])
51+
continue
52+
}
3953
let resp = await github.getChecks(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
4054
if (resp.data.check_runs.length === 0) {
4155
resp = await github.getCommitStatusesForRef(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
@@ -106,7 +120,8 @@ const getOverallStatusForAllRuns = module.exports.getOverallStatusForAllRuns = f
106120
// if includes null or pending or queued - overall status is pending
107121
if (statuses.includes(null) ||
108122
statuses.includes(pipelineStatusesEnum.PENDING) ||
109-
statuses.includes(pipelineStatusesEnum.QUEUED)
123+
statuses.includes(pipelineStatusesEnum.QUEUED) ||
124+
statuses.includes(pipelineStatusesEnum.MISSING)
110125
) {
111126
return pipelineStatusesEnum.PENDING
112127
}

test/cli/result.js

+51-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const gitFixture = require('../fixtures/git')
1010
const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby')
1111
const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures'))
1212

13+
const SUCCESS_RESULT_EXIT_CODE = 0
14+
const FAIL_RESULT_EXIT_CODE = 1
1315
const PENDING_RESULT_EXIT_CODE = 64
1416

1517
tap.test('result command', async (tap) => {
@@ -37,20 +39,64 @@ tap.test('result command', async (tap) => {
3739
childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, {
3840
env: {
3941
...process.env,
40-
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js`
42+
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-pass.js`
4143
}
4244
})
4345
} catch (e) {
4446
const result = e.output[1].toString().trim()
4547

4648
tap.equal(result, expected)
47-
tap.equal(e.status, PENDING_RESULT_EXIT_CODE)
49+
tap.equal(e.status, SUCCESS_RESULT_EXIT_CODE)
4850
}
4951
})
5052

5153
tap.test('result command should call result module with all deps from .wiby.json', async (tap) => {
5254
const expected = fs.readFileSync(
53-
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-multiple-dependant.md'),
55+
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-multiple-pass.md'),
56+
'utf-8'
57+
)
58+
.trim()
59+
60+
try {
61+
childProcess.execSync(`${wibyCommand} result`, {
62+
env: {
63+
...process.env,
64+
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-pass.js`
65+
}
66+
})
67+
} catch (e) {
68+
const result = e.output[1].toString().trim()
69+
70+
tap.equal(result, expected)
71+
tap.equal(e.status, SUCCESS_RESULT_EXIT_CODE)
72+
}
73+
})
74+
75+
tap.test('result command should call result module with all deps from .wiby.json (pending result)', async (tap) => {
76+
const expected = fs.readFileSync(
77+
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-multiple-pending.md'),
78+
'utf-8'
79+
)
80+
.trim()
81+
82+
try {
83+
childProcess.execSync(`${wibyCommand} result`, {
84+
env: {
85+
...process.env,
86+
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-pending.js`
87+
}
88+
})
89+
} catch (e) {
90+
const result = e.output[1].toString().trim()
91+
92+
tap.equal(result, expected)
93+
tap.equal(e.status, PENDING_RESULT_EXIT_CODE)
94+
}
95+
})
96+
97+
tap.test('result command should call result module with all deps from .wiby.json (missing branch result)', async (tap) => {
98+
const expected = fs.readFileSync(
99+
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-missing-branch.md'),
54100
'utf-8'
55101
)
56102
.trim()
@@ -59,7 +105,7 @@ tap.test('result command', async (tap) => {
59105
childProcess.execSync(`${wibyCommand} result`, {
60106
env: {
61107
...process.env,
62-
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js`
108+
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-missing-branch.js`
63109
}
64110
})
65111
} catch (e) {
@@ -136,7 +182,7 @@ tap.test('result command', async (tap) => {
136182
const result = e.output[1].toString().trim()
137183

138184
tap.equal(result, expected)
139-
tap.equal(e.status, 1)
185+
tap.equal(e.status, FAIL_RESULT_EXIT_CODE)
140186
}
141187
})
142188
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# wiby result command
2+
3+
Overall status - pending
4+
5+
## wiby-test/partial - success
6+
7+
Checks:
8+
9+
- partial_run - success
10+
- partial_run_2 - success
11+
12+
## wiby-test/fail - test branch missing
13+
14+
Checks:
15+
16+
## wiby-test/pass - success
17+
18+
Checks:
19+
20+
- pass_run - success
21+
- pass_run_2 - success
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# wiby result command
2+
3+
Overall status - success
4+
5+
## wiby-test/partial - success
6+
7+
Checks:
8+
9+
- partial_run - success
10+
- partial_run_2 - success
11+
12+
## wiby-test/fail - success
13+
14+
Checks:
15+
16+
- fail_run - success
17+
- fail_run_2 - success
18+
19+
## wiby-test/pass - success
20+
21+
Checks:
22+
23+
- pass_run - success
24+
- pass_run_2 - success

test/fixtures/http/result-command-empty-branch-checks-flat.js

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ nock('https://api.github.com')
2323
}
2424
}
2525
})
26+
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
27+
.reply(200, {})
2628
// get check results
2729
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
2830
.reply(200, {

test/fixtures/http/result-command-empty-branch-checks.js

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ nock('https://api.github.com')
2323
}
2424
}
2525
})
26+
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
27+
.reply(200, {})
2628
// get check results
2729
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
2830
.reply(200, {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
/**
4+
* Mocks of HTTP calls for "wiby result" command positive flow
5+
*/
6+
const nock = require('nock')
7+
8+
nock.disableNetConnect()
9+
10+
nock('https://api.github.com')
11+
// get package json
12+
.post('/graphql')
13+
.times(3)
14+
.reply(200, {
15+
data: {
16+
repository: {
17+
object: {
18+
text: JSON.stringify({
19+
dependencies: {
20+
wiby: '*'
21+
}
22+
})
23+
}
24+
}
25+
}
26+
})
27+
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
28+
.reply(200, {})
29+
.get('/repos/wiby-test/partial/branches/wiby-running-unit-tests')
30+
.reply(200, {})
31+
.get('/repos/wiby-test/pass/branches/wiby-running-unit-tests')
32+
.reply(200, {})
33+
.get('/repos/wiby-test/fail/branches/wiby-running-unit-tests')
34+
.reply(404, {})
35+
// get check results
36+
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
37+
.reply(200, {
38+
check_runs: [
39+
{ status: 'done', name: 'fake_run', conclusion: 'success' },
40+
{ status: 'done', name: 'fake_run_2', conclusion: 'success' }
41+
]
42+
})
43+
.get('/repos/wiby-test/fail/commits/wiby-running-unit-tests/check-runs')
44+
.reply(200, {
45+
check_runs: [
46+
{ status: 'done', name: 'fail_run', conclusion: 'success' },
47+
{ status: 'done', name: 'fail_run_2', conclusion: 'success' }
48+
]
49+
})
50+
.get('/repos/wiby-test/pass/commits/wiby-running-unit-tests/check-runs')
51+
.reply(200, {
52+
check_runs: [
53+
{ status: 'done', name: 'pass_run', conclusion: 'success' },
54+
{ status: 'done', name: 'pass_run_2', conclusion: 'success' }
55+
]
56+
})
57+
.get('/repos/wiby-test/partial/commits/wiby-running-unit-tests/check-runs')
58+
.reply(200, {
59+
check_runs: [
60+
{ status: 'done', name: 'partial_run', conclusion: 'success' },
61+
{ status: 'done', name: 'partial_run_2', conclusion: 'success' }
62+
]
63+
})

test/fixtures/http/result-command-positive-checks-failed.js

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ nock('https://api.github.com')
2525
}
2626
}
2727
})
28+
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
29+
.reply(200, {})
2830
// get check results
2931
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
3032
.reply(200, {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
/**
4+
* Mocks of HTTP calls for "wiby result" command positive flow
5+
*/
6+
const nock = require('nock')
7+
8+
nock.disableNetConnect()
9+
10+
nock('https://api.github.com')
11+
// get package json
12+
.post('/graphql')
13+
.times(3)
14+
.reply(200, {
15+
data: {
16+
repository: {
17+
object: {
18+
text: JSON.stringify({
19+
dependencies: {
20+
wiby: '*'
21+
}
22+
})
23+
}
24+
}
25+
}
26+
})
27+
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
28+
.reply(200, {})
29+
.get('/repos/wiby-test/partial/branches/wiby-running-unit-tests')
30+
.reply(200, {})
31+
.get('/repos/wiby-test/pass/branches/wiby-running-unit-tests')
32+
.reply(200, {})
33+
.get('/repos/wiby-test/fail/branches/wiby-running-unit-tests')
34+
.reply(200, {})
35+
// get check results
36+
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
37+
.reply(200, {
38+
check_runs: [
39+
{ status: 'done', name: 'fake_run', conclusion: 'success' },
40+
{ status: 'done', name: 'fake_run_2', conclusion: 'success' }
41+
]
42+
})
43+
.get('/repos/wiby-test/fail/commits/wiby-running-unit-tests/check-runs')
44+
.reply(200, {
45+
check_runs: [
46+
{ status: 'done', name: 'fail_run', conclusion: 'success' },
47+
{ status: 'done', name: 'fail_run_2', conclusion: 'success' }
48+
]
49+
})
50+
.get('/repos/wiby-test/pass/commits/wiby-running-unit-tests/check-runs')
51+
.reply(200, {
52+
check_runs: [
53+
{ status: 'done', name: 'pass_run', conclusion: 'success' },
54+
{ status: 'done', name: 'pass_run_2', conclusion: 'success' }
55+
]
56+
})
57+
.get('/repos/wiby-test/partial/commits/wiby-running-unit-tests/check-runs')
58+
.reply(200, {
59+
check_runs: [
60+
{ status: 'done', name: 'partial_run', conclusion: 'success' },
61+
{ status: 'done', name: 'partial_run_2', conclusion: 'success' }
62+
]
63+
})

test/fixtures/http/result-command-positive.js renamed to test/fixtures/http/result-command-positive-pending.js

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ nock('https://api.github.com')
2424
}
2525
}
2626
})
27+
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
28+
.reply(200, {})
29+
.get('/repos/wiby-test/partial/branches/wiby-running-unit-tests')
30+
.reply(200, {})
31+
.get('/repos/wiby-test/pass/branches/wiby-running-unit-tests')
32+
.reply(200, {})
33+
.get('/repos/wiby-test/fail/branches/wiby-running-unit-tests')
34+
.reply(200, {})
2735
// get check results
2836
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
2937
.reply(200, {

test/result.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ tap.test('wiby.result()', async (tap) => {
5353

5454
tap.test('result() should return correct data object', async (tap) => {
5555
// mock real http requests with positive scenario
56-
require('./fixtures/http/result-command-positive')
56+
require('./fixtures/http/result-command-positive-pass')
5757

5858
const output = await wiby.result({ dependents: [{ repository: 'https://github.com/wiby-test/fakeRepo' }] })
5959

60-
tap.equal(output.status, 'pending')
60+
tap.equal(output.status, 'success')
6161
tap.equal(output.results[0].dependent, 'wiby-test/fakeRepo')
62-
tap.equal(output.results[0].status, 'pending')
62+
tap.equal(output.results[0].status, 'success')
6363
tap.equal(output.results[0].runs.length, 2)
6464
})
6565

0 commit comments

Comments
 (0)