From 37e922428bce9c7438111265acff21d329ba6ca3 Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Thu, 6 May 2021 00:55:30 +1000 Subject: [PATCH] Custom defined name of the upload (#75) --- bin/codecov | 5 +++++ package.json | 11 ++++++----- src/helpers/web.js | 17 ++++++++++++++++- src/index.js | 20 ++++++++++++++++++++ test/index.test.js | 30 +++++++++++++++++++++++++++++- test/test_helpers.js | 2 -- 6 files changed, 76 insertions(+), 9 deletions(-) diff --git a/bin/codecov b/bin/codecov index 1e005ac53..4bc475cf2 100755 --- a/bin/codecov +++ b/bin/codecov @@ -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" diff --git a/package.json b/package.json index d12d018c2..08d06e557 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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/**" @@ -71,7 +68,11 @@ }, "standard": { "globals": [ - "expect", "it", "describe", "beforeEach", "afterEach" + "expect", + "it", + "describe", + "beforeEach", + "afterEach" ] } } diff --git a/src/helpers/web.js b/src/helpers/web.js index 766eee327..fe6d5301e 100644 --- a/src/helpers/web.js +++ b/src/helpers/web.js @@ -3,7 +3,7 @@ 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 @@ -11,6 +11,12 @@ function populateBuildParams (inputs, serviceParams) { return serviceParams } +/** + * + * @param {string} uploadURL + * @param {Buffer} uploadFile + * @returns {Promise<{ status: string, resultURL: string }>} + */ async function uploadToCodecovPUT (uploadURL, uploadFile) { console.log('Uploading...') @@ -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} + */ async function uploadToCodecov (uploadURL, token, query, uploadFile, version) { try { const result = await superagent diff --git a/src/index.js b/src/index.js index 9bcb88a58..84356a045 100644 --- a/src/index.js +++ b/src/index.js @@ -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 { /* @@ -149,6 +168,7 @@ async function main (args) { gzippedFile ) console.log(result) + return result } } catch (error) { // Output any exceptions and exit diff --git a/test/index.test.js b/test/index.test.js index 2767c325f..4225169e8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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) }) @@ -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) }) diff --git a/test/test_helpers.js b/test/test_helpers.js index 4b630e2bf..40f3abdcc 100644 --- a/test/test_helpers.js +++ b/test/test_helpers.js @@ -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)