forked from gharlan/alfred-github-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
492 lines (454 loc) · 18.2 KB
/
search.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
require 'workflow.php';
class Search
{
private static $enterprise;
private static $query;
private static $parts;
private static $user;
public static function run($scope, $query)
{
self::$enterprise = 'enterprise' === $scope;
if (' ' !== $query[0]) {
return '';
}
$query = ltrim($query);
self::$query = $query;
self::$parts = $parts = explode(' ', $query);
Workflow::init(self::$enterprise, $query);
if (Workflow::checkUpdate()) {
self::addUpdateCommands();
return Workflow::getItemsAsXml();
}
if (self::$enterprise && !Workflow::getBaseUrl()) {
self::addEnterpriseUrlCommand();
return Workflow::getItemsAsXml();
}
if (!Workflow::getAccessToken() || !(self::$user = Workflow::requestApi('/user'))) {
self::addLoginCommands();
return Workflow::getItemsAsXml();
}
Workflow::stopServer();
$isSystem = isset($query[0]) && $query[0] == '>';
$isMy = 'my' == $parts[0] && isset($parts[1]);
$isUser = isset($query[0]) && $query[0] == '@';
$isRepo = false;
$queryUser = null;
if ($isUser) {
$queryUser = ltrim($parts[0], '@');
} elseif (($pos = strpos($parts[0], '/')) !== false) {
$queryUser = substr($parts[0], 0, $pos);
$isRepo = true;
}
if ($isSystem) {
self::addSystemCommands();
Workflow::sortItems();
} else {
if ($isMy) {
self::addMyCommands();
} elseif ($isUser && isset($parts[1])) {
self::addUserSubCommands($queryUser);
} elseif (!$isUser && $isRepo && isset($parts[1])) {
self::addRepoSubCommands();
} else {
self::addDefaultCommands($isUser, $isRepo, $queryUser);
}
Workflow::sortItems();
if ($query) {
if (!$isUser && $isRepo && isset($parts[1])) {
$repoQuery = substr($query, strlen($parts[0]) + 1);
Workflow::addItem(Item::create()
->title("Search '$parts[0]' for '$repoQuery'")
->icon('search')
->arg('/' . $parts[0] . '/search?q=' . urlencode($repoQuery))
->autocomplete(false)
, false);
}
$path = $isUser ? $queryUser : 'search?q=' . urlencode($query);
$name = self::$enterprise ? 'GitHub Enterprise' : 'GitHub';
Workflow::addItem(Item::create()
->title("Search $name for '$query'")
->icon('search')
->arg('/' . $path)
->autocomplete(false)
, false);
}
}
return Workflow::getItemsAsXml();
}
private static function addUpdateCommands()
{
$cmds = array(
'update' => 'There is an update for this Alfred workflow',
'deactivate autoupdate' => 'Deactivate auto updating this Alfred Workflow'
);
foreach ($cmds as $cmd => $desc) {
Workflow::addItem(Item::create()
->title('> ' . $cmd)
->subtitle($desc)
->icon($cmd)
->arg('> ' . str_replace(' ', '-', $cmd))
->randomUid()
, false);
}
Workflow::addItem(Item::create()
->title('> changelog')
->subtitle('View the changelog')
->icon('file')
->arg('https://github.com/gharlan/alfred-github-workflow/blob/master/CHANGELOG.md')
->randomUid()
, false);
}
private static function addEnterpriseUrlCommand()
{
$url = null;
if (count(self::$parts) > 1 && self::$parts[0] == '>' && self::$parts[1] == 'url' && isset(self::$parts[2])) {
$url = self::$parts[2];
}
Workflow::addItem(Item::create()
->title('> url ' . $url)
->subtitle('Set the GitHub Enterprise URL')
->arg('> enterprise-url ' . $url)
->valid((bool) $url, '<URL>')
->randomUid()
, false);
}
private static function addLoginCommands()
{
Workflow::removeAccessToken();
$token = null;
if (count(self::$parts) > 1 && self::$parts[0] == '>' && self::$parts[1] == 'login' && isset(self::$parts[2])) {
$token = self::$parts[2];
}
if (!$token && !self::$enterprise) {
Workflow::addItem(Item::create()
->title('> login')
->subtitle('Generate OAuth access token')
->arg('> login')
->randomUid()
, false);
}
Workflow::addItem(Item::create()
->title('> login ' . $token)
->subtitle('Save access token')
->arg('> login ' . $token)
->valid((bool) $token, '<access_token>')
->randomUid()
, false);
if (!$token && self::$enterprise) {
Workflow::addItem(Item::create()
->title('> generate token')
->subtitle('Generate a new access token')
->arg('/settings/applications')
->randomUid()
, false);
Workflow::addItem(Item::create()
->title('> enterprise reset')
->subtitle('Reset the GitHub Enterprise URL')
->arg('> enterprise-reset')
->randomUid()
, false);
}
}
private static function addDefaultCommands($isUser, $isRepo, $queryUser)
{
$users = array();
$repos = array();
$curl = new Curl();
if (!$isUser) {
$getRepos = function ($url, $prio) use ($curl, &$repos) {
Workflow::requestApi($url, $curl, function ($urlRepos) use (&$repos, $prio) {
foreach ($urlRepos as $repo) {
$repo->prio = $prio;
$repos[$repo->id] = $repo;
}
});
};
if ($isRepo) {
$urls = array('/users/' . $queryUser . '/repos', '/orgs/' . $queryUser . '/repos');
} else {
Workflow::requestApi('/user/orgs', $curl, function ($orgs) use ($getRepos) {
foreach ($orgs as $org) {
$getRepos('/orgs/' . $org->login . '/repos', 0);
}
});
$urls = array('/user/starred', '/user/subscriptions', '/user/repos');
}
foreach ($urls as $prio => $url) {
$getRepos($url, $prio + 1);
}
}
if (!$isRepo) {
Workflow::requestApi('/user/following', $curl, function ($urlUsers) use (&$users) {
$users = $urlUsers;
});
}
$curl->execute();
foreach ($repos as $repo) {
$icon = 'repo';
if ($repo->fork) {
$icon = 'fork';
} elseif ($repo->mirror_url) {
$icon = 'mirror';
}
if ($repo->private) {
$icon = 'private-' . $icon;
}
Workflow::addItem(Item::create()
->title($repo->full_name . ' ')
->subtitle($repo->description)
->icon($icon)
->arg('/' . $repo->full_name)
->prio(30 + $repo->prio)
);
}
foreach ($users as $user) {
Workflow::addItem(Item::create()
->prefix('@', false)
->title($user->login . ' ')
->subtitle($user->type)
->arg($user->html_url)
->icon(lcfirst($user->type))
->prio(20)
);
}
Workflow::addItem(Item::create()
->title('my ')
->subtitle('Dashboard, settings, and more')
->prio(10)
->valid(false)
);
}
private static function addRepoSubCommands()
{
$parts = self::$parts;
if (isset($parts[1][0]) && in_array($parts[1][0], array('#', '@', '*', '/'))) {
switch ($parts[1][0]) {
case '*':
$commits = Workflow::requestApi('/repos/' . $parts[0] . '/commits');
foreach ($commits as $commit) {
Workflow::addItem(Item::create()
->title($commit->commit->message)
->comparator($parts[0] . ' *' . $commit->sha)
->subtitle($commit->commit->author->date . ' (' . $commit->sha .')')
->icon('commits')
->arg('https://github.com/' . $parts[0] . '/commit/' . $commit->sha)
);
}
break;
case '@':
$branches = Workflow::requestApi('/repos/' . $parts[0] . '/branches');
foreach ($branches as $branch) {
Workflow::addItem(Item::create()
->title('@' . $branch->name)
->comparator($parts[0] . ' @' . $branch->name)
->subtitle($branch->commit->sha)
->icon('branch')
->arg('/' . $parts[0] . '/tree/' . $branch->name)
);
}
break;
case '/':
$repo = Workflow::requestApi('/repos/' . $parts[0]);
$files = Workflow::requestApi('/repos/' . $parts[0] . '/git/trees/' . $repo->default_branch . '?recursive=1');
foreach ($files->tree as $file) {
if ('blob' === $file->type) {
Workflow::addItem(Item::create()
->title(basename($file->path))
->subtitle('/' . $file->path)
->comparator($parts[0] . ' /' . $file->path)
->icon('file')
->arg('/' . $parts[0] . '/blob/' . $repo->default_branch . '/' . $file->path)
);
}
}
break;
case '#':
$issues = Workflow::requestApi('/repos/' . $parts[0] . '/issues?sort=updated&state=all');
foreach ($issues as $issue) {
Workflow::addItem(Item::create()
->title('#' . $issue->number)
->comparator($parts[0] . ' #' . $issue->number)
->subtitle($issue->title)
->icon($issue->pull_request ? 'pull-request' : 'issue')
->arg($issue->html_url)
);
}
break;
}
} else {
$subs = array(
'admin' => array('Manage this repo'),
'graphs' => array('All the graphs'),
'issues ' => array('List, show and create issues', 'issue'),
'network' => array('See the network', 'graphs'),
'pulls' => array('Show open pull requests', 'pull-request'),
'pulse' => array('See recent activity'),
'wiki' => array('Pull up the wiki'),
'commits' => array('View commit history')
);
foreach ($subs as $key => $sub) {
Workflow::addItem(Item::create()
->title($parts[0] . ' ' . $key)
->subtitle($sub[0])
->icon(isset($sub[1]) ? $sub[1] : $key)
->arg('/' . $parts[0] . '/' . $key)
);
}
Workflow::addItem(Item::create()
->title($parts[0] . ' new issue')
->subtitle('Create new issue')
->icon('issue')
->arg('/' . $parts[0] . '/issues/new?source=c')
);
Workflow::addItem(Item::create()
->title($parts[0] . ' new pull')
->subtitle('Create new pull request')
->icon('pull-request')
->arg('/' . $parts[0] . '/pull/new?source=c')
);
Workflow::addItem(Item::create()
->title($parts[0] . ' milestones')
->subtitle('View milestones')
->icon('milestone')
->arg('/' . $parts[0] . '/milestones')
);
if (empty($parts[1])) {
$subs = array(
'#' => array('Show a specific issue by number', 'issue'),
'@' => array('Show a specific branch', 'branch'),
'*' => array('Show a specific commit', 'commits'),
'/' => array('Show a blob', 'file')
);
foreach ($subs as $key => $sub) {
Workflow::addItem(Item::create()
->title($parts[0] . ' ' . $key)
->subtitle($sub[0])
->icon($sub[1])
->arg($key . ' ' . $parts[0])
->valid(false)
);
}
}
Workflow::addItem(Item::create()
->title($parts[0] . ' clone')
->subtitle('Clone this repo')
->icon('clone')
->arg('/' . $parts[0] . '.git')
);
}
}
private static function addUserSubCommands($queryUser)
{
$subs = array(
'contributions' => array($queryUser, "View $queryUser's contributions"),
'repositories' => array($queryUser . '?tab=repositories', "View $queryUser's repositories", 'repo'),
'activity' => array($queryUser . '?tab=activity', "View $queryUser's public activity"),
'stars' => array('stars/' . $queryUser, "View $queryUser's stars")
);
$prio = count($subs) + 1;
foreach ($subs as $key => $sub) {
Workflow::addItem(Item::create()
->prefix('@', false)
->title($queryUser . ' ' . $key)
->subtitle($sub[1])
->icon(isset($sub[2]) ? $sub[2] : $key)
->arg('/' . $sub[0])
->prio($prio--)
);
}
Workflow::addItem(Item::create()
->prefix('@', false)
->title($queryUser . ' gists')
->subtitle("View $queryUser's' gists")
->icon('gists')
->arg(Workflow::getGistUrl() . '/' . $queryUser)
->prio(1)
);
}
private static function addMyCommands()
{
$parts = self::$parts;
if (isset($parts[2]) && in_array($parts[1], array('pulls', 'issues'))) {
$icon = $parts[1] === 'pulls' ? 'pull-request' : 'issue';
$items = $icon . 's';
$subs = array(
'created' => array($parts[1], 'View your ' . $items),
'assigned' => array($parts[1] . '/assigned', 'View your assigned ' . $items),
'mentioned' => array($parts[1] . '/mentioned', 'View ' . $items . ' that mentioned you'),
);
foreach ($subs as $key => $sub) {
Workflow::addItem(Item::create()
->title('my ' . $parts[1] . ' ' . $key)
->subtitle($sub[1])
->icon($icon)
->arg('/' . $sub[0])
->prio(1)
);
}
return;
}
$myPages = array(
'dashboard' => array('', 'View your dashboard'),
'pulls ' => array('pulls', 'View your pull requests', 'pull-request'),
'issues ' => array('issues', 'View your issues', 'issue'),
'stars' => array('stars', 'View your starred repositories'),
'profile' => array(self::$user->login, 'View your public user profile', 'user'),
'settings' => array('settings', 'View or edit your account settings'),
'notifications' => array('notifications', 'View all your notifications')
);
foreach ($myPages as $key => $my) {
Workflow::addItem(Item::create()
->title('my ' . $key)
->subtitle($my[1])
->icon(isset($my[2]) ? $my[2] : rtrim($key))
->arg('/' . $my[0])
->prio(1)
);
}
Workflow::addItem(Item::create()
->title('my gists')
->subtitle('View your gists')
->icon('gists')
->arg(Workflow::getGistUrl() . '/' . self::$user->login)
->prio(1)
);
}
private static function addSystemCommands()
{
$cmds = array(
'delete cache' => 'Delete GitHub Cache',
'logout' => 'Log out this workflow',
'update' => 'Update this Alfred workflow'
);
if (Workflow::getConfig('autoupdate', true)) {
$cmds['deactivate autoupdate'] = 'Deactivate auto updating this Alfred Workflow';
} else {
$cmds['activate autoupdate'] = 'Activate auto updating this Alfred Workflow';
}
if (self::$enterprise) {
$cmds['enterprise reset'] = 'Reset the GitHub Enterprise URL';
}
foreach ($cmds as $cmd => $desc) {
Workflow::addItem(Item::create()
->title('> ' . $cmd)
->subtitle($desc)
->icon($cmd)
->arg('> ' . str_replace(' ', '-', $cmd))
);
}
$cmds = array(
'help' => 'readme',
'changelog' => 'changelog',
);
foreach ($cmds as $cmd => $file) {
Workflow::addItem(Item::create()
->title('> ' . $cmd)
->subtitle('View the ' . $file)
->icon('file')
->arg('https://github.com/gharlan/alfred-github-workflow/blob/master/' . strtoupper($file) . '.md')
);
}
}
}
print Search::run($argv[1], $argv[2]);