v1.2 #3
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1 @@
|
|||||||
/config.php
|
/vendor/
|
||||||
|
25
README.md
25
README.md
@ -1,5 +1,26 @@
|
|||||||
|
# GiteaBot
|
||||||
Simple PHP library to interface with Gitea API
|
Simple PHP library to interface with Gitea API
|
||||||
|
|
||||||
Designed to make a "Bot" for manipulating labels on issues.
|
Designed to make a "Bot" for manipulating labels and issues.
|
||||||
|
|
||||||
Usage example: [no_label_milestone.php](https://git.jhodges.co.uk/james/GiteaBot/src/branch/master/no_label_milestone.php)
|
For usage examples see https://git.jhodges.co.uk/james/GiteaBot-examples
|
||||||
|
|
||||||
|
## Change Log
|
||||||
|
|
||||||
|
v1.2
|
||||||
|
|
||||||
|
* Added namespacing and composer.json
|
||||||
|
* Moved usage example code to seperate repo
|
||||||
|
* Added support for add/delete comment
|
||||||
|
|
||||||
|
V1.1.1
|
||||||
|
|
||||||
|
* Refactor, bootstrap, examples
|
||||||
|
|
||||||
|
V1.1
|
||||||
|
|
||||||
|
* Refactor, better OO, examples
|
||||||
|
|
||||||
|
v1.0
|
||||||
|
|
||||||
|
* Initial Prototype
|
@ -1,4 +0,0 @@
|
|||||||
<?php
|
|
||||||
spl_autoload_register(function ($class_name) {
|
|
||||||
include 'src/'.$class_name . '.php';
|
|
||||||
});
|
|
18
composer.json
Normal file
18
composer.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "jhodges/giteabot",
|
||||||
|
"description": "Simple PHP library to interface with Gitea API",
|
||||||
|
"type": "library",
|
||||||
|
"license": "GPL3",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "James",
|
||||||
|
"email": "inbox.dev@jhodges.co.uk"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"JHodges\\GiteaBot\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"require": {}
|
||||||
|
}
|
@ -1,6 +0,0 @@
|
|||||||
<?php
|
|
||||||
return [
|
|
||||||
'url'=>'',
|
|
||||||
'user'=>'',
|
|
||||||
'pass'=>'',
|
|
||||||
];
|
|
@ -1,48 +0,0 @@
|
|||||||
<?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('bootstrap.php');
|
|
||||||
|
|
||||||
$config=require('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
repeat.php
114
repeat.php
@ -1,114 +0,0 @@
|
|||||||
<?php
|
|
||||||
require('bootstrap.php');
|
|
||||||
|
|
||||||
$config=require("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,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace JHodges\GiteaBot;
|
||||||
|
|
||||||
class Client{
|
class Client{
|
||||||
|
|
||||||
public function __construct($url,$user,$pass,$debug=false){
|
public function __construct($url,$user,$pass,$debug=false){
|
||||||
@ -42,9 +44,14 @@ class Client{
|
|||||||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||||
curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
|
curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
|
||||||
}
|
}
|
||||||
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
||||||
$result = curl_exec($ch);
|
$result = curl_exec($ch);
|
||||||
|
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
|
if(in_array($status_code,[200,201,204])){
|
||||||
return json_decode($result);
|
return json_decode($result);
|
||||||
|
}else{
|
||||||
|
throw new \Exception("API $status_code Code: ".$result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace JHodges\GiteaBot;
|
||||||
|
|
||||||
class GiteaData{
|
class GiteaData{
|
||||||
|
|
||||||
protected $data=null;
|
protected $data=null;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace JHodges\GiteaBot;
|
||||||
|
|
||||||
class GiteaRepoData extends GiteaData{
|
class GiteaRepoData extends GiteaData{
|
||||||
|
|
||||||
private $repo;
|
private $repo;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace JHodges\GiteaBot;
|
||||||
|
|
||||||
class Issue extends GiteaRepoData{
|
class Issue extends GiteaRepoData{
|
||||||
|
|
||||||
public function __construct(Repo $repo,$data){
|
public function __construct(Repo $repo,$data){
|
||||||
@ -24,6 +26,10 @@ class Issue extends GiteaRepoData{
|
|||||||
return $this->request($this->url.'/comments','POST',$args);
|
return $this->request($this->url.'/comments','POST',$args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getComments(){
|
||||||
|
return $this->request($this->url.'/comments','GET');
|
||||||
|
}
|
||||||
|
|
||||||
public function hasLabel($label){
|
public function hasLabel($label){
|
||||||
$id=$label->id;
|
$id=$label->id;
|
||||||
foreach($this->labels as $label){
|
foreach($this->labels as $label){
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace JHodges\GiteaBot;
|
||||||
|
|
||||||
class Label extends GiteaRepoData{
|
class Label extends GiteaRepoData{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
20
src/Repo.php
20
src/Repo.php
@ -1,6 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace JHodges\GiteaBot;
|
||||||
|
|
||||||
class Repo{
|
class Repo{
|
||||||
|
|
||||||
|
private $cache=[];
|
||||||
|
|
||||||
public function __construct(Client $client,$user,$repo){
|
public function __construct(Client $client,$user,$repo){
|
||||||
$this->client=$client;
|
$this->client=$client;
|
||||||
$this->user=$user;
|
$this->user=$user;
|
||||||
@ -30,8 +34,11 @@ class Repo{
|
|||||||
|
|
||||||
public function getLabelByName($name){
|
public function getLabelByName($name){
|
||||||
$url="repos/{$this->user}/{$this->repo}/labels";
|
$url="repos/{$this->user}/{$this->repo}/labels";
|
||||||
$data=$this->client->request($url);
|
if(!isset($this->cache['lables'])){
|
||||||
foreach($data as $datum){
|
$this->cache['lables']=$this->client->request($url);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($this->cache['lables'] as $datum){
|
||||||
if($datum->name==$name){
|
if($datum->name==$name){
|
||||||
$datum->url=$url."/".$datum->id; // not set by api for some reason (bug?)
|
$datum->url=$url."/".$datum->id; // not set by api for some reason (bug?)
|
||||||
return new Label($this,$datum);
|
return new Label($this,$datum);
|
||||||
@ -47,6 +54,15 @@ class Repo{
|
|||||||
return new Label($this,$data);
|
return new Label($this,$data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function deleteLabel($label){
|
||||||
|
$data=$this->client->request($label->url,'DELETE');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteComment($comment){
|
||||||
|
$url="repos/{$this->user}/{$this->repo}/issues/comments/{$comment->id}";
|
||||||
|
$data=$this->client->request($url,'DELETE');
|
||||||
|
}
|
||||||
|
|
||||||
public function createIssue($args){
|
public function createIssue($args){
|
||||||
$url="repos/{$this->user}/{$this->repo}/issues";
|
$url="repos/{$this->user}/{$this->repo}/issues";
|
||||||
$data=$this->client->request($url,'POST',$args);
|
$data=$this->client->request($url,'POST',$args);
|
||||||
|
Loading…
Reference in New Issue
Block a user