4 * Copyright 2011, Franck Villaume - Capgemini
5 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
6 * Copyright 2012, Franck Villaume - TrivialDev
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 class scmhookPlugin extends Plugin {
25 function __construct() {
27 $this->name = 'scmhook';
28 $this->text = 'Scmhook'; // To show in the tabs, use...
29 $this->_addHook('groupmenu'); // To put into the project tabs
30 $this->_addHook('groupisactivecheckbox'); // The "use ..." checkbox in editgroupinfo
31 $this->_addHook('groupisactivecheckboxpost'); //
32 $this->_addHook('scm_admin_page');
33 $this->_addHook('scm_admin_update');
34 $this->_addHook('artifact_extra_detail');
35 $this->_addHook('task_extra_detail');
36 $this->_addHook('javascript_file');
39 function CallHook($hookname, &$params) {
42 case 'scm_admin_page': {
43 $group_id = $params['group_id'];
44 $scm_plugin = $params['scm_plugin'];
45 $group = &group_get_object($group_id);
46 if ($group->usesPlugin($this->name) && $scm_plugin) {
47 $this->displayScmHook($group_id, $scm_plugin);
51 case 'scm_admin_update': {
52 $this->update($params);
55 case 'artifact_extra_detail': {
56 $group_id = $params['group_id'];
57 $group = &group_get_object($group_id);
58 if ($group->usesPlugin($this->name)) {
59 $this->artifact_extra_detail($params);
63 case 'task_extra_detail': {
64 $group_id = $params['group_id'];
65 $group = &group_get_object($group_id);
66 if ($group->usesPlugin($this->name)) {
67 $this->task_extra_detail($params);
71 case 'javascript_file': {
72 use_javascript('/js/sortable.js');
79 function exists($group_id) {
80 $res = db_query_params('SELECT id_group FROM plugin_scmhook WHERE id_group = $1', array($group_id));
90 function add($group_id) {
91 if (!$this->exists($group_id)) {
92 $res = db_query_params('INSERT INTO plugin_scmhook (id_group) VALUES ($1)', array($group_id));
100 function remove($group_id) {
101 if ($this->exists($group_id)) {
102 $res = db_query_params('DELETE FROM plugin_scmhook where id_group = $1', array($group_id));
110 function update($params) {
111 $group_id = $params['group_id'];
113 foreach($params as $key => $value) {
114 if ($key == strstr($key, 'scm')) {
115 $hookname = preg_replace('/scm[a-z][a-z]+_/','',$key);
116 if ($key != $hookname) { //handle the case of scm_enable_anonymous
117 if (strlen($hooksString)) {
118 $hooksString .= '|'.$hookname;
120 $hooksString .= $hookname;
125 $res = db_query_params('UPDATE plugin_scmhook set hooks = $1, need_update = 1 where id_group = $2',
126 array($hooksString, $group_id));
134 function displayScmHook($group_id, $scm) {
135 $hooksAvailable = $this->getAvailableHooks($group_id);
136 $statusDeploy = $this->getStatusDeploy($group_id);
137 $hooksEnabled = $this->getEnabledHooks($group_id);
138 if (count($hooksAvailable)) {
139 echo '<div id="scmhook">';
141 echo '<p class="warning">'._('Hooks management update process waiting ...').'</p>';
143 echo '<h2>'._('Enable Repository Hooks').'</h2>';
146 $this->displayScmSvnHook($hooksAvailable, $statusDeploy, $hooksEnabled);
150 $this->displayScmHgHook($hooksAvailable, $statusDeploy, $hooksEnabled);
154 echo '<div class="warning">'._('SCM Type not supported yet by scmhook').'</div>';
160 echo '<p class="information">'._('No hooks available').'</p>';
164 function getStatusDeploy($group_id) {
165 $res = db_query_params('SELECT need_update FROM plugin_scmhook WHERE id_group = $1', array($group_id));
169 $row = db_fetch_array($res);
170 return $row['need_update'];
173 function getAvailableHooks($group_id) {
174 $listScm = $this->getListLibraryScm();
175 $group = &group_get_object($group_id);
176 for ($i = 0; $i < count($listScm); $i++) {
177 if ($group->usesPlugin($listScm[$i])) {
178 return $this->getListLibraryHook($listScm[$i]);
184 function getEnabledHooks($group_id) {
185 $res = db_query_params('SELECT hooks FROM plugin_scmhook WHERE id_group = $1', array($group_id));
189 $row = db_fetch_array($res);
191 return explode('|', $row['hooks']);
197 function getListLibraryScm() {
198 return array_values(array_diff(scandir(dirname(__FILE__).'/../library/'), Array('.', '..', '.svn')));
201 function getListLibraryHook($scm) {
202 $listHooks = array_values(array_diff(scandir(dirname(__FILE__).'/../library/'.$scm), array('.', '..', '.svn', 'skel', 'hooks', 'depends', 'cronjobs')));
203 $validHooks = array();
204 foreach ($listHooks as $hook) {
205 if (!stristr($hook,'~')) {
206 include_once dirname(__FILE__).'/../library/'.$scm.'/'.$hook;
207 $hookClassName = preg_replace('/.class.php/','', $hook);
208 $hookObject = new $hookClassName;
209 $validHooks[] = $hookObject;
215 function artifact_extra_detail($params) {
216 $hooksAvailable = $this->getAvailableHooks($params['group_id']);
217 $hooksEnabled = $this->getEnabledHooks($params['group_id']);
218 foreach ($hooksAvailable as $hookAvailable) {
219 if (in_array($hookAvailable->getClassname(), $hooksEnabled)) {
220 if (method_exists($hookAvailable,'artifact_extra_detail')) {
221 $hookAvailable->artifact_extra_detail($params);
227 function task_extra_detail($params) {
228 $hooksAvailable = $this->getAvailableHooks($params['group_id']);
229 $hooksEnabled = $this->getEnabledHooks($params['group_id']);
230 foreach ($hooksAvailable as $hookAvailable) {
231 if (in_array($hookAvailable->getClassname(), $hooksEnabled)) {
232 if (method_exists($hookAvailable,'task_extra_detail')) {
233 $hookAvailable->task_extra_detail($params);
240 * override default groupisactivecheckboxpost function for init value in db
242 function groupisactivecheckboxpost(&$params) {
243 // this code actually activates/deactivates the plugin after the form was submitted in the project edit public info page
244 $group = group_get_object($params['group']);
245 $flag = strtolower('use_'.$this->name);
246 if (getIntFromRequest($flag) == 1) {
247 $group->setPluginUse($this->name);
248 $this->add($group->getID());
250 $group->setPluginUse($this->name, false);
251 $this->remove($group->getID());
256 function displayScmSvnHook($hooksAvailable, $statusDeploy, $hooksEnabled) {
258 $hooksPreCommit = array();
259 $hooksPreRevPropChange = array();
260 $hooksPostCommit = array();
261 foreach ($hooksAvailable as $hook) {
262 switch ($hook->getHookType()) {
264 $hooksPreCommit[] = $hook;
267 case "pre-revprop-change": {
268 $hooksPreRevPropChange[] = $hook;
271 case "post-commit": {
272 $hooksPostCommit[] = $hook;
276 //byebye hook.... we do not know you...
281 if (count($hooksPreCommit)) {
282 echo '<h3>'._('pre-commit Hooks').'</h3>';
283 $tabletop = array('', _('Hook Name'), _('Description'));
284 $classth = array('unsortable', '', '');
285 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_precommit', 'sortable', $classth);
286 foreach ($hooksPreCommit as $hookPreCommit) {
288 if (! empty($hookPreCommit->onlyGlobalAdmin) && ! Permission::isGlobalAdmin()) {
289 echo '<tr style="display: none;" ><td>';
294 echo '<input type="checkbox" ';
295 echo 'name="'.$hookPreCommit->getLabel().'_'.$hookPreCommit->getClassname().'" ';
296 if (in_array($hookPreCommit->getClassname(), $hooksEnabled))
297 echo ' checked="checked"';
301 echo ' disabled="disabled"';
303 if (!$isdisabled && !$hookPreCommit->isAvailable())
304 echo ' disabled="disabled"';
308 if (!$hookPreCommit->isAvailable())
309 echo ' class="tabtitle-w" title="'.$hookPreCommit->getDisabledMessage().'"';
312 echo $hookPreCommit->getName();
314 echo $hookPreCommit->getDescription();
317 echo $HTML->listTableBottom();
319 if (count($hooksPreRevPropChange)) {
320 echo '<h3>'._('pre-revprop-change Hooks').'</h3>';
321 $tabletop = array('', _('Hook Name'), _('Description'));
322 $classth = array('unsortable', '', '');
323 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_precommit', 'sortable', $classth);
324 foreach ($hooksPreRevPropChange as $hook) {
326 if (! empty($hook->onlyGlobalAdmin) && ! Permission::isGlobalAdmin()) {
327 echo '<tr style="display: none;" ><td>';
332 echo '<input type="checkbox" ';
333 echo 'name="'.$hook->getLabel().'_'.$hook->getClassname().'" ';
334 if (in_array($hook->getClassname(), $hooksEnabled))
335 echo ' checked="checked"';
339 echo ' disabled="disabled"';
341 if (!$isdisabled && !$hook->isAvailable())
342 echo ' disabled="disabled"';
346 if (!$hook->isAvailable())
347 echo ' class="tabtitle-w" title="'.$hook->getDisabledMessage().'"';
350 echo $hook->getName();
352 echo $hook->getDescription();
355 echo $HTML->listTableBottom();
357 if (count($hooksPostCommit)) {
358 echo '<h3>'._('post-commit Hooks').'</h3>';
359 $tabletop = array('', _('Hook Name'), _('Description'));
360 $classth = array('unsortable', '', '');
361 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_postcommit', 'sortable', $classth);
362 foreach ($hooksPostCommit as $hookPostCommit) {
364 if (! empty($hookPostCommit->onlyGlobalAdmin) && ! Permission::isGlobalAdmin()) {
365 echo '<tr style="display: none;" ><td>';
370 echo '<input type="checkbox" ';
371 echo 'name="'.$hookPostCommit->getLabel().'_'.$hookPostCommit->getClassname().'" ';
372 if (in_array($hookPostCommit->getClassname(), $hooksEnabled))
373 echo ' checked="checked"';
377 echo ' disabled="disabled"';
380 if (!$isdisabled && !$hookPostCommit->isAvailable())
381 echo ' disabled="disabled"';
385 if (!$hookPostCommit->isAvailable())
386 echo ' class="tabtitle-w" title="'.$hookPostCommit->getDisabledMessage().'"';
389 echo $hookPostCommit->getName();
391 echo $hookPostCommit->getDescription();
394 echo $HTML->listTableBottom();
398 function displayScmHgHook($hooksAvailable, $statusDeploy, $hooksEnabled) {
400 $hooksServePushPullBundle = array();
401 foreach ($hooksAvailable as $hook) {
402 switch ($hook->getHookType()) {
403 case "serve-push-pull-bundle": {
404 $hooksServePushPullBundle[] = $hook;
408 //byebye hook.... we do not know you...
413 if (count($hooksServePushPullBundle)) {
414 echo '<h3>'._('serve-push-pull-bundle Hooks').'</h3>';
415 $tabletop = array('', _('Hook Name'), _('Description'));
416 $classth = array('unsortable', '', '');
417 echo $HTML->listTableTop($tabletop, false, 'sortable_scmhook_serve-push-pull-bundle', 'sortable', $classth);
418 foreach ($hooksServePushPullBundle as $hookServePushPullBundle) {
420 if (! empty($hookServePushPullBundle->onlyGlobalAdmin) && ! Permission::isGlobalAdmin()) {
421 echo '<tr style="display: none;" ><td>';
426 echo '<input type="checkbox" ';
427 echo 'name="'.$hookServePushPullBundle->getLabel().'_'.$hookServePushPullBundle->getClassname().'" ';
428 if (in_array($hookServePushPullBundle->getClassname(), $hooksEnabled))
429 echo ' checked="checked"';
433 echo ' disabled="disabled"';
436 if (!$isdisabled && !$hookServePushPullBundle->isAvailable())
437 echo ' disabled="disabled"';
441 if (!$hookServePushPullBundle->isAvailable())
442 echo ' class="tabtitle-w" title="'.$hookServePushPullBundle->getDisabledMessage().'"';
445 echo $hookServePushPullBundle->getName();
447 echo $hookServePushPullBundle->getDescription();
450 echo $HTML->listTableBottom();
457 // c-file-style: "bsd"