Dismiss approved reviews after synchronize

This commit is contained in:
Jordan Brown
2017-09-13 09:44:17 -04:00
parent 1faf29bf06
commit 592a8a63b2

View File

@@ -236,8 +236,13 @@ function remove_ready_for_review($payload, $labels = null){
apisend($url, 'PUT', $labels);
}
function dismiss_review($payload, $id){
$content = array('message' => 'Out of date review');
apisend($payload['pull_request']['url'] . '/reviews/' . $id . '/dismissals', 'PUT', $content);
}
function check_ready_for_review($payload, $labels = null){
$r4rlabel = 'Ready for Review';
$r4rlabel = 'Ready for Review';
$has_label_already = false;
if($labels == null)
$labels = get_labels($payload);
@@ -252,43 +257,54 @@ function check_ready_for_review($payload, $labels = null){
$reviews = json_decode(apisend($payload['pull_request']['url'] . '/reviews'), true);
$reviews_ids_with_changes_requested = array();
$dismissed_an_approved_review = false;
foreach($reviews as $R){
if(strtolower($R['state']) == 'changes_requested' && isset($R['author_association'])){
if(isset($R['author_association'])){
$lower_association = strtolower($R['author_association']);
if($lower_association == 'member' || $lower_association == 'contributor' || $lower_association == 'owner')
$reviews_ids_with_changes_requested[] = $R['id'];
if($lower_association == 'member' || $lower_association == 'contributor' || $lower_association == 'owner'){
$lowerstate = strtolower($R['state']);
if($lower_state == 'changes_requested')
$reviews_ids_with_changes_requested[] = $R['id'];
else if ($lower_state == 'approved'){
dismiss_review($payload, $R['id']);
$dismissed_an_approved_review = true;
}
}
}
}
if(count($reviews_ids_with_changes_requested) == 0){
if(!$dismissed_an_approved_review && count($reviews_ids_with_changes_requested) == 0){
if($has_label_already)
remove_ready_for_review($payload, $labels);
return; //no need to be here
}
echo count($reviews_ids_with_changes_requested) . ' offending reviews';
if(count($reviews_ids_with_changes_requested) > 0){
//now get the review comments for the offending reviews
//now get the review comments for the offending reviews
$review_comments = json_decode(apisend($payload['pull_request']['review_comments_url']), true);
$review_comments = json_decode(apisend($payload['pull_request']['review_comments_url']), true);
foreach($review_comments as $C){
//make sure they are part of an offending review
if(!in_array($C['pull_request_review_id'], $reviews_ids_with_changes_requested))
continue;
//review comments which are outdated have a null position
if($C['position'] !== null){
if($has_label_already)
remove_ready_for_review($payload, $labels);
return; //no need to tag
foreach($review_comments as $C){
//make sure they are part of an offending review
if(!in_array($C['pull_request_review_id'], $reviews_ids_with_changes_requested))
continue;
//review comments which are outdated have a null position
if($C['position'] !== null){
if($has_label_already)
remove_ready_for_review($payload, $labels);
return; //no need to tag
}
}
}
$labels[] = $r4rlabel;
$url = $payload['pull_request']['issue_url'] . '/labels';
apisend($url, 'PUT', $labels);
//finally, add it if necessary
if(!$has_label_already){
$labels[] = $r4rlabel;
$url = $payload['pull_request']['issue_url'] . '/labels';
apisend($url, 'PUT', $labels);
}
}
function handle_pr($payload) {