working rrule prototype

This commit is contained in:
James 2019-10-03 14:03:48 +01:00
parent 02b42c9529
commit 500eff3946
3 changed files with 100 additions and 146 deletions

55
src/convert.php Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/php
<?php
/*
* Convert the old labely repeat system to the new rrule system
*/
// 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');
//define the repeat terms
$terms=[
'months'=>[1=>'janurary',2=>'feburary',3=>'march',4=>'april',5=>'may',6=>'june',7=>'july',8=>'august',9=>'september',10=>'october',11=>'november',12=>'december'],
'days'=>['MO'=>'monday','TU'=>'tuesday','WE'=>'wednesday','TH'=>'thursday','FR'=>'friday','SA'=>'saturday','SU'=>'sunday'],
'generic'=>['daily','weekly','fortnightly','monthly','yearly']
];
$month=function($issue) use($terms){
$t=getRepeatTerms($issue,$terms);
print_r($t);
$ms=[];
foreach($t['months'] as $id=>$m){
$ms[]=$id;
}
$ms=implode(',',$ms);
$rrule="RRULE: FREQ=MONTHLY;INTERVAL=1;BYMONTH=$ms;BYMONTHDAY=1";
echo "\n\n$rrule\n\n";
$issue->body=preg_replace("#RRULE:.*#",'',$issue->body);
$issue->body=$rrule."\n\n".$issue->body;
$issue->save();
};
function getRepeatTerms($issue,$terms){
$labels=[];
$found=[];
foreach($issue->labels as $label){
$labels[]=$label->name;
}
print_r($labels);
foreach($terms as $name=>$values){
foreach($values as $id=>$value){
if( in_array("$value/repeat",$labels) ){
$found[$name][$id]=$value;
}
}
}
return $found;
}
foreach($terms['months'] as $value){
$repo->forIssues($month,['labels'=>"$value/repeat"]);
$repo->forIssues($month,['state'=>'closed','labels'=>"$value/repeat"]);
}

View File

@ -1,129 +0,0 @@
#!/usr/bin/php
<?php
/***********************************************************************
* This is designed to allow the use of labels to reopen recuring issues
* on a specific day, month or duration (daily/weekly/monthly etc)
* NOTE: This is not 100% implemented/tested. It's a WIP
* It will process any issue that is tagged with 'xxx/repeat' label
* The labels are grouped into days/months
* If an issue is closed then it will be reopened
* If an issue is already open the bot will comment "NAG!" about it
***********************************************************************/
// 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');
//define the repeat terms
$terms=[
'months'=>['janurary','feburary','march','april','may','june','july','august','september','october','november','december'],
'days'=>['monday','tuesday','wednesday','thursday','friday','saturday','sunday'],
'generic'=>['daily','weekly','fortnightly','monthly','yearly']
];
/* create the labels */
foreach($terms as $group=>$names){
foreach($names as $name){
$name="{$name}/repeat";
$label=$repo->getLabelByName($name);
if(!$label){
$label=$repo->createLabel([
'name'=>$name,
'color'=>'#ffff00',
'description'=>'Reopen this issue with the given frequency'
]);
}
}
}
//define the function to process each issue
$daily=function($issue){
$day_today=date('U')/60/60/24;
$days_closed_ago=$issue->closed_at?$day_today-(date('U',strtotime($issue->closed_at))/60/60/24):null;
$days_updated_ago=$day_today-(date('U',strtotime($issue->updated_at))/60/60/24);
echo "Daily: ". $issue->title." C:".$issue->closed_at." ($days_closed_ago) U:".$issue->updated_at." ($days_updated_ago) \n";
if($days_closed_ago>1){
if($issue->state=="closed"){
$issue->state='open';
$issue->save();
}else{
if($days_updated_ago>1){
$issue->addComment("NAG!");
}
}
}
};
$weekly=function($issue){
$day_today=date('U')/60/60/24;
$days_closed_ago=$issue->closed_at?$day_today-(date('U',strtotime($issue->closed_at))/60/60/24):null;
$days_updated_ago=$day_today-(date('U',strtotime($issue->updated_at))/60/60/24);
echo "Weekly: ". $issue->title." C:".$issue->closed_at." ($days_closed_ago) U:".$issue->updated_at." ($days_updated_ago) \n";
if( $days_closed_ago>7 ){
if($issue->state=="closed"){
$issue->state='open';
$issue->save();
}else{
if($days_updated_ago>3){
$issue->addComment("NAG!");
}
}
}
};
$monthly=function($issue) use ($terms){
$day_today=date('U')/60/60/24;
$days_closed_ago=$issue->closed_at?$day_today-(date('U',strtotime($issue->closed_at))/60/60/24):null;
$days_updated_ago=$day_today-(date('U',strtotime($issue->updated_at))/60/60/24);
$month_closed=date('m/Y',strtotime($issue->closed_at));
$month_today=date('m/Y');
echo "Monthly: ". $issue->title." [MC: $month_closed, MT: $month_today] C:".$issue->closed_at." ($days_closed_ago) U:".$issue->updated_at." ($days_updated_ago) \n";
//print_r($issue->labels);
//print_r(getRepeatTerms($issue,$terms));
//die();
if($month_closed!=$month_today){
if($issue->state=="closed"){
$issue->state='open';
$issue->save();
}else{
if($days_updated_ago>7){
$issue->addComment("NAG!");
}
}
}
};
function getRepeatTerms($issue,$terms){
$labels=[];
$found=[];
foreach($issue->labels as $label){
$labels[]=$label->name;
}
print_r($labels);
foreach($terms as $name=>$values){
foreach($values as $value){
if( in_array("$value/repeat",$labels) ){
$found[$name][]=$value;
}
}
}
return $found;
}
//prep the curent day/month names
$day_name=strtolower(date('l'));
$month_name=strtolower(date('F'));
// nag about open issues
$repo->forIssues($weekly,['labels'=>'weekly/repeat']);
$repo->forIssues($daily,['labels'=>'daily/repeat']);
$repo->forIssues($daily,['labels'=>$day_name.'/repeat']);
$repo->forIssues($monthly,['labels'=>'monthly/repeat']);
$repo->forIssues($monthly,['labels'=>$month_name.'/repeat']);
// reopen closed issues
$repo->forIssues($weekly,['state'=>'closed','labels'=>'weekly/repeat']);
$repo->forIssues($daily,['state'=>'closed','labels'=>'daily/repeat']);
$repo->forIssues($daily,['state'=>'closed','labels'=>$day_name.'/repeat']);
$repo->forIssues($monthly,['state'=>'closed','labels'=>'monthly/repeat']);
$repo->forIssues($monthly,['state'=>'closed','labels'=>$month_name.'/repeat']);

View File

@ -1,13 +1,25 @@
#!/usr/bin/php #!/usr/bin/php
<?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 // load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available // we will then have $client and $repo available
require('setup.php'); require('setup.php');
use RRule\RRule; use RRule\RRule;
//callback function to add/replace the tag with our text //function to add/replace the tag with our text
/*
function updateIssueBody($issue,$text){ function updateIssueBody($issue,$text){
$issue->addComment($text);
return;
// add a tag to the body if to doesn't exist // add a tag to the body if to doesn't exist
if( strpos($issue->body,'<!--BOT-->')===false ){ if( strpos($issue->body,'<!--BOT-->')===false ){
$issue->body.="\n<!--BOT--><hr/><!--ENDBOT-->"; $issue->body.="\n<!--BOT--><hr/><!--ENDBOT-->";
@ -16,9 +28,15 @@ function updateIssueBody($issue,$text){
$issue->body=preg_replace("#<!--BOT-->.*?<!--ENDBOT-->#s",'<!--BOT--><hr/>'.$text.'<!--ENDBOT-->',$issue->body); $issue->body=preg_replace("#<!--BOT-->.*?<!--ENDBOT-->#s",'<!--BOT--><hr/>'.$text.'<!--ENDBOT-->',$issue->body);
$issue->save(); $issue->save();
}; };
*/
//loop through each issue that has a RRULE //loop through each issue that has a RRULE
foreach($repo->getIssues(['q'=>'RRULE:']) as $issue){ $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 //extract the rule text
if(preg_match('/RRULE:\s?(.*)/',$issue->body,$matches)){ if(preg_match('/RRULE:\s?(.*)/',$issue->body,$matches)){
$rruletxt=$matches[1]; $rruletxt=$matches[1];
@ -26,12 +44,12 @@ foreach($repo->getIssues(['q'=>'RRULE:']) as $issue){
try{ try{
$rrule = new RRule($rruletxt); $rrule = new RRule($rruletxt);
}catch(Exception $e){ }catch(Exception $e){
$result="\n```plain\n".$e->getMessage()." ($rruletxt)\n```\n"; $result="Error in RRULE\n```plain\n".$e->getMessage()."\n\n$rruletxt\n```\n";
updateIssueBody($issue,$result); $issue->addComment($result);
continue; continue;
} }
}else{ //couldn't extract, report error }else{ //couldn't extract, report error
updateIssueBody($issue,"Can't extract RRULE:"); $issue->addComment("Can't extract RRULE:");
continue; continue;
} }
@ -42,22 +60,32 @@ foreach($repo->getIssues(['q'=>'RRULE:']) as $issue){
$next_exc=$rrule->getOccurrencesAfter(date('Y-m-d'),$inclusive=false,$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'); $today=$next_inc->format('Y-m-d')==date('Y-m-d');
//some debug if($debug){
$result=$rrule->humanReadable(). echo $rrule->humanReadable().
"<br/>\n NOW: ".date('Y-m-d'). "\n NOW: ".date('Y-m-d').
"<br/>\n First: ".$rrule[0]->format('Y-m-d'). "\n First: ".$rrule[0]->format('Y-m-d').
"<br/>\n Second: ".$rrule[1]->format('Y-m-d'). "\n Second: ".$rrule[1]->format('Y-m-d').
"<br/>\n Today: ".($today?'Yes':'no'). "\n Today: ".($today?'Yes':'no').
"<br/>\n Next Inc: ".$next_inc->format('Y-m-d'). "\n Next Inc: ".$next_inc->format('Y-m-d').
"<br/>\n Next Exc: ".$next_exc->format('Y-m-d'); "\n Next Exc: ".$next_exc->format('Y-m-d').
updateIssueBody($issue,$result); "\n";
}
//is it today? //is it today?
if($today){ if($today){
$issue->addComment("Today (NEXT:".$next_exc->format('Y-m-d').")"); 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 // not working https://github.com/go-gitea/gitea/issues/8179
//$issue->due_date=$rrule[0]->format('c'); //$issue->due_date=$rrule[0]->format('c');
//$issue->save(); //$issue->save();
//if( date('Y-m-d',strtotime($issue->due_date)) != $next_exc->format('Y-m-d') ){
/*if( !$issue->due_date ){
$issue->setDueDate($next_exc);
}*/
} }