Skip to content

Commit

Permalink
Merge pull request #380 from Automattic/update/results-output
Browse files Browse the repository at this point in the history
Update --output functionality
  • Loading branch information
gudmdharalds authored Nov 2, 2023
2 parents 5c9bc0d + 044687e commit 05f141e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 17 deletions.
5 changes: 3 additions & 2 deletions main.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ function vipgoci_help_print() :void {
"\t" . ' to be PHPCS scanned to be specified in file in root' . PHP_EOL .
"\t" . ' of repository (.vipgoci_phpcs_skip_folders).' . PHP_EOL .
"\t" . ' Folders should be separated by newlines.' . PHP_EOL .
"\t" . '--output=FILE Where to save PHPCS output.' . PHP_EOL .
PHP_EOL .
'SVG scanning configuration:' . PHP_EOL .
"\t" . '--svg-checks=BOOL Enable or disable SVG checks, both auto-approval of SVG' . PHP_EOL .
Expand Down Expand Up @@ -215,6 +214,8 @@ function vipgoci_help_print() :void {
"\t" . '--scan-details-msg-include=BOOL If to include additional detail about the scan, versions of' . PHP_EOL .
"\t" . ' software used, options altered and so forth. Enabled by default.' . PHP_EOL .
PHP_EOL .
"\t" . '--output=FILE Where to save results output.' . PHP_EOL .
PHP_EOL .
'Generic support comments configuration:' . PHP_EOL .
"\t" . '--post-generic-pr-support-comments=BOOL Whether to post generic comment to pull requests' . PHP_EOL .
"\t" . ' with support-related information for users. Will' . PHP_EOL .
Expand Down Expand Up @@ -3080,7 +3081,7 @@ function vipgoci_run_scan(
'repo-owner' => $options['repo-owner'],
'repo-name' => $options['repo-name'],
'commit' => $options['commit'],
'prs_implicated' => array_keys( $prs_implicated ),
'prs_implicated' => $prs_implicated,
)
);
}
Expand Down
64 changes: 55 additions & 9 deletions results.php
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,14 @@ function vipgoci_results_filter_duplicate(
function vipgoci_results_output_dump(
string $output_file,
array $data
) :void {
): void {
vipgoci_log(
'Preparing to dump results to file',
array(
'output' => $output_file,
)
);

if (
( is_file( $output_file ) ) &&
( ! is_writeable( $output_file ) )
Expand All @@ -1089,15 +1096,54 @@ function vipgoci_results_output_dump(
'output_file' => $output_file,
)
);

return;
}

if ( isset( $data['prs_implicated'] ) ) {
$tmp_prs_implicated = $data['prs_implicated'];

$data['prs_implicated'] = array();

foreach ( $tmp_prs_implicated as $pr_number => $pr_data ) {
if ( isset( $pr_data->title ) ) {
$data['prs_implicated'][ $pr_number ]['title'] = $pr_data->title;
}

if ( isset( $pr_data->base->ref ) ) {
$data['prs_implicated'][ $pr_number ]['base_branch'] = $pr_data->base->ref;
}

if ( isset( $pr_data->head->ref ) ) {
$data['prs_implicated'][ $pr_number ]['head_branch'] = $pr_data->head->ref;
}

if ( isset( $pr_data->user->login ) ) {
$data['prs_implicated'][ $pr_number ]['creator'] = $pr_data->user->login;
}
}

unset( $tmp_prs_implicated );
unset( $pr_number );
unset( $pr_data );
}

$res = file_put_contents(
$output_file,
json_encode(
$data,
JSON_PRETTY_PRINT
),
FILE_APPEND
);

if ( false === $res ) {
vipgoci_log(
'Unable to write results to output file due to error',
);
} else {
file_put_contents(
$output_file,
json_encode(
$data,
JSON_PRETTY_PRINT
),
FILE_APPEND
vipgoci_log(
'Successfully wrote results to file'
);
}
}

53 changes: 47 additions & 6 deletions tests/integration/ResultsOutputDumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ final class ResultsOutputDumpTest extends TestCase {
*
* @return void
*/
protected function setUp() :void {
protected function setUp(): void {
require_once __DIR__ . '/../../results.php';
require_once __DIR__ . '/../../log.php';

$this->temp_dump_file = tempnam(
sys_get_temp_dir(),
Expand Down Expand Up @@ -63,11 +64,32 @@ public function testDumpResults(): void {
'repo-name' => 'test-repo',
'commit' => 'abc123',
'prs_implicated' => array(
1 => array(
'test1',
1 => (object) array(
'title' => 'testing #1',
'base' => (object) array(
'ref' => 'main',
),
'head' => (object) array(
'ref' => 'add/testing1',
),
'user' => (object) array(
'login' => 'user1',
),
),
2 => array(
'test2',
2 => (object) array(
'title' => 'testing #2',
'base' => (object) array(
'ref' => 'main',
),
'head' => (object) array(
'ref' => 'add/testing2',
),
'user' => (object) array(
'login' => 'user2',
),
),
3 => (object) array(
'invalid' => false,
),
),
);
Expand All @@ -87,7 +109,26 @@ public function testDumpResults(): void {
);

$this->assertSame(
$data,
array(
'results' => array( 1, 2, 3, 4 ),
'repo-owner' => 'test-owner',
'repo-name' => 'test-repo',
'commit' => 'abc123',
'prs_implicated' => array(
1 => array(
'title' => 'testing #1',
'base_branch' => 'main',
'head_branch' => 'add/testing1',
'creator' => 'user1',
),
2 => array(
'title' => 'testing #2',
'base_branch' => 'main',
'head_branch' => 'add/testing2',
'creator' => 'user2',
),
),
),
$dumped_contents
);
}
Expand Down

0 comments on commit 05f141e

Please sign in to comment.