commit 3403c1a14835e8cb2ea62480fa8d94bd22fb15ce Author: James Date: Sat Sep 14 12:31:25 2019 +0100 forked from jhodges/GiteaBot-examples diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..011fdab --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/src/config.php +/vendor/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..660611c --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Bot to manage repeating issues. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0ad7839 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "jhodges/giteabot-examples", + "description": "Some examples for the giteabot php api", + "authors": [ + { + "name": "James", + "email": "inbox.dev@jhodges.co.uk" + } + ], + "require": { + "jhodges/giteabot": "~1.2.0" + }, + "repositories": { + "repo-name": { + "type": "composer", + "url": "https://git.jhodges.co.uk/composer" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..dd7a3e0 --- /dev/null +++ b/composer.lock @@ -0,0 +1,44 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "8e21cb9a16c0817b84a9643dc47c1a78", + "packages": [ + { + "name": "jhodges/giteabot", + "version": "v1.2.1", + "source": { + "type": "git", + "url": "https://git.jhodges.co.uk/jhodges/GiteaBot", + "reference": "a2c44392b2b4eede6be4b132e55e3479232fda93" + }, + "type": "library", + "autoload": { + "psr-4": { + "JHodges\\GiteaBot\\": "src/" + } + }, + "license": [ + "GPL3" + ], + "authors": [ + { + "name": "James", + "email": "inbox.dev@jhodges.co.uk" + } + ], + "description": "Simple PHP library to interface with Gitea API", + "time": "2019-09-07T08:43:48+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/src/close_all.php b/src/close_all.php new file mode 100755 index 0000000..9f22e76 --- /dev/null +++ b/src/close_all.php @@ -0,0 +1,16 @@ +#!/usr/bin/php +getIssues() as $issue){ + $issue->state='closed'; + $issue->save(); +} diff --git a/src/config.php.dist b/src/config.php.dist new file mode 100644 index 0000000..c1e65f0 --- /dev/null +++ b/src/config.php.dist @@ -0,0 +1,6 @@ +'', + 'user'=>'', + 'pass'=>'', +]; diff --git a/src/delete_labels.php b/src/delete_labels.php new file mode 100755 index 0000000..3939b7e --- /dev/null +++ b/src/delete_labels.php @@ -0,0 +1,27 @@ +#!/usr/bin/php +getLabelByName($name); + if($label){ //it exists + //delete it + $repo->deleteLabel($label); + } +} diff --git a/src/delete_me.php b/src/delete_me.php new file mode 100755 index 0000000..38d6495 --- /dev/null +++ b/src/delete_me.php @@ -0,0 +1,28 @@ +#!/usr/bin/php +getIssues(['q'=>'delete me']) as $issue){ + // check for exact match + if($issue->title=='delete me'){ + // remove all labels + foreach($issue->labels as $label){ + $issue->removeLabel($label); + } + //change status and title. + $issue->state='closed'; + $issue->title='deleted'; + $issue->save(); + } +}; diff --git a/src/delete_nags.php b/src/delete_nags.php new file mode 100755 index 0000000..0f15da6 --- /dev/null +++ b/src/delete_nags.php @@ -0,0 +1,24 @@ +#!/usr/bin/php +getIssues(['q'=>'NAG!']) as $issue){ + // loop through the current issues comments + foreach($issue->getComments() as $comment){ + // if the comment body matches + if($comment->body=='NAG!'){ + // delete the commend + $repo->deleteComment($comment); + } + } +} diff --git a/src/due.php b/src/due.php new file mode 100644 index 0000000..c365671 --- /dev/null +++ b/src/due.php @@ -0,0 +1,55 @@ +#!/usr/bin/php +'today','this week'=>'this week','this month'=>'this month']; + +/* load or create the labels */ +foreach($labels as $k=>$v){ + $label=$repo->getLabelByName($v); + if(!$label){ + $label=$repo->createLabel([ + 'name'=>$v, + 'color'=>'#00ffff', + 'description'=>'Due $v' + ]); + } + $labels[$k]=$label; +} + +// we are processing ALL issues, and not changing the state +// (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1) +// so we can use forIssues() here to keep memory usage down +$repo->forIssues(function($issue) use ($labels) { + if($issue->due_date){ + $day_today=date('U')/60/60/24; + $days_until_due=(date('U',strtotime($issue->due_date))/60/60/24)-$day_today; + echo "$issue->title : $days_until_due \n"; + if($days_until_due<0){ + $issue->addComment("NAG!"); + } + if($days_until_due<1){ + if(!$issue->hasLabel($labels['today'])){ + $issue->addLabel($labels['today']); + } + } + if($days_until_due<7){ + if(!$issue->hasLabel($labels['this week'])){ + $issue->addLabel($labels['this week']); + } + } + if($days_until_due<30){ + if(!$issue->hasLabel($labels['this month'])){ + $issue->addLabel($labels['this month']); + } + } + } +}); diff --git a/src/edit.php b/src/edit.php new file mode 100755 index 0000000..67b802c --- /dev/null +++ b/src/edit.php @@ -0,0 +1,31 @@ +#!/usr/bin/php +number."\n".$issue->title."\ntexting\n".time()."\n```\n"; +}; + +//callback function to add/replace the tag with our text +$tmp=function($issue) use ($text){ + // add a tag to the body if to doesn't exist + if( strpos($issue->body,'')===false ){ + $issue->body.='
'; + } + //replace the tag contents with the result from our $text function + $issue->body=preg_replace("#.*?#s",''.$text($issue).'',$issue->body); + $issue->save(); +}; + +//process ALL issues +$repo->forIssues($tmp); diff --git a/src/repeat.php b/src/repeat.php new file mode 100755 index 0000000..a7de9d4 --- /dev/null +++ b/src/repeat.php @@ -0,0 +1,129 @@ +#!/usr/bin/php +['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']); diff --git a/src/setup.php b/src/setup.php new file mode 100644 index 0000000..bd77de3 --- /dev/null +++ b/src/setup.php @@ -0,0 +1,17 @@ + [debug]\n"); +} +$debug = ($argc==4 && $argv[3]=='debug'); + +//open connection and repo +$client=new Client($config['url'],$config['user'],$config['pass'],$debug); +$repo=$client->getRepo($argv[1],$argv[2]); +