Skip to content

Commit

Permalink
Merge pull request #9 from snyk-tech-services/feat/link-to-projects-a…
Browse files Browse the repository at this point in the history
…nd-org

feat: link back to org & projects in Snyk
  • Loading branch information
lili2311 authored Oct 1, 2020
2 parents 61d90f2 + 304bea0 commit 89a04db
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/lib/generate-output/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -38,19 +39,22 @@ 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);
}

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
Expand Down
8 changes: 6 additions & 2 deletions src/lib/generate-output/html/templates/licenses-view.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@

<main class="u-padding--xl">
<h1>Licenses Attribution Report</h1>
<h2>Org ID: {{orgPublicId}}</h2>
<h2>Org: <a href="{{orgData.url}}">{{orgData.name}}</a></h2>
{{#each licenses}}
<div class="u-padding-top--sm">
<h1 class="license_title license_title--{{severity}}">
Expand Down Expand Up @@ -187,7 +187,11 @@
<div class="border-bottom u-padding--sm">
<strong>Projects in Snyk</strong>
</div>
<div class="u-padding--sm">{{#each projects}}{{id}}({{name}})<br>{{/each}}</div>
<div class="u-padding--sm">
{{#each projects}}
<a class="u-padding-top--sm" href="{{../../orgData.url}}/project/{{id}}">{{name}}</a><br>
{{/each}}
</div>
</div>
<div class="border-left">
<div class="border-bottom u-padding--sm">
Expand Down
33 changes: 33 additions & 0 deletions src/lib/get-org-data.ts
Original file line number Diff line number Diff line change
@@ -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<OrgData> {
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;
}
}
14 changes: 10 additions & 4 deletions test/lib/__snapshots__/generate-html-report.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ exports[`Generate HTML report License HTML Report is generated as expected 1`] =
<main class=\\"u-padding--xl\\">
<h1>Licenses Attribution Report</h1>
<h2>Org ID: f35105cf-1aff-449b-8456-259331391dac</h2>
<h2>Org: <a href=\\"https://app.snyk.io/org/snyk-licences-test-org-do-not-modify\\">Snyk Licences Test Org - do not modify</a></h2>
<div class=\\"u-padding-top--sm\\">
<h1 class=\\"license_title license_title--medium\\">
<a href=\\"https://spdx.org/licenses/BSD-2-Clause.html\\">BSD-2-Clause</a>
Expand Down Expand Up @@ -181,7 +181,9 @@ exports[`Generate HTML report License HTML Report is generated as expected 1`] =
<div class=\\"border-bottom u-padding--sm\\">
<strong>Projects in Snyk</strong>
</div>
<div class=\\"u-padding--sm\\">XXXX(snyk-fixtures/npm-with-single-dep:package.json)<br></div>
<div class=\\"u-padding--sm\\">
<a class=\\"u-padding-top--sm\\" href=\\"https://app.snyk.io/org/snyk-licences-test-org-do-not-modify/project/XXXX\\">snyk-fixtures/npm-with-single-dep:package.json</a><br>
</div>
</div>
<div class=\\"border-left\\">
<div class=\\"border-bottom u-padding--sm\\">
Expand Down Expand Up @@ -382,7 +384,9 @@ THIS SOFTWARE IS PROVIDED BY <<var;name=\\"copyrightHolderAsIs\\";original=\\"TH
<div class=\\"border-bottom u-padding--sm\\">
<strong>Projects in Snyk</strong>
</div>
<div class=\\"u-padding--sm\\">XXX(snyk-fixtures/tiny-monorepo:gradle/build.gradle)<br></div>
<div class=\\"u-padding--sm\\">
<a class=\\"u-padding-top--sm\\" href=\\"https://app.snyk.io/org/snyk-licences-test-org-do-not-modify/project/XXX\\">snyk-fixtures/tiny-monorepo:gradle/build.gradle</a><br>
</div>
</div>
<div class=\\"border-left\\">
<div class=\\"border-bottom u-padding--sm\\">
Expand Down Expand Up @@ -427,7 +431,9 @@ THIS SOFTWARE IS PROVIDED BY <<var;name=\\"copyrightHolderAsIs\\";original=\\"TH
<div class=\\"border-bottom u-padding--sm\\">
<strong>Projects in Snyk</strong>
</div>
<div class=\\"u-padding--sm\\">XXX(snyk-fixtures/npm-with-single-dep:package.json)<br></div>
<div class=\\"u-padding--sm\\">
<a class=\\"u-padding-top--sm\\" href=\\"https://app.snyk.io/org/snyk-licences-test-org-do-not-modify/project/XXX\\">snyk-fixtures/npm-with-single-dep:package.json</a><br>
</div>
</div>
<div class=\\"border-left\\">
<div class=\\"border-bottom u-padding--sm\\">
Expand Down

0 comments on commit 89a04db

Please sign in to comment.