Add an option to specify a webhook URL for updates in the downloads section

and tweak the help texts for the original source web hook a bit. Also remove
the superfluous inline help code from the SourceConf form that was actually
not used.

This feature was sponsored by Scilab.
This commit is contained in:
Thomas Keller 2011-09-25 02:03:35 +02:00
parent dc50e9b316
commit b29acd71cb
7 changed files with 63 additions and 21 deletions

View File

@ -297,7 +297,7 @@ class IDF_Commit extends Pluf_Model
'creation_date' => $this->creation_dtime,
),
'project_id' => $project->id,
'authkey' => $project->getPostCommitHookKey(),
'authkey' => $project->getWebHookKey(),
'url' => $url,
);
$item = new IDF_Queue();

View File

@ -49,13 +49,10 @@ class IDF_Form_SourceConf extends Pluf_Form
'widget' => 'Pluf_Form_Widget_PasswordInput',
));
}
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
$url = Pluf_HTTP_URL_urlForView('idf_faq').'#webhooks';
$this->fields['webhook_url'] = new Pluf_Form_Field_Url(
array('required' => false,
'label' => __('Webhook URL'),
'initial' => $this->conf->getVal('webhook_url', ''),
'help_text' => sprintf(__('Learn more about the <a href="%s">post-commit web hooks</a>.'), $url),
'widget_attrs' => array('size' => 35),
));

View File

@ -60,10 +60,18 @@ Deprecated = Most users should NOT download this';
$this->fields['labels_download_one_max'] = new Pluf_Form_Field_Varchar(
array('required' => false,
'label' => __('Each download may have at most one label with each of these classes'),
'initial' => self::init_one_max,
'initial' => self::init_one_max,
'widget_attrs' => array('size' => 60),
));
$this->conf = $extra['conf'];
$this->fields['upload_webhook_url'] = new Pluf_Form_Field_Url(
array('required' => false,
'label' => __('Webhook URL'),
'initial' => $this->conf->getVal('upload_webhook_url', ''),
'widget_attrs' => array('size' => 60),
));
}
}

View File

@ -494,12 +494,12 @@ GROUP BY uid";
}
/**
* Get the post commit hook key.
* Get the web hook key.
*
* The goal is to get something predictable but from which one
* cannot reverse find the secret key.
*/
public function getPostCommitHookKey()
public function getWebHookKey()
{
return md5($this->id.sha1(Pluf::f('secret_key')).$this->shortname);
}

View File

@ -375,8 +375,11 @@ class IDF_Views_Project
$title = sprintf(__('%s Downloads Configuration'), (string) $prj);
$conf = new IDF_Conf();
$conf->setProject($prj);
$extra = array(
'conf' => $conf,
);
if ($request->method == 'POST') {
$form = new IDF_Form_UploadConf($request->POST);
$form = new IDF_Form_UploadConf($request->POST, $extra);
if ($form->isValid()) {
foreach ($form->cleaned_data as $key=>$val) {
$conf->setVal($key, $val);
@ -388,7 +391,7 @@ class IDF_Views_Project
}
} else {
$params = array();
$keys = array('labels_download_predefined', 'labels_download_one_max');
$keys = array('labels_download_predefined', 'labels_download_one_max', 'upload_webhook_url');
foreach ($keys as $key) {
$_val = $conf->getVal($key, false);
if ($_val !== false) {
@ -398,12 +401,13 @@ class IDF_Views_Project
if (count($params) == 0) {
$params = null; //Nothing in the db, so new form.
}
$form = new IDF_Form_UploadConf($params);
$form = new IDF_Form_UploadConf($params, $extra);
}
return Pluf_Shortcuts_RenderToResponse('idf/admin/downloads.html',
array(
'page_title' => $title,
'form' => $form,
'hookkey' => $prj->getWebHookKey(),
),
$request);
}
@ -598,7 +602,7 @@ class IDF_Views_Project
'repository_size' => $prj->getRepositorySize(),
'page_title' => $title,
'form' => $form,
'hookkey' => $prj->getPostCommitHookKey(),
'hookkey' => $prj->getWebHookKey(),
),
$request);
}

View File

@ -1,5 +1,5 @@
{extends "idf/admin/base.html"}
{block docclass}yui-t1{assign $inDownloads = true}{/block}
{block docclass}yui-t3{assign $inDownloads = true}{/block}
{block body}
<form method="post" action=".">
<table class="form" summary="">
@ -16,6 +16,15 @@
</td>
</tr>
<tr>
<td>{$form.f.upload_webhook_url.labelTag}:<br />
{if $form.f.upload_webhook_url.errors}{$form.f.upload_webhook_url.fieldErrors}{/if}
{$form.f.upload_webhook_url|unsafe}<br>
</td>
</tr>
<tr>
<td>{trans 'Web-Hook authentication key:'} {$hookkey}</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="{trans 'Save Changes'}" name="submit" />
</td>
@ -31,4 +40,29 @@
<p>Optionally, use an equals-sign to document the meaning of each status value.</p>
{/blocktrans}
</div>
<div class="issue-submit-info">
{blocktrans}<p>The webhook URL setting specifies an URL to which a HTTP <strong>PUT</strong>
request is sent after a new download has been added or to which a HTTP <strong>POST</strong>
request is sent after an existing download has been updated.
If this field is empty, notifications are disabled.</p>
<p>Only properly-escaped <strong>HTTP</strong> URLs are supported, for example:</p>
<ul>
<li><code>http://domain.com/upload</code></li>
<li><code>http://domain.com/upload?my%20param</code></li>
</ul>
<p>In addition, the URL may contain the following "%" notation, which
will be replaced with specific project values for each download:</p>
<ul>
<li><code>%p</code> - project name</li>
<li><code>%d</code> - download id</li>
</ul>
<p>For example, updating download 123 of project 'my-project' with
web hook URL <code>http://mydomain.com/%p/%d</code> would send a POST request to
<code>http://mydomain.com/my-project/123</code>.</p>{/blocktrans}</div>
{/block}

View File

@ -42,11 +42,10 @@
<th>{$form.f.webhook_url.labelTag}:</th>
<td>{if $form.f.webhook_url.errors}{$form.f.webhook_url.fieldErrors}{/if}
{$form.f.webhook_url|unsafe}<br>
</td>
</tr>
<tr>
<th>{trans 'Post-commit authentication key:'}</th>
<th>{trans 'Web-Hook authentication key:'}</th>
<td>{$hookkey}
</td>
</tr>
@ -68,26 +67,26 @@
<br>
<div class="issue-submit-info">
{blocktrans}<p>The webhook URL setting specifies a URL to which a HTTP POST
{blocktrans}<p>The webhook URL setting specifies an URL to which a HTTP POST
request is sent after each repository commit. If this field is empty,
notifications are disabled.</p>
<p>Only properly-escaped <strong>HTTP</strong> URLs are supported, for example:</p>
<ul>
<li>http://domain.com/commit</li>
<li>http://domain.com/commit?my%20param</li>
<li><code>http://domain.com/commit</code></li>
<li><code>http://domain.com/commit?my%20param</code></li>
</ul>
<p>In addition, the URL may contain the following "%" notation, which
will be replaced with specific project values for each commit:</p>
<ul>
<li>%p - project name</li>
<li>%r - revision number</li>
<li><code>%p</code> - project name</li>
<li><code>%r</code> - revision number</li>
</ul>
<p>For example, committing revision 123 to project 'my-project' with
post-commit URL http://mydomain.com/%p/%r would send a request to
http://mydomain.com/my-project/123.</p>{/blocktrans}</div>
post-commit URL <code>http://mydomain.com/%p/%r</code> would send a request to
<code>http://mydomain.com/my-project/123</code>.</p>{/blocktrans}</div>
{/block}