Skip to content

Commit

Permalink
chore: SP-1355 Enhance Copyleft Output in License Details Table
Browse files Browse the repository at this point in the history
  • Loading branch information
agustingroh committed Aug 20, 2024
1 parent 10db5a0 commit afed636
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
26 changes: 20 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as outputs from './app.output';

import { scanService, uploadResults } from './services/scan.service';
import { policyManager } from './policies/policy.manager';
import { licenseUtil } from './utils/license.utils';

/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
Expand Down
7 changes: 3 additions & 4 deletions src/policies/copyleft-policy-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import { CHECK_NAME } from '../app.config';
import { PolicyCheck } from './policy-check';
import { Component, getComponents } from '../services/result.service';
import { generateTable } from '../utils/markdown.utils';
import * as core from '@actions/core';
import { context } from '@actions/github';
import { licenseUtil } from '../utils/license.utils';

/**
Expand Down Expand Up @@ -103,11 +101,12 @@ export class CopyleftPolicyCheck extends PolicyCheck {
if (components.length === 0) return undefined;

const headers = ['Component', 'Version', 'License', 'URL', 'Copyleft'];
const centeredColumns = [4];
const rows: string[][] = [];

components.forEach(component => {
component.licenses.forEach(license => {
const copyleftIcon = licenseUtil.isCopyLeft(license.spdxid?.trim().toLowerCase()) ? ':x:' : ' ';
const copyleftIcon = licenseUtil.isCopyLeft(license.spdxid?.trim().toLowerCase()) ? 'YES' : 'NO';
rows.push([
component.purl,
component.version,
Expand All @@ -117,7 +116,7 @@ export class CopyleftPolicyCheck extends PolicyCheck {
]);
});
});
return `### Copyleft licenses \n ${generateTable(headers, rows)}`;
return `### Copyleft licenses \n ${generateTable(headers, rows, centeredColumns)}`;
}

artifactPolicyFileName(): string {
Expand Down
5 changes: 3 additions & 2 deletions src/services/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ export async function generateJobSummary(scannerResults: ScannerResults, policie

const LicensesTable = (items: License[]): string => {
const HEADERS: string[] = ['License', 'Copyleft', 'URL'];
const centeredColumns = [1];
const ROWS: string[][] = [];

items.forEach(l => {
const copyleftIcon = l.copyleft ? ':x:' : ' ';
const copyleftIcon = l.copyleft ? 'YES' : 'NO';
ROWS.push([l.spdxid, copyleftIcon, `${licenseUtil.getOSADL(l?.spdxid) || ''}`]);
});
return generateTable(HEADERS, ROWS);
return generateTable(HEADERS, ROWS, centeredColumns);
};

const PoliciesTable = (items: PolicyCheck[]): string => {
Expand Down
16 changes: 14 additions & 2 deletions src/utils/markdown.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,24 @@
THE SOFTWARE.
*/

export const generateTable = (headers: string[], rows: string[][]): string => {
export const generateTable = (headers: string[], rows: string[][], centeredColumns?: number[]): string => {
const COL_SEP = ' | ';
const centeredColumnMapper: Set<number> = new Set<number>();
if (centeredColumns) centeredColumns.forEach(c => centeredColumnMapper.add(c));

const rowSeparator =
COL_SEP +
headers
.map((header, index) => {
if (!centeredColumns) return '-';
return centeredColumnMapper.has(index) ? ':-:' : '-';
})
.join(COL_SEP) +
COL_SEP;

return `
${COL_SEP} ${headers.join(COL_SEP)} ${COL_SEP}
${COL_SEP + new Array(headers.length).fill('-').join(COL_SEP) + COL_SEP}
${rowSeparator}
${rows.map(row => COL_SEP + row.join(COL_SEP) + COL_SEP).join('\n')}
`;
};

0 comments on commit afed636

Please sign in to comment.