Skip to content

Commit 027620c

Browse files
Merge pull request #317 from codecov/fix-issue-316
fix: url parsing from post results
2 parents cc79a3a + 4fb31e3 commit 027620c

File tree

2 files changed

+67
-19
lines changed

2 files changed

+67
-19
lines changed

src/helpers/web.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { snakeCase } from "snake-case";
1+
import { snakeCase } from 'snake-case'
22
import superagent from 'superagent'
33

44
import { version } from '../../package.json'
5-
import { IServiceParams, UploaderInputs } from '../types'
6-
import { info, logError } from './logger'
5+
import { IServiceParams, UploaderArgs, UploaderInputs } from '../types'
6+
import { info, logError, verbose } from './logger'
77
import * as validateHelpers from './validate'
88
import { checkValueType } from './validate'
99

@@ -50,9 +50,7 @@ export async function uploadToCodecovPUT(
5050
): Promise<{ status: string; resultURL: string }> {
5151
info('Uploading...')
5252

53-
const parts = uploadURL.split('\n')
54-
const putURL = parts[1]
55-
const codecovResultURL = parts[0]
53+
const { putURL, resultURL } = parsePOSTResults(uploadURL)
5654

5755
try {
5856
const result = await superagent
@@ -63,7 +61,7 @@ export async function uploadToCodecovPUT(
6361
.set('Content-Encoding', 'gzip')
6462

6563
if (result.status === 200) {
66-
return { status: 'success', resultURL: codecovResultURL }
64+
return { status: 'success', resultURL }
6765
}
6866
throw new Error(`${result.status}, ${result.body}`)
6967
} catch (error) {
@@ -120,3 +118,26 @@ export function generateQuery(queryParams: IServiceParams): string {
120118
.map(([key, value]) => `${snakeCase(key)}=${value}`)
121119
.join('&')
122120
}
121+
122+
export function parsePOSTResults(uploadURL: string): {
123+
putURL: string
124+
resultURL: string
125+
} {
126+
// JS for [[:graph:]] https://www.regular-expressions.info/posixbrackets.html
127+
const re = /([\x21-\x7E]+)[\r\n]?/gm
128+
129+
const matches = uploadURL.match(re)
130+
131+
if (matches === null) {
132+
throw new Error(`Parsing results from POST failed: (${uploadURL})`)
133+
}
134+
135+
if (matches?.length !== 2) {
136+
throw new Error(`Incorrect number of urls when parsing results from POST: ${matches.length}`)
137+
}
138+
139+
const putURL = matches[1]
140+
const resultURL = matches[0].trimEnd() // This match may have trailing 0x0A and 0x0D that must be trimmed
141+
142+
return { putURL, resultURL }
143+
}

test/helpers/web.test.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import nock from 'nock'
22

33
import { version } from '../../package.json'
4-
import * as webHelper from '../../src/helpers/web'
4+
import { generateQuery, getPackage, parsePOSTResults, populateBuildParams, uploadToCodecov, uploadToCodecovPUT } from '../../src/helpers/web'
55
import { IServiceParams } from '../../src/types'
66

77
describe('Web Helpers', () => {
@@ -33,7 +33,7 @@ describe('Web Helpers', () => {
3333
.query(true)
3434
.reply(200, 'testPOSTHTTP')
3535

36-
const response = await webHelper.uploadToCodecov(
36+
const response = await uploadToCodecov(
3737
uploadURL,
3838
token,
3939
query,
@@ -56,7 +56,7 @@ describe('Web Helpers', () => {
5656
.query(true)
5757
.reply(200, 'testPOSTHTTPS')
5858

59-
const response = await webHelper.uploadToCodecov(
59+
const response = await uploadToCodecov(
6060
uploadURL,
6161
token,
6262
query,
@@ -68,9 +68,10 @@ describe('Web Helpers', () => {
6868

6969
it('Can PUT to the storage endpoint', async () => {
7070
jest.spyOn(console, 'log').mockImplementation(() => {})
71-
uploadURL = 'https://results.codecov.io\nhttps://codecov.io'
72-
const response = await webHelper.uploadToCodecovPUT(uploadURL, uploadFile)
73-
expect(response.resultURL).toBe('https://results.codecov.io')
71+
uploadURL = `https://results.codecov.io
72+
https://codecov.io`
73+
const response = await uploadToCodecovPUT(uploadURL, uploadFile)
74+
expect(response.resultURL).toEqual('https://results.codecov.io')
7475
})
7576

7677
it('Can generate query URL', () => {
@@ -87,13 +88,13 @@ describe('Web Helpers', () => {
8788
pr: 2,
8889
job: '6',
8990
}
90-
expect(webHelper.generateQuery(queryParams)).toBe(
91+
expect(generateQuery(queryParams)).toBe(
9192
'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',
9293
)
9394
})
9495

9596
it('can populateBuildParams() from args', () => {
96-
const result = webHelper.populateBuildParams(
97+
const result = populateBuildParams(
9798
{ args: { flags: 'testFlag', tag: 'testTag' }, environment: {} },
9899
{
99100
name: '',
@@ -113,7 +114,7 @@ describe('Web Helpers', () => {
113114
})
114115

115116
it('can populateBuildParams() from args with multiple flags as string', () => {
116-
const result = webHelper.populateBuildParams(
117+
const result = populateBuildParams(
117118
{ args: { flags: 'testFlag1,testFlag2', tag: 'testTag' }, environment: {} },
118119
{
119120
name: '',
@@ -133,7 +134,7 @@ describe('Web Helpers', () => {
133134
})
134135

135136
it('can populateBuildParams() from args with multiple flags as list', () => {
136-
const result = webHelper.populateBuildParams(
137+
const result = populateBuildParams(
137138
{ args: { flags: ['testFlag1', 'testFlag2'], tag: 'testTag' }, environment: {} },
138139
{
139140
name: '',
@@ -153,12 +154,38 @@ describe('Web Helpers', () => {
153154
})
154155

155156
it('can getPackage() from source', () => {
156-
const result = webHelper.getPackage('github-actions-2.0.0')
157+
const result = getPackage('github-actions-2.0.0')
157158
expect(result).toBe(`github-actions-2.0.0-uploader-${version}`)
158159
})
159160

160161
it('can getPackage() from no source', () => {
161-
const result = webHelper.getPackage('')
162+
const result = getPackage('')
162163
expect(result).toBe(`uploader-${version}`)
163164
})
165+
166+
describe('parsePOSTResults()', () => {
167+
it('will throw when unable to match', () => {
168+
const testURL = `🤨`
169+
expect(() => parsePOSTResults(testURL)).toThrowError(/Parsing results from POST failed/)
170+
})
171+
172+
it('will throw when can not match exactly twice', () => {
173+
const testURL = `dummyURL`
174+
expect(() => parsePOSTResults(testURL)).toThrowError('Incorrect number of urls when parsing results from POST: 1')
175+
})
176+
177+
it('will return an object when parsing correctly and input has multiple linebreaks', () => {
178+
const testURL = `dummyURL
179+
180+
181+
182+
183+
184+
185+
186+
OtherURL`
187+
const expectedResults = { putURL: 'OtherURL', resultURL: 'dummyURL'}
188+
expect(parsePOSTResults(testURL)).toEqual(expectedResults)
189+
})
190+
})
164191
})

0 commit comments

Comments
 (0)