Skip to content

Commit

Permalink
Contributors can add the ready tag to their own prs (tgstation#32062)
Browse files Browse the repository at this point in the history
* Contributors can add the ready tag

* Webhook synchronize case pushes update at the end

* Fixes pr not getting tagged when opened
  • Loading branch information
JJRcop authored and optimumtact committed Nov 1, 2017
1 parent 9d53124 commit 204330a
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions tools/WebhookProcessor/github_webhook_processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@ function check_tag_and_replace($payload, $title_tag, $label, &$array_to_add_labe
return false;
}

function set_labels($payload, $labels, $remove) {
$existing = get_labels($payload);

$tags = array();

$tags = array_merge($labels, $existing);
$tags = array_unique($tags);
$tags = array_diff($tags, $remove);

$final = array();
foreach($tags as $t)
$final[] = $t;

$url = $payload['pull_request']['issue_url'] . '/labels';
echo apisend($url, 'PUT', $final);
}

//rip bs-12
function tag_pr($payload, $opened) {
//get the mergeable state
Expand Down Expand Up @@ -215,31 +232,16 @@ function tag_pr($payload, $opened) {
$tags[] = $tag;

check_tag_and_replace($payload, '[dnm]', 'Do Not Merge', $tags);
if(!check_tag_and_replace($payload, '[wip]', 'Work In Progress', $tags))
check_tag_and_replace($payload, '[ready]', 'Work In Progress', $remove);
if(!check_tag_and_replace($payload, '[wip]', 'Work In Progress', $tags) && check_tag_and_replace($payload, '[ready]', 'Work In Progress', $remove))
$tags[] = 'Needs Review';

$url = $payload['pull_request']['issue_url'] . '/labels';

$existing = get_labels($payload);

$tags = array_merge($tags, $existing);
$tags = array_unique($tags);
$tags = array_diff($tags, $remove);

$final = array();
foreach($tags as $t)
$final[] = $t;


echo apisend($url, 'PUT', $final);

return $final;
return array($tags, $remove);
}

function remove_ready_for_review($payload, $labels = null){
if($labels == null)
$labels = get_labels($payload);
$index = array_search('Review Again', $labels);
$index = array_search('Needs Review', $labels);
if($index !== FALSE)
unset($labels[$index]);
$url = $payload['pull_request']['issue_url'] . '/labels';
Expand All @@ -255,13 +257,14 @@ function get_reviews($payload){
return json_decode(apisend($payload['pull_request']['url'] . '/reviews'), true);
}

function check_ready_for_review($payload, $labels = null){
$r4rlabel = 'Review Again';
function check_ready_for_review($payload, $labels = null, $remove = array()){
$r4rlabel = 'Needs Review';
$labels_which_should_not_be_ready = array('Do Not Merge', 'Work In Progress', 'Merge Conflict');
$has_label_already = false;
$should_not_have_label = false;
if($labels == null)
$labels = get_labels($payload);
$returned = array($labels, $remove);
//if the label is already there we may need to remove it
foreach($labels as $L){
if(in_array($L, $labels_which_should_not_be_ready))
Expand All @@ -271,8 +274,8 @@ function check_ready_for_review($payload, $labels = null){
}

if($has_label_already && $should_not_have_label){
remove_ready_for_review($payload, $labels, $r4rlabel);
return;
$remove[] = $r4rlabel;
return $returned;
}

//find all reviews to see if changes were requested at some point
Expand All @@ -294,8 +297,8 @@ function check_ready_for_review($payload, $labels = null){

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
$remove[] = $r4rlabel;
return $returned; //no need to be here
}

if(count($reviews_ids_with_changes_requested) > 0){
Expand All @@ -311,18 +314,17 @@ function check_ready_for_review($payload, $labels = null){
//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
$remove[] = $r4rlabel;
return $returned; //no need to tag
}
}
}

//finally, add it if necessary
if(!$has_label_already){
$labels[] = $r4rlabel;
$url = $payload['pull_request']['issue_url'] . '/labels';
apisend($url, 'PUT', $labels);
}
return $returned;
}

function check_dismiss_changelog_review($payload){
Expand Down Expand Up @@ -359,7 +361,8 @@ function handle_pr($payload) {
$validated = validate_user($payload);
switch ($payload["action"]) {
case 'opened':
tag_pr($payload, true);
list($labels, $remove) = tag_pr($payload, true);
set_labels($payload, $labels, $remove);
if($no_changelog)
check_dismiss_changelog_review($payload);
if(get_pr_code_friendliness($payload) < 0){
Expand All @@ -372,9 +375,10 @@ function handle_pr($payload) {
case 'edited':
check_dismiss_changelog_review($payload);
case 'synchronize':
$labels = tag_pr($payload, false);
list($labels, $remove) = tag_pr($payload, false);
if($payload['action'] == 'synchronize')
check_ready_for_review($payload, $labels);
list($labels, $remove) = check_ready_for_review($payload, $labels, $remove);
set_labels($payload, $labels, $remove);
return;
case 'reopened':
$action = $payload['action'];
Expand Down

0 comments on commit 204330a

Please sign in to comment.