Skip to content

Commit

Permalink
SCP-135 Includes policies on summary report
Browse files Browse the repository at this point in the history
  • Loading branch information
isasmendiagus authored and francostramana committed Feb 27, 2024
1 parent 2559d5f commit 47f1eed
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 77 deletions.
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ inputs:
required: false
default: ${{ github.token }}


# Define your outputs here.
outputs:
result-filepath:
Expand Down
113 changes: 77 additions & 36 deletions dist/index.js

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

14 changes: 10 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,29 @@ export async function run(): Promise<void> {

// create policies
core.debug(`Creating policies`);

//Read declared policies on input parameter 'policies' and create an instance for each one.
const policies = policyManager.getPolicies();
policies.forEach(async policy => policy.start());
for (const policy of policies) {
await policy.start();
}

// run scan
const { scan, stdout } = await scanService.scan();
await uploadResults();

// run policies
policies.forEach(async policy => await policy.run(scan));
for (const policy of policies) {
await policy.run(scan);
}

if (isPullRequest()) {
// create reports
const report = generateSummary(scan);
createCommentOnPR(report);
await createCommentOnPR(report);
}

await generateJobSummary(scan);
await generateJobSummary(scan, policies);
// set outputs for other workflow steps to use
core.setOutput(outputs.RESULT_FILEPATH, inputs.OUTPUT_FILEPATH);
core.setOutput(outputs.STDOUT_SCAN_COMMAND, stdout);
Expand Down
51 changes: 42 additions & 9 deletions src/policies/policy-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { ScannerResults } from '../services/result.interfaces';
import { GitHub } from '@actions/github/lib/utils';
import * as inputs from '../app.input';

const UNINITIALIZED = -1;

export enum CONCLUSION {

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

View workflow job for this annotation

GitHub Actions / Lint Codebase

'CONCLUSION' is already declared in the upper scope on line 8 column 13

Check warning on line 8 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 8 column 13
ActionRequired = 'action_required',
Cancelled = 'cancelled',
Expand All @@ -18,17 +16,32 @@ export enum CONCLUSION {
TimedOut = 'timed_out'
}

export enum STATUS {

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

View workflow job for this annotation

GitHub Actions / Lint Codebase

'STATUS' is already declared in the upper scope on line 19 column 13

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

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'STATUS' is already declared in the upper scope on line 19 column 13
UNINITIALIZED = 'UNINITIALIZED',
INITIALIZED = 'INITIALIZED',
RUNNING = 'RUNNING',
FINISHED = 'FINISHED'
}

export abstract class PolicyCheck {
private octokit: InstanceType<typeof GitHub>;

private checkName: string;

private checkRunId: number;

private _raw: any;

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

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type

private _status: STATUS;

private _conclusion: CONCLUSION;

constructor(checkName: string) {
this.octokit = getOctokit(inputs.GITHUB_TOKEN);
this.checkName = checkName;
this.checkRunId = UNINITIALIZED;
this._status = STATUS.UNINITIALIZED;
this._conclusion = CONCLUSION.Neutral;
this.checkRunId = -1;
}

async start(): Promise<any> {

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

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type
Expand All @@ -40,37 +53,57 @@ export abstract class PolicyCheck {
});

this.checkRunId = result.data.id;
this._raw = result.data;

this._status = STATUS.INITIALIZED;
return result.data;
}

get name(): string {
return this.checkName;
}

get conclusion(): CONCLUSION {
return this._conclusion;
}

get raw(): any {
return this._raw;
}

get url(): string {
return `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}/job/${this.raw.id}`;
}

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

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

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

protected async reject(summary: string, text: string): Promise<void> {
return await this.finish(inputs.POLICIES_HALT_ON_FAILURE ? CONCLUSION.Failure : CONCLUSION.Neutral, summary, text);
if (inputs.POLICIES_HALT_ON_FAILURE) this._conclusion = CONCLUSION.Failure;
else this._conclusion = CONCLUSION.Neutral;
return await this.finish(summary, text);
}

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

const result = await this.octokit.rest.checks.update({
owner: context.repo.owner,
repo: context.repo.repo,
check_run_id: this.checkRunId,
status: 'completed',
conclusion,
conclusion: this._conclusion,
output: {
title: this.checkName,
summary,
Expand Down
Loading

0 comments on commit 47f1eed

Please sign in to comment.