add stale bot example

This commit is contained in:
James 2020-10-16 12:46:15 +01:00
parent 7e7ea149d8
commit e1a240873f

62
src/stale.php Normal file
View File

@ -0,0 +1,62 @@
#!/usr/bin/php
<?php
/*
1. loop through all issues:
2. if issue has stale "label":
2.1. if activity since label was added then remove label
2.2. if still no activity in 7 days then close
3. if issue doesn not has stale "label"
3.1. if no activity in >30 dyas then add "stale" label
*/
// load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available
require 'setup.php';
//get the stale label or create if not exist
if (!$staleLabel = $repo->getLabelByName('stale')) {
$staleLabel = $repo->createLabel(['name' => 'stale', 'color' => '#ffffff']);
}
//define the function to process each issue
$callback = function($issue) use ($staleLabel) {
$updatedTime=strtotime($issue->updated_at);
$updateDaysAgo=(time()-$updatedTime)/60/60/24;
echo "#{$issue->number} - $updateDaysAgo days ago - {$issue->title}\n";
if($issue->hasLabel($staleLabel)){
//when was it added?
$staleTime=null;
foreach($issue->getComments() as $comment){
if(substr($comment->body,0,35)=='This issue has been marked as stale'){
$staleTime=strtotime($comment->created_at);
$staleDaysAgo=(time()-$staleTime)/60/60/24;
}
}
if(!$staleTime){
echo "ERROR: cant find stale time\n";
return;
}
//if activity sice added then remove label
if($updatedTime>$staleTime){
$issue->removeLabel($staleLabel);
return;
}
//if no activity in 7 days then close
if($updateDaysAgo>7) {
$issue->state = 'closed';
$issue->save();
$issue->addComment('This issue has been automatically closed due to inactivity.');
return;
}
}elseif($updateDaysAgo>30){ //no stale label and >30 days
$issue->addLabel($staleLabel);
$issue->addComment('This issue has been marked as stale due to 30+ days of inactivity. If there is no futrher activity in the next 7 days it will be automatically closed.');
$issue->save();
return;
}
};
//loop through all issues and call the callback
$repo->forIssues($callback);