Skip to content

Commit

Permalink
Custom defined name of the upload (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiemoore authored May 5, 2021
1 parent dabad4d commit 37e9224
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 9 deletions.
5 changes: 5 additions & 0 deletions bin/codecov
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ var argv = require("yargs") // eslint-disable-line
default: "",
description: "Flag the upload to group coverage metrics"
},
name: {
alias: "n",
default: "",
description: "Custom defined name of the upload. Visible in Codecov UI"
},
pr: {
alias: "P",
description: "Specify the pull request number mannually"
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"superagent": "6.1.0"
},
"devDependencies": {
"expect": "26.6.2",
"jest": "26.6.3",
"nock": "13.0.11",
"pkg": "4.4.8",
Expand All @@ -48,15 +47,13 @@
"exclude": [
"src/ci_providers/provider_template.js",
"rollup.config.js",
"test/**/*",
"src/index.js"
"test/**/*"
]
},
"jest": {
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.js",
"!src/index.js",
"!src/ci_providers/provider_template.js",
"!**/node_modules/**",
"!**/vendor/**"
Expand All @@ -71,7 +68,11 @@
},
"standard": {
"globals": [
"expect", "it", "describe", "beforeEach", "afterEach"
"expect",
"it",
"describe",
"beforeEach",
"afterEach"
]
}
}
17 changes: 16 additions & 1 deletion src/helpers/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ const validateHelpers = require('./validate')

function populateBuildParams (inputs, serviceParams) {
const { args, envs } = inputs
serviceParams.name = envs.CODECOV_NAME || ''
serviceParams.name = args.name || envs.CODECOV_NAME || ''
serviceParams.tag = args.tag || ''
serviceParams.flags = validateHelpers.validateFlags(args.flags)
? args.flags
: ''
return serviceParams
}

/**
*
* @param {string} uploadURL
* @param {Buffer} uploadFile
* @returns {Promise<{ status: string, resultURL: string }>}
*/
async function uploadToCodecovPUT (uploadURL, uploadFile) {
console.log('Uploading...')

Expand All @@ -35,6 +41,15 @@ async function uploadToCodecovPUT (uploadURL, uploadFile) {
}
}

/**
*
* @param {string} uploadURL The upload url
* @param {string} token Covecov token
* @param {string} query Query parameters
* @param {Buffer} uploadFile Coverage file to upload
* @param {string} version uploader version number
* @returns {Promise<string>}
*/
async function uploadToCodecov (uploadURL, token, query, uploadFile, version) {
try {
const result = await superagent
Expand Down
20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ function dryRun (uploadHost, token, query, uploadFile) {
process.exit()
}

/**
*
* @param {Object} args
* @param {string} args.build Specify the build number manually
* @param {string} args.branch Specify the branch manually
* @param {string} args.sha Specify the commit SHA mannually
* @param {string} args.file Target file(s) to upload
* @param {string} args.flags Flag the upload to group coverage metrics
* @param {string} args.name Custom defined name of the upload. Visible in Codecov UI
* @param {string} args.pr Specify the pull request number mannually
* @param {string} args.token Codecov upload token
* @param {string} args.tag Specify the git tag
* @param {boolean} args.verbose Run with verbose logging
* @param {string} args.rootDir Specify the project root directory when not in a git repo
* @param {boolean} args.nonZero Should errors exit with a non-zero (default: false)
* @param {boolean} args.dryRun Don't upload files to Codecov
* @param {string} args.slug Specify the slug manually (Enterprise use)
* @param {string} args.url Change the upload host (Enterprise use)
*/
async function main (args) {
try {
/*
Expand Down Expand Up @@ -149,6 +168,7 @@ async function main (args) {
gzippedFile
)
console.log(result)
return result
}
} catch (error) {
// Output any exceptions and exit
Expand Down
30 changes: 29 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const app = require('../src')

const { version } = require('../package.json')
const nock = require('nock')

describe('Uploader Core', function () {
const env = process.env

afterEach(() => {
process.env = env
})

it('Can return version', function () {
expect(app.getVersion()).toBe(version)
})
Expand All @@ -16,6 +23,27 @@ describe('Uploader Core', function () {
| |___| (_) | (_| | __/ (_| (_) \\ V /
\\_____\\___/ \\__,_|\\___|\\___\\___/ \\_/
Codecov report uploader 0.1.0`)
Codecov report uploader ${version}`)
})

it('Can upload with custom name', async function () {
process.env.CI = 'true'
process.env.CIRCLECI = 'true'

nock('https://codecov.io')
.post('/upload/v4')
.query(actualQueryObject => actualQueryObject.name === 'customname')
.reply(200, 'https://results.codecov.io\nhttps://codecov.io')

nock('https://codecov.io')
.put('/')
.reply(200, 'success')

const result = await app.main({
name: 'customname',
token: 'abcdefg',
url: 'https://codecov.io'
})
expect(result).toEqual({ status: 'success', resultURL: 'https://results.codecov.io' })
}, 30000)
})
2 changes: 0 additions & 2 deletions test/test_helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const td = require('testdouble')
// eslint-disable-next-line no-unused-vars
const expect = require('expect')

// eslint-disable-next-line no-undef
require('testdouble-jest')(td, jest)

0 comments on commit 37e9224

Please sign in to comment.