Skip to content

Commit c7d1eb3

Browse files
authored
parent commit sha (#87)
1 parent c92854b commit c7d1eb3

File tree

5 files changed

+45
-29
lines changed

5 files changed

+45
-29
lines changed

bin/codecov

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ var argv = require("yargs") // eslint-disable-line
3131
default: "",
3232
description: "Custom defined name of the upload. Visible in Codecov UI"
3333
},
34+
parent: {
35+
alias: "N",
36+
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."
37+
},
3438
pr: {
3539
alias: "P",
3640
description: "Specify the pull request number mannually"

src/helpers/web.js

+12-24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function populateBuildParams (inputs, serviceParams) {
88
serviceParams.flags = validateHelpers.validateFlags(args.flags)
99
? args.flags
1010
: ''
11+
serviceParams.parent = args.parent || ''
1112
return serviceParams
1213
}
1314

@@ -67,31 +68,18 @@ async function uploadToCodecov (uploadURL, token, query, uploadFile, version) {
6768
}
6869
}
6970

71+
72+
function camelToSnake(str) {
73+
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)
74+
.map(s => s.toLowerCase())
75+
.join('_')
76+
}
77+
7078
function generateQuery (queryParams) {
71-
const query = ''.concat(
72-
'branch=',
73-
queryParams.branch,
74-
'&commit=',
75-
queryParams.commit,
76-
'&build=',
77-
queryParams.build,
78-
'&build_url=',
79-
queryParams.buildURL,
80-
'&name=',
81-
queryParams.name,
82-
'&tag=',
83-
queryParams.tag,
84-
'&slug=',
85-
queryParams.slug,
86-
'&service=',
87-
queryParams.service,
88-
'&flags=',
89-
queryParams.flags,
90-
'&pr=',
91-
queryParams.pr,
92-
'&job=',
93-
queryParams.job
94-
)
79+
const query = Object
80+
.entries(queryParams)
81+
.map(([key, value]) => `${camelToSnake(key)}=${value}`)
82+
.join('&')
9583
return query
9684
}
9785

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function dryRun (uploadHost, token, query, uploadFile) {
2323
* @param {string} args.file Target file(s) to upload
2424
* @param {string} args.flags Flag the upload to group coverage metrics
2525
* @param {string} args.name Custom defined name of the upload. Visible in Codecov UI
26+
* @param {string} args.parent The commit SHA of the parent for which you are uploading coverage.
2627
* @param {string} args.pr Specify the pull request number mannually
2728
* @param {string} args.token Codecov upload token
2829
* @param {string} args.tag Specify the git tag

test/helpers/web.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ describe('Web Helpers', function () {
7878
const queryParams = {}
7979
queryParams.branch = 'testBranch'
8080
queryParams.commit = 'commitSHA'
81-
queryParams.buildURL = 'https://ci-providor.local/job/xyz'
82-
queryParams.job = '6'
83-
queryParams.flags = 'unit,uploader'
84-
queryParams.slug = 'testOrg/testRepo'
8581
queryParams.build = '4'
86-
queryParams.service = 'testingCI'
82+
queryParams.buildURL = 'https://ci-providor.local/job/xyz'
8783
queryParams.name = 'testName'
8884
queryParams.tag = 'tagV1'
85+
queryParams.slug = 'testOrg/testRepo'
86+
queryParams.service = 'testingCI'
87+
queryParams.flags = 'unit,uploader'
8988
queryParams.pr = '2'
89+
queryParams.job = '6'
9090
expect(webHelper.generateQuery(queryParams)).toBe(
9191
'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'
9292
)

test/index.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,27 @@ describe('Uploader Core', function () {
4646
})
4747
expect(result).toEqual({ status: 'success', resultURL: 'https://results.codecov.io' })
4848
}, 30000)
49+
50+
it('Can upload with parent sha', async function () {
51+
process.env.CI = 'true'
52+
process.env.CIRCLECI = 'true'
53+
54+
const parent = '2x4bqz123abc'
55+
56+
nock('https://codecov.io')
57+
.post('/upload/v4')
58+
.query(actualQueryObject => actualQueryObject.parent === parent)
59+
.reply(200, 'https://results.codecov.io\nhttps://codecov.io')
60+
61+
nock('https://codecov.io')
62+
.put('/')
63+
.reply(200, 'success')
64+
65+
const result = await app.main({
66+
token: 'abcdefg',
67+
url: 'https://codecov.io',
68+
parent,
69+
})
70+
expect(result).toEqual({ status: 'success', resultURL: 'https://results.codecov.io' })
71+
}, 30000)
4972
})

0 commit comments

Comments
 (0)