Skip to content

Commit

Permalink
Implement maximum number of inline comments per GitHub PR review subm…
Browse files Browse the repository at this point in the history
…ission
  • Loading branch information
gudmdharalds committed Jun 7, 2018
1 parent 8ae9a82 commit 06c8312
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 8 deletions.
92 changes: 85 additions & 7 deletions github-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 20 additions & 1 deletion vip-go-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ function vipgoci_run() {
'repo-name:',
'commit:',
'token:',
'review-comments-max:',
'branches-ignore:',
'output:',
'dry-run:',
Expand Down Expand Up @@ -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 .
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 06c8312

Please sign in to comment.