Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
907c686bad | ||
|
83eea50cf9 | ||
|
b8a7da7a79 | ||
|
42952f6d74 | ||
|
ba51d4ad7d | ||
|
0ce51d3306 | ||
|
311a8ad399 | ||
|
1c3738c365 | ||
|
cd20d09279 | ||
|
aef0c2166e |
10
src/ApiException.php
Normal file
10
src/ApiException.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
namespace JHodges\GiteaBot;
|
||||||
|
|
||||||
|
class ApiException extends \Exception{
|
||||||
|
|
||||||
|
public function __toString() {
|
||||||
|
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -29,17 +29,20 @@ class Client{
|
|||||||
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
|
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
|
||||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||||
|
|
||||||
if($type!='GET'){
|
if($type!='GET' && $type!='FORM'){
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!is_null($postFields)) {
|
if(!is_null($postFields)) {
|
||||||
|
if($type=='FORM'){
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
|
||||||
|
}else{
|
||||||
$postFields = json_encode($postFields);
|
$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',
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json',
|
||||||
'Content-Length: '.strlen($postFields), ]);
|
'Content-Length: '.strlen($postFields), ]);
|
||||||
}
|
}
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
|
||||||
|
}
|
||||||
if($this->user) {
|
if($this->user) {
|
||||||
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);
|
||||||
@ -51,7 +54,29 @@ class Client{
|
|||||||
if(in_array($status_code,[200,201,204])){
|
if(in_array($status_code,[200,201,204])){
|
||||||
return json_decode($result);
|
return json_decode($result);
|
||||||
}else{
|
}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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
68
src/Repo.php
68
src/Repo.php
@ -51,6 +51,32 @@ class Repo{
|
|||||||
return $all_issues;
|
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){
|
public function getLabelByName($name){
|
||||||
$url="repos/{$this->user}/{$this->repo}/labels";
|
$url="repos/{$this->user}/{$this->repo}/labels";
|
||||||
if(!isset($this->cache['lables'])){
|
if(!isset($this->cache['lables'])){
|
||||||
@ -89,4 +115,46 @@ class Repo{
|
|||||||
return new Issue($this,$data);
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user