Skip to content

Commit

Permalink
SCP-64 clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
francostramana committed Jan 24, 2024
1 parent cd877f5 commit d4b22d3
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ rules:
'@typescript-eslint/no-require-imports': 'error',
'@typescript-eslint/no-unnecessary-qualifier': 'error',
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-useless-constructor': 'error',
'@typescript-eslint/no-var-requires': 'error',
'@typescript-eslint/prefer-for-of': 'warn',
Expand Down
51 changes: 51 additions & 0 deletions __tests__/report.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ScannerResults } from '../src/services/result.interfaces';

Check failure on line 1 in __tests__/report.service.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Filename 'report.service.test.ts' does not match the naming convention

Check failure on line 1 in __tests__/report.service.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Filename 'report.service.test.ts' does not match the naming convention
import { getLicenses, Licenses } from '../src/services/result.service';
import { resultsMock } from './results.mock';

const licenseTableTest = [
{
...resultsMock[0],
licenses: [{ spdxid: 'MIT', url: null, copyleft: null }]
},
{
...resultsMock[1],
licenses: [
{ spdxid: 'MIT', url: null, copyleft: null },
{ spdxid: 'Apache-2.0', url: null, copyleft: null },
{ spdxid: '0BSD', url: null, copyleft: null }
]
},
{
...resultsMock[2],
licenses: [
{ spdxid: 'GPL-2.0-only', url: 'https://spdx.org/licenses/GPL-2.0-only.html', copyleft: true },
{ spdxid: 'GPL-2.0-or-later', url: 'https://spdx.org/licenses/GPL-2.0-or-later.html', copyleft: true },
{ spdxid: 'JSON', url: 'https://spdx.org/licenses/JSON.html', copyleft: null },
{ spdxid: 'LicenseRef-scancode-unknown-license-reference', url: null, copyleft: null }
]
},
{
...resultsMock[3],
licenses: [
{ spdxid: 'MIT', url: null, copyleft: null },
{ spdxid: 'GPL-2.0-only', url: 'https://spdx.org/licenses/GPL-2.0-only.html', copyleft: true },
{ spdxid: 'JSON', url: 'https://spdx.org/licenses/JSON.html', copyleft: null },
{ spdxid: 'LicenseRef-scancode-unknown-license-reference', url: null, copyleft: null },
{ spdxid: 'GPL-2.0-or-later', url: 'https://spdx.org/licenses/GPL-2.0-or-later.html', copyleft: true },
{ spdxid: 'Apache-2.0', url: null, copyleft: null },
{ spdxid: '0BSD', url: null, copyleft: null }
]
}
];

describe('Test Results service', () => {
for (const t of licenseTableTest) {
it(`${t.name}`, () => {
const scannerResults = JSON.parse(t.content) as ScannerResults;
const licenses = getLicenses(scannerResults);

const sortFn = (a: Licenses, b: Licenses): number => a.spdxid.localeCompare(b.spdxid);
expect(licenses.sort(sortFn)).toEqual(t.licenses.sort(sortFn));
});
}
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ScannerResults } from './result.interfaces';
import { getLicenses, Licenses } from './result.service';
import { ScannerResults } from '../src/services/result.interfaces';

Check failure on line 1 in __tests__/result.service.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Filename 'result.service.test.ts' does not match the naming convention

Check failure on line 1 in __tests__/result.service.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Filename 'result.service.test.ts' does not match the naming convention
import { getLicenses, Licenses } from '../src/services/result.service';

const licenseTableTest: { name: string; description: string; content: string; licenses: Licenses[] }[] = [
{
Expand Down
26 changes: 26 additions & 0 deletions __tests__/results.mock.ts

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ inputs:
outputs:
result-filepath:
description: 'Scanner results filepath'

output-command:
description: 'Scanner command output'

runs:
using: node20
main: dist/index.js
Expand Down
29 changes: 8 additions & 21 deletions dist/index.js

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

30 changes: 7 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,26 @@ export async function run(): Promise<void> {
const policies = [new LicensePolicyCheck()];
policies.forEach(async policy => policy.start());

// options to get standar output
const options: exec.ExecOptions = {};
let output = '';
options.listeners = {
stdout: (data: Buffer) => {
output += data.toString();
},
stderr: (data: Buffer) => {
output += data.toString();
}
};
options.silent = true;

// run scan
await exec.exec(
const { stdout, stderr } = await exec.getExecOutput(

Check warning on line 22 in src/main.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'stderr' is assigned a value but never used

Check warning on line 22 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

'stderr' is assigned a value but never used
`docker run -v "${repoDir}":"/scanoss" ghcr.io/scanoss/scanoss-py:v1.9.0 scan . --output ${outputPath}`,
[],
options
[]
);

const scannerResults = await readResult(outputPath);
const licenses = getLicenses(scannerResults);

// create reports
const licensesReport = getLicensesReport(licenses);

// run policies // TODO: define run action for each policy
policies.forEach(async policy => await policy.run(licensesReport));
policies.forEach(async policy => await policy.run(scannerResults));

if (isPullRequest()) {
// create reports
const licenses = getLicenses(scannerResults);
const licensesReport = getLicensesReport(licenses);
createCommentOnPR(licensesReport);
}

// set outputs for other workflow steps to use
core.setOutput('licenses', licenses.toString());
core.setOutput('output-command', output);
core.setOutput('output-command', stdout);
core.setOutput('result-filepath', outputPath);
} catch (error) {
// fail the workflow run if an error occurs
Expand Down
5 changes: 3 additions & 2 deletions src/policies/policy-check.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { context, getOctokit } from '@actions/github';
import * as core from '@actions/core';
import { getSHA } from '../utils/github.utils';
import { ScannerResults } from '../services/result.interfaces';

const NO_INITIALIZATE = -1;

Expand Down Expand Up @@ -33,7 +34,7 @@ export abstract class PolicyCheck {
return result.data;
}

async run(text: string): Promise<any> {
async run(scannerResults: ScannerResults): Promise<any> {

Check warning on line 37 in src/policies/policy-check.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'scannerResults' is defined but never used

Check warning on line 37 in src/policies/policy-check.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type

Check warning on line 37 in src/policies/policy-check.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

'scannerResults' is defined but never used

Check warning on line 37 in src/policies/policy-check.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Unexpected any. Specify a different type
// Promise<OctokitResponse>
if (this.checkRunId === NO_INITIALIZATE)
throw new Error(`Error on finish. Check "${this.checkName}" is not created.`);
Expand All @@ -47,7 +48,7 @@ export abstract class PolicyCheck {
output: {
title: this.checkName,
summary: 'Policy checker completed successfully',
text
text: ''
}
});

Expand Down

0 comments on commit d4b22d3

Please sign in to comment.