2014-05-02 18:32:18 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Helper;
|
2014-05-02 18:32:18 +02:00
|
|
|
|
2018-03-04 09:56:02 +01:00
|
|
|
use PHPCensor\Config;
|
2017-06-21 17:26:15 +02:00
|
|
|
use GuzzleHttp\Client;
|
2014-05-02 18:32:18 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* The Github Helper class provides some Github API call functionality.
|
|
|
|
*/
|
2014-05-02 18:32:18 +02:00
|
|
|
class Github
|
|
|
|
{
|
2014-05-27 14:06:38 +02:00
|
|
|
/**
|
2015-02-20 14:37:36 +01:00
|
|
|
* Create a comment on a specific file (and commit) in a Github Pull Request.
|
|
|
|
* @param $repo
|
|
|
|
* @param $pullId
|
|
|
|
* @param $commitId
|
|
|
|
* @param $file
|
|
|
|
* @param $line
|
|
|
|
* @param $comment
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function createPullRequestComment($repo, $pullId, $commitId, $file, $line, $comment)
|
|
|
|
{
|
2016-07-21 19:02:11 +02:00
|
|
|
$token = Config::getInstance()->get('php-censor.github.token');
|
2015-02-20 14:37:36 +01:00
|
|
|
|
|
|
|
if (!$token) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = '/repos/' . strtolower($repo) . '/pulls/' . $pullId . '/comments';
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$params = [
|
|
|
|
'body' => $comment,
|
2015-02-20 14:37:36 +01:00
|
|
|
'commit_id' => $commitId,
|
2016-04-20 17:39:48 +02:00
|
|
|
'path' => $file,
|
|
|
|
'position' => $line,
|
|
|
|
];
|
2015-02-20 14:37:36 +01:00
|
|
|
|
2017-06-21 17:26:15 +02:00
|
|
|
$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,
|
2016-04-20 17:39:48 +02:00
|
|
|
]);
|
2015-02-20 14:37:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a comment on a Github commit.
|
|
|
|
* @param $repo
|
|
|
|
* @param $commitId
|
|
|
|
* @param $file
|
|
|
|
* @param $line
|
|
|
|
* @param $comment
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function createCommitComment($repo, $commitId, $file, $line, $comment)
|
|
|
|
{
|
2016-07-21 19:02:11 +02:00
|
|
|
$token = Config::getInstance()->get('php-censor.github.token');
|
2015-02-20 14:37:36 +01:00
|
|
|
|
|
|
|
if (!$token) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = '/repos/' . strtolower($repo) . '/commits/' . $commitId . '/comments';
|
|
|
|
|
2016-04-20 17:39:48 +02:00
|
|
|
$params = [
|
|
|
|
'body' => $comment,
|
|
|
|
'path' => $file,
|
2015-02-20 14:37:36 +01:00
|
|
|
'position' => $line,
|
2016-04-20 17:39:48 +02:00
|
|
|
];
|
2015-02-20 14:37:36 +01:00
|
|
|
|
2017-06-21 17:26:15 +02:00
|
|
|
$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,
|
2016-04-20 17:39:48 +02:00
|
|
|
]);
|
2015-02-20 14:37:36 +01:00
|
|
|
}
|
2014-05-02 18:38:43 +02:00
|
|
|
}
|