-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushover.php
175 lines (161 loc) · 6.02 KB
/
pushover.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
<?php
/*
Add the Pushover hook to all of your repos
* At this phase, it's just a quick and dirty script. If I
still have the motivation, I'll blow it up into something
better. And probably in Python.
Copyright (c) 2012, Bryce Chidester <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
Syntax: $0 github_username github_password pushover_apikey [pushover_device]
*/
list($app, $github_username, $github_password, $pushover_apikey, $pushover_device) = $_SERVER['argv'];
if($_SERVER['argc'] < 2 || $_SERVER['argv'][1] == '-h' || $_SERVER['argv'][1] == '--help')
die("Syntax: $app github_username github_password pushover_apikey [pushover_device]".PHP_EOL);
if(!$github_username)
die("You must specify your GitHub username.".PHP_EOL);
if(!$github_password)
die("You must specify your GitHub password.".PHP_EOL);
if(!$pushover_apikey)
die("You must specify your Pushover.net API key.".PHP_EOL);
$ch_repos = curl_init("https://api.github.com/user/repos");
apply_curl_setopt($ch_repos);
$response = curl_exec($ch_repos);
$json = json_decode($response);
if($json === false)
die("Unable to fetch your GitHub repos: $response".PHP_EOL);
elseif(sizeof($json) === 0)
die("You have no GitHub repos, at all. I cannot continue.".PHP_EOL);
/* Fetch the list of all repos to which we have Admin privileges. */
$repos = array();
foreach($json as $repo)
{
if($repo->permissions->admin)
{
$repos[ $repo->full_name ] = $repo->url;
echo "Adding {$repo->full_name} to the list of repos.".PHP_EOL;
} else
echo "Skipping {$repo->full_name} -- You do not have Admin privileges.".PHP_EOL;
}
if(sizeof($repos) === 0)
die("You have no workable GitHub repos. You can only add hooks to repos to which you have administrative privileges.".PHP_EOL);
/* Now review those repos and exclude any with Pushover already enabled */
$ch_repos = curl_multi_init();
$ch = array();
foreach($repos as $repo_name => $repo_url)
{
$ch[ $repo_name ] = curl_init($repo_url."/hooks");
apply_curl_setopt($ch[ $repo_name ]);
curl_multi_add_handle($ch_repos, $ch[ $repo_name ]);
}
// Spool the requests
$active = null;
do
{
$mrc = curl_multi_exec($ch_repos, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// Everything has been "spooled", so now we handle things as they're ready
while ($active && $mrc == CURLM_OK)
{
// select() and wait for one to return
if(curl_multi_select($ch_repos) != -1)
{
do
{
$mrc = curl_multi_exec($ch_repos, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
// All sockets complete, now we get to process the returned data
foreach($ch as $repo_name => $c)
{
$response = curl_multi_getcontent($c);
curl_multi_remove_handle($ch_repos, $c);
$json = json_decode($response);
if($json === false)
{
echo "ERROR! Error in GitHub's response for /hooks on $repo_name. Skipping.".PHP_EOL;
echo $response.PHP_EOL;
unset($repos[ $repo_name ]);
continue;
}
foreach($json as $hook)
{
if($hook->name == "pushover")
{
echo "Pushover service hook already enabled for $repo_name. Ignoring this repo.".PHP_EOL;
unset($repos[ $repo_name ]);
break;
} // else, it stays in the list $repos
}
}
curl_multi_close($ch_repos);
echo "Adding the Pushover hook to ".sizeof($repos)." repos.".PHP_EOL;
// Create the object we'll use
$hook = new stdClass;
$hook->active = true;
$hook->name = "pushover";
$hook->config = new stdClass;
$hook->config->user_key = $pushover_apikey;
$hook->config->device_name = (string)$pushover_device;
$json = json_encode($hook);
$ch_repos = curl_multi_init();
$ch = array();
foreach($repos as $repo_name => $repo_url)
{
$ch[ $repo_name ] = curl_init($repo_url."/hooks");
apply_curl_setopt($ch[ $repo_name ]);
curl_setopt($ch[ $repo_name ], CURLOPT_CUSTOMREQUEST, "POST"); // CustomRequest so avoid setting an encoding type
curl_setopt($ch[ $repo_name ], CURLOPT_POSTFIELDS, $json);
curl_multi_add_handle($ch_repos, $ch[ $repo_name ]);
}
// Spool the requests
$active = null;
do
{
$mrc = curl_multi_exec($ch_repos, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// Everything has been "spooled", so now we handle things as they're ready
while ($active && $mrc == CURLM_OK)
{
// select() and wait for one to return
if(curl_multi_select($ch_repos) != -1)
{
do
{
$mrc = curl_multi_exec($ch_repos, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
// All sockets complete, now we get to process the returned data
foreach($ch as $repo_name => $c)
{
$response = curl_multi_getcontent($c);
curl_multi_remove_handle($ch_repos, $c);
$json = json_decode($response);
if($json === false)
{
echo "ERROR! Error in GitHub's response for POST /hooks on $repo_name. You should check this manually.".PHP_EOL;
echo $response.PHP_EOL;
continue;
}
if($json->id)
echo "Pushover service hook added to $repo_name.".PHP_EOL;
else
echo "Pushover service hook not added to $repo_name, or something else is weird. $response".PHP_EOL;
}
curl_multi_close($ch_repos);
function apply_curl_setopt(&$ch)
{
global $github_username, $github_password;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "{$github_username}:{$github_password}");
curl_setopt($ch, CURLOPT_USERAGENT, "GitHub-Multihook-Enable v1.0 (Pushover, http://github.com/brycied00d/GitHub-Multihook-Enable)");
}
?>