From c7d1eb38706243f8c3f68b170779024f196aa4b5 Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Mon, 10 May 2021 22:35:02 +1000 Subject: [PATCH] parent commit sha (#87) --- bin/codecov | 4 ++++ src/helpers/web.js | 36 ++++++++++++------------------------ src/index.js | 1 + test/helpers/web.test.js | 10 +++++----- test/index.test.js | 23 +++++++++++++++++++++++ 5 files changed, 45 insertions(+), 29 deletions(-) diff --git a/bin/codecov b/bin/codecov index 4bc475cf2..3fde59d65 100755 --- a/bin/codecov +++ b/bin/codecov @@ -31,6 +31,10 @@ var argv = require("yargs") // eslint-disable-line default: "", description: "Custom defined name of the upload. Visible in Codecov UI" }, + parent: { + alias: "N", + description: "The commit SHA of the parent for which you are uploading coverage. If not present, the parent will be determined using the API of your repository provider. When using the repository provider's API, the parent is determined via finding the closest ancestor to the commit." + }, pr: { alias: "P", description: "Specify the pull request number mannually" diff --git a/src/helpers/web.js b/src/helpers/web.js index fe6d5301e..b88d5fd36 100644 --- a/src/helpers/web.js +++ b/src/helpers/web.js @@ -8,6 +8,7 @@ function populateBuildParams (inputs, serviceParams) { serviceParams.flags = validateHelpers.validateFlags(args.flags) ? args.flags : '' + serviceParams.parent = args.parent || '' return serviceParams } @@ -67,31 +68,18 @@ async function uploadToCodecov (uploadURL, token, query, uploadFile, version) { } } + +function camelToSnake(str) { + return str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) + .map(s => s.toLowerCase()) + .join('_') +} + function generateQuery (queryParams) { - const query = ''.concat( - 'branch=', - queryParams.branch, - '&commit=', - queryParams.commit, - '&build=', - queryParams.build, - '&build_url=', - queryParams.buildURL, - '&name=', - queryParams.name, - '&tag=', - queryParams.tag, - '&slug=', - queryParams.slug, - '&service=', - queryParams.service, - '&flags=', - queryParams.flags, - '&pr=', - queryParams.pr, - '&job=', - queryParams.job - ) + const query = Object + .entries(queryParams) + .map(([key, value]) => `${camelToSnake(key)}=${value}`) + .join('&') return query } diff --git a/src/index.js b/src/index.js index 84356a045..b950dd3f3 100644 --- a/src/index.js +++ b/src/index.js @@ -23,6 +23,7 @@ function dryRun (uploadHost, token, query, uploadFile) { * @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.parent The commit SHA of the parent for which you are uploading coverage. * @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 diff --git a/test/helpers/web.test.js b/test/helpers/web.test.js index 800febe5d..9667949f7 100644 --- a/test/helpers/web.test.js +++ b/test/helpers/web.test.js @@ -78,15 +78,15 @@ describe('Web Helpers', function () { const queryParams = {} queryParams.branch = 'testBranch' queryParams.commit = 'commitSHA' - queryParams.buildURL = 'https://ci-providor.local/job/xyz' - queryParams.job = '6' - queryParams.flags = 'unit,uploader' - queryParams.slug = 'testOrg/testRepo' queryParams.build = '4' - queryParams.service = 'testingCI' + queryParams.buildURL = 'https://ci-providor.local/job/xyz' queryParams.name = 'testName' queryParams.tag = 'tagV1' + queryParams.slug = 'testOrg/testRepo' + queryParams.service = 'testingCI' + queryParams.flags = 'unit,uploader' queryParams.pr = '2' + queryParams.job = '6' expect(webHelper.generateQuery(queryParams)).toBe( 'branch=testBranch&commit=commitSHA&build=4&build_url=https://ci-providor.local/job/xyz&name=testName&tag=tagV1&slug=testOrg/testRepo&service=testingCI&flags=unit,uploader&pr=2&job=6' ) diff --git a/test/index.test.js b/test/index.test.js index 4225169e8..082e26757 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -46,4 +46,27 @@ describe('Uploader Core', function () { }) expect(result).toEqual({ status: 'success', resultURL: 'https://results.codecov.io' }) }, 30000) + + it('Can upload with parent sha', async function () { + process.env.CI = 'true' + process.env.CIRCLECI = 'true' + + const parent = '2x4bqz123abc' + + nock('https://codecov.io') + .post('/upload/v4') + .query(actualQueryObject => actualQueryObject.parent === parent) + .reply(200, 'https://results.codecov.io\nhttps://codecov.io') + + nock('https://codecov.io') + .put('/') + .reply(200, 'success') + + const result = await app.main({ + token: 'abcdefg', + url: 'https://codecov.io', + parent, + }) + expect(result).toEqual({ status: 'success', resultURL: 'https://results.codecov.io' }) + }, 30000) })