This commit is contained in:
Bob 2019-08-25 11:04:55 +01:00
commit 41ff24e0ab
10 changed files with 286 additions and 0 deletions

2
.gitignore vendored Normal file
View File

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

3
README.md Normal file
View File

@ -0,0 +1,3 @@
Examples for GiteaBot
Copy config.php.dist to config.php and set your Gitea instalce URL and bots user/pass.

19
composer.json Normal file
View File

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

41
composer.lock generated Normal file
View File

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

20
src/close_all.php Normal file
View File

@ -0,0 +1,20 @@
<?php
require(__DIR__.'/../vendor/autoload.php');
$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]);
//close all issues
$repo->forIssues(function($issue){
$issue->state='closed';
$issue->save();
});

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

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

View File

@ -0,0 +1,48 @@
<?php
//1a. Adds a no-milestone label to any issues with no milestone
//1b. Also removes the label if present on an issue with a milestone
//2a. Adds a no-label label to any issues with no labels (excluding the no-label and no-milestone)
//2b. Also removes the label if present on an issue with a label (excluding the no-label and no-milestone)
require(__DIR__.'/../vendor/autoload.php');
$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]);
//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);

114
src/repeat.php Normal file
View File

@ -0,0 +1,114 @@
<?php
require(__DIR__.'/../vendor/autoload.php');
$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]);
$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']);

1
src/test.csv Normal file
View File

@ -0,0 +1 @@
"example title","example body","test"
1 example title example body test

32
src/test.php Normal file
View File

@ -0,0 +1,32 @@
<?php
require(__DIR__.'/../vendor/autoload.php');
$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]);
//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();