From 41ff24e0ab4dd494923d3700c5eb5bae51a5652d Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 25 Aug 2019 11:04:55 +0100 Subject: [PATCH] init --- .gitignore | 2 + README.md | 3 + composer.json | 19 +++++++ composer.lock | 41 +++++++++++++ src/close_all.php | 20 +++++++ src/config.php.dist | 6 ++ src/no_label_milestone.php | 48 ++++++++++++++++ src/repeat.php | 114 +++++++++++++++++++++++++++++++++++++ src/test.csv | 1 + src/test.php | 32 +++++++++++ 10 files changed, 286 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 src/close_all.php create mode 100644 src/config.php.dist create mode 100644 src/no_label_milestone.php create mode 100644 src/repeat.php create mode 100644 src/test.csv create mode 100644 src/test.php 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..2b66a6a --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Examples for GiteaBot + +Copy config.php.dist to config.php and set your Gitea instalce URL and bots user/pass. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0d6b98a --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "acme/bot", + "description": "my bot!", + "authors": [ + { + "name": "Me", + "email": "me@example.com" + } + ], + "require": { + "jh/giteabot": "dev-composer" + }, + "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..222d622 --- /dev/null +++ b/composer.lock @@ -0,0 +1,41 @@ +{ + "_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": "e4333e0eb4df20a9093b9e14e4ac3536", + "packages": [ + { + "name": "jh/giteabot", + "version": "dev-composer", + "source": { + "type": "git", + "url": "https://git.jhodges.co.uk/james/GiteaBot", + "reference": "d6ca95922617f25903dda431d9ed695c7a270ae4" + }, + "type": "library", + "license": [ + "OS" + ], + "authors": [ + { + "name": "James", + "email": "inbox.dev@jhodges.co.uk" + } + ], + "description": "Simple PHP library to interface with Gitea API", + "time": "2019-08-24T13:52:48+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": { + "jh/giteabot": 20 + }, + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/src/close_all.php b/src/close_all.php new file mode 100644 index 0000000..33b6cd3 --- /dev/null +++ b/src/close_all.php @@ -0,0 +1,20 @@ + [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]); + +//close all issues +$repo->forIssues(function($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/no_label_milestone.php b/src/no_label_milestone.php new file mode 100644 index 0000000..4113927 --- /dev/null +++ b/src/no_label_milestone.php @@ -0,0 +1,48 @@ + [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]); + +//get the labels of interest +$nolabelLabel=$repo->getLabelByName('no-label'); +if(!$nolabelLabel) die ("Can't find 'no-label' label in repo\n"); + +$nomilestoneLabel=$repo->getLabelByName('no-milestone'); +if(!$nomilestoneLabel) die ("Can't find 'no-milestone' label in repo\n"); + +//define the function to process each issue +$callback=function($issue) use ($nolabelLabel,$nomilestoneLabel){ + // do the no-label thing + $labelCount=count($issue->labels); + if($issue->hasLabel($nolabelLabel)) $labelCount--; //dont count the no-label label + if($nomilestoneLabel && $issue->hasLabel($nomilestoneLabel)) $labelCount--; //dont count the no-milestone label + if($labelCount==0 && !$issue->hasLabel($nolabelLabel) ){ + $issue->addLabel( $nolabelLabel ); + }elseif( $labelCount>0 && $issue->hasLabel($nolabelLabel) ){ + $issue->removeLabel( $nolabelLabel ); + } + //do the no-milestone thing + if( !$issue->milestone && !$issue->hasLabel($nomilestoneLabel) ){ + $issue->addLabel( $nomilestoneLabel ); + }elseif( $issue->milestone && $issue->hasLabel($nomilestoneLabel) ){ + $issue->removeLabel( $nomilestoneLabel ); + } +}; + +//loop through issues and call the callback +$repo->forIssues($callback); diff --git a/src/repeat.php b/src/repeat.php new file mode 100644 index 0000000..d924a0a --- /dev/null +++ b/src/repeat.php @@ -0,0 +1,114 @@ + [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]); + +$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 $k=>$vs){ + foreach($vs as $v){ + $label=$repo->getLabelByName($v); + if(!$label){ + $label=$repo->createLabel(['name'=>$new_name,'color'=>'#ffff00','description'=>'Reopen this issue with the given frequency']); + } + } +} +*/ + +//define the function to process each issue + +$daily=function($issue){ + $day_updated=date('d/m/Y',strtotime($issue->updated_at)); + $day_closed=date('d/m/Y',strtotime($issue->closed_at)); + $day_today=date('d/m/Y'); + echo "Daily: ". $issue->title." ".$issue->closed_at." ($day_closed, $day_today) \n"; + if($day_closed!=$day_today){ + if($issue->state=="closed"){ + $issue->state='open'; + $issue->save(); + }else{ + if($day_updated!=$day_today){ + $issue->addComment("NAG!"); + } + } + } +}; + +$monthly=function($issue) use ($terms){ + $month_closed=date('m/Y',strtotime($issue->closed_at)); + $month_today=date('m/Y'); + echo "Monthly: ".$issue->title." ".$issue->closed_at." ($month_closed, $month_today) \n"; + //print_r($issue->labels); + //print_r(getRepeatTerms($issue,$terms)); + //die(); + if($month_closed!=$month_today){ + $issue->state='open'; + $issue->save(); + } +}; + +$weekly=function($issue){ + $day_updated=date('z',strtotime($issue->updated_at)); + $day_closed=date('z',strtotime($issue->closed_at)); + $day_today=date('z'); + echo "Weekly: ". $issue->title." ".$issue->closed_at." ($day_closed, $day_today => ".(abs($day_closed-$day_today)).") \n"; + if( abs($day_closed-$day_today)>7 ){ + if($issue->state=="closed"){ + $issue->state='open'; + $issue->save(); + }else{ + if($day_updated!=$day_today){ + $issue->addComment("NAG!"); + } + } + } +}; + +$tmp=function($issue){ + //$hours_ago=(time()-strtotime($issue->closed_at))/60/60; +}; + +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; +} + +//loop through issues and call the callback +$day_name=strtolower(date('l')); +$month_name=strtolower(date('F')); + +//$repo->forIssues($daily,['state'=>'open','labels'=>'daily/repeat']); +//die(); +$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/test.csv b/src/test.csv new file mode 100644 index 0000000..4ea922e --- /dev/null +++ b/src/test.csv @@ -0,0 +1 @@ +"example title","example body","test" diff --git a/src/test.php b/src/test.php new file mode 100644 index 0000000..917d057 --- /dev/null +++ b/src/test.php @@ -0,0 +1,32 @@ + [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]); + +//get the test label or create if not exist +if(!$label=$repo->getLabelByName('Test')){ + $label=$repo->createLabel(['name'=>'Test','color'=>'#336699']); +} + +//create a test issue +$issue=$repo->createIssue([ + 'title'=>'Test', + 'body'=>'Just testing' +]); +$issue->addLabel($label); +$issue->addComment('Test comment'); +$issue->save(); + +//change the issue body +$issue->body.=' (Edited!)'; +$issue->save();