-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_commit.module
204 lines (182 loc) · 6.15 KB
/
bot_commit.module
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
<?php
/**
* @file
* Enables queued messages for unavailable users.
*/
function bot_commit_perm() {
return array('access github callback');
}
/**
* Implementation of hook_help().
*/
function bot_commit_help($path, $arg) {
switch ($path) {
case 'irc:features':
return array(t('Bot Commit'));
case 'irc:features#bot_commi':
return t('Post commit messages and gitweb links to IRC. They are sent to the bot via XML-RPC from the git post-receive hook, or from github.');
}
}
/**
* Implementation of hook_menu().
*/
function bot_commit_menu() {
$items = array();
$items['admin/settings/bot/bot_commit'] = array(
'title' => 'Bot commit',
'description' => 'Configure the commit messages the bot can push to IRC.',
'page callback' => 'drupal_get_form',
'page arguments' => array('bot_commit_settings'),
'access arguments' => array('administer bot'),
);
$items['bot_commit/github'] = array(
'title' => 'Github callback',
'page callback' => 'bot_commit_github_receive',
'access arguments' => array('access github callback'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Submit handler for github.
*/
function bot_commit_github_receive() {
if ($payload = filter_input(INPUT_POST, 'payload')) {
$payload = json_decode($payload);
foreach ($payload->commits as $commit) {
bot_commit_queue($commit->id, $commit->author->name, $commit->message, $payload->repository->name, $commit->url);
}
}
}
function bot_commit_settings() {
$form = array();
// get a list of all joined channels, sans passwords.
$joined_channels = preg_split('/\s*,\s*/', variable_get('bot_channels', '#test'));
$channel_options = array(); // HOW MAY I HELP YOU!?
foreach ($joined_channels as $k => $v) {
$channel = preg_replace('/(.*) .*/', '\1', $v);
$channel_options[$channel] = $channel;
}
$form['bot_commit_channel'] = array(
'#title' => t('IRC channel'),
'#description' => t('Select the channel to broadcast commits.'),
'#type' => 'select',
'#size' => 10,
'#options' => $channel_options,
'#default_value' => variable_get('bot_commit_channel', ''),
);
$form['bot_commit_gitweb'] = array(
'#title' => t('Gitweb'),
'#description' => t('Specify the gitweb url you want to use. Use [commit] for the commit id and [repo] for the project repository. Example: http://git.example.com/?p=[repo];a=commitdiff;h=[commit].'),
'#type' => 'textfield',
'#size' => 40,
'#default_value' => variable_get('bot_commit_gitweb', ''),
);
$form['bot_commit_key'] = array(
'#title' => t('Message key'),
'#description' => t('Place this key in the configuration part of the git-irc.php so that only authorised sources can send messages to your IRC channel.'),
'#type' => 'textfield',
'#size' => 40,
'#default_value' => variable_get('bot_commit_key', ''),
);
if (module_exists('shorten')) {
$options = array(0 => t('None'));
$services = module_invoke_all('shorten_service');
foreach ($services as $service => $url) {
$options[$service] = $service;
}
$form['bot_commit_shorten_api'] = array(
'#title' => t('URL Shortening'),
'#description' => t('Set the preferred url shortening service that will be used to shorten the gitweb url.'),
'#type' => 'select',
'#options' => $options,
'#default_value' => variable_get('bot_commit_shorten_api', 0),
);
}
return system_settings_form($form);
}
/**
* Add the commit to the queue.
*/
function bot_commit_xmlrpc() {
return array(
array(
'bot_commit.recordCommit',
'bot_commit_record_commit',
array(
'string', // Return the commit id.
'string', // Message key.
'string', // Commit id.
'string', // Commit author.
'string', // Commit message.
'string', // Project repository.
),
t('Forwarding a commit message to IRC'),
),
);
}
/**
* Receive and record a commit entry from the XML-RPC call.
*/
function bot_commit_record_commit($key, $id, $author, $message, $repo) {
if (variable_get('bot_commit_key', '') == $key) {
bot_commit_queue($id, $author, $message, $repo);
}
else {
watchdog('bot_commit', 'Unauthorized commit record request.', array(), WATCHDOG_WARNING);
}
}
function bot_commit_queue($id, $author, $message, $repo, $url = '') {
$commit = new StdClass;
$commit->id = $id;
$commit->message = $message;
$commit->author = $author;
$commit->repo = $repo;
$commit->url = $url;
$commit->timestamp = time();
drupal_write_record('bot_commit_queue', $commit);
}
/**
* Implementation of hook_irc_bot_cron_fastest().
*/
function bot_commit_irc_bot_cron_fastest() {
// Go through the commit messages and push them to irc.
$result = db_query('SELECT qid, id, author, message, repo, timestamp, url FROM {bot_commit_queue} ORDER BY timestamp DESC');
while ($commit = db_fetch_object($result)) {
bot_commit_publish_message($commit);
db_query('DELETE FROM {bot_commit_queue} WHERE qid = %d', $commit->qid);
}
}
function bot_commit_publish_message($commit) {
if ($channel = variable_get('bot_commit_channel', '')) {
$project_name = str_replace('.git', '', $commit->repo);
// We work either from the specified url for this commit, or try to assemble one ourselves.
if ($commit->url) {
$url = $commit->url;
}
elseif ($gitweb = variable_get('bot_commit_gitweb', '')) {
$url = str_replace('[commit]', $commit->id, $gitweb);
$url = str_replace('[repo]', $commit->repo, $url);
}
if (isset($url) && module_exists('shorten') && $shorturl_service = variable_get('bot_commit_shorten_api', 0)) {
$url = shorten_url($url, $shorturl_service);
}
if (isset($url)) {
bot_message($channel, t('[@repo] @author: @message @url',
array(
'@author' => $commit->author,
'@message' => $commit->message,
'@repo' => $project_name,
'@url' => $url,
)));
}
else {
bot_message($channel, t('[@repo] @author: @message',
array(
'@author' => $commit->author,
'@message' => $commit->message,
'@repo' => $project_name
)));
}
}
}