refactor
This commit is contained in:
parent
4290eab80a
commit
5bac8a9772
@ -1,4 +0,0 @@
|
||||
<?php
|
||||
include("Gitea.php");
|
||||
$config=include("config.php");
|
||||
$gitea=new Gitea($config['url'],$config['user'],$config['pass']);
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
include("bootstrap.php");
|
||||
|
||||
$reopen=function($issue) use ($gitea){
|
||||
$gitea->editIssue( $issue, ['state'=>'open']);
|
||||
};
|
||||
|
||||
$gitea->forIssues('james','rc',$reopen,['state'=>'closed','labels'=>'test']);
|
27
no_milestone.php
Normal file
27
no_milestone.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
//Adds a no-milestone label to any issues with no milestone
|
||||
//Also removes the label if present on an issue with a milestone
|
||||
|
||||
include("src/Client.php");
|
||||
include("src/Issue.php");
|
||||
include("src/Label.php");
|
||||
include("src/Repo.php");
|
||||
$config=include("config.php");
|
||||
|
||||
$client=new Client($config['url'],$config['user'],$config['pass']);
|
||||
$repo=$client->getRepo('james','test');
|
||||
|
||||
$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");
|
||||
|
||||
$callback=function($issue) use ($nomilestoneLabel){
|
||||
if( count($issue->getData()->milestone)==0 && !$issue->hasLabel($nomilestoneLabel) ){
|
||||
$issue->addLabel( $nomilestoneLabel );
|
||||
}elseif( count($issue->getData()->milestone)>0 && $issue->hasLabel($nomilestoneLabel) ){
|
||||
$issue->removeLabel( $nomilestoneLabel );
|
||||
}
|
||||
};
|
||||
|
||||
$repo->forIssues($callback);
|
@ -1,60 +1,24 @@
|
||||
<?php
|
||||
class Gitea{
|
||||
class Client{
|
||||
|
||||
public function __construct($url,$user,$pass){
|
||||
public function __construct($url,$user,$pass,$debug=false){
|
||||
$this->url=$url;
|
||||
$this->user=$user;
|
||||
$this->pass=$pass;
|
||||
$this->debug=$debug;
|
||||
}
|
||||
|
||||
public function forIssues($user,$repo,$callback,$args=null){
|
||||
$page=1;
|
||||
while(1){
|
||||
$url="repos/$user/$repo/issues?page=$page";
|
||||
if($args){
|
||||
$url.='&'.http_build_query($args);
|
||||
}
|
||||
$issues=$this->request($url);
|
||||
if(!$issues) break;
|
||||
foreach($issues as $issue){
|
||||
call_user_func($callback,$issue);
|
||||
}
|
||||
$page++;
|
||||
}
|
||||
}
|
||||
|
||||
public function editIssue($issue,$args){
|
||||
return $this->request($issue->url,'PATCH',$args);
|
||||
}
|
||||
|
||||
public function addLabelsToIssue($issue,$labels){
|
||||
$args=['labels'=>$labels];
|
||||
return $this->request($issue->url.'/labels','POST',$args);
|
||||
}
|
||||
|
||||
public function removeLabelFromIssue($issue,$id){
|
||||
return $this->request($issue->url."/labels/$id",'DELETE');
|
||||
}
|
||||
|
||||
public function addComment($issue,$body){
|
||||
$args=['body'=>$body];
|
||||
return $this->request($issue->url.'/comments','POST',$args);
|
||||
}
|
||||
|
||||
public function issueHasLabel($issue,$id){
|
||||
foreach($issue->labels as $label){
|
||||
if($label->id==$id){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
public function getRepo($user,$repo){
|
||||
return new Repo($this,$user,$repo);
|
||||
}
|
||||
|
||||
public function request($url, $type='GET', $postFields = null){
|
||||
if(substr($url,0,4)!='http'){
|
||||
$url=$this->url.$url;
|
||||
}
|
||||
echo ">> $type $url ".json_encode($postFields)." <<\n";
|
||||
if($this->debug){
|
||||
echo ">> $type $url ".json_encode($postFields)." <<\n";
|
||||
}
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'james/GiteaBot');
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
49
src/Issue.php
Normal file
49
src/Issue.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
class Issue{
|
||||
|
||||
public function __construct(Repo $repo,$data){
|
||||
$this->repo=$repo;
|
||||
$this->data=$data;
|
||||
}
|
||||
|
||||
public function request($url, $type='GET', $postFields = null){
|
||||
return $this->repo->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){
|
||||
$args=['labels'=>[$label->getData()->id]];
|
||||
return $this->request($this->data->url.'/labels','POST',$args);
|
||||
}
|
||||
|
||||
public function removeLabel($label){
|
||||
$id=$label->getData()->id;
|
||||
return $this->request($this->data->url."/labels/$id",'DELETE');
|
||||
}
|
||||
|
||||
public function addComment($body){
|
||||
$args=['body'=>$body];
|
||||
return $this->request($this->data->url.'/comments','POST',$args);
|
||||
}
|
||||
|
||||
public function hasLabel($label){
|
||||
$id=$label->getData()->id;
|
||||
foreach($this->data->labels as $label){
|
||||
if($label->id==$id){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
17
src/Label.php
Normal file
17
src/Label.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
class Label{
|
||||
|
||||
public function __construct(Repo $repo,$data){
|
||||
$this->repo=$repo;
|
||||
$this->data=$data;
|
||||
}
|
||||
|
||||
public function getData(){
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function getRepo(){
|
||||
return $this->repo;
|
||||
}
|
||||
|
||||
}
|
42
src/Repo.php
Normal file
42
src/Repo.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
class Repo{
|
||||
|
||||
public function __construct(Client $client,$user,$repo){
|
||||
$this->client=$client;
|
||||
$this->user=$user;
|
||||
$this->repo=$repo;
|
||||
}
|
||||
|
||||
public function request($url, $type='GET', $postFields = null){
|
||||
return $this->client->request($url, $type, $postFields);
|
||||
}
|
||||
|
||||
public function forIssues($callback,$args=null){
|
||||
$page=1;
|
||||
while(1){
|
||||
$url="repos/{$this->user}/{$this->repo}/issues?page=$page";
|
||||
if($args){
|
||||
$url.='&'.http_build_query($args);
|
||||
}
|
||||
$issues=$this->client->request($url);
|
||||
if(!$issues) break;
|
||||
foreach($issues as $data){
|
||||
$issue=new Issue($this,$data);
|
||||
call_user_func($callback,$issue);
|
||||
}
|
||||
$page++;
|
||||
}
|
||||
}
|
||||
|
||||
public function getLabelByName($name){
|
||||
$url="repos/{$this->user}/{$this->repo}/labels";
|
||||
$data=$this->client->request($url);
|
||||
foreach($data as $datum){
|
||||
if($datum->name==$name){
|
||||
return new Label($this,$datum);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user