GiteaBot/Gitea.php
2019-06-20 20:13:31 +01:00

60 lines
1.6 KiB
PHP

<?php
class Gitea{
public function __construct($url,$user,$pass){
$this->url=$url;
$this->user=$user;
$this->pass=$pass;
}
public function forIssues($user,$repo,$callback,$args){
$page=1;
while(1){
$issues=$this->request("repos/$user/$repo/issues?page=$page&".http_build_query($args));
if(!$issues) break;
foreach($issues as $issue){
call_user_func($callback,$issue);
}
$page++;
}
}
public function editIssue($issue,$args){
$this->request($issue->url,$args,true);
}
public function request($url, $postFields = null, $patch=false){
if(substr($url,0,4)!='http'){
$url=$this->url.$url;
}
echo ">> $url\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Bot');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if($patch){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
}
if (!is_null($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',
'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);
curl_close($ch);
return json_decode($result);
}
}