refactor! v1.1

This commit is contained in:
James 2019-07-06 16:20:33 +01:00
parent dac8ef2142
commit 647feac50a
7 changed files with 105 additions and 56 deletions

View File

@ -5,6 +5,8 @@
//2b. Also removes the label if present on an issue with a label (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("src/Client.php"); require("src/Client.php");
require("src/GiteaData.php");
require("src/GiteaRepoData.php");
require("src/Issue.php"); require("src/Issue.php");
require("src/Label.php"); require("src/Label.php");
require("src/Repo.php"); require("src/Repo.php");
@ -30,7 +32,7 @@ if(!$nomilestoneLabel) die ("Can't find 'no-milestone' label in repo\n");
//define the function to process each issue //define the function to process each issue
$callback=function($issue) use ($nolabelLabel,$nomilestoneLabel){ $callback=function($issue) use ($nolabelLabel,$nomilestoneLabel){
// do the no-label thing // do the no-label thing
$labelCount=count($issue->getData()->labels); $labelCount=count($issue->labels);
if($issue->hasLabel($nolabelLabel)) $labelCount--; //dont count the no-label label if($issue->hasLabel($nolabelLabel)) $labelCount--; //dont count the no-label label
if($nomilestoneLabel && $issue->hasLabel($nomilestoneLabel)) $labelCount--; //dont count the no-milestone label if($nomilestoneLabel && $issue->hasLabel($nomilestoneLabel)) $labelCount--; //dont count the no-milestone label
if($labelCount==0 && !$issue->hasLabel($nolabelLabel) ){ if($labelCount==0 && !$issue->hasLabel($nolabelLabel) ){
@ -39,9 +41,9 @@ $callback=function($issue) use ($nolabelLabel,$nomilestoneLabel){
$issue->removeLabel( $nolabelLabel ); $issue->removeLabel( $nolabelLabel );
} }
//do the no-milestone thing //do the no-milestone thing
if( !$issue->getData()->milestone && !$issue->hasLabel($nomilestoneLabel) ){ if( !$issue->milestone && !$issue->hasLabel($nomilestoneLabel) ){
$issue->addLabel( $nomilestoneLabel ); $issue->addLabel( $nomilestoneLabel );
}elseif( $issue->getData()->milestone && $issue->hasLabel($nomilestoneLabel) ){ }elseif( $issue->milestone && $issue->hasLabel($nomilestoneLabel) ){
$issue->removeLabel( $nomilestoneLabel ); $issue->removeLabel( $nomilestoneLabel );
} }
}; };

56
repeat.php Normal file
View File

@ -0,0 +1,56 @@
<?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("src/Client.php");
require("src/GiteaData.php");
require("src/GiteaRepoData.php");
require("src/Issue.php");
require("src/Label.php");
require("src/Repo.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){
//$hours_ago=(time()-strtotime($issue->closed_at))/60/60;
$day_closed=date('z',strtotime($issue->closed_at));
$day_today=date('z');
echo $issue->title." ".$issue->closed_at." ($day_closed, $day_today) \n";
if($day_closed!=$day_today){
$issue->state='open';
$issue->save();
}
};
//loop through issues and call the callback
$repo->forIssues($daily,['state'=>'closed','labels'=>'daily/repeat']);

View File

@ -1,25 +1,15 @@
<?php <?php
class GiteaData{ class GiteaData{
private $data=null; protected $data=null;
public function __construct(Repo $repo,$data){ public function __construct($data=null){
$this->repo=$repo; if(!$data){
$this->data=$data; $data=new stdClass();
}
$this->data=$data;
} }
public function save(){
return $this->request($this->data->url,'PATCH',$this->data);
}
public function getData(){
return $this->data;
}
public function getRepo(){
return $this->repo;
}
public function __set($k,$v){ public function __set($k,$v){
$this->data->$k=$v; $this->data->$k=$v;
} }

19
src/GiteaRepoData.php Normal file
View File

@ -0,0 +1,19 @@
<?php
class GiteaRepoData extends GiteaData{
private $repo;
public function __construct(Repo $repo,$data){
$this->repo=$repo;
parent::__construct($data);
}
public function getRepo(){
return $this->repo;
}
public function save(){
return $this->repo->request($this->url,'PATCH',$this->data);
}
}

View File

@ -1,45 +1,32 @@
<?php <?php
class Issue{ class Issue extends GiteaRepoData{
public function __construct(Repo $repo,$data){ public function __construct(Repo $repo,$data){
$this->repo=$repo; parent::__construct($repo,$data);
$this->data=$data;
} }
public function request($url, $type='GET', $postFields = null){ public function request($url, $type='GET', $postFields = null){
return $this->repo->request($url, $type, $postFields); return $this->getRepo()->request($url, $type, $postFields);
} }
public function getData(){
return $this->data;
}
public function getRepo(){
return $this->repo;
}
public function save(){
return $this->request($this->data->url,'PATCH',$this->data);
}
public function addLabel($label){ public function addLabel($label){
$args=['labels'=>[$label->getData()->id]]; $args=['labels'=>[$label->id]];
return $this->request($this->data->url.'/labels','POST',$args); return $this->request($this->url.'/labels','POST',$args);
} }
public function removeLabel($label){ public function removeLabel($label){
$id=$label->getData()->id; $id=$label->id;
return $this->request($this->data->url."/labels/$id",'DELETE'); return $this->request($this->url."/labels/$id",'DELETE');
} }
public function addComment($body){ public function addComment($body){
$args=['body'=>$body]; $args=['body'=>$body];
return $this->request($this->data->url.'/comments','POST',$args); return $this->request($this->url.'/comments','POST',$args);
} }
public function hasLabel($label){ public function hasLabel($label){
$id=$label->getData()->id; $id=$label->id;
foreach($this->data->labels as $label){ foreach($this->labels as $label){
if($label->id==$id){ if($label->id==$id){
return true; return true;
} }

View File

@ -1,17 +1,4 @@
<?php <?php
class Label{ class Label extends GiteaRepoData{
public function __construct(Repo $repo,$data){
$this->repo=$repo;
$this->data=$data;
}
public function getData(){
return $this->data;
}
public function getRepo(){
return $this->repo;
}
} }

View File

@ -33,10 +33,18 @@ class Repo{
$data=$this->client->request($url); $data=$this->client->request($url);
foreach($data as $datum){ foreach($data as $datum){
if($datum->name==$name){ if($datum->name==$name){
$datum->url=$url."/".$datum->id; // not set by api for some reason (bug?)
return new Label($this,$datum); return new Label($this,$datum);
} }
} }
return null; return null;
} }
public function createLabel($args){
$url="repos/{$this->user}/{$this->repo}/labels";
$data=$this->client->request($url,'POST',$args);
$data->url=$url."/".$data->id; // not set by api for some reason (bug?)
return new Label($this,$data);
}
} }