1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nopolabs\Yabot\Plugins\GitHubV4; |
4
|
|
|
|
5
|
|
|
use Nopolabs\Yabot\Guzzle\Guzzle; |
6
|
|
|
use Nopolabs\Yabot\Helpers\GuzzleTrait; |
7
|
|
|
use Nopolabs\Yabot\Message\Message; |
8
|
|
|
use Nopolabs\Yabot\Plugin\PluginInterface; |
9
|
|
|
use Nopolabs\Yabot\Plugin\PluginTrait; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
|
13
|
|
|
class GitHubPlugin implements PluginInterface |
14
|
|
|
{ |
15
|
|
|
use PluginTrait { |
16
|
|
|
init as private traitInit; |
17
|
|
|
} |
18
|
|
|
use GuzzleTrait; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
Guzzle $guzzle, |
22
|
|
|
LoggerInterface $logger, |
23
|
|
|
array $config = []) |
24
|
|
|
{ |
25
|
|
|
$this->setGuzzle($guzzle); |
26
|
|
|
$this->setLog($logger); |
27
|
|
|
$this->setConfig(array_merge( |
28
|
|
|
[ |
29
|
|
|
'matchers' => [ |
30
|
|
|
'graphql' => "/\\bgraphql\\b/i", |
31
|
|
|
], |
32
|
|
|
], |
33
|
|
|
$config |
34
|
|
|
)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function init(string $pluginId, array $params) |
38
|
|
|
{ |
39
|
|
|
$this->traitInit($pluginId, $params); |
40
|
|
|
|
41
|
|
|
$token = $this->get('graphql.token'); |
42
|
|
|
$options = [ |
43
|
|
|
'headers' => [ |
44
|
|
|
'Authorization' => "bearer $token", |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$this->setOptions($options); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function graphql(Message $msg, array $matches) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$url = 'https://api.github.com/graphql'; |
54
|
|
|
|
55
|
|
|
$this->postAsync($url, ['body' => '{"query": "query { viewer { login }}"}']) |
56
|
|
|
->then( |
57
|
|
|
function (ResponseInterface $response) use ($msg) { |
58
|
|
|
$json = $response->getBody(); |
59
|
|
|
$msg->reply($json); |
60
|
|
|
} |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// $ curl -H "Authorization: bearer 85fe3989ec4162bb0084e72d84b8edc7eaa798ae" -X POST -d "{\"query\": \"query { viewer { login }}\"}" https://api.github.com/graphql |
66
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.