forked from symfony/flex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GithubApi.php
204 lines (170 loc) · 6.04 KB
/
GithubApi.php
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Flex;
use Composer\Util\HttpDownloader;
use Composer\Util\RemoteFilesystem;
class GithubApi
{
/** @var HttpDownloader|RemoteFilesystem */
private $downloader;
public function __construct($downloader)
{
$this->downloader = $downloader;
}
/**
* Attempts to find data about when the recipe was installed.
*
* Returns an array containing:
* commit: The git sha of the last commit of the recipe
* date: The date of the commit
* new_commits: An array of commit sha's in this recipe's directory+version since the commit
* The key is the sha & the value is the date
*/
public function findRecipeCommitDataFromTreeRef(string $package, string $repo, string $branch, string $version, string $lockRef): ?array
{
$repositoryName = $this->getRepositoryName($repo);
if (!$repositoryName) {
return null;
}
$recipePath = sprintf('%s/%s', $package, $version);
$commitsData = $this->requestGitHubApi(sprintf(
'https://api.github.com/repos/%s/commits?path=%s&sha=%s',
$repositoryName,
$recipePath,
$branch
));
$commitShas = [];
foreach ($commitsData as $commitData) {
$commitShas[$commitData['sha']] = $commitData['commit']['committer']['date'];
// go back the commits one-by-one
$treeUrl = $commitData['commit']['tree']['url'].'?recursive=true';
// fetch the full tree, then look for the tree for the package path
$treeData = $this->requestGitHubApi($treeUrl);
foreach ($treeData['tree'] as $treeItem) {
if ($treeItem['path'] !== $recipePath) {
continue;
}
if ($treeItem['sha'] === $lockRef) {
// remove *this* commit from the new commits list
array_pop($commitShas);
return [
// shorten for brevity
'commit' => substr($commitData['sha'], 0, 7),
'date' => $commitData['commit']['committer']['date'],
'new_commits' => $commitShas,
];
}
}
}
return null;
}
public function getVersionsOfRecipe(string $repo, string $branch, string $recipePath): ?array
{
$repositoryName = $this->getRepositoryName($repo);
if (!$repositoryName) {
return null;
}
$url = sprintf(
'https://api.github.com/repos/%s/contents/%s?ref=%s',
$repositoryName,
$recipePath,
$branch
);
$contents = $this->requestGitHubApi($url);
$versions = [];
foreach ($contents as $fileData) {
if ('dir' !== $fileData['type']) {
continue;
}
$versions[] = $fileData['name'];
}
return $versions;
}
public function getCommitDataForPath(string $repo, string $path, string $branch): array
{
$repositoryName = $this->getRepositoryName($repo);
if (!$repositoryName) {
return [];
}
$commitsData = $this->requestGitHubApi(sprintf(
'https://api.github.com/repos/%s/commits?path=%s&sha=%s',
$repositoryName,
$path,
$branch
));
$data = [];
foreach ($commitsData as $commitData) {
$data[$commitData['sha']] = $commitData['commit']['committer']['date'];
}
return $data;
}
public function getPullRequestForCommit(string $commit, string $repo): ?array
{
$data = $this->requestGitHubApi('https://api.github.com/search/issues?q='.$commit);
if (0 === \count($data['items'])) {
return null;
}
$repositoryName = $this->getRepositoryName($repo);
if (!$repositoryName) {
return null;
}
$bestItem = null;
foreach ($data['items'] as $item) {
// make sure the PR referenced isn't from a different repository
if (false === strpos($item['html_url'], sprintf('%s/pull', $repositoryName))) {
continue;
}
if (null === $bestItem) {
$bestItem = $item;
continue;
}
// find the first PR to reference - avoids rare cases where an invalid
// PR that references *many* commits is first
// e.g. https://api.github.com/search/issues?q=a1a70353f64f405cfbacfc4ce860af623442d6e5
if ($item['number'] < $bestItem['number']) {
$bestItem = $item;
}
}
if (!$bestItem) {
return null;
}
return [
'number' => $bestItem['number'],
'url' => $bestItem['html_url'],
'title' => $bestItem['title'],
];
}
private function requestGitHubApi(string $path)
{
if ($this->downloader instanceof HttpDownloader) {
$contents = $this->downloader->get($path)->getBody();
} else {
$contents = $this->downloader->getContents('api.github.com', $path, false);
}
return json_decode($contents, true);
}
/**
* Converts the "repo" stored in symfony.lock to a repository name.
*
* For example: "github.com/symfony/recipes" => "symfony/recipes"
*/
private function getRepositoryName(string $repo): ?string
{
// only supports public repository placement
if (0 !== strpos($repo, 'github.com')) {
return null;
}
$parts = explode('/', $repo);
if (3 !== \count($parts)) {
return null;
}
return implode('/', [$parts[1], $parts[2]]);
}
}