Skip to content

Commit f3e9967

Browse files
authored
Ensure GitHub token (#161)
* feat: add ensureGithubToken utility to validate GitHub token presence * feat: add tests to validate GITHUB_TOKEN presence in commands
1 parent 3d4b742 commit f3e9967

File tree

13 files changed

+84
-0
lines changed

13 files changed

+84
-0
lines changed

lib/clean.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const context = require('./context')
44
const github = require('./github')
55
const gitURLParse = require('git-url-parse')
66
const logger = require('./logger')
7+
const { ensureGithubToken } = require('./utils')
78

89
// setup logger namespace
910
const cleanCommandNamespace = 'wiby:clean'
@@ -13,6 +14,8 @@ module.exports = async ({ dependents }, { all, dryRun }) => {
1314
// enable log output for clean command without DEBUG env
1415
logger.enableLogs(cleanCommandNamespace)
1516

17+
ensureGithubToken()
18+
1619
const parentPkgJSON = await context.getLocalPackageJSON()
1720
const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url)
1821
debug(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`)

lib/closePR.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ const github = require('../lib/github')
44
const logger = require('./logger')
55
const context = require('./context')
66
const gitURLParse = require('git-url-parse')
7+
const { ensureGithubToken } = require('./utils')
78

89
const debug = logger('wiby:closepr')
910

1011
module.exports = async ({ dependents }) => {
12+
ensureGithubToken()
13+
1114
const closedPrs = []
1215
for (const { repository: url, pullRequest } of dependents) {
1316
if (pullRequest) {

lib/result.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const context = require('./context')
44
const github = require('./github')
55
const gitURLParse = require('git-url-parse')
66
const logger = require('./logger')
7+
const { ensureGithubToken } = require('./utils')
78

89
const debug = logger('wiby:result')
910

@@ -27,6 +28,8 @@ module.exports = async function ({ dependents }) {
2728
const output = { status: pipelineStatusesEnum.PENDING, results: [] }
2829
const allDependentsChecks = []
2930

31+
ensureGithubToken()
32+
3033
debug(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`)
3134
for (const { repository: url, sha } of dependents) {
3235
const dependentPkgInfo = gitURLParse(url)

lib/test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const context = require('./context')
44
const github = require('./github')
55
const gitURLParse = require('git-url-parse')
66
const logger = require('./logger')
7+
const { ensureGithubToken } = require('./utils')
78

89
// setup logger namespace
910
const testCommandNamespace = 'wiby:test'
@@ -13,6 +14,7 @@ module.exports = async function ({ dependents }) {
1314
// enable log output for test command without DEBUG env
1415
logger.enableLogs(testCommandNamespace)
1516

17+
ensureGithubToken()
1618
const parentPkgJSON = await context.getLocalPackageJSON()
1719
const parentRepositoryInfo = gitURLParse(parentPkgJSON.repository.url)
1820
const parentBranchName = await context.getParentBranchName()

lib/utils/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const ensureGithubToken = () => {
4+
if (!process.env.GITHUB_TOKEN) {
5+
throw new Error('GITHUB_TOKEN is required')
6+
}
7+
}
8+
9+
module.exports = {
10+
ensureGithubToken
11+
}

test/clean.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const wiby = require('..')
1111

1212
tap.test('wiby.clean()', async (tap) => {
1313
tap.beforeEach(async () => {
14+
process.env.GITHUB_TOKEN = 'ghp_123_abc'
1415
nock.disableNetConnect()
1516
gitFixture.init()
1617
})

test/cli/clean.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,22 @@ const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures'))
1111

1212
tap.test('clean command', async (tap) => {
1313
tap.beforeEach(async () => {
14+
process.env.GITHUB_TOKEN = 'ghp_123_abc'
1415
gitFixture.init()
1516
})
1617

18+
tap.test('clean command should fail when github token is not set', async (tap) => {
19+
process.env.GITHUB_TOKEN = ''
20+
gitFixture.init()
21+
22+
try {
23+
childProcess.execSync(`${wibyCommand} clean`).toString()
24+
tap.fail()
25+
} catch (err) {
26+
tap.equal(true, err.message.includes('GITHUB_TOKEN is required'))
27+
}
28+
})
29+
1730
tap.test('should delete test branch in all configured test modules', async (tap) => {
1831
const result = childProcess.execSync(`${wibyCommand} clean`, {
1932
env: {

test/cli/closePR.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,23 @@ const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures'))
1010

1111
tap.test('closePRs command', async (t) => {
1212
t.beforeEach(async () => {
13+
process.env.GITHUB_TOKEN = 'ghp_123_abc'
1314
nock.disableNetConnect()
1415
gitFixture.init()
1516
})
1617

18+
tap.test('close-pr command should fail when github token is not set', async (tap) => {
19+
process.env.GITHUB_TOKEN = ''
20+
gitFixture.init()
21+
22+
try {
23+
childProcess.execSync(`${wibyCommand} close-pr`).toString()
24+
tap.fail()
25+
} catch (err) {
26+
tap.equal(true, err.message.includes('GITHUB_TOKEN is required'))
27+
}
28+
})
29+
1730
t.test('close-pr should fail if config and dependent provided', async (t) => {
1831
try {
1932
childProcess.execSync(`${wibyCommand} close-pr --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`)

test/cli/result.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,22 @@ const PENDING_RESULT_EXIT_CODE = 64
1616

1717
tap.test('result command', async (tap) => {
1818
tap.beforeEach(async () => {
19+
process.env.GITHUB_TOKEN = 'ghp_123_abc'
1920
gitFixture.init()
2021
})
2122

23+
tap.test('result command should fail when github token is not set', async (tap) => {
24+
process.env.GITHUB_TOKEN = ''
25+
gitFixture.init()
26+
27+
try {
28+
childProcess.execSync(`${wibyCommand} result`).toString()
29+
tap.fail()
30+
} catch (err) {
31+
tap.equal(true, err.message.includes('GITHUB_TOKEN is required'))
32+
}
33+
})
34+
2235
tap.test('result command should fail when config and dependent provided', async (tap) => {
2336
try {
2437
childProcess.execSync(`${wibyCommand} result --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`)

test/cli/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby')
1010
const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures'))
1111

1212
tap.test('test command', async (tap) => {
13+
tap.beforeEach(async () => {
14+
process.env.GITHUB_TOKEN = 'ghp_123_abc'
15+
})
16+
17+
tap.test('test command should fail when github token is not set', async (tap) => {
18+
process.env.GITHUB_TOKEN = ''
19+
gitFixture.init()
20+
21+
try {
22+
childProcess.execSync(`${wibyCommand} test`).toString()
23+
tap.fail()
24+
} catch (err) {
25+
tap.equal(true, err.message.includes('GITHUB_TOKEN is required'))
26+
}
27+
})
28+
1329
tap.test('test command should fail when config and dependent provided', async (tap) => {
1430
gitFixture.init()
1531

0 commit comments

Comments
 (0)