From 304bea0acb018c128e848228070d19d877ee1302 Mon Sep 17 00:00:00 2001 From: ghe Date: Thu, 1 Oct 2020 13:23:50 +0100 Subject: [PATCH] feat: link back to org & projects in Snyk --- src/lib/generate-output/html/index.ts | 8 +++-- .../html/templates/licenses-view.hbs | 8 +++-- src/lib/get-org-data.ts | 33 +++++++++++++++++++ .../generate-html-report.test.ts.snap | 14 +++++--- 4 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 src/lib/get-org-data.ts diff --git a/src/lib/generate-output/html/index.ts b/src/lib/generate-output/html/index.ts index aeca073..b23f666 100644 --- a/src/lib/generate-output/html/index.ts +++ b/src/lib/generate-output/html/index.ts @@ -4,6 +4,7 @@ import * as fs from 'fs'; import * as debugLib from 'debug'; import { LicenseReportData } from '../../generate-org-license-report'; +import { getOrgData, OrgData } from '../../get-org-data'; const debug = debugLib('snyk-licenses:generateHtmlReport'); const DEFAULT_TEMPLATE = './templates/licenses-view.hbs'; @@ -38,7 +39,8 @@ export async function generateHtmlReport( const htmlTemplate = await compileTemplate(hbsTemplate); debug(`✅ Compiled template ${hbsTemplate}`); - const transformedData = transformDataFunc[view](orgPublicId, data); + const orgData = await getOrgData(orgPublicId); + const transformedData = transformDataFunc[view](orgPublicId, data, orgData); return htmlTemplate(transformedData); } @@ -46,11 +48,13 @@ export async function generateHtmlReport( function transformDataForLicenseView( orgPublicId: string, data: LicenseReportData, + orgData: OrgData, ): { licenses: LicenseReportData; orgPublicId: string; + orgData: OrgData; } { - return { licenses: data, orgPublicId }; + return { licenses: data, orgPublicId, orgData }; } // TODO: support later diff --git a/src/lib/generate-output/html/templates/licenses-view.hbs b/src/lib/generate-output/html/templates/licenses-view.hbs index 6011aed..9d25110 100644 --- a/src/lib/generate-output/html/templates/licenses-view.hbs +++ b/src/lib/generate-output/html/templates/licenses-view.hbs @@ -140,7 +140,7 @@

Licenses Attribution Report

-

Org ID: {{orgPublicId}}

+

Org: {{orgData.name}}

{{#each licenses}}

@@ -187,7 +187,11 @@
Projects in Snyk
-
{{#each projects}}{{id}}({{name}})
{{/each}}
+
+ {{#each projects}} + {{name}}
+ {{/each}} +

diff --git a/src/lib/get-org-data.ts b/src/lib/get-org-data.ts new file mode 100644 index 0000000..cea1faf --- /dev/null +++ b/src/lib/get-org-data.ts @@ -0,0 +1,33 @@ +import 'source-map-support/register'; +import * as debugLib from 'debug'; +import * as snykApiSdk from 'snyk-api-ts-client'; +import { getApiToken } from './get-api-token'; +import { string } from 'yargs'; +import _ = require('lodash'); + +const debug = debugLib('snyk-licenses:getLicenseDataForOrg'); + +export interface OrgData { + name: string; + id: string; + slug: string; + url: string; + group: { + name: string; + id: string; + }; +} + +export async function getOrgData(orgPublicId: string): Promise { + getApiToken(); + + try { + const snykApiClient = await new snykApiSdk.Orgs(); + const allOrgs: OrgData[] = _.get(await snykApiClient.get(), 'orgs', []); + const orgData = allOrgs.filter(org => org.id === orgPublicId)[0]; + return orgData; + } catch (e) { + debug('❌ Failed to get org data for org ID:' + orgPublicId); + throw e; + } +} diff --git a/test/lib/__snapshots__/generate-html-report.test.ts.snap b/test/lib/__snapshots__/generate-html-report.test.ts.snap index 742fe16..b5ffd4d 100644 --- a/test/lib/__snapshots__/generate-html-report.test.ts.snap +++ b/test/lib/__snapshots__/generate-html-report.test.ts.snap @@ -141,7 +141,7 @@ exports[`Generate HTML report License HTML Report is generated as expected 1`] =

Licenses Attribution Report

-

Org ID: f35105cf-1aff-449b-8456-259331391dac

+

Org: Snyk Licences Test Org - do not modify

BSD-2-Clause @@ -181,7 +181,9 @@ exports[`Generate HTML report License HTML Report is generated as expected 1`] =
Projects in Snyk
-
XXXX(snyk-fixtures/npm-with-single-dep:package.json)
+

@@ -382,7 +384,9 @@ THIS SOFTWARE IS PROVIDED BY < Projects in Snyk
-
XXX(snyk-fixtures/tiny-monorepo:gradle/build.gradle)
+
@@ -427,7 +431,9 @@ THIS SOFTWARE IS PROVIDED BY < Projects in Snyk
-
XXX(snyk-fixtures/npm-with-single-dep:package.json)
+