Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
Fix some CS issues and run pipeline on pull requests as well
Browse files Browse the repository at this point in the history
  • Loading branch information
sweoggy committed May 28, 2021
1 parent 5722a28 commit 0a30f64
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-staging.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test CI pipeline for staging

on: [push]
on: [push, pull_request]

jobs:
tests:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test CI pipeline

on: [push]
on: [push, pull_request]

jobs:
tests:
Expand Down
27 changes: 15 additions & 12 deletions src/Command/CheckScanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ protected function configure(): void
}

private const ACTION_STRINGS = [
'warnPipeline' => "a pipeline warning",
'failPipeline' => "a pipeline failure",
'warnPipeline' => 'a pipeline warning',
'failPipeline' => 'a pipeline failure',
'sendEmail' => 'an email notification',
'markUnaffected' => 'the vulnerabilities to be marked as unaffected',
'markVulnerable' => 'the vulnerabilities to be flagged as vulnerable',
];

private function writeAutomationOutput(array $ruleOutputData, SymfonyStyle $io)
/**
* @param mixed[] $ruleOutputData
*/
private function writeAutomationOutput(array $ruleOutputData, SymfonyStyle $io): void
{
$io->block($ruleOutputData['ruleDescription'], null, 'fg=cyan;bg=default', ' | ');

Expand All @@ -91,7 +94,7 @@ private function writeAutomationOutput(array $ruleOutputData, SymfonyStyle $io)
$actions = $ruleOutputData['ruleActions'];

$causingString = '';
for ($i = 0; $i < \count($actions); $i++) {
for ($i = 0; $i < \count($actions); ++$i) {
if ($i !== 0) {
$causingString .= $i + 1 === \count($actions) ? ' and ' : ', ';
}
Expand All @@ -102,7 +105,7 @@ private function writeAutomationOutput(array $ruleOutputData, SymfonyStyle $io)

if (\in_array('failPipeline', $actions)) {
$fgColor = 'red';
} else if (\in_array('warnPipeline', $actions)) {
} elseif (\in_array('warnPipeline', $actions)) {
$fgColor = 'yellow';
} else {
$fgColor = 'blue';
Expand All @@ -111,7 +114,7 @@ private function writeAutomationOutput(array $ruleOutputData, SymfonyStyle $io)
$io->text("<fg=${fgColor};options=bold>⨯ The rule triggered, causing ${causingString}</>");
}

$io->text(' Manage rule: <fg=blue>' . $ruleOutputData['ruleLink'] . '</>');
$io->text(' Manage rule: <fg=blue>'.$ruleOutputData['ruleLink'].'</>');

if ($ruleOutputData['triggered'] === true) {
$io->newLine();
Expand All @@ -127,12 +130,12 @@ private function writeAutomationOutput(array $ruleOutputData, SymfonyStyle $io)
$tableRows = \array_map(function ($trigger) use ($hasCves) {
$row = [];
if ($hasCves === true) {
$row[] = $trigger['cve'] . "\n<fg=blue>" . $trigger['cveLink'] . "</>\n";
$row[] = $trigger['cve']."\n<fg=blue>".$trigger['cveLink']."</>\n";
$row[] = $trigger['cvss2'] ?? '';
$row[] = $trigger['cvss3'] ?? '';
}

$row[] = $trigger['dependency'] . "\n<fg=blue>" . $trigger['dependencyLink'] . "</>\n";
$row[] = $trigger['dependency']."\n<fg=blue>".$trigger['dependencyLink']."</>\n";
$row[] = \implode(', ', $trigger['licenses']);

return $row;
Expand Down Expand Up @@ -228,13 +231,13 @@ public function execute(InputInterface $input, OutputInterface $output): int
$io->text($urlMessage);

if (isset($status['automationRules'])) {
$io->section("Output from automations");
$io->section('Output from automations');

$numRulesChecked = \count($status['automationRules']);
if ($numRulesChecked === 0) {
$io->text("No rules were checked");
} else if ($numRulesChecked === 1) {
$io->text("1 rule was checked:");
$io->text('No rules were checked');
} elseif ($numRulesChecked === 1) {
$io->text('1 rule was checked:');
} else {
$io->text("${numRulesChecked} rules were checked:");
}
Expand Down
49 changes: 24 additions & 25 deletions tests/Command/CheckScanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ private function runAutomationsActionTest(
int $expectedStatusCode = 0,
string $automationsActionFieldName = 'automationsAction',
array $automationRules = null
): string
{
): string {
$response = new MockResponse(\json_encode([
'progress' => 100,
'vulnerabilitiesFound' => 0,
Expand Down Expand Up @@ -132,7 +131,7 @@ public function testAutomationOutputUntriggered()
'ruleDescription' => 'untriggered description<fg=red>',
'ruleLink' => 'link-to-rule',
'triggered' => false,
]
],
]);

$this->assertContains("\nOutput from automations\n", $output);
Expand All @@ -152,7 +151,7 @@ public function testAutomationOutputPipelineFailure()
'ruleActions' => ['failPipeline', 'sendEmail'],
'hasCves' => true,
'triggerEvents' => [],
]
],
]);

$this->assertContains("\nOutput from automations\n", $output);
Expand All @@ -173,7 +172,7 @@ public function testAutomationOutputPipelineWarning()
'ruleActions' => ['warnPipeline'],
'hasCves' => true,
'triggerEvents' => [],
]
],
]);

$this->assertContains("\nOutput from automations\n", $output);
Expand All @@ -188,7 +187,7 @@ public function testAutomationOutputMultipleRules()
{
$output = $this->runAutomationsActionTest('fail', 2, 'automationsAction', [
[
'ruleDescription' => "rule description 1",
'ruleDescription' => 'rule description 1',
'ruleLink' => 'link-to-rule-1',
'triggered' => true,
'ruleActions' => ['failPipeline'],
Expand All @@ -201,26 +200,26 @@ public function testAutomationOutputMultipleRules()
'cveLink' => 'cve-link-1',
'dependency' => 'dep-1',
'dependencyLink' => 'dep-link-1',
'licenses' => ['gpl3']
'licenses' => ['gpl3'],
],
[
'cve' => 'cve-2',
'cveLink' => 'cve-link-2',
'cvss2' => 7,
'dependency' => 'dep-2',
'dependencyLink' => 'dep-link-2',
'licenses' => ['mit']
]
'licenses' => ['mit'],
],
],
],
[
'ruleDescription' => "rule description 2",
'ruleDescription' => 'rule description 2',
'ruleLink' => 'link-to-rule-2',
'triggered' => false,
'ruleActions' => ['failPipeline']
'ruleActions' => ['failPipeline'],
],
[
'ruleDescription' => "rule description 3",
'ruleDescription' => 'rule description 3',
'ruleLink' => 'link-to-rule-3',
'triggered' => true,
'ruleActions' => ['warnPipeline', 'sendEmail'],
Expand All @@ -229,13 +228,13 @@ public function testAutomationOutputMultipleRules()
[
'dependency' => 'dep-1',
'dependencyLink' => 'dep-link-1',
'licenses' => ['apache', 'mit']
'licenses' => ['apache', 'mit'],
],
[
'dependency' => 'dep-3',
'dependencyLink' => 'dep-link-3',
'licenses' => ['mit']
]
'licenses' => ['mit'],
],
],
],
]);
Expand All @@ -253,25 +252,25 @@ public function testAutomationOutputMultipleRules()
$this->assertStringStartsWith(' ---', $outputLines[$rule1Begin + 6]);
$this->assertEquals(
['Vulnerability', 'CVSS2', 'CVSS3', 'Dependency', 'Dependency', 'Licenses'],
\preg_split('/\s+/', $outputLines[$rule1Begin + 7], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule1Begin + 7], null, PREG_SPLIT_NO_EMPTY)
);
$this->assertStringStartsWith(' ---', $outputLines[$rule1Begin + 8]);
$this->assertEquals(
['cve-1', '8', '9', 'dep-1', 'gpl3'],
\preg_split('/\s+/', $outputLines[$rule1Begin + 9], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule1Begin + 9], null, PREG_SPLIT_NO_EMPTY)
);
$this->assertEquals(
['cve-link-1', 'dep-link-1'],
\preg_split('/\s+/', $outputLines[$rule1Begin + 10], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule1Begin + 10], null, PREG_SPLIT_NO_EMPTY)
);
$this->assertEquals('', $outputLines[$rule1Begin + 11]);
$this->assertEquals(
['cve-2', '7', 'dep-2', 'mit'],
\preg_split('/\s+/', $outputLines[$rule1Begin + 12], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule1Begin + 12], null, PREG_SPLIT_NO_EMPTY)
);
$this->assertEquals(
['cve-link-2', 'dep-link-2'],
\preg_split('/\s+/', $outputLines[$rule1Begin + 13], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule1Begin + 13], null, PREG_SPLIT_NO_EMPTY)
);

$rule2Begin = \array_search(' | rule description 2', $outputLines);
Expand All @@ -287,25 +286,25 @@ public function testAutomationOutputMultipleRules()
$this->assertStringStartsWith(' ---', $outputLines[$rule3Begin + 6]);
$this->assertEquals(
['Dependency', 'Dependency', 'Licenses'],
\preg_split('/\s+/', $outputLines[$rule3Begin + 7], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule3Begin + 7], null, PREG_SPLIT_NO_EMPTY)
);
$this->assertStringStartsWith(' ---', $outputLines[$rule3Begin + 8]);
$this->assertEquals(
['dep-1', 'apache,', 'mit'],
\preg_split('/\s+/', $outputLines[$rule3Begin + 9], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule3Begin + 9], null, PREG_SPLIT_NO_EMPTY)
);
$this->assertEquals(
['dep-link-1'],
\preg_split('/\s+/', $outputLines[$rule3Begin + 10], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule3Begin + 10], null, PREG_SPLIT_NO_EMPTY)
);
$this->assertEquals('', $outputLines[$rule3Begin + 11]);
$this->assertEquals(
['dep-3', 'mit'],
\preg_split('/\s+/', $outputLines[$rule3Begin + 12], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule3Begin + 12], null, PREG_SPLIT_NO_EMPTY)
);
$this->assertEquals(
['dep-link-3'],
\preg_split('/\s+/', $outputLines[$rule3Begin + 13], NULL, PREG_SPLIT_NO_EMPTY)
\preg_split('/\s+/', $outputLines[$rule3Begin + 13], null, PREG_SPLIT_NO_EMPTY)
);

$this->assertGreaterThan($rule1Begin, $rule2Begin);
Expand Down

0 comments on commit 0a30f64

Please sign in to comment.