Skip to content

Commit

Permalink
fix: avoid crashing when getting results for a missing branch
Browse files Browse the repository at this point in the history
Bullet point in #46
  • Loading branch information
dominykas committed Apr 18, 2021
1 parent ce845d4 commit 5da4f8c
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 10 deletions.
19 changes: 17 additions & 2 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ const debug = logger('wiby:result')

// enum containing possible pipeline checks statuses
const pipelineStatusesEnum = module.exports.pipelineStatusesEnum = Object.freeze({
// statuses returned by github
FAILED: 'failure',
QUEUED: 'queued',
PENDING: 'pending',
SUCCEED: 'success'
SUCCEED: 'success',

// custom statuses
MISSING: 'test branch missing'
})

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

const parentBranchName = await context.getParentBranchName()
const branch = await context.getTestingBranchName(parentBranchName)
const exists = await github.getBranch(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
if (!exists) {
output.results.push({
dependent: `${dependentPkgInfo.owner}/${dependentPkgInfo.name}`,
status: pipelineStatusesEnum.MISSING,
runs: []
})
allDependentsChecks.push([undefined, pipelineStatusesEnum.MISSING])
continue
}
let resp = await github.getChecks(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
if (resp.data.check_runs.length === 0) {
resp = await github.getCommitStatusesForRef(dependentPkgInfo.owner, dependentPkgInfo.name, branch)
Expand Down Expand Up @@ -106,7 +120,8 @@ const getOverallStatusForAllRuns = module.exports.getOverallStatusForAllRuns = f
// if includes null or pending or queued - overall status is pending
if (statuses.includes(null) ||
statuses.includes(pipelineStatusesEnum.PENDING) ||
statuses.includes(pipelineStatusesEnum.QUEUED)
statuses.includes(pipelineStatusesEnum.QUEUED) ||
statuses.includes(pipelineStatusesEnum.MISSING)
) {
return pipelineStatusesEnum.PENDING
}
Expand Down
56 changes: 51 additions & 5 deletions test/cli/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const gitFixture = require('../fixtures/git')
const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby')
const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures'))

const SUCCESS_RESULT_EXIT_CODE = 0
const FAIL_RESULT_EXIT_CODE = 1
const PENDING_RESULT_EXIT_CODE = 64

tap.test('result command', async (tap) => {
Expand Down Expand Up @@ -37,20 +39,64 @@ tap.test('result command', async (tap) => {
childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js`
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-pass.js`
}
})
} catch (e) {
const result = e.output[1].toString().trim()

tap.equal(result, expected)
tap.equal(e.status, PENDING_RESULT_EXIT_CODE)
tap.equal(e.status, SUCCESS_RESULT_EXIT_CODE)
}
})

tap.test('result command should call result module with all deps from .wiby.json', async (tap) => {
const expected = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-multiple-dependant.md'),
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-multiple-pass.md'),
'utf-8'
)
.trim()

try {
childProcess.execSync(`${wibyCommand} result`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-pass.js`
}
})
} catch (e) {
const result = e.output[1].toString().trim()

tap.equal(result, expected)
tap.equal(e.status, SUCCESS_RESULT_EXIT_CODE)
}
})

tap.test('result command should call result module with all deps from .wiby.json (pending result)', async (tap) => {
const expected = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-multiple-pending.md'),
'utf-8'
)
.trim()

try {
childProcess.execSync(`${wibyCommand} result`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-pending.js`
}
})
} catch (e) {
const result = e.output[1].toString().trim()

tap.equal(result, expected)
tap.equal(e.status, PENDING_RESULT_EXIT_CODE)
}
})

tap.test('result command should call result module with all deps from .wiby.json (missing branch result)', async (tap) => {
const expected = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-missing-branch.md'),
'utf-8'
)
.trim()
Expand All @@ -59,7 +105,7 @@ tap.test('result command', async (tap) => {
childProcess.execSync(`${wibyCommand} result`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js`
NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-missing-branch.js`
}
})
} catch (e) {
Expand Down Expand Up @@ -136,7 +182,7 @@ tap.test('result command', async (tap) => {
const result = e.output[1].toString().trim()

tap.equal(result, expected)
tap.equal(e.status, 1)
tap.equal(e.status, FAIL_RESULT_EXIT_CODE)
}
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# wiby result command

Overall status - pending

## wiby-test/partial - success

Checks:

- partial_run - success
- partial_run_2 - success

## wiby-test/fail - test branch missing

Checks:

## wiby-test/pass - success

Checks:

- pass_run - success
- pass_run_2 - success
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# wiby result command

Overall status - success

## wiby-test/partial - success

Checks:

- partial_run - success
- partial_run_2 - success

## wiby-test/fail - success

Checks:

- fail_run - success
- fail_run_2 - success

## wiby-test/pass - success

Checks:

- pass_run - success
- pass_run_2 - success
2 changes: 2 additions & 0 deletions test/fixtures/http/result-command-empty-branch-checks-flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ nock('https://api.github.com')
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/http/result-command-empty-branch-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ nock('https://api.github.com')
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
Expand Down
63 changes: 63 additions & 0 deletions test/fixtures/http/result-command-missing-branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'

/**
* Mocks of HTTP calls for "wiby result" command positive flow
*/
const nock = require('nock')

nock.disableNetConnect()

nock('https://api.github.com')
// get package json
.post('/graphql')
.times(3)
.reply(200, {
data: {
repository: {
object: {
text: JSON.stringify({
dependencies: {
wiby: '*'
}
})
}
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/partial/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/pass/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/fail/branches/wiby-running-unit-tests')
.reply(404, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'fake_run', conclusion: 'success' },
{ status: 'done', name: 'fake_run_2', conclusion: 'success' }
]
})
.get('/repos/wiby-test/fail/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'fail_run', conclusion: 'success' },
{ status: 'done', name: 'fail_run_2', conclusion: 'success' }
]
})
.get('/repos/wiby-test/pass/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'pass_run', conclusion: 'success' },
{ status: 'done', name: 'pass_run_2', conclusion: 'success' }
]
})
.get('/repos/wiby-test/partial/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'partial_run', conclusion: 'success' },
{ status: 'done', name: 'partial_run_2', conclusion: 'success' }
]
})
2 changes: 2 additions & 0 deletions test/fixtures/http/result-command-positive-checks-failed.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ nock('https://api.github.com')
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
Expand Down
63 changes: 63 additions & 0 deletions test/fixtures/http/result-command-positive-pass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'

/**
* Mocks of HTTP calls for "wiby result" command positive flow
*/
const nock = require('nock')

nock.disableNetConnect()

nock('https://api.github.com')
// get package json
.post('/graphql')
.times(3)
.reply(200, {
data: {
repository: {
object: {
text: JSON.stringify({
dependencies: {
wiby: '*'
}
})
}
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/partial/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/pass/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/fail/branches/wiby-running-unit-tests')
.reply(200, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'fake_run', conclusion: 'success' },
{ status: 'done', name: 'fake_run_2', conclusion: 'success' }
]
})
.get('/repos/wiby-test/fail/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'fail_run', conclusion: 'success' },
{ status: 'done', name: 'fail_run_2', conclusion: 'success' }
]
})
.get('/repos/wiby-test/pass/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'pass_run', conclusion: 'success' },
{ status: 'done', name: 'pass_run_2', conclusion: 'success' }
]
})
.get('/repos/wiby-test/partial/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
check_runs: [
{ status: 'done', name: 'partial_run', conclusion: 'success' },
{ status: 'done', name: 'partial_run_2', conclusion: 'success' }
]
})
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ nock('https://api.github.com')
}
}
})
.get('/repos/wiby-test/fakeRepo/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/partial/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/pass/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/fail/branches/wiby-running-unit-tests')
.reply(200, {})
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
Expand Down
6 changes: 3 additions & 3 deletions test/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ tap.test('wiby.result()', async (tap) => {

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

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

tap.equal(output.status, 'pending')
tap.equal(output.status, 'success')
tap.equal(output.results[0].dependent, 'wiby-test/fakeRepo')
tap.equal(output.results[0].status, 'pending')
tap.equal(output.results[0].status, 'success')
tap.equal(output.results[0].runs.length, 2)
})

Expand Down

0 comments on commit 5da4f8c

Please sign in to comment.