-
Notifications
You must be signed in to change notification settings - Fork 0
/
GithubRepositoryAdaptor.php
262 lines (227 loc) · 8.77 KB
/
GithubRepositoryAdaptor.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php namespace Netcarver;
require_once 'GitRepositoryInterface.php';
require_once 'GitRepositoryAdaptor.php';
/**
*
*/
class GithubRepositoryAdaptor extends GitRepositoryAdaptor implements GitRepositoryInterface {
// Data common across all instances...
static protected $info = [
'name' => 'Github',
'icon' => '<i class="fa fa-icon fa-github"></i>', // Fontawesome icon markup
'limit' => 60,
'remaining' => 60,
'reset' => 0,
'capabilities' => [
'tags',
'release-notes',
'tag-to-tag-commits',
'changelog-access',
'commits',
'forks',
],
];
/**
* Returns information about this repository interface.
*
* This is a mixture of static and per-instance information.
*/
public function GetInfo() {
return array_merge(
self::$info,
[
'remote' => $this->remote,
'owner' => $this->owner,
'repo' => $this->repo,
'tags' => $this->tags,
]
);
}
/**
*
*/
public function __construct($http, $remote, $application, array $options) {
$owner = '';
$repo = '';
$m = [];
if (array_key_exists('debug', $options)) {
$this->debug($options['debug']);
}
if (array_key_exists('cache_dir', $options)) {
$this->cache = new \Netcarver\FileCache();
$this->cache->setCacheDirectory($options['cache_dir']);
} else {
throw new \Exception('Cache directory is needed.');
}
$ok = preg_match('~^https?://github.com/([^/]++)/(.++)~i', $remote, $m);
if ($ok) {
$this->headers['Accept'] = 'application/vnd.github.v3+json'; // As requested by the github v3 api documentation.
$this->headers['User-Agent'] = $application;
$this->http = $http;
$this->remote = $remote;
$this->owner = $m[1];
$this->repo = $m[2];
$this->encoded_owner = rawurlencode($m[1]);
$this->encoded_repo = rawurlencode($m[2]);
$this->GetTags();
} else {
throw new \Exception('Invalid repository signature');
}
}
/**
* Returns the URL for humans to review the Release Notes at GH.
*/
public function GetReleaseNoteViewUrl($version) {
$encoded_version = rawurlencode($version);
return "https://github.com/$this->encoded_owner}/{$this->encoded_repo}/releases/tags/$encoded_version";
}
/**
* Retrieves release notes field from the Github repo.
*
* No formatting is applied. It's up to the consumer to apply whatever formatting it wants.
*
* GetReleaseNotes('1.2.0'); // returns release notes for the given tag.
* GetReleaseNotes(); // returns all the release notes for the owner-repository. - Use with care.
*/
public function GetReleaseNotes($version=null) {
//
// If we could not access any tags for this repo, then there will be no release notes.
//
if (null === $this->tags) return null;
$encoded_version = '';
if (is_string($version) && !empty($version)) {
// If the version is not in the tag list, we waste a read just to fetch a 404.
if (!in_array($version, $this->tags)) {
return null;
}
$encoded_version = '/tags/' . rawurlencode($version);
}
$url = "https://api.github.com/repos/{$this->encoded_owner}/{$this->encoded_repo}/releases$encoded_version";
$http_code = null;
$reply = $this->RepositoryRead($url, $http_code);
if ((200 == $http_code || 304 == $http_code) && !empty($reply['body'])) {
return $reply['body'];
}
return null;
}
/**
*
*/
public function GetTags() {
if (null === $this->tags) {
$url = "https://api.github.com/repos/{$this->encoded_owner}/{$this->encoded_repo}/git/refs/tags";
$http_code = null;
$reply = $this->RepositoryRead($url, $http_code);
if (200 == $http_code || 304 == $http_code) {
$result = [];
$num = count($reply);
if ($num) {
foreach ($reply as $tagref) {
$sha = $tagref['object']['sha'];
$tag = str_replace('refs/tags/', '', $tagref['ref']);
$result[$sha] = $tag;
}
}
$this->tags = $result;
}
}
return $this->tags;
}
/**
* GetCommits(12); // Returns the last 12 commits.
* GetCommits('0.3.0', '0.4.0'); // Returns the commits between the given tags
* GetCommits('0.3.0'); // Returns the commits between '0.3.0' and HEAD.
*
* If the commits cannot be accessed, null is returned.
*/
public function GetCommits($startref, $endref='HEAD') {
if (is_string($startref) && !empty(trim($startref))) {
//
// If we could not access any tags for this repo, then there will be no tag-to-tag commit.
//
// If the startref or the endref (when not HEAD) is not in the taglist there will be no tag-to-tag list.
if (null === $this->tags || !in_array($startref, $this->tags)) {
return null;
}
if ('HEAD' !== $endref && !in_array($endref, $this->tags)) {
return null;
}
//
// Pull all commits between the startref and endref reference points.
// Useful for getting a list of commits between tags.
//
$startref_encoded = rawurlencode($startref);
$endref_encoded = rawurlencode($endref);
$url = "https://api.github.com/repos/{$this->encoded_owner}/{$this->encoded_repo}/compare/$startref_encoded...$endref_encoded";
$slice = false;
} else {
//
// Pull the last n commits.
//
$url = "https://api.github.com/repos/{$this->encoded_owner}/{$this->encoded_repo}/commits";
$slice = intval($startref);
if ($slice < 1) $slice = 1;
if ($slice > 30) $slice = 30;
}
$http_code = null;
$reply = $this->RepositoryRead($url, $http_code);
if(200 == $http_code || 304 == $http_code) {
if ($slice) {
$repo_url = null;
$commits = array_slice($reply, 0, $slice);
} else {
$repo_url = $reply['html_url'];
$commits = array_reverse($reply['commits']); // We want them most-recent-first
}
$history = [];
foreach ($commits as $commit) {
$entry = [];
$entry['sha'] = $commit['sha'];
$entry['url'] = $commit['html_url'];
$entry['author'] = $commit['commit']['author']['name'];
$entry['date'] = $commit['commit']['committer']['date'];
$entry['message'] = $commit['commit']['message'];
$entry['tag'] = @$this->tags[$entry['sha']];
ksort($entry);
$history[] = $entry;
}
return ['commits' => $history, 'url' => $repo_url];
}
return null;
}
/**
*
*/
public function GetChangelog() {
$url = "https://raw.githubusercontent.com/{$this->encoded_owner}/{$this->encoded_repo}/master/CHANGELOG.md";
$http_code = null;
$reply = $this->RepositoryRead($url, $http_code, false);
return $reply;
}
/**
*
*/
public function GetForks($sort) {
if (!in_array($sort, ['newest', 'oldest', 'watchers'])) {
$sort = 'newest';
}
$url = "https://api.github.com/repos/{$this->encoded_owner}/{$this->encoded_repo}/forks?sort=$sort&page=1";
$http_code = null;
$reply = $this->RepositoryRead($url, $http_code);
return $reply;
}
/**
* Override the postRead() function from the base class.
*
* For github, we use this to record the remaining reads from the reply headers.
*/
protected function postRead(array $data) {
parent::postRead($data);
$reply_headers = $data['reply_headers'];
if (isset($reply_headers['x-ratelimit-remaining'])) {
self::$info['remaining'] = $reply_headers['x-ratelimit-remaining'];
self::$info['limit'] = $reply_headers['x-ratelimit-limit'];
self::$info['reset'] = $reply_headers['x-ratelimit-reset'];
}
}
}