File: hudsonPlugin.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (419 lines) | stat: -rw-r--r-- 16,108 bytes parent folder | download | duplicates (3)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/**
 * hudsonPlugin
 *
 * Copyright (c) Xerox Corporation, Codendi 2007-2008.
 * @author Marc Nazarian <marc.nazarian@xrce.xerox.com>
 * Copyright (C) 2010-2011 Alain Peyrat - Alcatel-Lucent
 * Copyright 2013-2014, Franck Villaume - TrivialDev
 *
 * This file is a part of Fusionforge.
 *
 * Fusionforge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Fusionforge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
 */

require_once 'PluginHudsonJobDao.class.php';

class hudsonPlugin extends Plugin {

	function hudsonPlugin($id=0) {
		$this->Plugin($id);
		$this->name = "hudson";
		$this->text = _('Hudson/Jenkins'); // To show in the tabs, use...
		$this->_addHook("user_personal_links"); //to make a link to the user's personal part of the plugin
		//$this->_addHook("usermenu") ;
		$this->_addHook("groupmenu");	// To put into the project tabs
		$this->_addHook("groupisactivecheckbox") ; // The "use ..." checkbox in editgroupinfo
		$this->_addHook("groupisactivecheckboxpost") ; //
		//$this->_addHook("userisactivecheckbox") ; // The "use ..." checkbox in user account
		//$this->_addHook("userisactivecheckboxpost") ; //
		$this->_addHook("project_admin_plugins"); // to show up in the admin page fro group
		$this->_addHook('javascript',  false);
		$this->_addHook('cssfile', 'cssFile', false);
		$this->_addHook('group_delete', 'projectIsDeleted', false);
		$this->_addHook('widget_instance', 'myPageBox', false);
		$this->_addHook('widgets', 'widgets', false);
		$this->_addHook('get_available_reference_natures', 'getAvailableReferenceNatures', false);
		$this->_addHook('ajax_reference_tooltip', 'ajax_reference_tooltip', false);
		$this->_addHook('role_get');
		$this->_addHook('role_normalize');
		$this->_addHook('role_unlink_project');
		$this->_addHook('role_translate_strings');
		$this->_addHook('role_has_permission');
		$this->_addHook('role_get_setting');
		$this->_addHook('list_roles_by_permission');
	}

	function CallHook ($hookname, &$params) {
		global $G_SESSION,$HTML;
// 		if ($hookname == "usermenu") {
// 			$text = $this->text; // this is what shows in the tab
// 			if ($G_SESSION->usesPlugin("hudson")) {
// 				$param = '?type=user&amp;id=' . $G_SESSION->getId()."&amp;pluginname=".$this->name; // we indicate the part we're calling is the user one
// 				echo $HTML->PrintSubMenu(array($text), array('/plugins/hudson/index.php'.$param));
// 			}
// 		} elseif ($hookname == "groupmenu") {
		if ($hookname == "groupmenu") {
			$group_id=$params['group'];
			$project = group_get_object($group_id);
			if (!$project || !is_object($project)) {
				return;
			}
			if ($project->isError()) {
				return;
			}
			if (!$project->isProject()) {
				return;
			}
			if ( $project->usesPlugin( $this->name )) {
				$params['TITLES'][] = $this->text;
				$params['DIRS'][] = '/plugins/hudson/index.php?group_id=' . $group_id . "&amp;pluginname=" . $this->name; // we indicate the part we're calling is the project one
				$params['ADMIN'][] = '';
				$params['TOOLTIPS'][] = _('Continuus Integration Scheduler');
			}
			if (isset($params['toptab'])) {
				(($params['toptab'] == $this->name) ? $params['selected']=(count($params['TITLES'])-1) : '' );
			}
		} elseif ($hookname =='cssfile') {
			use_stylesheet('/plugins/hudson/themes/default/css/style.css');
		} elseif ($hookname == "cssfile") {
			$this->cssFile($params);
		} elseif ($hookname == 'group_delete') {
			$this->projectIsDeleted($params);
		} elseif ($hookname == "widget_instance") {
			$this->myPageBox($params);
		} elseif ($hookname == "widgets") {
			$this->widgets($params);
		} elseif ($hookname == "get_available_reference_natures") {
			$this->getAvailableReferenceNatures($params);
		} elseif ($hookname == "ajax_reference_tooltip") {
			$this->ajax_reference_tooltip($params);
		} elseif ($hookname == 'role_get') {
			$this->role_get($params);
		} elseif ($hookname == 'role_normalize') {
			$this->role_normalize($params);
		} elseif ($hookname == 'role_translate_strings') {
			$this->role_translate_strings($params);
		} elseif ($hookname == 'role_has_permission') {
			$this->role_has_permission($params);
		} elseif ($hookname == 'role_get_setting') {
			$this->role_get_setting($params);
		} elseif ($hookname == 'list_roles_by_permission') {
			$this->list_roles_by_permission($params);
		}
	}

	function &getPluginInfo() {
		if (!is_a($this->pluginInfo, 'hudsonPluginInfo')) {
			require_once 'hudsonPluginInfo.class.php';
			$this->pluginInfo = new hudsonPluginInfo($this);
		}
		return $this->pluginInfo;
	}

	function cssFile($params) {
		// Only show the stylesheet if we're actually in the hudson pages.
		// This stops styles inadvertently clashing with the main site.
		if (strpos($_SERVER['REQUEST_URI'], $this->getPluginPath()) === 0 ||
				strpos($_SERVER['REQUEST_URI'], '/my/') === 0 ||
				strpos($_SERVER['REQUEST_URI'], '/projects/') === 0 ||
				strpos($_SERVER['REQUEST_URI'], '/widgets/') === 0
		   ) {
			use_stylesheet($this->getThemePath().'/css/style.css');
		}
	}

	/**
	 * When a project is deleted,
	 * we delete all the hudson jobs of this project
	 *
	 * @param mixed $params ($param['group_id'] the ID of the deleted project)
	 */
	function projectIsDeleted($params) {
		global $gfcommon;
		require_once $gfcommon.'dao/CodendiDataAccess.class.php';
		$group_id = $params['group_id'];
		$job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
		$dar = $job_dao->deleteHudsonJobsByGroupID($group_id);
	}

	function myPageBox($params) {
		require_once 'common/widget/WidgetLayoutManager.class.php';

		$user = UserManager::instance()->getCurrentUser();

		// MY
		if ($params['widget'] == 'plugin_hudson_my_jobs') {
			require_once 'hudson_Widget_MyMonitoredJobs.class.php';
			$params['instance'] = new hudson_Widget_MyMonitoredJobs($this);
		}
		if ($params['widget'] == 'plugin_hudson_my_joblastbuilds') {
			require_once 'hudson_Widget_JobLastBuilds.class.php';
			$params['instance'] = new hudson_Widget_JobLastBuilds(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
		}
		if ($params['widget'] == 'plugin_hudson_my_jobtestresults') {
			require_once 'hudson_Widget_JobTestResults.class.php';
			$params['instance'] = new hudson_Widget_JobTestResults(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
		}
		if ($params['widget'] == 'plugin_hudson_my_jobtesttrend') {
			require_once 'hudson_Widget_JobTestTrend.class.php';
			$params['instance'] = new hudson_Widget_JobTestTrend(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
		}
		if ($params['widget'] == 'plugin_hudson_my_jobbuildhistory') {
			require_once 'hudson_Widget_JobBuildHistory.class.php';
			$params['instance'] = new hudson_Widget_JobBuildHistory(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
		}
		if ($params['widget'] == 'plugin_hudson_my_joblastartifacts') {
			require_once 'hudson_Widget_JobLastArtifacts.class.php';
			$params['instance'] = new hudson_Widget_JobLastArtifacts(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
		}

		// PROJECT
		if ($params['widget'] == 'plugin_hudson_project_jobsoverview') {
			require_once 'hudson_Widget_ProjectJobsOverview.class.php';
			$params['instance'] = new hudson_Widget_ProjectJobsOverview($this);
		}
		if ($params['widget'] == 'plugin_hudson_project_joblastbuilds') {
			require_once 'hudson_Widget_JobLastBuilds.class.php';
			$params['instance'] = new hudson_Widget_JobLastBuilds(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
		}
		if ($params['widget'] == 'plugin_hudson_project_jobtestresults') {
			require_once 'hudson_Widget_JobTestResults.class.php';
			$params['instance'] = new hudson_Widget_JobTestResults(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
		}
		if ($params['widget'] == 'plugin_hudson_project_jobtesttrend') {
			require_once 'hudson_Widget_JobTestTrend.class.php';
			$params['instance'] = new hudson_Widget_JobTestTrend(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
		}
		if ($params['widget'] == 'plugin_hudson_project_jobbuildhistory') {
			require_once 'hudson_Widget_JobBuildHistory.class.php';
			$params['instance'] = new hudson_Widget_JobBuildHistory(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
		}
		if ($params['widget'] == 'plugin_hudson_project_joblastartifacts') {
			require_once 'hudson_Widget_JobLastArtifacts.class.php';
			$params['instance'] = new hudson_Widget_JobLastArtifacts(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
		}
	}

	function widgets($params) {
		$group = group_get_object($GLOBALS['group_id']);
		if ( !$group || !$group->usesPlugin ( $this->name ) ) {
			return false;
		}

		require_once 'common/widget/WidgetLayoutManager.class.php';
		if ($params['owner_type'] == WidgetLayoutManager::OWNER_TYPE_USER) {
			$params['codendi_widgets'][] = 'plugin_hudson_my_jobs';
			$params['codendi_widgets'][] = 'plugin_hudson_my_joblastbuilds';
			$params['codendi_widgets'][] = 'plugin_hudson_my_jobtestresults';
			$params['codendi_widgets'][] = 'plugin_hudson_my_jobtesttrend';
			$params['codendi_widgets'][] = 'plugin_hudson_my_jobbuildhistory';
			$params['codendi_widgets'][] = 'plugin_hudson_my_joblastartifacts';
		}
		if ($params['owner_type'] == WidgetLayoutManager::OWNER_TYPE_GROUP) {
			$params['codendi_widgets'][] = 'plugin_hudson_project_jobsoverview';
			$params['codendi_widgets'][] = 'plugin_hudson_project_joblastbuilds';
			$params['codendi_widgets'][] = 'plugin_hudson_project_jobtestresults';
			$params['codendi_widgets'][] = 'plugin_hudson_project_jobtesttrend';
			$params['codendi_widgets'][] = 'plugin_hudson_project_jobbuildhistory';
			$params['codendi_widgets'][] = 'plugin_hudson_project_joblastartifacts';
		}
	}

	function getAvailableReferenceNatures($params) {
		$hudson_plugin_reference_natures = array(
				'hudson_build'  => array('keyword' => 'build', 'label' => _("Hudson Build")),
				'hudson_job' => array('keyword' => 'job', 'label' => _("Hudson Job")));
		$params['natures'] = array_merge($params['natures'], $hudson_plugin_reference_natures);
	}

	function ajax_reference_tooltip($params) {
		require_once 'HudsonJob.class.php';
		require_once 'HudsonBuild.class.php';
		require_once 'hudson_Widget_JobLastBuilds.class.php';

		$ref = $params['reference'];
		switch ($ref->getNature()) {
			case 'hudson_build':
				$val = $params['val'];
				$group_id = $params['group_id'];
				$job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
				if (strpos($val, "/") !== false) {
					$arr = explode("/", $val);
					$job_name = $arr[0];
					$build_id = $arr[1];
					$dar = $job_dao->searchByJobName($job_name, $group_id);
				} else {
					$build_id = $val;
					$dar = $job_dao->searchByGroupID($group_id);
					if ($dar->rowCount() != 1) {
						$dar = null;
					}
				}
				if ($dar && $dar->valid()) {
					$row = $dar->current();
					$build = new HudsonBuild($row['job_url'].'/'.$build_id.'/');
					echo '<strong>' . _("Build performed on:") . '</strong> ' . $build->getBuildTime() . '<br />';
					echo '<strong>' . _("Status") . _(": ") . '</strong> ' . $build->getResult();
				} else {
					echo '<span class="error">'._("Error: Hudson object not found.").'</span>';
				}
				break;
			case 'hudson_job':
				$job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
				$job_name = $params['val'];
				$group_id = $params['group_id'];
				$dar = $job_dao->searchByJobName($job_name, $group_id);
				if ($dar->valid()) {
					$row = $dar->current();
					try {
						$job = new HudsonJob($row['job_url']);
						$job_id = $row['job_id'];
						$html = '';
						$html .= '<table>';
						$html .= ' <tr>';
						$html .= '  <td colspan="2">';
						$html .= '   '.$job->getName().': <img src="'.$job->getStatusIcon().'" />';
						$html .= '  </td>';
						$html .= ' </tr>';
						$html .= ' <tr>';
						$html .= '  <td>';
						$html .= '   <ul>';
						if ($job->hasBuilds()) {
							$html .= ' <li>'._("Last Build:").' <a href="/plugins/hudson/?action=view_build&group_id='.$group_id.'&job_id='.$job_id.'&build_id='.$job->getLastBuildNumber().'"># '.$job->getLastBuildNumber().'</a></li>';
							$html .= ' <li>'._("Last Success")._(": ").'<a href="/plugins/hudson/?action=view_build&group_id='.$group_id.'&job_id='.$job_id.'&build_id='.$job->getLastSuccessfulBuildNumber().'"># '.$job->getLastSuccessfulBuildNumber().'</a></li>';
							$html .= ' <li>'._("Last Failure")._(": ").'<a href="/plugins/hudson/?action=view_build&group_id='.$group_id.'&job_id='.$job_id.'&build_id='.$job->getLastFailedBuildNumber().'"># '.$job->getLastFailedBuildNumber().'</a></li>';
						} else {
							$html .= ' <li>'. _("No build found for this job.") . '</li>';
						}
						$html .= '   </ul>';
						$html .= '  </td>';
						$html .= '  <td class="widget_lastbuilds_weather">';
						$html .= _("Weather Report:").'<img src="'.$job->getWeatherReportIcon().'" align="middle" />';
						$html .= '  </td>';
						$html .= ' </tr>';
						$html .= '</table>';
						echo $html;
					} catch (Exception $e) {
					}
				} else {
					echo '<span class="error">'._("Error: Hudson object not found.").'</span>';
				}
				break;
		}
	}

	function process() {
		require_once 'hudson.class.php';
		$controler = new hudson();
		$controler->process();
	}

	function role_get(&$params) {
		$role =& $params['role'];

		// Read access
		$right = new PluginSpecificRoleSetting($role, 'plugin_hudson_read');
		$right->SetAllowedValues(array('0', '1'));
		$right->SetDefaultValues(array('Admin' => '1',
						'Senior Developer' => '1',
						'Junior Developer' => '1',
						'Doc Writer' => '1',
						'Support Tech' => '1'));
		return true;
	}

	function role_normalize(&$params) {
		$role =& $params['role'];
		$new_pa =& $params['new_pa'];

		$projects = $role->getLinkedProjects();
		foreach ($projects as $p) {
			$role->normalizePermsForSection($new_pa, 'plugin_hudson_read', $p->getID());
		}
		return true;
	}

	function role_unlink_project(&$params) {
		$role =& $params['role'] ;
		$project =& $params['project'] ;

		$settings = array('plugin_hudson_read');

		foreach ($settings as $s) {
			db_query_params('DELETE FROM pfo_role_setting WHERE role_id=$1 AND section_name=$2 AND ref_id=$3',
					array($role->getID(),
					      $s,
					      $project->getID()));
		}
	}

	function role_translate_strings(&$params) {
		$right = new PluginSpecificRoleSetting($params['role'], 'plugin_hudson_read');
		$right->setDescription(_('Hudson access'));
		$right->setValueDescriptions(array('0' => _('No Access'),
							'1' => _('Full access')));
		return true;
	}

	function role_has_permission(&$params) {
		$value = $params['value'];

		switch ($params['section']) {
			case 'plugin_hudson_read':
			default:
				switch ($params['action']) {
					case 'read':
					default:
						$params['result'] |= ($value >= 1);
						break;
				}
				break;
		}
		return true;
	}

	function role_get_setting(&$params) {
		$role = $params['role'];
		$reference = $params['reference'];
		$value = $params['value'];

		switch ($params['section']) {
			case 'plugin_hudson_read':
			default:
				if ($role->hasPermission('project_admin', $reference)) {
					$params['result'] = 1;
				} else {
					$params['result'] = $value;
				}
				break;
		}
		return true;
	}

	function list_roles_by_permission(&$params) {
		switch ($params['section']) {
			case 'plugin_hudson_read':
			default:
				switch ($params['action']) {
					case 'read':
					default:
						$params['qpa'] = db_construct_qpa($params['qpa'], ' AND perm_val >= 1') ;
						break;
				}
				break;
		}
		return true;
	}
}