-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GiteaBridge.php
322 lines (282 loc) · 10.8 KB
/
GiteaBridge.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* Gitea is a community managed lightweight code hosting solution.
* https://docs.gitea.io/en-us/
*/
class GiteaBridge extends BridgeAbstract
{
const NAME = 'Gitea';
const URI = 'https://gitea.io';
const DESCRIPTION = 'Returns the latest issues, commits or releases';
const MAINTAINER = 'gileri';
const CACHE_TIMEOUT = 300; // 5 minutes
const PARAMETERS = [
'global' => [
'host' => [
'name' => 'Host',
'exampleValue' => 'https://gitea.com',
'required' => true,
'title' => 'Host name with its protocol, without trailing slash',
],
'user' => [
'name' => 'Username',
'exampleValue' => 'gitea',
'required' => true,
'title' => 'User name as it appears in the URL',
],
'project' => [
'name' => 'Project name',
'exampleValue' => 'helm-chart',
'required' => true,
'title' => 'Project name as it appears in the URL',
],
],
'Commits' => [
'branch' => [
'name' => 'Branch name',
'defaultValue' => 'master',
'required' => true,
'title' => 'Branch name as it appears in the URL',
],
],
'Issues' => [
'include_description' => [
'name' => 'Include issue description',
'type' => 'checkbox',
'title' => 'Activate to include the issue description',
],
],
'Single issue' => [
'issue' => [
'name' => 'Issue number',
'type' => 'number',
'exampleValue' => 100,
'required' => true,
'title' => 'Issue number from the issues list',
],
],
'Single pull request' => [
'pull_request' => [
'name' => 'Pull request number',
'type' => 'number',
'exampleValue' => 100,
'required' => true,
'title' => 'Pull request number from the issues list',
],
],
'Pull requests' => [
'include_description' => [
'name' => 'Include pull request description',
'type' => 'checkbox',
'title' => 'Activate to include the pull request description',
],
],
'Releases' => [],
'Tags' => [],
];
private $title = '';
public function getIcon()
{
return 'https://gitea.io/images/gitea.png';
}
public function getName()
{
switch ($this->queriedContext) {
case 'Commits':
case 'Issues':
case 'Pull requests':
case 'Releases':
case 'Tags':
return $this->title . ' ' . $this->queriedContext;
case 'Single issue':
return 'Issue ' . $this->getInput('issue') . ': ' . $this->title;
case 'Single pull request':
return 'Pull request ' . $this->getInput('pull_request') . ': ' . $this->title;
default:
return parent::getName();
}
}
public function getURI()
{
switch ($this->queriedContext) {
case 'Commits':
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/commits/' . $this->getInput('branch');
case 'Issues':
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/issues/';
case 'Single issue':
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/issues/' . $this->getInput('issue');
case 'Releases':
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/releases/';
case 'Tags':
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/tags/';
case 'Pull requests':
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/pulls/';
case 'Single pull request':
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/pulls/' . $this->getInput('pull_request');
default:
return parent::getURI();
}
}
public function collectData()
{
$html = getSimpleHTMLDOM($this->getURI())
or returnServerError('Could not request ' . $this->getURI());
$html = defaultLinkTo($html, $this->getURI());
$this->title = $html->find('[property="og:title"]', 0)->content;
switch ($this->queriedContext) {
case 'Commits':
$this->collectCommitsData($html);
break;
case 'Issues':
$this->collectIssuesData($html);
break;
case 'Pull requests':
$this->collectPullRequestsData($html);
break;
case 'Single issue':
$this->collectSingleIssueOrPrData($html);
break;
case 'Single pull request':
$this->collectSingleIssueOrPrData($html);
break;
case 'Releases':
$this->collectReleasesData($html);
break;
case 'Tags':
$this->collectTagsData($html);
break;
}
}
protected function collectReleasesData($html)
{
$releases = $html->find('#release-list > li')
or returnServerError('Unable to find releases');
foreach ($releases as $release) {
$this->items[] = [
'author' => $release->find('.author', 0)->plaintext,
'uri' => $release->find('a', 0)->href,
'title' => 'Release ' . $release->find('h4', 0)->plaintext,
'timestamp' => $release->find('.time-since', 0)->title,
];
}
}
protected function collectTagsData($html)
{
$tags = $html->find('table#tags-table > tbody > tr')
or returnServerError('Unable to find tags');
foreach ($tags as $tag) {
$this->items[] = [
'uri' => $tag->find('a', 0)->href,
'title' => 'Tag ' . $tag->find('.release-tag-name > a', 0)->plaintext,
];
}
}
protected function collectCommitsData($html)
{
$commits = $html->find('#commits-table tbody tr')
or returnServerError('Unable to find commits');
foreach ($commits as $commit) {
$this->items[] = [
'uri' => $commit->find('a.sha', 0)->href,
'title' => $commit->find('.message span', 0)->plaintext,
'author' => $commit->find('.author', 0)->plaintext,
'timestamp' => $commit->find('.time-since', 0)->title,
'uid' => $commit->find('.sha', 0)->plaintext,
];
}
}
protected function collectIssuesData($html)
{
$issues = $html->find('.issue.list li')
or returnServerError('Unable to find issues');
foreach ($issues as $issue) {
$uri = $issue->find('a', 0)->href;
$item = [
'uri' => $uri,
'title' => trim($issue->find('a.index', 0)->plaintext) . ' | ' . $issue->find('a.title', 0)->plaintext,
'author' => $issue->find('.desc a', 1)->plaintext,
'timestamp' => $issue->find('.time-since', 0)->title,
];
if ($this->getInput('include_description')) {
$issue_html = getSimpleHTMLDOMCached($uri, 3600)
or returnServerError('Unable to load issue description');
$issue_html = defaultLinkTo($issue_html, $uri);
$item['content'] = $issue_html->find('.comment .markup', 0);
}
$this->items[] = $item;
}
}
protected function collectSingleIssueOrPrData($html)
{
$comments = $html->find('.comment')
or returnServerError('Unable to find comments');
foreach ($comments as $comment) {
if (
strpos($comment->getAttribute('class'), 'form') !== false
|| strpos($comment->getAttribute('class'), 'merge') !== false
) {
// Ignore comment form and merge information
continue;
}
$commentLink = $comment->find('a[href*="#issue"]', 0);
$item = [
'author' => $comment->find('a.author', 0)->plaintext,
'content' => $comment->find('.render-content', 0),
];
if ($commentLink !== null) {
// Regular comment
$item['uri'] = $commentLink->href;
$item['title'] = str_replace($commentLink->plaintext, '', $comment->find('span', 0)->plaintext);
$item['timestamp'] = $comment->find('.time-since', 0)->title;
} else {
// Change request comment
$item['uri'] = $this->getURI() . '#' . $comment->getAttribute('id');
$item['title'] = $comment->find('.comment-header .text', 0)->plaintext;
}
$this->items[] = $item;
}
$this->items = array_reverse($this->items);
}
protected function collectPullRequestsData($html)
{
$issues = $html->find('.issue.list li')
or returnServerError('Unable to find pull requests');
foreach ($issues as $issue) {
$uri = $issue->find('a', 0)->href;
$item = [
'uri' => $uri,
'title' => trim($issue->find('a.index', 0)->plaintext) . ' | ' . $issue->find('a.title', 0)->plaintext,
'author' => $issue->find('.desc a', 1)->plaintext,
'timestamp' => $issue->find('.time-since', 0)->title,
];
if ($this->getInput('include_description')) {
$issue_html = getSimpleHTMLDOMCached($uri, 3600)
or returnServerError('Unable to load issue description');
$issue_html = defaultLinkTo($issue_html, $uri);
$item['content'] = $issue_html->find('.comment .markup', 0);
}
$this->items[] = $item;
}
}
}