Skip to content

Commit

Permalink
SCP-68 Adds CopyLeft Policy Checker
Browse files Browse the repository at this point in the history
  • Loading branch information
francostramana committed Jan 25, 2024
1 parent b071d74 commit 9771699
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 44 deletions.
70 changes: 50 additions & 20 deletions dist/index.js

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

1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@actions/github": "^6.0.0"
},
"devDependencies": {
"@octokit/types": "^12.4.0",
"@types/jest": "^29.5.11",
"@types/node": "^20.11.0",
"@typescript-eslint/eslint-plugin": "^6.18.1",
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from '@actions/core';
import * as exec from '@actions/exec';
import { getLicenses, readResult } from './services/result.service';
import { createCommentOnPR, isPullRequest } from './utils/github.utils';
import { LicensePolicyCheck } from './policies/license-policy-check';
import { CopyleftPolicyCheck } from './policies/copyleft-policy-check';
import { getLicensesReport } from './services/report.service';

/**
Expand All @@ -18,7 +18,7 @@ export async function run(): Promise<void> {

// create policies
core.debug(`Creating policies`);
const policies = [new LicensePolicyCheck()];
const policies = [new CopyleftPolicyCheck()];
policies.forEach(async policy => policy.start());

// run scan
Expand Down
22 changes: 22 additions & 0 deletions src/policies/copyleft-policy-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ScannerResults } from '../services/result.interfaces';
import { CHECK_NAME } from '../app.config';
import { PolicyCheck } from './policy-check';
import { getLicenses } from '../services/result.service';

export class CopyleftPolicyCheck extends PolicyCheck {
constructor() {
super(`${CHECK_NAME}: Copyleft Policy`);
}

async run(scannerResults: ScannerResults): Promise<void> {
super.run(scannerResults);
const licenses = getLicenses(scannerResults);

const hasCopyleft = licenses.some(license => !!license.copyleft);
if (!hasCopyleft) {
this.success('Completed succesfully', 'Not copyleft licenses were found');
} else {
this.reject('Completed failure', 'Copyleft licenses were found:'); // TODO: create a table with copyleft licenses
}
}
}
8 changes: 0 additions & 8 deletions src/policies/license-policy-check.ts

This file was deleted.

51 changes: 37 additions & 14 deletions src/policies/policy-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,38 @@ import { context, getOctokit } from '@actions/github';
import * as core from '@actions/core';
import { getSHA } from '../utils/github.utils';
import { ScannerResults } from '../services/result.interfaces';
import { GitHub } from '@actions/github/lib/utils';
import { OctokitResponse } from '@octokit/types';

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

View workflow job for this annotation

GitHub Actions / Lint Codebase

'OctokitResponse' is defined but never used

Check failure on line 6 in src/policies/policy-check.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Unable to resolve path to module '@octokit/types'

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

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'OctokitResponse' is defined but never used

Check failure on line 6 in src/policies/policy-check.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unable to resolve path to module '@octokit/types'

const NO_INITIALIZATE = -1;
const UNINITIALIZED = -1;

export enum CONCLUSION {

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

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'CONCLUSION' is already declared in the upper scope on line 10 column 13
ActionRequired = 'action_required',
Cancelled = 'cancelled',
Failure = 'failure',
Neutral = 'neutral',
Success = 'success',
Skipped = 'skipped',
Stale = 'stale',
TimedOut = 'timed_out'
}

export abstract class PolicyCheck {
private octokit; // TODO: type from actions/github ?
private octokit: InstanceType<typeof GitHub>;

private checkName: string;

private checkRunId: number;

constructor(checkName: string) {
const GITHUB_TOKEN = core.getInput('github-token'); // TODO: move to inputs.ts file?
const GITHUB_TOKEN = core.getInput('github-token');

this.octokit = getOctokit(GITHUB_TOKEN);
this.checkName = checkName;
this.checkRunId = NO_INITIALIZATE;
this.checkRunId = UNINITIALIZED;
}

async start(): Promise<any> {

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

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type
// Promise<OctokitResponse>
const result = await this.octokit.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -34,24 +46,35 @@ export abstract class PolicyCheck {
return result.data;
}

async run(scannerResults: ScannerResults): Promise<any> {
// Promise<OctokitResponse>
if (this.checkRunId === NO_INITIALIZATE)
throw new Error(`Error on finish. Check "${this.checkName}" is not created.`);
async run(scannerResults: ScannerResults): Promise<void> {
if (this.checkRunId === UNINITIALIZED)
throw new Error(`Error on finish. Policy "${this.checkName}" is not created.`);

core.debug(`Running policy check: ${this.checkName}`);
}

protected async success(summary: string, text: string): Promise<void> {
await this.finish(CONCLUSION.Success, summary, text);
}

protected async reject(summary: string, text: string): Promise<void> {
await this.finish(CONCLUSION.Failure, summary, text);
}

protected async finish(conclusion: CONCLUSION | undefined, summary: string, text: string): Promise<void> {
core.debug(`Finish policy check: ${this.checkName}. (conclusion=${conclusion})`);

const result = await this.octokit.rest.checks.update({
owner: context.repo.owner,
repo: context.repo.repo,
check_run_id: this.checkRunId,
status: 'completed',
conclusion: 'success',
conclusion,
output: {
title: this.checkName,
summary: 'Policy checker completed successfully',
text: ''
summary,
text
}
});

return result.data;
}
}

0 comments on commit 9771699

Please sign in to comment.