php-censor/src/Helper/Github.php

85 lines
2.2 KiB
PHP
Raw Permalink Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Helper;
2018-03-04 09:56:02 +01:00
use PHPCensor\Config;
2017-06-21 17:26:15 +02:00
use GuzzleHttp\Client;
/**
* The Github Helper class provides some Github API call functionality.
*/
class Github
{
/**
* 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');
if (!$token) {
return null;
}
$url = '/repos/' . strtolower($repo) . '/pulls/' . $pullId . '/comments';
2016-04-20 17:39:48 +02:00
$params = [
'body' => $comment,
'commit_id' => $commitId,
2016-04-20 17:39:48 +02:00
'path' => $file,
'position' => $line,
];
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
]);
}
/**
* 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');
if (!$token) {
return null;
}
$url = '/repos/' . strtolower($repo) . '/commits/' . $commitId . '/comments';
2016-04-20 17:39:48 +02:00
$params = [
'body' => $comment,
'path' => $file,
'position' => $line,
2016-04-20 17:39:48 +02: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
]);
}
2014-05-02 18:38:43 +02:00
}