GiteaBot-repeat/src/rrule.php
2019-10-06 09:46:07 +01:00

97 lines
2.9 KiB
PHP
Executable File

#!/usr/bin/php
<?php
/*
* This will find all issues with RRULE: in the body
* 1. extract the RRULE string and calculate the recurring dates.
* 2. if the rule is due to occure today then:
* 2a. if the issue is closed then it will re-open it,
* 2b. and it will add a comment "Today!"
*/
// 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');
use RRule\RRule;
//function to add/replace the tag with our text
/*
function updateIssueBody($issue,$text){
$issue->addComment($text);
return;
// add a tag to the body if to doesn't exist
if( strpos($issue->body,'<!--BOT-->')===false ){
$issue->body.="\n<!--BOT--><hr/><!--ENDBOT-->";
}
//replace the tag contents with the result from our $text function
$issue->body=preg_replace("#<!--BOT-->.*?<!--ENDBOT-->#s",'<!--BOT--><hr/>'.$text.'<!--ENDBOT-->',$issue->body);
$issue->save();
};
*/
//loop through each issue that has a RRULE
$issues=array_merge(
$repo->getIssues(['q'=>'RRULE:']),
$repo->getIssues(['q'=>'RRULE:','state'=>'closed'])
);
foreach($issues as $issue){
echo "\n\n### ".$issue->number.": ".$issue->title."\n";
//extract the rule text
if(preg_match('/RRULE:\s?(.*)/',$issue->body,$matches)){
$rruletxt=$matches[1];
//create the rule, report any exceptions
try{
$rrule = new RRule($rruletxt);
}catch(Exception $e){
$result="Error in RRULE\n```plain\n".$e->getMessage()."\n\n$rruletxt\n```\n";
$issue->addComment($result);
continue;
}
}else{ //couldn't extract, report error
$issue->addComment("Can't extract RRULE:");
continue;
}
// if we get here, then there is a valid rrule :)
// calcultate some useful info
$next_inc=$rrule->getOccurrencesAfter(date('Y-m-d'),$inclusive=true,$limit=1)[0];
$next_exc=$rrule->getOccurrencesAfter(date('Y-m-d'),$inclusive=false,$limit=1)[0];
$today=$next_inc->format('Y-m-d')==date('Y-m-d');
if($debug){
echo $rrule->humanReadable().
"\n NOW: ".date('Y-m-d').
"\n First: ".$rrule[0]->format('Y-m-d').
"\n Second: ".$rrule[1]->format('Y-m-d').
"\n Today: ".($today?'Yes':'no').
"\n Next Inc: ".$next_inc->format('Y-m-d').
"\n Next Exc: ".$next_exc->format('Y-m-d').
"\n";
}
//is it today?
if($today){
echo "@Today\n";
if($issue->state=='closed'){
$issue->state='open';
$issue->save();
}
$issue->addComment("Today!!\n\nNext: ".$next_exc->format('Y-m-d'));
}
// not working https://github.com/go-gitea/gitea/issues/8179
//$issue->due_date=$rrule[0]->format('c');
//$issue->save();
//if( date('Y-m-d',strtotime($issue->due_date)) != $next_exc->format('Y-m-d') ){
if( $issue->due_date ){
if( strtotime(date('Y-m-d',strtotime($issue->due_date))) < strtotime($next_exc->format('Y-m-d')) ){
echo "@Overdue\n";
$issue->addComment("Overdue!!\n\nNext: ".$next_exc->format('Y-m-d'));
}
}else{
$issue->setDueDate($next_exc);
}
}