-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e1beda
commit a2e5f4f
Showing
10 changed files
with
208 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const yaml = require('js-yaml') | ||
|
||
const { log } = require('./logger') | ||
const validateHelpers = require('./validate') | ||
|
||
function getToken(inputs, projectRoot) { | ||
const { args, envs } = inputs | ||
const options = [ | ||
[args.token, 'arguments'], | ||
[envs.CODECOV_TOKEN, 'environment variables'], | ||
[getTokenFromYaml(projectRoot), 'Codecov yaml config'], | ||
] | ||
|
||
for (const option of options) { | ||
if (option[0] && validateHelpers.validateToken(option[0])) { | ||
log(`-> Token set by ${option[1]}`) | ||
return option[0] | ||
} | ||
} | ||
|
||
return '' | ||
} | ||
|
||
function getTokenFromYaml(projectRoot) { | ||
const dirNames = [ | ||
'', | ||
'.github', | ||
'dev', | ||
] | ||
|
||
const yamlNames = [ | ||
'.codecov.yaml', | ||
'.codecov.yml', | ||
'codecov.yaml', | ||
'codecov.yml', | ||
] | ||
|
||
for (const dir of dirNames) { | ||
for (const name of yamlNames) { | ||
const filePath = path.join(projectRoot, dir, name); | ||
|
||
try { | ||
if (fs.existsSync(filePath)) { | ||
const fileContents = fs.readFileSync(filePath, { | ||
encoding: 'utf-8', | ||
}); | ||
const yamlConfig = yaml.load(fileContents); | ||
if ( | ||
yamlConfig['codecov_token'] && | ||
validateHelpers.validateToken(yamlConfig['codecov_token']) | ||
) { | ||
return yamlConfig['codecov_token'] | ||
} | ||
} | ||
} catch(err) { | ||
log(`Error searching for upload token in ${filePath}: ${err}`, { level: 'debug' }) | ||
} | ||
} | ||
} | ||
return '' | ||
} | ||
|
||
|
||
module.exports = { | ||
getToken, | ||
getTokenFromYaml, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
codecov_token: invalid token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
codecov_token: faketoken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
codecov_token: anotherfaketoken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const path = require('path') | ||
|
||
const fileHelpers = require('../../src/helpers/files') | ||
const tokenHelpers = require('../../src/helpers/token') | ||
|
||
describe('Get tokens', () => { | ||
const fixturesDir = path.join(fileHelpers.fetchGitRoot(), 'test/fixtures/yaml') | ||
console.log(fixturesDir) | ||
describe('From yaml', () => { | ||
it('Returns empty with no yaml file', () => { | ||
expect(tokenHelpers.getTokenFromYaml('.')).toBe('') | ||
}) | ||
|
||
it('Returns the correct token from file', () => { | ||
expect(tokenHelpers.getTokenFromYaml(fixturesDir)).toBe('faketoken') | ||
}) | ||
}) | ||
|
||
describe('From right source', () => { | ||
it('Returns from args', () => { | ||
const inputs = { | ||
args: { token: 'argtoken' }, | ||
envs: { CODECOV_TOKEN: 'envtoken' } | ||
} | ||
expect(tokenHelpers.getToken(inputs, fixturesDir)).toBe('argtoken') | ||
}) | ||
|
||
it('Returns from env', () => { | ||
const inputs = { | ||
args: {}, | ||
envs: { CODECOV_TOKEN: 'envtoken' } | ||
} | ||
expect(tokenHelpers.getToken(inputs, fixturesDir)).toBe('envtoken') | ||
}) | ||
|
||
it('Returns from env', () => { | ||
const inputs = { | ||
args: {}, | ||
envs: {} | ||
} | ||
expect(tokenHelpers.getToken(inputs, fixturesDir)).toBe('faketoken') | ||
}) | ||
|
||
it('Returns from no source', () => { | ||
const inputs = { | ||
args: {}, | ||
envs: {} | ||
} | ||
expect(tokenHelpers.getToken(inputs, '.')).toBe('') | ||
}) | ||
}) | ||
}) |