diff --git a/services/snyk/snyk-test-helpers.js b/services/snyk/snyk-test-helpers.js deleted file mode 100644 index 9f37e02ecdbe1..0000000000000 --- a/services/snyk/snyk-test-helpers.js +++ /dev/null @@ -1,6 +0,0 @@ -const zeroVulnerabilitiesSvg = - 'vulnerabilitiesvulnerabilities00' -const twoVulnerabilitiesSvg = - 'vulnerabilitiesvulnerabilities22' - -export { zeroVulnerabilitiesSvg, twoVulnerabilitiesSvg } diff --git a/services/snyk/snyk-vulnerability-base.js b/services/snyk/snyk-vulnerability-base.js deleted file mode 100644 index ee25d1dd449c6..0000000000000 --- a/services/snyk/snyk-vulnerability-base.js +++ /dev/null @@ -1,40 +0,0 @@ -import Joi from 'joi' -import { BaseSvgScrapingService } from '../index.js' - -const schema = Joi.object({ - message: Joi.alternatives() - .try(Joi.string().regex(/^\d*$/), Joi.equal('unknown')) - .required(), -}).required() - -export default class SnykVulnerabilityBase extends BaseSvgScrapingService { - static category = 'analysis' - - static defaultBadgeData = { - label: 'vulnerabilities', - } - - static render({ vulnerabilities }) { - let color = 'red' - if (vulnerabilities === '0') { - color = 'brightgreen' - } - return { - message: vulnerabilities, - color, - } - } - - async fetch({ url, searchParams, httpErrors }) { - const { message: vulnerabilities } = await this._requestSvg({ - url, - schema, - options: { - searchParams, - }, - httpErrors, - }) - - return { vulnerabilities } - } -} diff --git a/services/snyk/snyk-vulnerability-github.service.js b/services/snyk/snyk-vulnerability-github.service.js index 7611e4ffd4e6c..afcdbb99a7727 100644 --- a/services/snyk/snyk-vulnerability-github.service.js +++ b/services/snyk/snyk-vulnerability-github.service.js @@ -1,48 +1,11 @@ -import SynkVulnerabilityBase from './snyk-vulnerability-base.js' +import { deprecatedService } from '../index.js' -export default class SnykVulnerabilityGitHub extends SynkVulnerabilityBase { - static route = { +export default deprecatedService({ + category: 'analysis', + route: { base: 'snyk/vulnerabilities/github', - pattern: ':user/:repo/:manifestFilePath*', - } - - static examples = [ - { - title: 'Snyk Vulnerabilities for GitHub Repo', - pattern: ':user/:repo', - namedParams: { - user: 'badges', - repo: 'shields', - }, - staticPreview: this.render({ vulnerabilities: '0' }), - }, - { - title: 'Snyk Vulnerabilities for GitHub Repo (Specific Manifest)', - pattern: ':user/:repo/:manifestFilePath', - namedParams: { - user: 'badges', - repo: 'shields', - manifestFilePath: 'badge-maker/package.json', - }, - staticPreview: this.render({ vulnerabilities: '0' }), - documentation: `

- Provide the path to your target manifest file relative to the base of your repository. - Snyk does not support using a specific branch for this, so do not include "blob" nor a branch name. -

- `, - }, - ] - - async handle({ user, repo, manifestFilePath }) { - const url = `https://snyk.io/test/github/${user}/${repo}/badge.svg` - const searchParams = { targetFile: manifestFilePath } - const { vulnerabilities } = await this.fetch({ - url, - searchParams, - httpErrors: { - 404: 'repo or manifest not found', - }, - }) - return this.constructor.render({ vulnerabilities }) - } -} + pattern: ':various*', + }, + label: 'vulnerabilities', + dateAdded: new Date('2023-07-03'), +}) diff --git a/services/snyk/snyk-vulnerability-github.tester.js b/services/snyk/snyk-vulnerability-github.tester.js index b5fdc00d00306..a9ff28a0c71be 100644 --- a/services/snyk/snyk-vulnerability-github.tester.js +++ b/services/snyk/snyk-vulnerability-github.tester.js @@ -1,94 +1,18 @@ -import Joi from 'joi' -import { createServiceTester } from '../tester.js' -import { - twoVulnerabilitiesSvg, - zeroVulnerabilitiesSvg, -} from './snyk-test-helpers.js' -export const t = await createServiceTester() +import { ServiceTester } from '../tester.js' +export const t = new ServiceTester({ + id: 'SnykVulnerabilityGitHub', + title: 'SnykVulnerabilityGitHub', + pathPrefix: '/snyk/vulnerabilities/github', +}) -t.create('valid repo').get('/snyk/snyk.json').timeout(20000).expectBadge({ +t.create('repo').get('/snyk/snyk.json').expectBadge({ label: 'vulnerabilities', - message: Joi.number().required(), + message: 'no longer available', }) -t.create('non existent repo') - .get('/badges/not-real.json') - .timeout(20000) - .expectBadge({ - label: 'vulnerabilities', - message: 'repo or manifest not found', - }) - -t.create('valid target manifest path') +t.create('manifest path') .get('/snyk/snyk/test/fixtures/demo-os/package.json.json') - .timeout(20000) - .expectBadge({ - label: 'vulnerabilities', - message: Joi.number().required(), - }) - -t.create('invalid target manifest path') - .get('/badges/shields/badge-maker/requirements.txt.json') - .timeout(20000) - .expectBadge({ - label: 'vulnerabilities', - message: 'repo or manifest not found', - }) - -t.create('repo has no vulnerabilities') - .get('/badges/shields.json') - .intercept(nock => - nock('https://snyk.io/test/github/badges/shields') - .get('/badge.svg') - .reply(200, zeroVulnerabilitiesSvg) - ) - .expectBadge({ - label: 'vulnerabilities', - message: '0', - color: 'brightgreen', - }) - -t.create('repo has vulnerabilities') - .get('/badges/shields.json') - .intercept(nock => - nock('https://snyk.io/test/github/badges/shields') - .get('/badge.svg') - .reply(200, twoVulnerabilitiesSvg) - ) - .expectBadge({ - label: 'vulnerabilities', - message: '2', - color: 'red', - }) - -t.create('target manifest file has no vulnerabilities') - .get('/badges/shields/badge-maker/package.json.json') - .intercept(nock => - nock('https://snyk.io/test/github/badges/shields') - .get('/badge.svg') - .query({ - targetFile: 'badge-maker/package.json', - }) - .reply(200, zeroVulnerabilitiesSvg) - ) - .expectBadge({ - label: 'vulnerabilities', - message: '0', - color: 'brightgreen', - }) - -t.create('target manifest file has vulnerabilities') - .get('/badges/shields/badge-maker/package.json.json') - .intercept(nock => - nock('https://snyk.io/test/github/badges/shields') - .get('/badge.svg') - .query({ - targetFile: 'badge-maker/package.json', - }) - .reply(200, twoVulnerabilitiesSvg) - ) .expectBadge({ label: 'vulnerabilities', - message: '2', - color: 'red', + message: 'no longer available', }) diff --git a/services/snyk/snyk-vulnerability-npm.service.js b/services/snyk/snyk-vulnerability-npm.service.js index 5043608400e5d..303204126592f 100644 --- a/services/snyk/snyk-vulnerability-npm.service.js +++ b/services/snyk/snyk-vulnerability-npm.service.js @@ -1,60 +1,11 @@ -import { NotFound } from '../index.js' -import SynkVulnerabilityBase from './snyk-vulnerability-base.js' +import { deprecatedService } from '../index.js' -export default class SnykVulnerabilityNpm extends SynkVulnerabilityBase { - static route = { +export default deprecatedService({ + category: 'analysis', + route: { base: 'snyk/vulnerabilities/npm', - pattern: ':packageName(.+?)', - } - - static examples = [ - { - title: 'Snyk Vulnerabilities for npm package', - pattern: ':packageName', - namedParams: { - packageName: 'mocha', - }, - staticPreview: this.render({ vulnerabilities: '0' }), - }, - { - title: 'Snyk Vulnerabilities for npm package version', - pattern: ':packageName', - namedParams: { - packageName: 'mocha@4.0.0', - }, - staticPreview: this.render({ vulnerabilities: '1' }), - }, - { - title: 'Snyk Vulnerabilities for npm scoped package', - pattern: ':packageName', - namedParams: { - packageName: '@babel/core', - }, - staticPreview: this.render({ vulnerabilities: '0' }), - }, - ] - - async handle({ packageName }) { - const url = `https://snyk.io/test/npm/${packageName}/badge.svg` - - try { - const { vulnerabilities } = await this.fetch({ - url, - // Snyk returns an HTTP 200 with an HTML page when the specified - // npm package is not found that contains the text 404. - // Including this in case Snyk starts returning a 404 response code instead. - httpErrors: { - 404: 'npm package is invalid or does not exist', - }, - }) - return this.constructor.render({ vulnerabilities }) - } catch (e) { - // If the package is invalid/nonexistent Snyk will return an HTML page - // which will result in an InvalidResponse error being thrown by the valueFromSvgBadge() - // function. Catching it here to switch to a more contextualized error message. - throw new NotFound({ - prettyMessage: 'npm package is invalid or does not exist', - }) - } - } -} + pattern: ':various*', + }, + label: 'vulnerabilities', + dateAdded: new Date('2023-07-03'), +}) diff --git a/services/snyk/snyk-vulnerability-npm.tester.js b/services/snyk/snyk-vulnerability-npm.tester.js index 5df48e99502e1..5a44401b2e7f5 100644 --- a/services/snyk/snyk-vulnerability-npm.tester.js +++ b/services/snyk/snyk-vulnerability-npm.tester.js @@ -1,86 +1,20 @@ -import Joi from 'joi' -import { createServiceTester } from '../tester.js' -import { - twoVulnerabilitiesSvg, - zeroVulnerabilitiesSvg, -} from './snyk-test-helpers.js' -export const t = await createServiceTester() - -t.create('valid package latest version') - .get('/commander.json') - .timeout(20000) - .expectBadge({ - label: 'vulnerabilities', - message: Joi.number().required(), - }) - -t.create('valid scoped package latest version') - .get('/@babel/core.json') - .timeout(20000) - .expectBadge({ - label: 'vulnerabilities', - message: Joi.number().required(), - }) - -t.create('non existent package') - .get('/mochaabcdef.json') - .timeout(20000) - .expectBadge({ - label: 'vulnerabilities', - message: 'npm package is invalid or does not exist', - }) - -t.create('valid package specific version') - .get('/commander@2.20.0.json') - .timeout(20000) - .expectBadge({ - label: 'vulnerabilities', - message: Joi.number().required(), - }) - -t.create('non existent package version') - .get('/gh-badges@0.3.4.json') - .timeout(20000) - .expectBadge({ - label: 'vulnerabilities', - message: 'npm package is invalid or does not exist', - }) - -t.create('package has no vulnerabilities') - .get('/mocha.json') - .intercept(nock => - nock('https://snyk.io/test/npm/mocha') - .get('/badge.svg') - .reply(200, zeroVulnerabilitiesSvg) - ) - .expectBadge({ - label: 'vulnerabilities', - message: '0', - color: 'brightgreen', - }) - -t.create('package has vulnerabilities') - .get('/mocha.json') - .intercept(nock => - nock('https://snyk.io/test/npm/mocha') - .get('/badge.svg') - .reply(200, twoVulnerabilitiesSvg) - ) - .expectBadge({ - label: 'vulnerabilities', - message: '2', - color: 'red', - }) - -t.create('package not found') - .get('/not-mocha-fake-ish@13.0.0.json') - .intercept(nock => - nock('https://snyk.io/test/npm/not-mocha-fake-ish@13.0.0') - .get('/badge.svg') - .reply(200, 'foo') - ) - .expectBadge({ - label: 'vulnerabilities', - message: 'npm package is invalid or does not exist', - color: 'red', - }) +import { ServiceTester } from '../tester.js' +export const t = new ServiceTester({ + id: 'SnykVulnerabilityNpm', + title: 'SnykVulnerabilityNpm', + pathPrefix: '/snyk/vulnerabilities/npm', +}) +t.create('latest version').get('/commander.json').expectBadge({ + label: 'vulnerabilities', + message: 'no longer available', +}) + +t.create('scoped package latest version').get('/@babel/core.json').expectBadge({ + label: 'vulnerabilities', + message: 'no longer available', +}) + +t.create('package specific version').get('/commander@2.20.0.json').expectBadge({ + label: 'vulnerabilities', + message: 'no longer available', +})