Indefero

Indefero Commit Details


Date:2011-09-25 17:51:33 (13 years 2 months ago)
Author:Thomas Keller
Branch:develop, release-1.3
Commit:7ff298af79f43a23d40633f608ef6749810eb3de
Parents: b29acd71cb32521c63d9ce94539c586b89d43c68
Message:Call the configured download webhook when new downloads are created or existing downloads are updated.

This feature is sponsored by Scilab.
Changes:

File differences

src/IDF/Commit.php
299299
300300
301301
302
302303
303304
304305
'project_id' => $project->id,
'authkey' => $project->getWebHookKey(),
'url' => $url,
'method' => 'POST',
);
$item = new IDF_Queue();
$item->type = 'new_commit';
src/IDF/Form/UpdateUpload.php
9696
9797
9898
99
99
100100
101101
102102
......
146146
147147
148148
149
150
151
149152
150153
151154
......
166169
167170
168171
169
172
170173
171174
172175
$this->cleaned_data['label'.$i] = trim($this->cleaned_data['label'.$i]);
if (strpos($this->cleaned_data['label'.$i], ':') !== false) {
list($class, $name) = explode(':', $this->cleaned_data['label'.$i], 2);
list($class, $name) = array(mb_strtolower(trim($class)),
list($class, $name) = array(mb_strtolower(trim($class)),
trim($name));
} else {
$class = 'other';
$this->upload->modif_dtime = gmdate('Y-m-d H:i:s');
$this->upload->update();
$this->upload->batchAssoc('IDF_Tag', $tags);
// Send the notification
$this->upload->notify($this->project->getConf(), false);
/**
* [signal]
*
*
*/
$params = array('upload' => $this->upload);
Pluf_Signal::send('IDF_Upload::update',
Pluf_Signal::send('IDF_Upload::update',
'IDF_Form_UpdateUpload', $params);
return $this->upload;
}
src/IDF/Upload.php
234234
235235
236236
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
237276
238277
239278
*/
public function notify($conf, $create=true)
{
$project = $this->get_project();
$url = str_replace(array('%p', '%d'),
array($project->shortname, $this->id),
$conf->getVal('upload_webhook_url', ''));
$tags = array();
foreach ($this->get_tags_list() as $tag) {
$tags[] = $tag->class.':'.$tag->name;
}
$payload = array(
'to_send' => array(
'project' => $project->shortname,
'id' => $this->id,
'summary' => $this->summary,
'changelog' => $this->changelog,
'filename' => $this->file,
'filesize' => $this->filesize,
'md5sum' => $this->md5,
'tags' => $tags,
),
'project_id' => $project->id,
'authkey' => $project->getWebHookKey(),
'url' => $url,
);
if ($create === true) {
$payload['method'] = 'PUT';
$payload['to_send']['creation_date'] = $this->creation_dtime;
} else {
$payload['method'] = 'POST';
$payload['to_send']['update_date'] = $this->modif_dtime;
}
$item = new IDF_Queue();
$item->type = 'upload';
$item->payload = $payload;
$item->create();
if ('' == $conf->getVal('downloads_notification_email', '')) {
return;
}
src/IDF/Webhook.php
3131
3232
3333
34
34
3535
3636
3737
3838
39
39
4040
4141
4242
4343
44
44
4545
4646
47
47
4848
4949
5050
......
6161
6262
6363
64
64
6565
6666
6767
......
7676
7777
7878
79
79
8080
8181
8282
83
83
8484
8585
8686
......
9090
9191
9292
93
93
9494
9595
9696
class IDF_Webhook
{
/**
* Perform the POST request given the webhook payload.
* Perform the request given the webhook payload.
*
* @param array Payload
* @return bool Success or error
*/
public static function postNotification($payload)
public static function processNotification($payload)
{
$data = json_encode($payload['to_send']);
$sign = hash_hmac('md5', $data, $payload['authkey']);
$params = array('http' => array(
'method' => 'POST',
'method' => empty($payload['method']) ? 'POST' : $payload['method'],
'content' => $data,
'user_agent' => 'Indefero Hook Sender (http://www.indefero.net)',
'max_redirects' => 0,
'max_redirects' => 0,
'timeout' => 15,
'header'=> 'Post-Commit-Hook-Hmac: '.$sign."\r\n"
.'Content-Type: application/json'."\r\n",
if (!isset($meta['wrapper_data'][0]) or $meta['timed_out']) {
return false;
}
if (0 === strpos($meta['wrapper_data'][0], 'HTTP/1.1 2') or
if (0 === strpos($meta['wrapper_data'][0], 'HTTP/1.1 2') or
0 === strpos($meta['wrapper_data'][0], 'HTTP/1.1 3')) {
return true;
}
public static function process($sender, &$params)
{
$item = $params['item'];
if ($item->type != 'new_commit') {
if (!in_array($item->type, array('new_commit', 'upload'))) {
// We do nothing.
return;
}
if (isset($params['res']['IDF_Webhook::process']) and
if (isset($params['res']['IDF_Webhook::process']) and
$params['res']['IDF_Webhook::process'] == true) {
// Already processed.
return;
return;
}
// We have either to retry or to push for the first time.
$res = self::postNotification($item->payload);
$res = self::processNotification($item->payload);
if ($res) {
$params['res']['IDF_Webhook::process'] = true;
} elseif ($item->trials >= 9) {

Archive Download the corresponding diff file

Page rendered in 0.08677s using 13 queries.