Skip to content

Commit

Permalink
feat: Allow for token from yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrockhu committed Jul 21, 2021
1 parent 3e1beda commit a2e5f4f
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 26 deletions.
79 changes: 66 additions & 13 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"homepage": "https://github.com/codecov/uploader#readme",
"dependencies": {
"glob": "7.1.7",
"js-yaml": "4.1.0",
"line-reader": "0.4.0",
"superagent": "6.1.0",
"typescript": "4.3.5",
Expand Down
15 changes: 8 additions & 7 deletions src/helpers/files.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @ts-check
const childProcess = require('child_process')
const fs = require('fs')
const path = require('path').posix
const glob = require('glob')
const path = require('path').posix

const { log } = require('./logger')

/**
Expand Down Expand Up @@ -337,16 +338,16 @@ function removeFile(projectRoot, filePath) {
}

module.exports = {
readCoverageFile,
getFileListing,
coverageFilePatterns,
endEnvironmentMarker,
endFileMarker,
endNetworkMarker,
endEnvironmentMarker,
fileHeader,
fetchGitRoot,
parseGitIgnore,
fileHeader,
getCoverageFiles,
coverageFilePatterns,
getFileListing,
getFilePath,
parseGitIgnore,
readCoverageFile,
removeFile,
}
69 changes: 69 additions & 0 deletions src/helpers/token.js
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,
}
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const zlib = require('zlib')
const { version } = require('../package.json')
const fileHelpers = require('./helpers/files')
const validateHelpers = require('./helpers/validate')
const tokenHelpers = require('./helpers/token')
const webHelpers = require('./helpers/web')
const { log } = require('./helpers/logger')
const providers = require('./ci_providers')
Expand Down Expand Up @@ -71,7 +72,6 @@ async function main(args) {
? args.url
: 'https://codecov.io'

const token = validateHelpers.getToken(args)
log(generateHeader(getVersion()))

// == Step 2: detect if we are in a git repo
Expand All @@ -84,7 +84,10 @@ async function main(args) {

log(`=> Project root located at: ${projectRoot}`)

// == Step 3: get network
// == Step 3: sanitize and set token
const token = await tokenHelpers.getToken(inputs, projectRoot)

// == Step 4: get network
let uploadFile = ''

if (!args.feature || args.feature.split(',').includes('network') === false) {
Expand All @@ -101,7 +104,7 @@ async function main(args) {
.concat(fileHelpers.endNetworkMarker())
}

// == Step 4: select coverage files (search or specify)
// == Step 5: select coverage files (search or specify)

// Look for files
let coverageFilePaths = []
Expand Down Expand Up @@ -136,7 +139,7 @@ async function main(args) {
}
log('End of network processing', { level: 'debug', args })

// == Step 5: generate upload file
// == Step 6: generate upload file
// TODO: capture envs

// Get coverage report contents
Expand Down Expand Up @@ -181,7 +184,7 @@ async function main(args) {

const gzippedFile = zlib.gzipSync(uploadFile)

// == Step 6: determine CI provider
// == Step 7: determine CI provider

// Determine CI provider
let serviceParams
Expand All @@ -197,7 +200,7 @@ async function main(args) {
throw new Error('Unable to detect service, please specify manually.')
}

// == Step 7: either upload or dry-run
// == Step 8: either upload or dry-run

const query = webHelpers.generateQuery(
webHelpers.populateBuildParams(inputs, serviceParams),
Expand Down
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/yaml/.codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
codecov_token: invalid token
1 change: 1 addition & 0 deletions test/fixtures/yaml/codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
codecov_token: faketoken
1 change: 1 addition & 0 deletions test/fixtures/yaml/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
codecov_token: anotherfaketoken
52 changes: 52 additions & 0 deletions test/helpers/token.test.js
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('')
})
})
})

0 comments on commit a2e5f4f

Please sign in to comment.