Changed HttpClient to Guzzle library.
This commit is contained in:
parent
d9cde29c5e
commit
9041457af4
8 changed files with 99 additions and 314 deletions
|
|
@ -5,6 +5,7 @@ namespace PHPCensor\Controller;
|
|||
use b8;
|
||||
use b8\Store;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Model\Project;
|
||||
|
|
@ -13,7 +14,6 @@ use PHPCensor\Store\BuildStore;
|
|||
use PHPCensor\Store\ProjectStore;
|
||||
use b8\Controller;
|
||||
use b8\Config;
|
||||
use b8\HttpClient;
|
||||
use b8\Exception\HttpException\NotFoundException;
|
||||
|
||||
/**
|
||||
|
|
@ -299,29 +299,34 @@ class WebhookController extends Controller
|
|||
$token = Config::getInstance()->get('php-censor.github.token');
|
||||
|
||||
if (!empty($token)) {
|
||||
$headers[] = 'Authorization: token ' . $token;
|
||||
$headers['Authorization'] = 'token ' . $token;
|
||||
}
|
||||
|
||||
$url = $payload['pull_request']['commits_url'];
|
||||
$http = new HttpClient();
|
||||
$http->setHeaders($headers);
|
||||
$url = $payload['pull_request']['commits_url'];
|
||||
|
||||
//for large pull requests, allow grabbing more then the default number of commits
|
||||
$custom_per_page = Config::getInstance()->get('php-censor.github.per_page');
|
||||
$params = [];
|
||||
if ($custom_per_page) {
|
||||
$params["per_page"] = $custom_per_page;
|
||||
$params['per_page'] = $custom_per_page;
|
||||
}
|
||||
$response = $http->get($url, $params);
|
||||
|
||||
$client = new Client();
|
||||
$response = $client->get($url, [
|
||||
'headers' => $headers,
|
||||
'query' => $params,
|
||||
]);
|
||||
$status = (integer)$response->getStatusCode();
|
||||
|
||||
// Check we got a success response:
|
||||
if (!$response['success']) {
|
||||
if ($status < 200 || $status >= 300) {
|
||||
throw new Exception('Could not get commits, failed API request.');
|
||||
}
|
||||
|
||||
$results = [];
|
||||
$status = 'failed';
|
||||
foreach ($response['body'] as $commit) {
|
||||
$commits = json_decode($response->getBody(), true);
|
||||
foreach ($commits as $commit) {
|
||||
// Skip all but the current HEAD commit ID:
|
||||
$id = $commit['sha'];
|
||||
if ($id != $payload['pull_request']['head']['sha']) {
|
||||
|
|
|
|||
|
|
@ -4,27 +4,13 @@ namespace PHPCensor\Helper;
|
|||
|
||||
use b8\Cache;
|
||||
use b8\Config;
|
||||
use b8\HttpClient;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
/**
|
||||
* The Github Helper class provides some Github API call functionality.
|
||||
*/
|
||||
class Github
|
||||
{
|
||||
/**
|
||||
* Make a request to the Github API.
|
||||
* @param $url
|
||||
* @param $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function makeRequest($url, $params)
|
||||
{
|
||||
$http = new HttpClient('https://api.github.com');
|
||||
$res = $http->get($url, $params);
|
||||
|
||||
return $res['body'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Make all GitHub requests following the Link HTTP headers.
|
||||
*
|
||||
|
|
@ -36,15 +22,23 @@ class Github
|
|||
*/
|
||||
public function makeRecursiveRequest($url, $params, $results = [])
|
||||
{
|
||||
$http = new HttpClient('https://api.github.com');
|
||||
$res = $http->get($url, $params);
|
||||
|
||||
foreach ($res['body'] as $item) {
|
||||
$client = new Client();
|
||||
$response = $client->get(('https://api.github.com' . $url), [
|
||||
'query' => $params,
|
||||
]);
|
||||
|
||||
$body = json_decode($response->getBody(), true);
|
||||
$headers = $response->getHeaders();
|
||||
|
||||
foreach ($body as $item) {
|
||||
$results[] = $item;
|
||||
}
|
||||
|
||||
foreach ($res['headers'] as $header) {
|
||||
if (preg_match('/^Link: <([^>]+)>; rel="next"/', $header, $r)) {
|
||||
foreach ($headers as $header_name => $header) {
|
||||
if (
|
||||
'Link' === $header_name &&
|
||||
preg_match('/^<([^>]+)>; rel="next"/', implode(', ', $header), $r)
|
||||
) {
|
||||
$host = parse_url($r[1]);
|
||||
|
||||
parse_str($host['query'], $params);
|
||||
|
|
@ -65,21 +59,28 @@ class Github
|
|||
$token = Config::getInstance()->get('php-censor.github.token');
|
||||
|
||||
if (!$token) {
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
|
||||
$cache = Cache::getCache(Cache::TYPE_APC);
|
||||
$rtn = $cache->get('php-censor-github-repos');
|
||||
$rtn = $cache->get('php-censor-github-repos');
|
||||
|
||||
if (!$rtn) {
|
||||
$orgs = $this->makeRequest('/user/orgs', ['access_token' => $token]);
|
||||
$client = new Client();
|
||||
$response = $client->get('https://api.github.com/user/orgs', [
|
||||
'query' => [
|
||||
'access_token' => $token
|
||||
],
|
||||
]);
|
||||
|
||||
$orgs = json_decode($response->getBody(), true);
|
||||
|
||||
$params = ['type' => 'all', 'access_token' => $token];
|
||||
$repos = ['user' => []];
|
||||
$repos['user'] = $this->makeRecursiveRequest('/user/repos', $params);
|
||||
|
||||
foreach ($orgs as $org) {
|
||||
$repos[$org['login']] = $this->makeRecursiveRequest('/orgs/'.$org['login'].'/repos', $params);
|
||||
$repos[$org['login']] = $this->makeRecursiveRequest('/orgs/' . $org['login'] . '/repos', $params);
|
||||
}
|
||||
|
||||
$rtn = [];
|
||||
|
|
@ -122,13 +123,14 @@ class Github
|
|||
'position' => $line,
|
||||
];
|
||||
|
||||
$http = new HttpClient('https://api.github.com');
|
||||
$http->setHeaders([
|
||||
'Content-Type: application/x-www-form-urlencoded',
|
||||
'Authorization: Basic ' . base64_encode($token . ':x-oauth-basic'),
|
||||
$client = new Client();
|
||||
$client->post(('https://api.github.com' . $url), [
|
||||
'headers' => [
|
||||
'Authorization' => 'Basic ' . base64_encode($token . ':x-oauth-basic'),
|
||||
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||
],
|
||||
'json' => $params,
|
||||
]);
|
||||
|
||||
$http->post($url, json_encode($params));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -156,12 +158,13 @@ class Github
|
|||
'position' => $line,
|
||||
];
|
||||
|
||||
$http = new HttpClient('https://api.github.com');
|
||||
$http->setHeaders([
|
||||
'Content-Type: application/x-www-form-urlencoded',
|
||||
'Authorization: Basic ' . base64_encode($token . ':x-oauth-basic'),
|
||||
$client = new Client();
|
||||
$client->post(('https://api.github.com' . $url), [
|
||||
'headers' => [
|
||||
'Authorization' => 'Basic ' . base64_encode($token . ':x-oauth-basic'),
|
||||
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||
],
|
||||
'json' => $params,
|
||||
]);
|
||||
|
||||
$http->post($url, json_encode($params));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
namespace PHPCensor\Model\Build;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use PHPCensor\Builder;
|
||||
use PHPCensor\Helper\Diff;
|
||||
use PHPCensor\Helper\Github;
|
||||
use b8\Config;
|
||||
use b8\HttpClient;
|
||||
use PHPCensor\Model\BuildError;
|
||||
|
||||
/**
|
||||
|
|
@ -57,9 +57,6 @@ class GithubBuild extends RemoteGitBuild
|
|||
return;
|
||||
}
|
||||
|
||||
$url = 'https://api.github.com/repos/'.$project->getReference().'/statuses/'.$this->getCommitId();
|
||||
$http = new HttpClient();
|
||||
|
||||
switch ($this->getStatus()) {
|
||||
case 0:
|
||||
case 1:
|
||||
|
|
@ -82,20 +79,20 @@ class GithubBuild extends RemoteGitBuild
|
|||
|
||||
$phpCensorUrl = Config::getInstance()->get('php-censor.url');
|
||||
|
||||
$params = [
|
||||
'state' => $status,
|
||||
'target_url' => $phpCensorUrl . '/build/view/' . $this->getId(),
|
||||
'description' => $description,
|
||||
'context' => 'PHP Censor',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'Authorization: token ' . $token,
|
||||
'Content-Type: application/x-www-form-urlencoded'
|
||||
];
|
||||
|
||||
$http->setHeaders($headers);
|
||||
$http->request('POST', $url, json_encode($params));
|
||||
$url = 'https://api.github.com/repos/' . $project->getReference() . '/statuses/' . $this->getCommitId();
|
||||
$client = new Client();
|
||||
$client->post($url, [
|
||||
'headers' => [
|
||||
'Authorization' => 'token ' . $token,
|
||||
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||
],
|
||||
'json' => [
|
||||
'state' => $status,
|
||||
'target_url' => $phpCensorUrl . '/build/view/' . $this->getId(),
|
||||
'description' => $description,
|
||||
'context' => 'PHP Censor',
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Plugin;
|
||||
|
||||
use b8\HttpClient;
|
||||
use GuzzleHttp\Client;
|
||||
use PHPCensor\Builder;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Plugin;
|
||||
|
|
@ -55,17 +55,27 @@ class Deployer extends Plugin
|
|||
return false;
|
||||
}
|
||||
|
||||
$http = new HttpClient();
|
||||
|
||||
$response = $http->post($this->webhookUrl, [
|
||||
'reason' => $this->builder->interpolate($this->reason),
|
||||
'source' => 'PHP Censor',
|
||||
'url' => $this->builder->interpolate('%BUILD_URI%'),
|
||||
'branch' => $this->builder->interpolate('%BRANCH%'),
|
||||
'commit' => $this->builder->interpolate('%COMMIT%'),
|
||||
'update_only' => $this->updateOnly,
|
||||
]);
|
||||
|
||||
return $response['success'];
|
||||
$client = new Client();
|
||||
$response = $client->post(
|
||||
$this->webhookUrl,
|
||||
[
|
||||
'form_params' => [
|
||||
'reason' => $this->builder->interpolate($this->reason),
|
||||
'source' => 'PHP Censor',
|
||||
'url' => $this->builder->interpolate('%BUILD_URI%'),
|
||||
'branch' => $this->builder->interpolate('%BRANCH%'),
|
||||
'commit' => $this->builder->interpolate('%COMMIT%'),
|
||||
'update_only' => $this->updateOnly,
|
||||
]
|
||||
]
|
||||
);
|
||||
|
||||
$status = (integer)$response->getStatusCode();
|
||||
|
||||
return (
|
||||
($status >= 200 && $status < 300)
|
||||
? true
|
||||
: false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue