Compare commits

..

10 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
3 changed files with 110 additions and 7 deletions

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

@ -29,17 +29,20 @@ 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)) {
if($type=='FORM'){
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
}else{
$postFields = json_encode($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), ]);
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
if($this->user) {
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
@ -51,7 +54,29 @@ class Client{
if(in_array($status_code,[200,201,204])){
return json_decode($result);
}else{
throw new \Exception("API $status_code Code: ".$result);
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

@ -51,6 +51,32 @@ class Repo{
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";
if(!isset($this->cache['lables'])){
@ -89,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');
}
}