-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.php
192 lines (178 loc) · 6.6 KB
/
irc.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
<?php
/*
Add the IRC 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 <bryce@cobryce.com>
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 irc_server irc_port irc_room irc_nick
*/
list($app, $github_username, $github_password, $irc_server, $irc_port, $irc_room, $irc_nick) = $_SERVER['argv'];
if($_SERVER['argc'] < 2 || $_SERVER['argv'][1] == '-h' || $_SERVER['argv'][1] == '--help')
die("Syntax: $app github_username github_password irc_server irc_port irc_room irc_nick".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(!$irc_server)
die("You must specify the IRC server.".PHP_EOL);
if(!$irc_port)
die("You must specify the IRC server port.".PHP_EOL);
if(!$irc_room)
die("You must specify the IRC channel.".PHP_EOL);
if(!$irc_nick)
die("You must specify the IRC nickname.".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 IRC 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 == "irc")
{
echo "IRC 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 IRC hook to ".sizeof($repos)." repos.".PHP_EOL;
// Create the object we'll use
$hook = new stdClass;
$hook->active = true;
$hook->name = "irc";
$hook->config = new stdClass;
$hook->config->server = $irc_server;
$hook->config->port = $irc_port;
$hook->config->room = $irc_room;
$hook->config->nick = $irc_nick;
// XXX Find a friendlier way to change these
// By default, SSL is off, message_without_join is off, no_colors is off, long_url is on, and notice is off.
$hook->config->branch_regexes = "";
$hook->config->password = "";
$hook->config->ssl = false;
$hook->config->message_without_join = false;
$hook->config->no_colors = false;
$hook->config->long_url = true;
$hook->config->notice = false;
$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 "IRC service hook added to $repo_name.".PHP_EOL;
else
echo "IRC 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 (IRC, http://github.com/brycied00d/GitHub-Multihook-Enable)");
}
?>