diff --git a/main.php b/main.php index ceaa4266..25eb93ad 100755 --- a/main.php +++ b/main.php @@ -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 . @@ -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 . @@ -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, ) ); } diff --git a/results.php b/results.php index ed345bdd..46890c52 100644 --- a/results.php +++ b/results.php @@ -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 ) ) @@ -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' ); } } - diff --git a/tests/integration/ResultsOutputDumpTest.php b/tests/integration/ResultsOutputDumpTest.php index 4e02d63a..9c511639 100644 --- a/tests/integration/ResultsOutputDumpTest.php +++ b/tests/integration/ResultsOutputDumpTest.php @@ -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(), @@ -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, ), ), ); @@ -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 ); }