forked from gharlan/alfred-github-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
90 lines (77 loc) · 3.29 KB
/
action.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
<?php
require 'workflow.php';
Workflow::init();
$query = trim($argv[1]);
$parts = explode(' ', $query);
switch ($parts[0]) {
case '>':
switch ($parts[1]) {
case 'login':
$password = Workflow::askForPassword('GitHub Login', 'Password for "' . $parts[2] . '"');
$content = Workflow::request('https://github.com/session', $status, $etag, true, array(
'authenticity_token' => Workflow::getToken(),
'login' => $parts[2],
'password' => $password
));
if ($status === 200 && false === strpos($content, '<title>Sign in · GitHub</title>')) {
$authCode = Workflow::askForPassword('GitHub two-factor authentication', 'Authentication code');
$content = Workflow::request('https://github.com/sessions/two_factor', $status, $etag2, true, array(
'authenticity_token' => Workflow::getToken($content),
'otp' => $authCode
));
}
if ($status === 302 && false !== strpos(Workflow::request('https://github.com/'), '<title>GitHub</title>')) {
echo 'Successfully logged in';
Workflow::deleteCache();
Workflow::setConfig('user', $parts[2]);
} else {
echo 'Login failed';
}
break;
case 'logout':
Workflow::request('https://github.com/logout', $status, $etag, true, array(
'authenticity_token' => Workflow::getToken()
));
Workflow::deleteCookies();
Workflow::deleteCache();
echo 'Successfully logged out';
break;
case 'delete-cache':
Workflow::deleteCache();
echo 'Successfully deleted cache';
break;
case 'refresh-cache':
Workflow::requestCache($parts[2], 0, false);
break;
case 'activate-autoupdate':
Workflow::setConfig('autoupdate', 1);
echo 'Activated auto updating';
break;
case 'deactivate-autoupdate':
Workflow::setConfig('autoupdate', 0);
echo 'Deactivated auto updating';
break;
case 'update':
$c = Workflow::request('http://gh01.de/alfred/github/github.alfredworkflow', $status);
if ($status != 200) {
echo 'Update failed';
exit;
}
$zip = __DIR__ . '/workflow.zip';
file_put_contents($zip, $c);
$phar = new PharData($zip);
foreach ($phar as $path => $file) {
copy($path, __DIR__ . '/' . $file->getFilename());
}
unlink($zip);
Workflow::deleteCache();
echo 'Successfully updated the GitHub Workflow';
break;
}
break;
default:
if ('.git' == substr($query, -4)) {
$query = 'github-mac://openRepo/' . substr($query, 0, -4);
}
exec('osascript -e "open location \"' . $query . '\""');
}