working rrule prototype
This commit is contained in:
parent
02b42c9529
commit
500eff3946
55
src/convert.php
Executable file
55
src/convert.php
Executable 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"]);
|
||||
}
|
129
src/repeat.php
129
src/repeat.php
@ -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']);
|
@ -1,13 +1,25 @@
|
||||
#!/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;
|
||||
|
||||
//callback function to add/replace the tag with our text
|
||||
//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-->";
|
||||
@ -16,9 +28,15 @@ function updateIssueBody($issue,$text){
|
||||
$issue->body=preg_replace("#<!--BOT-->.*?<!--ENDBOT-->#s",'<!--BOT--><hr/>'.$text.'<!--ENDBOT-->',$issue->body);
|
||||
$issue->save();
|
||||
};
|
||||
*/
|
||||
|
||||
//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
|
||||
if(preg_match('/RRULE:\s?(.*)/',$issue->body,$matches)){
|
||||
$rruletxt=$matches[1];
|
||||
@ -26,12 +44,12 @@ foreach($repo->getIssues(['q'=>'RRULE:']) as $issue){
|
||||
try{
|
||||
$rrule = new RRule($rruletxt);
|
||||
}catch(Exception $e){
|
||||
$result="\n```plain\n".$e->getMessage()." ($rruletxt)\n```\n";
|
||||
updateIssueBody($issue,$result);
|
||||
$result="Error in RRULE\n```plain\n".$e->getMessage()."\n\n$rruletxt\n```\n";
|
||||
$issue->addComment($result);
|
||||
continue;
|
||||
}
|
||||
}else{ //couldn't extract, report error
|
||||
updateIssueBody($issue,"Can't extract RRULE:");
|
||||
$issue->addComment("Can't extract RRULE:");
|
||||
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];
|
||||
$today=$next_inc->format('Y-m-d')==date('Y-m-d');
|
||||
|
||||
//some debug
|
||||
$result=$rrule->humanReadable().
|
||||
"<br/>\n NOW: ".date('Y-m-d').
|
||||
"<br/>\n First: ".$rrule[0]->format('Y-m-d').
|
||||
"<br/>\n Second: ".$rrule[1]->format('Y-m-d').
|
||||
"<br/>\n Today: ".($today?'Yes':'no').
|
||||
"<br/>\n Next Inc: ".$next_inc->format('Y-m-d').
|
||||
"<br/>\n Next Exc: ".$next_exc->format('Y-m-d');
|
||||
updateIssueBody($issue,$result);
|
||||
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){
|
||||
$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
|
||||
//$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 ){
|
||||
$issue->setDueDate($next_exc);
|
||||
}*/
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user