diff --git a/package-lock.json b/package-lock.json index c57a1e3..82a11d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30,7 +30,7 @@ "jest": "^25.1.0", "nock": "^13.0.2", "prettier": "^1.19.1", - "snyk": "^1.685.0", + "snyk": "^1.760.0", "ts-jest": "^25.1.0", "ts-node": "8.6.2", "tsc-watch": "^4.1.0", @@ -8219,9 +8219,9 @@ } }, "node_modules/snyk": { - "version": "1.733.0", - "resolved": "https://registry.npmjs.org/snyk/-/snyk-1.733.0.tgz", - "integrity": "sha512-Mi/wk9tw8ma4P2+2QwgzGDHcIG0Tfj0Wn7cliuUqd7CM8bg+Oryq3g4NcNK6mJZz0VaISF8MCIcIzbqV8v0JYg==", + "version": "1.760.0", + "resolved": "https://registry.npmjs.org/snyk/-/snyk-1.760.0.tgz", + "integrity": "sha512-pVH3AbY30zNerW+F+okULIRYVj0r8Ksvd8WW+beMddghOH31ZePpAWjK5F+D6d3Dy3WJ3n/JyAywxiTS+2d6kQ==", "bin": { "snyk": "bin/snyk" }, @@ -16258,9 +16258,9 @@ } }, "snyk": { - "version": "1.733.0", - "resolved": "https://registry.npmjs.org/snyk/-/snyk-1.733.0.tgz", - "integrity": "sha512-Mi/wk9tw8ma4P2+2QwgzGDHcIG0Tfj0Wn7cliuUqd7CM8bg+Oryq3g4NcNK6mJZz0VaISF8MCIcIzbqV8v0JYg==" + "version": "1.760.0", + "resolved": "https://registry.npmjs.org/snyk/-/snyk-1.760.0.tgz", + "integrity": "sha512-pVH3AbY30zNerW+F+okULIRYVj0r8Ksvd8WW+beMddghOH31ZePpAWjK5F+D6d3Dy3WJ3n/JyAywxiTS+2d6kQ==" }, "snyk-api-ts-client": { "version": "1.8.0", diff --git a/package.json b/package.json index f7028f4..ad5ea1b 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "jest": "^25.1.0", "nock": "^13.0.2", "prettier": "^1.19.1", - "snyk": "^1.685.0", + "snyk": "^1.760.0", "ts-jest": "^25.1.0", "ts-node": "8.6.2", "tsc-watch": "^4.1.0", diff --git a/src/lib/github/prComments.ts b/src/lib/github/prComments.ts index 67d4019..29685c4 100644 --- a/src/lib/github/prComments.ts +++ b/src/lib/github/prComments.ts @@ -152,7 +152,7 @@ export const createPrComment = async ( export const deletePrComment = async ( ghDetails: ghDetails -) : Promise => { +) : Promise => { const baseUrl = process.env.GH_API || 'https://api.github.com'; const commentUrl = `/repos/${ghDetails.orgName}/${ghDetails.repoName}/issues/${ghDetails.prNumber}/comments`; @@ -191,5 +191,5 @@ export const deletePrComment = async ( url); }); - return + return ghResponse.data as ghPrCommentsStatus } \ No newline at end of file diff --git a/src/lib/index.ts b/src/lib/index.ts index 60b21c2..5715f02 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -3,7 +3,7 @@ import 'source-map-support/register'; import { getDelta, SnykDeltaOutput } from 'snyk-delta'; import { sendCommitStatus } from './github/commitStatus'; import { createPrComment, deletePrComment } from './github/prComments'; -import { ghActivity, ghDetails } from './github/types'; +import { ghActivity, ghDetails, ghPrCommentsStatus } from './github/types'; import { snykProjectDetails } from './types'; import * as fs from 'fs'; import * as debugLib from 'debug'; @@ -120,17 +120,20 @@ const main = async () => { noBaseline, ); + let ghPrCommentsCreateResponse: ghPrCommentsStatus + const allProjectPost = (i > 0) ? true : false + if (ghPRNumber) { - if (snykDeltaResults.result === 0 && !keepHistory) { + if (snykDeltaResults.result === 0 && !keepHistory && !allProjectPost) { debug('Deleting comments on PR') - await deletePrComment(githubDetails) + ghPrCommentsCreateResponse = await deletePrComment(githubDetails) } } debug(`shouldCommentPr = ${shouldCommentPr}`) - const ghPrCommentsCreateResponse = shouldCommentPr + ghPrCommentsCreateResponse = shouldCommentPr ? await createPrComment(snykDeltaResults, githubDetails, keepHistory) : {}; diff --git a/test/lib/index.test.ts b/test/lib/index.test.ts index 1ea344e..46d3b2f 100644 --- a/test/lib/index.test.ts +++ b/test/lib/index.test.ts @@ -489,6 +489,87 @@ New Issues Introduced! test('[snyk-delta module] Is it working with --all-projects with PR number and mixed results', async () => { // 2 projects, 1 without new issue and 1 with a new issue so we can verify that one commit status fails while the other one passes + + process.env.SNYK_DEBUG = 'true'; + const logConsoleStream = fs.createWriteStream('./logConsoleFile.log', { + flags: 'a', + }); + process.stderr._write = function(chunk, encoding, callback) { + logConsoleStream.write(chunk, encoding, callback); + }; + + process.argv = [ + '', + '', + path.resolve(__dirname, '..') + + '/fixtures/snyktest-all-projects-with-one-more-vuln-for-one-project-only.json', + '123', + '123', + '123', + '123', + '123', + ]; + const response = await main(); + + delete process.env.SNYK_DEBUG; + process.stderr.unpipe; + + try { + const data = fs.readFileSync('./logConsoleFile.log', 'utf8'); + expect(data.includes('Deleting comments on PR')).toEqual(false); + } catch (err) { + console.error(err); + } + + expect(response).toEqual([ + { + status: { + context: 'Snyk Prevent (playground - package-lock.json)', + description: 'No new issue found', + state: 'success', + // eslint-disable-next-line + target_url: + 'https://app.snyk.io/org/playground/project/09235fa4-c241-42c6-8c63-c053bd272789', + }, + prComment: {}, + }, + { + status: { + context: 'Snyk Prevent (playground - subfolder/package-lock.json)', + description: 'New issue(s) found', + state: 'failure', + // eslint-disable-next-line + target_url: + 'https://app.snyk.io/org/playground/project/09235fa4-c241-42c6-8c63-c053bd272790', + }, + /* eslint-disable no-useless-escape */ + prComment: { + body: `### ******* Vulnerabilities report for commit number 123 ******* +New Issue Introduced! +## Security +1 issue found +* 1/1: Regular Expression Denial of Service (ReDoS) [High Severity] +\t+ Via: goof@0.0.3 => express-fileupload@0.0.5 => @snyk/nodejs-runtime-agent@1.14.0 => acorn@5.7.3 +\t+ Fixed in: acorn, 5.7.4, 6.4.1, 7.1.1 +\t+ Fixable by upgrade: @snyk/nodejs-runtime-agent@1.14.0=>acorn@5.7.4 +`, + }, + /* eslint-enable no-useless-escape */ + }, + ]); + }); + + test('[snyk-delta module] Is it working with --all-projects with PR number and mixed results and keepHistory', async () => { + // 2 projects, 1 without new issue and 1 with a new issue so we can verify that one commit status fails while the other one passes + + process.env.SNYK_DEBUG = 'true'; + const logConsoleStream = fs.createWriteStream('./logConsoleFile.log', { + flags: 'a', + }); + process.stderr._write = function(chunk, encoding, callback) { + logConsoleStream.write(chunk, encoding, callback); + }; + process.argv = [ '', '', @@ -499,8 +580,20 @@ New Issues Introduced! '123', '123', '123', + 'keepHistory', ]; const response = await main(); + + delete process.env.SNYK_DEBUG; + process.stderr.unpipe; + + try { + const data = fs.readFileSync('./logConsoleFile.log', 'utf8'); + expect(data.includes('Deleting comments on PR')).toEqual(false); + } catch (err) { + console.error(err); + } + expect(response).toEqual([ { status: {