forked from gharlan/alfred-github-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
278 lines (251 loc) · 10.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
<?php
require 'workflow.php';
$query = ltrim($argv[1]);
$parts = explode(' ', $query);
Workflow::init($query);
if (Workflow::checkUpdate()) {
$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()
->prefix('gh ')
->title('> ' . $cmd)
->subtitle($desc)
->arg('> ' . str_replace(' ', '-', $cmd))
->randomUid()
, false);
}
print Workflow::getItemsAsXml();
exit;
}
if (
!Workflow::getConfig('user') ||
!($users = Workflow::requestCacheJson('https://github.com/command_bar/users', 'results')) &&
!Workflow::requestCache('https://github.com/settings/profile')
) {
Workflow::removeConfig('user');
$user = null;
if (count($parts) > 1 && $parts[0] == '>' && $parts[1] == 'login' && isset($parts[2])) {
$user = $parts[2];
}
Workflow::addItem(Item::create()
->prefix('gh ')
->title('> login ' . $user)
->subtitle('Log in to GitHub')
->arg('> login ' . $user)
->valid((bool) $user, '<user>')
);
print Workflow::getItemsAsXml();
return;
}
$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) {
if (!$isUser && !$isMy && $isRepo && isset($parts[1])) {
if (isset($parts[1][0]) && in_array($parts[1][0], array('#', '@', '/'))) {
$compareDescription = false;
$pathAdd = '';
switch ($parts[1][0]) {
case '@':
$path = 'branches';
$url = 'tree';
break;
case '/':
$masterBranch = Workflow::requestCacheJson('https://api.github.com/repos/' . $parts[0], 'master_branch');
$branches = Workflow::requestCacheJson('https://github.com/command_bar/' . $parts[0] . '/branches', 'results');
foreach ($branches as $branch) {
if ($branch->display === $masterBranch) {
$pathAdd = $masterBranch . '?q=' . substr($parts[1], 1) . '&sha=' . $branch->description;
break;
}
}
$path = 'paths/';
$url = 'blob/' . $masterBranch;
break;
case '#':
$path = 'issues';
$url = 'issues';
if (isset($parts[1][1]) && intval($parts[1][1]) == 0) {
$pathAdd = '?q=' . substr($parts[1], 1);
$compareDescription = true;
}
break;
}
$subs = Workflow::requestCacheJson('https://github.com/command_bar/' . $parts[0] . '/' . $path . $pathAdd, 'results');
foreach ($subs as $sub) {
if (0 === strpos($sub->command, $parts[0] . ' ' . $parts[1][0])) {
$endPart = substr($sub->command, strlen($parts[0] . ' ' . $parts[1][0]));
Workflow::addItem(Item::create()
->title($sub->command)
->comparator($parts[0] . ' ' . $parts[1][0] . ($compareDescription ? $sub->description : $endPart))
->subtitle($sub->description)
->arg('https://github.com/' . $parts[0] . '/' . $url . '/' . $endPart)
->prio((isset($sub->multiplier) ? $sub->multiplier : 1))
);
}
}
} else {
$subs = array(
'admin' => 'Manage this repo',
'graphs' => 'All the graphs',
'issues ' => 'List, show and create issues',
'network' => 'See the network',
'pulls' => 'Show open pull requests',
'pulse' => 'See recent activity',
'wiki' => 'Pull up the wiki'
);
foreach ($subs as $key => $sub) {
Workflow::addItem(Item::create()
->title($parts[0] . ' ' . $key)
->subtitle($sub)
->arg('https://github.com/' . $parts[0] . '/' . $key)
);
}
Workflow::addItem(Item::create()
->title($parts[0] . ' new issue')
->subtitle('Create new issue')
->arg('https://github.com/' . $parts[0] . '/issues/new?source=c')
);
Workflow::addItem(Item::create()
->title($parts[0] . ' new pull')
->subtitle('Create new pull request')
->arg('https://github.com/' . $parts[0] . '/pull/new?source=c')
);
Workflow::addItem(Item::create()
->title($parts[0] . ' milestones')
->subtitle('View milestones')
->arg('https://github.com/' . $parts[0] . '/issues/milestones')
);
if (empty($parts[1])) {
$subs = array(
'#' => 'Show a specific issue by number',
'@' => 'Show a specific branch',
'/' => 'Show a blob'
);
foreach ($subs as $key => $subtitle) {
Workflow::addItem(Item::create()
->title($parts[0] . ' ' . $key)
->subtitle($subtitle)
->arg($key . ' ' . $parts[0])
->valid(false)
);
}
}
Workflow::addItem(Item::create()
->title($parts[0] . ' clone')
->subtitle('Clone this repo')
->arg('https://github.com/' . $parts[0] . '.git')
);
}
} elseif (!$isUser && !$isMy) {
$path = $isRepo ? 'repos_for/' . $queryUser : 'repos';
$repos = Workflow::requestCacheJson('https://github.com/command_bar/' . $path, 'results');
foreach ($repos as $repo) {
Workflow::addItem(Item::create()
->title($repo->command . ' ')
->subtitle($repo->description)
->arg('https://github.com/' . $repo->command)
->prio(30 + (isset($repo->multiplier) ? $repo->multiplier : 1))
);
}
}
if ($isUser && isset($parts[1])) {
$subs = array(
'contributions' => array($queryUser, "View $queryUser's contributions"),
'repositories' => array($queryUser . '?tab=repositories', "View $queryUser's repositories"),
'activity' => array($queryUser . '?tab=activity', "View $queryUser's public activity"),
'stars' => array('stars/' . $queryUser, "View $queryUser's stars")
);
$prio = count($subs);
foreach ($subs as $key => $sub) {
Workflow::addItem(Item::create()
->prefix('@', false)
->title($queryUser . ' ' . $key)
->subtitle($sub[1])
->arg('https://github.com/' . $sub[0])
->prio($prio--)
);
}
} elseif (!$isMy) {
if (!$isRepo) {
if ($queryUser) {
$users = Workflow::requestCacheJson('https://github.com/command_bar/users?q=' . urlencode($queryUser), 'results');
}
foreach ($users as $user) {
$name = substr($user->command, 1);
Workflow::addItem(Item::create()
->prefix('@', false)
->title($name . ' ')
->subtitle($user->description)
->arg('https://github.com/' . $name)
->prio(20 + (isset($user->multiplier) ? $user->multiplier : 1))
);
}
}
Workflow::addItem(Item::create()
->title('my ')
->subtitle('Dashboard, settings, and more')
->prio(10)
->valid(false)
);
} else {
$myPages = array(
'dashboard' => array('', 'View your dashboard'),
'pulls' => array('dashboard/pulls', 'View your pull requests'),
'issues' => array('dashboard/issues', 'View your issues'),
'stars' => array('stars', 'View your starred repositories'),
'profile' => array(Workflow::getConfig('user'), 'View your public user profile'),
'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])
->arg('https://github.com/' . $my[0])
->prio(1)
);
}
}
Workflow::sortItems();
if ($query) {
$path = $isUser ? $queryUser : 'search?q=' . urlencode($query);
Workflow::addItem(Item::create()
->title("Search GitHub for '$query'")
->arg('https://github.com/' . $path)
->autocomplete(false)
, false);
}
} else {
$cmds = array(
'logout' => 'Log out from GitHub (only this Alfred Workflow)',
'delete cache' => 'Delete GitHub Cache (only for this Alfred 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';
}
foreach ($cmds as $cmd => $desc) {
Workflow::addItem(Item::create()
->prefix('gh ')
->title('> ' . $cmd)
->subtitle($desc)
->arg('> ' . str_replace(' ', '-', $cmd))
);
}
Workflow::sortItems();
}
print Workflow::getItemsAsXml();