diff --git a/github-api.php b/github-api.php index 563100690..1a51eef59 100644 --- a/github-api.php +++ b/github-api.php @@ -1231,7 +1231,8 @@ function vipgoci_github_pr_review_submit( $github_token, $commit_id, $results, - $dry_run + $dry_run, + $github_review_comments_max ) { $stats_types_to_process = array( @@ -1446,13 +1447,90 @@ function vipgoci_github_pr_review_submit( } } - // Actually send a request to GitHub - $github_post_res = vipgoci_github_post_url( - $github_url, - $github_postfields, - $github_token - ); + /* + * Only submit a specific number of comments in one go. + * + * This hopefully will reduce the likelihood of problems + * with the GitHub API. Also, it will avoid excessive number + * of comments being posted at once. + * + * Do this by picking out a few comments at a time, + * submit, and repeat. + */ + + if ( + count( $github_postfields['comments'] ) > + $github_review_comments_max + ) { + // Append a comment that there will be more reviews + $github_postfields['body'] .= + "\n\r" . + 'Posting will continue in further review(s)'; + } + + + do { + /* + * Set temporary variable we use for posting + * and remove all comments from it. + */ + $github_postfields_tmp = $github_postfields; + + unset( $github_postfields_tmp['comments'] ); + + /* + * Add in comments. + */ + + for ( $i = 0; $i < $github_review_comments_max; $i++ ) { + $y = count( $github_postfields['comments'] ); + + if ( 0 === $y ) { + /* No more items, break out */ + break; + } + + $y--; + + $github_postfields_tmp['comments'][] = + $github_postfields['comments'][ $y ]; + + unset( + $github_postfields['comments'][ $y ] + ); + } + + // Actually send a request to GitHub + $github_post_res_tmp = vipgoci_github_post_url( + $github_url, + $github_postfields_tmp, + $github_token + ); + + /* + * If something goes wrong with any submission, + * keep a note on that. + */ + if ( + ( ! isset( $github_post_res ) || + ( -1 !== $github_post_res ) ) + ) { + $github_post_res = $github_post_res_tmp; + } + + // Set a new post-body for future posting. + $github_postfields['body'] = 'Previous scan continued.'; + } while ( count( $github_postfields['comments'] ) > 0 ); + + unset( $github_post_res_tmp ); + unset( $y ); + unset( $i ); + + /* + * If one or more submissions went wrong, + * let humans know that there was a problem. + */ if ( -1 === $github_post_res ) { vipgoci_github_pr_comments_error_msg( $repo_owner, diff --git a/vip-go-ci.php b/vip-go-ci.php index 93a4dbb38..c9ed11d1d 100755 --- a/vip-go-ci.php +++ b/vip-go-ci.php @@ -284,6 +284,7 @@ function vipgoci_run() { 'repo-name:', 'commit:', 'token:', + 'review-comments-max:', 'branches-ignore:', 'output:', 'dry-run:', @@ -319,6 +320,10 @@ function vipgoci_run() { "\t" . '--repo-name=STRING Specify name of the repository' . PHP_EOL . "\t" . '--commit=STRING Specify the exact commit to scan (SHA)' . PHP_EOL . "\t" . '--token=STRING The access-token to use to communicate with GitHub' . PHP_EOL . + "\t" . '--review-comments-max=NUMBER Maximum number of inline comments to submit' . PHP_EOL . + "\t" . ' to GitHub in one review. If the number of ' . PHP_EOL . + "\t" . ' comments exceed this number, additional reviews ' . PHP_EOL . + "\t" . ' will be submitted.' . PHP_EOL . "\t" . '--phpcs=BOOL Whether to run PHPCS (true/false)' . PHP_EOL . "\t" . '--phpcs-path=FILE Full path to PHPCS script' . PHP_EOL . "\t" . '--phpcs-standard=STRING Specify which PHPCS standard to use' . PHP_EOL . @@ -560,6 +565,19 @@ function vipgoci_run() { } + /* + * Maximum number of inline comments posted to + * Github with one review -- from 5 to 100. + */ + + vipgoci_option_integer_handle( + $options, + 'review-comments-max', + 10, + range( 5, 100, 1 ) + ); + + /* * Log that we started working, * and the arguments provided as well. @@ -778,7 +796,8 @@ function vipgoci_run() { $options['token'], $options['commit'], $results, - $options['dry-run'] + $options['dry-run'], + $options['review-comments-max'] ); $github_api_rate_limit_usage =