From e1a240873f5e9a1597a533207738063e053acf27 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 16 Oct 2020 12:46:15 +0100 Subject: [PATCH] add stale bot example --- src/stale.php | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/stale.php diff --git a/src/stale.php b/src/stale.php new file mode 100644 index 0000000..bff6052 --- /dev/null +++ b/src/stale.php @@ -0,0 +1,62 @@ +#!/usr/bin/php +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);