Compare commits

...

41 Commits

Author SHA1 Message Date
James
907c686bad improve exception throwing with custom exception and use of code 2021-01-01 12:14:29 +00:00
James
83eea50cf9 update getTime functions to support upstream since/before args 2020-02-02 15:09:10 +00:00
James
b8a7da7a79 remove setDueDate function now upstream bug is fixed 2019-12-26 13:02:31 +00:00
James
42952f6d74 add function to get pull requests (Repo::getPulls) 2019-12-08 18:48:19 +00:00
James
ba51d4ad7d BUGFIX: Don't hardcode release attachemnt ID 2019-11-22 14:26:03 +00:00
James
0ce51d3306 add more get functions 2019-10-14 20:13:42 +01:00
James
311a8ad399 add getUserById 2019-10-14 20:13:14 +01:00
James
1c3738c365 add getUserTimes function 2019-10-14 18:29:52 +01:00
James
cd20d09279 add a setDueDate function 2019-09-30 09:35:34 +01:00
James
aef0c2166e Add Attachment support 2019-09-23 12:14:59 +01:00
a2c44392b2 Update 'README.md' 2019-09-07 09:43:48 +01:00
44344b6f23 Update 'README.md' 2019-09-07 09:41:30 +01:00
3ed592ce38 Update 'README.md' 2019-09-07 09:35:38 +01:00
James
238ce05f9a fix namespace bug (#2) 2019-09-07 09:25:34 +01:00
James
927e68f71b only POST new/changed data to the API when saving (#2) 2019-09-07 09:22:07 +01:00
James
fe713f6926 add getIssues (#1) 2019-09-07 09:02:53 +01:00
James
cd79454aa7 Update 'README.md' 2019-09-05 17:15:58 +01:00
James
47d54d63f7 support for comments 2019-09-05 16:27:23 +01:00
James
5959b1ffe5 add 204 as an non-error response code from API (recieved after a DELETE) 2019-09-03 10:55:41 +01:00
James
5cbf35add3 add deleteLabel 2019-09-02 20:36:16 +01:00
James
d56a75b629 bug: url not being set properly when reading from cache 2019-08-29 10:55:05 +01:00
James
a0865766b3 fix: warning: undefined index for $cache['labels'] 2019-08-29 07:45:08 +01:00
James
7aad548198 Update 'README.md' 2019-08-26 10:10:37 +01:00
James
d5c65bcb7e remove referances to old examples (now in seperate repo) 2019-08-26 09:51:06 +01:00
James
22b423cd5e fix namespace of Exception 2019-08-26 09:46:49 +01:00
James
4747e9b270 fix autoloader in composer 2019-08-26 09:40:58 +01:00
James
1064c50407 Update 'composer.json' 2019-08-26 09:07:38 +01:00
James
29e5699027 update composer with namespace info 2019-08-25 12:39:21 +01:00
James
2f04372d87 add namespaces 2019-08-25 12:30:35 +01:00
Bob
d4bdbc2c16 remove example code to seperate project 2019-08-25 10:58:21 +01:00
James
5eb7e5f872 Update 'README.md' 2019-08-25 10:47:04 +01:00
James
bb1e21db2e Update 'README.md' 2019-08-25 10:45:41 +01:00
James
cda81d95ae added install for composer 2019-08-25 09:27:36 +01:00
James
d6ca959226 add composer.json 2019-08-24 14:52:48 +01:00
James
9be8133616 add new close all example 2019-08-24 14:31:35 +01:00
James
a6bb48ef96 cache label names for MUCH quicker scripts when getting many labels 2019-08-24 14:31:26 +01:00
James
d85fe08504 update link to new location 2019-08-16 16:49:02 +01:00
James
ed0e70898a update example 2019-08-16 16:48:06 +01:00
James
6123f69996 fix examples and move into a dir of their own 2019-08-16 16:42:24 +01:00
James
1ee076aac6 throw exception on API error 2019-08-16 16:30:38 +01:00
James
ba76fded92 importer 2019-08-02 20:01:42 +01:00
14 changed files with 220 additions and 187 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
/config.php
/vendor/

View File

@ -1,5 +1,32 @@
# GiteaBot
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/jhodges/GiteaBot-examples
## Change Log
V1.2.1
* Fixed #1 Added Repo::GetIssues()
* Fixed #2 Only POST changed data on save
* Updated docs
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

View File

@ -1,4 +0,0 @@
<?php
spl_autoload_register(function ($class_name) {
include 'src/'.$class_name . '.php';
});

18
composer.json Normal file
View 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": {}
}

View File

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

View File

@ -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);

View File

@ -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']);

10
src/ApiException.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace JHodges\GiteaBot;
class ApiException extends \Exception{
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}

View File

@ -1,4 +1,6 @@
<?php
namespace JHodges\GiteaBot;
class Client{
public function __construct($url,$user,$pass,$debug=false){
@ -27,24 +29,54 @@ class Client{
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if($type!='GET'){
if($type!='GET' && $type!='FORM'){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
}
if (!is_null($postFields)) {
$postFields = json_encode($postFields);
if(!is_null($postFields)) {
if($type=='FORM'){
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
}else{
$postFields = json_encode($postFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json',
'Content-Length: '.strlen($postFields), ]);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json',
'Content-Length: '.strlen($postFields), ]);
}
if($this->user) {
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
}
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return json_decode($result);
if(in_array($status_code,[200,201,204])){
return json_decode($result);
}else{
throw new ApiException($result,$status_code);
}
}
public function getUserById($id){
$url="{$this->url}users/search?uid={$id}";
$data=$this->request($url,'GET');
return $data->data[0];
}
public function getCurrentUserTimes(\DateTime $since=null, \DateTime $before=null){
$args=[];
if($before){
$args['before'] = $before->format('c');
}
if($since){
$args['since'] = $since->format('c');
}
$url="{$this->url}user/times";
if($args){
$url.='?'.http_build_query($args);
}
$data=$this->request($url,'GET');
return $data;
}
}

View File

@ -1,17 +1,22 @@
<?php
namespace JHodges\GiteaBot;
class GiteaData{
protected $data=null;
protected $newData=null;
public function __construct($data=null){
$this->newData=new \stdClass();
if(!$data){
$data=new stdClass();
$data=new \stdClass();
}
$this->data=$data;
}
public function __set($k,$v){
$this->data->$k=$v;
$this->newData->$k=$v;
}
public function __get($k){

View File

@ -1,4 +1,6 @@
<?php
namespace JHodges\GiteaBot;
class GiteaRepoData extends GiteaData{
private $repo;
@ -13,7 +15,7 @@ class GiteaRepoData extends GiteaData{
}
public function save(){
return $this->repo->request($this->url,'PATCH',$this->data);
return $this->repo->request($this->url,'PATCH',$this->newData);
}
}

View File

@ -1,4 +1,6 @@
<?php
namespace JHodges\GiteaBot;
class Issue extends GiteaRepoData{
public function __construct(Repo $repo,$data){
@ -24,6 +26,10 @@ class Issue extends GiteaRepoData{
return $this->request($this->url.'/comments','POST',$args);
}
public function getComments(){
return $this->request($this->url.'/comments','GET');
}
public function hasLabel($label){
$id=$label->id;
foreach($this->labels as $label){

View File

@ -1,4 +1,6 @@
<?php
namespace JHodges\GiteaBot;
class Label extends GiteaRepoData{
}

View File

@ -1,6 +1,10 @@
<?php
namespace JHodges\GiteaBot;
class Repo{
private $cache=[];
public function __construct(Client $client,$user,$repo){
$this->client=$client;
$this->user=$user;
@ -28,10 +32,58 @@ class Repo{
}
}
public function getIssues($args=null){
$page=1;
$all_issues=[];
while(1){
$url="repos/{$this->user}/{$this->repo}/issues?page=$page";
if($args){
$url.='&'.http_build_query($args);
}
$issues=$this->client->request($url);
foreach($issues as $data){
$issue=new Issue($this,$data);
$all_issues[]=$issue;
}
if(!$issues) break;
$page++;
}
return $all_issues;
}
public function getPulls($args=null){
$page=1;
$all_pulls=[];
while(1){
$url="repos/{$this->user}/{$this->repo}/pulls?page=$page";
if($args){
$url.='&'.http_build_query($args);
}
$pulls=$this->client->request($url);
foreach($pulls as $data){
$pull=$data;//new Issue($this,$data);
$all_pulls[]=$pull;
}
if(!$pulls) break;
$page++;
}
return $all_pulls;
}
public function getIssue($id){
$url="repos/{$this->user}/{$this->repo}/issues/{$id}";
$data=$this->client->request($url);
return new Issue($this,$data);
}
public function getLabelByName($name){
$url="repos/{$this->user}/{$this->repo}/labels";
$data=$this->client->request($url);
foreach($data as $datum){
if(!isset($this->cache['lables'])){
$this->cache['lables']=$this->client->request($url);
}
foreach($this->cache['lables'] as $datum){
if($datum->name==$name){
$datum->url=$url."/".$datum->id; // not set by api for some reason (bug?)
return new Label($this,$datum);
@ -47,6 +99,15 @@ class Repo{
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){
$url="repos/{$this->user}/{$this->repo}/issues";
$data=$this->client->request($url,'POST',$args);
@ -54,4 +115,46 @@ class Repo{
return new Issue($this,$data);
}
public function addReleaseAttachment($releaseId,$path){
$url="repos/{$this->user}/{$this->repo}/releases/$releaseId/assets?name=test";
$args=['attachment'=>new \CurlFile($path)];
$data=$this->client->request($url,'FORM',$args);
return $data;
}
public function getTimes(\DateTime $since=null, \DateTime $before=null){
$args=[];
if($before){
$args['before'] = $before->format('c');
}
if($since){
$args['since'] = $since->format('c');
}
$url="repos/{$this->user}/{$this->repo}/times";
if($args){
$url.='?'.http_build_query($args);
}
return $this->client->request($url,'GET');
}
public function getTimesByUsername($username,\DateTime $since=null, \DateTime $before=null){
$args=[];
if($before){
$args['before'] = $before->format('c');
}
if($since){
$args['since'] = $since->format('c');
}
$url="repos/{$this->user}/{$this->repo}/times/{$username}";
if($args){
$url.='?'.http_build_query($args);
}
return $this->client->request($url,'GET');
}
public function getUsers(){
$url="repos/{$this->user}/{$this->repo}/collaborators";
return $this->client->request($url,'GET');
}
}