Skip to content

Commit e1b9d20

Browse files
#2 - Post results as an artifact for the submission
1 parent 6b879a0 commit e1b9d20

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"get-parameter-names": "^0.3.0",
3434
"http-status-codes": "^1.3.2",
3535
"joi": "^14.3.1",
36+
"jszip": "^3.2.2",
3637
"lodash": "^4.17.11",
3738
"superagent": "^3.8.3",
3839
"tc-core-library-js": "appirio-tech/tc-core-library-js.git",

src/common/helper.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,26 @@ const reqToSubmissionAPI = async (reqType, path, reqBody) => {
9393
}
9494
}
9595

96+
/**
97+
* Function to send request to V5 API with file
98+
* @param {Object} config Configuration object
99+
* @param (String) path Complete path of the API URL
100+
* @param {Object} formData multiple part form data
101+
* @param {String} the file field name in formData
102+
* @returns {Promise}
103+
*/
104+
const reqToV5APIWithFile = async (path, formData, fileFieldName) => {
105+
const token = await getM2Mtoken()
106+
return request
107+
.post(path)
108+
.set('Authorization', `Bearer ${token}`)
109+
.field(_.omit(formData, fileFieldName))
110+
.attach(fileFieldName, formData[fileFieldName].data, formData[fileFieldName].name)
111+
}
112+
96113
module.exports = {
97114
wrapExpress,
98115
autoWrapExpress,
99-
reqToSubmissionAPI
116+
reqToSubmissionAPI,
117+
reqToV5APIWithFile
100118
}

src/services/WebhookService.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const helper = require('../common/helper')
77
const logger = require('../common/logger')
88
const uuid = require('uuid/v4')
99
const config = require('config')
10+
const JSZip = require('jszip')
11+
12+
const resultFileName = 'SonarQubeResults'
13+
const typeId = 'c56a4180-65aa-42ec-a945-5fd21dec0501'
1014

1115
/**
1216
* Process the scan results received via Webhook from Sonarqube Server
@@ -21,9 +25,21 @@ const processScanResults = async (body) => {
2125
reviewerId: randomId,
2226
submissionId: body.project.key,
2327
scoreCardId: randomId,
24-
typeId: 'c56a4180-65aa-42ec-a945-5fd21dec0501' // Request will fail if we don't use existing review type id
28+
typeId // Request will fail if we don't use existing review type id
29+
}
30+
await helper.reqToSubmissionAPI('POST', `${config.SUBMISSION_API_URL}/reviews`, payload)
31+
32+
const zip = new JSZip()
33+
zip.file(`${resultFileName}.json`, JSON.stringify(body, null, 2))
34+
const content = await zip.generateAsync({ type: 'nodebuffer' })
35+
const artifactPayload = {
36+
artifact: {
37+
name: `${resultFileName}.zip`,
38+
data: content
39+
},
40+
typeId
2541
}
26-
helper.reqToSubmissionAPI('POST', `${config.SUBMISSION_API_URL}/reviews`, payload)
42+
await helper.reqToV5APIWithFile(`${config.SUBMISSION_API_URL}/submissions/${body.project.key}/artifacts`, artifactPayload, 'artifact')
2743
}
2844

2945
processScanResults.schema = {

test/prepare.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ const URL = require('url')
1212

1313
const authUrl = URL.parse(config.AUTH0_URL)
1414
const subApiUrl = URL.parse(`${config.SUBMISSION_API_URL}/reviews`)
15+
const artifactUrl = URL.parse(`${config.SUBMISSION_API_URL}/submissions/a34e1158-2c27-4d38-b079-5e5cca1bdcf7/artifacts`)
1516

1617
nock(/\//)
1718
.persist()
1819
.post(authUrl.path)
19-
.reply(200, { access_token: 'test' })
20+
.reply(200, { access_token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6WyJUb3Bjb2RlciBVc2VyIiwiQ29ubmVjdCBTdXBwb3J0IiwiYWRtaW5pc3RyYXRvciIsInRlc3RSb2xlIiwiYWFhIiwidG9ueV90ZXN0XzEiLCJDb25uZWN0IE1hbmFnZXIiLCJDb25uZWN0IEFkbWluIiwiY29waWxvdCIsIkNvbm5lY3QgQ29waWxvdCBNYW5hZ2VyIl0sImlzcyI6Imh0dHBzOi8vYXBpLnRvcGNvZGVyLWRldi5jb20iLCJoYW5kbGUiOiJUb255SiIsImV4cCI6MTU2NTY4MTkyMCwidXNlcklkIjoiODU0Nzg5OSIsImlhdCI6MTU1NTY4MTMyMCwiZW1haWwiOiJhamVmdHNAdG9wY29kZXIuY29tIiwianRpIjoiMTlhMDkzNzAtMjk4OC00N2I4LTkxODktMGRhODVjNjM0ZWQyIn0.V8nsQpbzQ_4iEd0dAbuYsfeydnhSAEQ95AKKwl8RONw' })
2021
.post(subApiUrl.path)
2122
.reply(200)
23+
.post(artifactUrl.path)
24+
.reply(200)

0 commit comments

Comments
 (0)