From b29acd71cb32521c63d9ce94539c586b89d43c68 Mon Sep 17 00:00:00 2001 From: Thomas Keller Date: Sun, 25 Sep 2011 02:03:35 +0200 Subject: [PATCH] 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. --- src/IDF/Commit.php | 2 +- src/IDF/Form/SourceConf.php | 3 -- src/IDF/Form/UploadConf.php | 10 +++++- src/IDF/Project.php | 4 +-- src/IDF/Views/Project.php | 12 +++++--- src/IDF/templates/idf/admin/downloads.html | 36 +++++++++++++++++++++- src/IDF/templates/idf/admin/source.html | 17 +++++----- 7 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/IDF/Commit.php b/src/IDF/Commit.php index 0e724ad..6be1a95 100644 --- a/src/IDF/Commit.php +++ b/src/IDF/Commit.php @@ -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(); diff --git a/src/IDF/Form/SourceConf.php b/src/IDF/Form/SourceConf.php index df2a6cc..5a9a183 100644 --- a/src/IDF/Form/SourceConf.php +++ b/src/IDF/Form/SourceConf.php @@ -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 post-commit web hooks.'), $url), 'widget_attrs' => array('size' => 35), )); diff --git a/src/IDF/Form/UploadConf.php b/src/IDF/Form/UploadConf.php index f6b4c7d..eec6788 100644 --- a/src/IDF/Form/UploadConf.php +++ b/src/IDF/Form/UploadConf.php @@ -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), + )); + } } diff --git a/src/IDF/Project.php b/src/IDF/Project.php index 598f4a3..ab25cf5 100644 --- a/src/IDF/Project.php +++ b/src/IDF/Project.php @@ -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); } diff --git a/src/IDF/Views/Project.php b/src/IDF/Views/Project.php index 70b2efa..54c52ee 100644 --- a/src/IDF/Views/Project.php +++ b/src/IDF/Views/Project.php @@ -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); } diff --git a/src/IDF/templates/idf/admin/downloads.html b/src/IDF/templates/idf/admin/downloads.html index c22be5e..a614484 100644 --- a/src/IDF/templates/idf/admin/downloads.html +++ b/src/IDF/templates/idf/admin/downloads.html @@ -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}
@@ -16,6 +16,15 @@ + + + + + + @@ -31,4 +40,29 @@

Optionally, use an equals-sign to document the meaning of each status value.

{/blocktrans} +
+ +{blocktrans}

The webhook URL setting specifies an URL to which a HTTP PUT +request is sent after a new download has been added or to which a HTTP POST +request is sent after an existing download has been updated. +If this field is empty, notifications are disabled.

+ +

Only properly-escaped HTTP URLs are supported, for example:

+ +
    +
  • http://domain.com/upload
  • +
  • http://domain.com/upload?my%20param
  • +
+ +

In addition, the URL may contain the following "%" notation, which +will be replaced with specific project values for each download:

+ +
    +
  • %p - project name
  • +
  • %d - download id
  • +
+ +

For example, updating download 123 of project 'my-project' with +web hook URL http://mydomain.com/%p/%d would send a POST request to +http://mydomain.com/my-project/123.

{/blocktrans}
{/block} diff --git a/src/IDF/templates/idf/admin/source.html b/src/IDF/templates/idf/admin/source.html index 3885227..69f3e88 100644 --- a/src/IDF/templates/idf/admin/source.html +++ b/src/IDF/templates/idf/admin/source.html @@ -42,11 +42,10 @@ - + @@ -68,26 +67,26 @@
-{blocktrans}

The webhook URL setting specifies a URL to which a HTTP POST +{blocktrans}

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.

Only properly-escaped HTTP URLs are supported, for example:

    -
  • http://domain.com/commit
  • -
  • http://domain.com/commit?my%20param
  • +
  • http://domain.com/commit
  • +
  • http://domain.com/commit?my%20param

In addition, the URL may contain the following "%" notation, which will be replaced with specific project values for each commit:

    -
  • %p - project name
  • -
  • %r - revision number
  • +
  • %p - project name
  • +
  • %r - revision number

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.

{/blocktrans}
+post-commit URL http://mydomain.com/%p/%r would send a request to +http://mydomain.com/my-project/123.

{/blocktrans} {/block}
{$form.f.upload_webhook_url.labelTag}:
+{if $form.f.upload_webhook_url.errors}{$form.f.upload_webhook_url.fieldErrors}{/if} +{$form.f.upload_webhook_url|unsafe}
+
{trans 'Web-Hook authentication key:'} {$hookkey}
{$form.f.webhook_url.labelTag}: {if $form.f.webhook_url.errors}{$form.f.webhook_url.fieldErrors}{/if} {$form.f.webhook_url|unsafe}
-
{trans 'Post-commit authentication key:'}{trans 'Web-Hook authentication key:'} {$hookkey}