forked from jhodges/GiteaBot-examples

This commit is contained in:
James 2019-09-14 12:31:25 +01:00
commit 3403c1a148
13 changed files with 399 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/src/config.php
/vendor/

1
README.md Normal file
View File

@ -0,0 +1 @@
Bot to manage repeating issues.

19
composer.json Normal file
View File

@ -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"
}
}
}

44
composer.lock generated Normal file
View File

@ -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": []
}

16
src/close_all.php Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/php
<?php
/******************
* close ALL issues
******************/
// 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');
// close all issues
// we use getIssues() here rather than forIssues() (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1)
foreach($repo->getIssues() as $issue){
$issue->state='closed';
$issue->save();
}

6
src/config.php.dist Normal file
View File

@ -0,0 +1,6 @@
<?php
return [
'url'=>'',
'user'=>'',
'pass'=>'',
];

27
src/delete_labels.php Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/php
<?php
/****************************************************
* This will loop through a list of label names
* If the label exists on the repo, delete the label
****************************************************/
// 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
$names=[
'janurary','feburary','march','april','may','june','july','august','september','october','november','december',
'monday','tuesday','wednesday','thursday','friday','saturday','sunday',
'daily','weekly','fortnightly','monthly','yearly'
];
//loop through the names
foreach($names as $name){
//see if label exists?
$label=$repo->getLabelByName($name);
if($label){ //it exists
//delete it
$repo->deleteLabel($label);
}
}

28
src/delete_me.php Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/php
<?php
/**************************************************
* this will process all issues titled "delete me":
* 1. remove all labels
* 2. close the issue
* 3. change the title to "deleted"
**************************************************/
// 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');
// loop through all matching issues
// we use getIssues() here rather than forIssues() (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1)
foreach($repo->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();
}
};

24
src/delete_nags.php Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/php
<?php
/**************************************************************
* This will delete all comments that match a specific keyword
* 1. Process all issues that match "NAG!":
* 2. Delete any comments that body exactly match "NAG!"
**************************************************************/
// 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');
// loop through the issues
// we use getIssues() here rather than forIssues() (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1)
foreach($repo->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);
}
}
}

55
src/due.php Normal file
View File

@ -0,0 +1,55 @@
#!/usr/bin/php
<?php
/**************************************************
* This will examine all issues, and add lables
* as the due date approaches!
**************************************************/
// 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 lebels we will use
$labels=['today'=>'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']);
}
}
}
});

31
src/edit.php Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/php
<?php
/*******************************************************
* This will loop through all issues and add some custom
* text to the issue body. It will use a placeholder so
* that it can be automatically updated without replacing
* the whole issue body
********************************************************/
// 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');
//this is the function to generate the custom text
$text=function($issue){
return "\n```plain\n".$issue->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,'<!--BOT-->')===false ){
$issue->body.='<hr/><!--BOT--><!--ENDBOT-->';
}
//replace the tag contents with the result from our $text function
$issue->body=preg_replace("#<!--BOT-->.*?<!--ENDBOT-->#s",'<!--BOT-->'.$text($issue).'<!--ENDBOT-->',$issue->body);
$issue->save();
};
//process ALL issues
$repo->forIssues($tmp);

129
src/repeat.php Executable file
View File

@ -0,0 +1,129 @@
#!/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']);

17
src/setup.php Normal file
View File

@ -0,0 +1,17 @@
<?php
require(__DIR__.'/../vendor/autoload.php');
use JHodges\GiteaBot\Client;
$config=require(__DIR__.'/config.php');
//pass cmd line args
if($argc<3){
die(basename(__FILE__)." <USER> <REPO> [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]);