-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathGithub.php
More file actions
85 lines (71 loc) · 2.27 KB
/
Github.php
File metadata and controls
85 lines (71 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace PHPCensor\Helper;
use GuzzleHttp\Client;
use PHPCensor\Common\Application\ConfigurationInterface;
/**
* The Github Helper class provides some Github API call functionality.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <[email protected]>
*/
class Github
{
private ConfigurationInterface $configuration;
public function __construct(ConfigurationInterface $configuration)
{
$this->configuration = $configuration;
}
/**
* Create a comment on a specific file (and commit) in a Github Pull Request.
* @return null
*/
public function createPullRequestComment($repo, $pullId, $commitId, $file, $line, $comment)
{
$token = $this->configuration->get('php-censor.github.token');
if (!$token) {
return null;
}
$url = '/repos/' . \strtolower($repo) . '/pulls/' . $pullId . '/comments';
$params = [
'body' => $comment,
'commit_id' => $commitId,
'path' => $file,
'position' => $line,
];
$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,
]);
}
/**
* Create a comment on a Github commit.
* @return null
*/
public function createCommitComment($repo, $commitId, $file, $line, $comment)
{
$token = $this->configuration->get('php-censor.github.token');
if (!$token) {
return null;
}
$url = '/repos/' . \strtolower($repo) . '/commits/' . $commitId . '/comments';
$params = [
'body' => $comment,
'path' => $file,
'position' => $line,
];
$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,
]);
}
}