1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GRC; |
4
|
|
|
|
5
|
|
|
use Debugbar; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
8
|
|
|
use MongoDB\Client as MongoClient; |
9
|
|
|
|
10
|
|
|
class Repository |
11
|
|
|
{ |
12
|
|
|
protected $client; |
13
|
|
|
protected $collection; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
$this->client = new Client([ |
18
|
|
|
'base_url' => 'https://api.github.com/', |
19
|
|
|
'defaults' => [ |
20
|
|
|
'query' => [ |
21
|
|
|
'client_id' => config('grc.client_id'), |
22
|
|
|
'client_secret' => config('grc.client_secret'), |
23
|
|
|
], |
24
|
|
|
], |
25
|
|
|
]); |
26
|
|
|
|
27
|
|
|
$connection = new MongoClient(); |
28
|
|
|
$db = $connection->grc; |
|
|
|
|
29
|
|
|
$this->collection = $collection = $db->{'api.github.com'}; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function get($repositories) |
33
|
|
|
{ |
34
|
|
|
$repositories = (array)$repositories; |
35
|
|
|
|
36
|
|
|
$results = []; |
37
|
|
|
foreach ($repositories as $repository) { |
38
|
|
|
$result = $this->getOne($repository); |
39
|
|
|
if ($result) { |
40
|
|
|
$results[$repository] = $result; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $results; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getOne($repository) |
48
|
|
|
{ |
49
|
|
|
if ( ! $this->isValidRepository($repository)) { |
50
|
|
|
throw new \RuntimeException('Repository must be follow the vendor/package format'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
list($vendor, $package) = explode('/', $repository); |
54
|
|
|
|
55
|
|
|
$result = $this->collection->findOne(['full_name' => $repository]); |
56
|
|
|
|
57
|
|
|
if ($result && $this->resultNotExpired($result)) { |
58
|
|
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
try { |
62
|
|
|
$result = $this->client->get('/repos/'.$vendor.'/'.$package)->json(); |
63
|
|
|
} catch (ClientException $e) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$result['_grc']['pulls_count'] = $this->getPullRequestCount($vendor, $package); |
68
|
|
|
$result['_grc']['open_issues_count'] = $result['open_issues_count'] - $result['_grc']['pulls_count']; |
69
|
|
|
|
70
|
|
|
$result = $this->setTrackingProperties($result); |
71
|
|
|
$this->collection->updateOne( |
72
|
|
|
[ |
73
|
|
|
'full_name' => $result['full_name'], |
74
|
|
|
], |
75
|
|
|
['$set' => $result], |
76
|
|
|
[ |
77
|
|
|
'upsert' => true, |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function resultNotExpired($data) |
85
|
|
|
{ |
86
|
|
|
return ($expiresAt = array_get($data, '_grc.expires_at')) && $expiresAt > time(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getPullRequestCount($vendor, $package) |
90
|
|
|
{ |
91
|
|
|
$results = $this->client->get('/repos/'.$vendor.'/'.$package.'/pulls')->json(); |
92
|
|
|
|
93
|
|
|
return count($results); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $data |
98
|
|
|
*/ |
99
|
|
|
protected function setTrackingProperties($data) |
100
|
|
|
{ |
101
|
|
|
array_set($data, '_grc.expires_at', time() + config('grc.cache.lifetime')); |
102
|
|
|
return $data; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function isValidRepository($repository) |
106
|
|
|
{ |
107
|
|
|
$parts = explode('/', $repository); |
108
|
|
|
|
109
|
|
|
return count($parts) === 2; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.