5 * Copyright (c) Xerox Corporation, Codendi 2007-2008.
6 * @author Marc Nazarian <marc.nazarian@xrce.xerox.com>
7 * Copyright (C) 2010-2011 Alain Peyrat - Alcatel-Lucent
8 * Copyright 2013, Franck Villaume - TrivialDev
10 * This file is a part of Fusionforge.
12 * Fusionforge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Fusionforge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
26 require_once 'PluginHudsonJobDao.class.php';
28 class hudsonPlugin extends Plugin {
30 function hudsonPlugin($id=0) {
32 $this->name = "hudson";
33 $this->text = _('Hudson/Jenkins'); // To show in the tabs, use...
34 $this->_addHook("user_personal_links"); //to make a link to the user's personal part of the plugin
35 $this->_addHook("usermenu") ;
36 $this->_addHook("groupmenu"); // To put into the project tabs
37 $this->_addHook("groupisactivecheckbox") ; // The "use ..." checkbox in editgroupinfo
38 $this->_addHook("groupisactivecheckboxpost") ; //
39 $this->_addHook("userisactivecheckbox") ; // The "use ..." checkbox in user account
40 $this->_addHook("userisactivecheckboxpost") ; //
41 $this->_addHook("project_admin_plugins"); // to show up in the admin page fro group
42 $this->_addHook('javascript', false);
43 $this->_addHook('cssfile', 'cssFile', false);
44 $this->_addHook('project_is_deleted', 'projectIsDeleted', false);
45 $this->_addHook('widget_instance', 'myPageBox', false);
46 $this->_addHook('widgets', 'widgets', false);
47 $this->_addHook('get_available_reference_natures', 'getAvailableReferenceNatures', false);
48 $this->_addHook('ajax_reference_tooltip', 'ajax_reference_tooltip', false);
49 $this->_addHook('role_get');
50 $this->_addHook('role_normalize');
51 $this->_addHook('role_translate_strings');
52 $this->_addHook('role_has_permission');
53 $this->_addHook('role_get_setting');
54 $this->_addHook('list_roles_by_permission');
57 function CallHook ($hookname, &$params) {
58 global $G_SESSION,$HTML;
59 if ($hookname == "usermenu") {
60 $text = $this->text; // this is what shows in the tab
61 if ($G_SESSION->usesPlugin("hudson")) {
62 $param = '?type=user&id=' . $G_SESSION->getId() . "&pluginname=" . $this->name; // we indicate the part we're calling is the user one
63 echo ' | ' . $HTML->PrintSubMenu (array ($text),
64 array ('/plugins/hudson/index.php' . $param ));
66 } elseif ($hookname == "groupmenu") {
67 $group_id=$params['group'];
68 $project = group_get_object($group_id);
69 if (!$project || !is_object($project)) {
72 if ($project->isError()) {
75 if (!$project->isProject()) {
78 if ( $project->usesPlugin( $this->name )) {
79 $params['TITLES'][] = $this->text;
80 $params['DIRS'][] = '/plugins/hudson/index.php?group_id=' . $group_id . "&pluginname=" . $this->name; // we indicate the part we're calling is the project one
81 $params['ADMIN'][] = '';
82 $params['TOOLTIPS'][] = _('Continuus Integration Scheduler');
84 if (isset($params['toptab'])) {
85 (($params['toptab'] == $this->name) ? $params['selected']=(count($params['TITLES'])-1) : '' );
87 } elseif ($hookname =='cssfile') {
88 use_stylesheet('/plugins/hudson/themes/default/css/style.css');
89 } elseif ($hookname == "cssfile") {
90 $this->cssFile($params);
91 } elseif ($hookname == "project_is_deleted") {
92 $this->projectIsDeleted($params);
93 } elseif ($hookname == "widget_instance") {
94 $this->myPageBox($params);
95 } elseif ($hookname == "widgets") {
96 $this->widgets($params);
97 } elseif ($hookname == "get_available_reference_natures") {
98 $this->getAvailableReferenceNatures($params);
99 } elseif ($hookname == "ajax_reference_tooltip") {
100 $this->ajax_reference_tooltip($params);
101 } elseif ($hookname == 'role_get') {
102 $this->role_get($params);
103 } elseif ($hookname == 'role_normalize') {
104 $this->role_normalize($params);
105 } elseif ($hookname == 'role_translate_strings') {
106 $this->role_translate_strings($params);
107 } elseif ($hookname == 'role_has_permission') {
108 $this->role_has_permission($params);
109 } elseif ($hookname == 'role_get_setting') {
110 $this->role_get_setting($params);
111 } elseif ($hookname == 'list_roles_by_permission') {
112 $this->list_roles_by_permission($params);
116 function &getPluginInfo() {
117 if (!is_a($this->pluginInfo, 'hudsonPluginInfo')) {
118 require_once 'hudsonPluginInfo.class.php';
119 $this->pluginInfo = new hudsonPluginInfo($this);
121 return $this->pluginInfo;
124 function cssFile($params) {
125 // Only show the stylesheet if we're actually in the hudson pages.
126 // This stops styles inadvertently clashing with the main site.
127 if (strpos($_SERVER['REQUEST_URI'], $this->getPluginPath()) === 0 ||
128 strpos($_SERVER['REQUEST_URI'], '/my/') === 0 ||
129 strpos($_SERVER['REQUEST_URI'], '/projects/') === 0 ||
130 strpos($_SERVER['REQUEST_URI'], '/widgets/') === 0
132 use_stylesheet($this->getThemePath().'/css/style.css');
137 * When a project is deleted,
138 * we delete all the hudson jobs of this project
140 * @param mixed $params ($param['group_id'] the ID of the deleted project)
142 function projectIsDeleted($params) {
143 $group_id = $params['group_id'];
144 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
145 $dar = $job_dao->deleteHudsonJobsByGroupID($group_id);
148 function myPageBox($params) {
149 require_once 'common/widget/WidgetLayoutManager.class.php';
151 $user = UserManager::instance()->getCurrentUser();
154 if ($params['widget'] == 'plugin_hudson_my_jobs') {
155 require_once 'hudson_Widget_MyMonitoredJobs.class.php';
156 $params['instance'] = new hudson_Widget_MyMonitoredJobs($this);
158 if ($params['widget'] == 'plugin_hudson_my_joblastbuilds') {
159 require_once 'hudson_Widget_JobLastBuilds.class.php';
160 $params['instance'] = new hudson_Widget_JobLastBuilds(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
162 if ($params['widget'] == 'plugin_hudson_my_jobtestresults') {
163 require_once 'hudson_Widget_JobTestResults.class.php';
164 $params['instance'] = new hudson_Widget_JobTestResults(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
166 if ($params['widget'] == 'plugin_hudson_my_jobtesttrend') {
167 require_once 'hudson_Widget_JobTestTrend.class.php';
168 $params['instance'] = new hudson_Widget_JobTestTrend(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
170 if ($params['widget'] == 'plugin_hudson_my_jobbuildhistory') {
171 require_once 'hudson_Widget_JobBuildHistory.class.php';
172 $params['instance'] = new hudson_Widget_JobBuildHistory(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
174 if ($params['widget'] == 'plugin_hudson_my_joblastartifacts') {
175 require_once 'hudson_Widget_JobLastArtifacts.class.php';
176 $params['instance'] = new hudson_Widget_JobLastArtifacts(WidgetLayoutManager::OWNER_TYPE_USER, $user->getId());
180 if ($params['widget'] == 'plugin_hudson_project_jobsoverview') {
181 require_once 'hudson_Widget_ProjectJobsOverview.class.php';
182 $params['instance'] = new hudson_Widget_ProjectJobsOverview($this);
184 if ($params['widget'] == 'plugin_hudson_project_joblastbuilds') {
185 require_once 'hudson_Widget_JobLastBuilds.class.php';
186 $params['instance'] = new hudson_Widget_JobLastBuilds(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
188 if ($params['widget'] == 'plugin_hudson_project_jobtestresults') {
189 require_once 'hudson_Widget_JobTestResults.class.php';
190 $params['instance'] = new hudson_Widget_JobTestResults(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
192 if ($params['widget'] == 'plugin_hudson_project_jobtesttrend') {
193 require_once 'hudson_Widget_JobTestTrend.class.php';
194 $params['instance'] = new hudson_Widget_JobTestTrend(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
196 if ($params['widget'] == 'plugin_hudson_project_jobbuildhistory') {
197 require_once 'hudson_Widget_JobBuildHistory.class.php';
198 $params['instance'] = new hudson_Widget_JobBuildHistory(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
200 if ($params['widget'] == 'plugin_hudson_project_joblastartifacts') {
201 require_once 'hudson_Widget_JobLastArtifacts.class.php';
202 $params['instance'] = new hudson_Widget_JobLastArtifacts(WidgetLayoutManager::OWNER_TYPE_GROUP, $GLOBALS['group_id']);
206 function widgets($params) {
207 $group = group_get_object($GLOBALS['group_id']);
208 if ( !$group || !$group->usesPlugin ( $this->name ) ) {
212 require_once 'common/widget/WidgetLayoutManager.class.php';
213 if ($params['owner_type'] == WidgetLayoutManager::OWNER_TYPE_USER) {
214 $params['codendi_widgets'][] = 'plugin_hudson_my_jobs';
215 $params['codendi_widgets'][] = 'plugin_hudson_my_joblastbuilds';
216 $params['codendi_widgets'][] = 'plugin_hudson_my_jobtestresults';
217 $params['codendi_widgets'][] = 'plugin_hudson_my_jobtesttrend';
218 $params['codendi_widgets'][] = 'plugin_hudson_my_jobbuildhistory';
219 $params['codendi_widgets'][] = 'plugin_hudson_my_joblastartifacts';
221 if ($params['owner_type'] == WidgetLayoutManager::OWNER_TYPE_GROUP) {
222 $params['codendi_widgets'][] = 'plugin_hudson_project_jobsoverview';
223 $params['codendi_widgets'][] = 'plugin_hudson_project_joblastbuilds';
224 $params['codendi_widgets'][] = 'plugin_hudson_project_jobtestresults';
225 $params['codendi_widgets'][] = 'plugin_hudson_project_jobtesttrend';
226 $params['codendi_widgets'][] = 'plugin_hudson_project_jobbuildhistory';
227 $params['codendi_widgets'][] = 'plugin_hudson_project_joblastartifacts';
231 function getAvailableReferenceNatures($params) {
232 $hudson_plugin_reference_natures = array(
233 'hudson_build' => array('keyword' => 'build', 'label' => _("Hudson Build")),
234 'hudson_job' => array('keyword' => 'job', 'label' => _("Hudson Job")));
235 $params['natures'] = array_merge($params['natures'], $hudson_plugin_reference_natures);
238 function ajax_reference_tooltip($params) {
239 require_once 'HudsonJob.class.php';
240 require_once 'HudsonBuild.class.php';
241 require_once 'hudson_Widget_JobLastBuilds.class.php';
243 $ref = $params['reference'];
244 switch ($ref->getNature()) {
246 $val = $params['val'];
247 $group_id = $params['group_id'];
248 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
249 if (strpos($val, "/") !== false) {
250 $arr = explode("/", $val);
253 $dar = $job_dao->searchByJobName($job_name, $group_id);
256 $dar = $job_dao->searchByGroupID($group_id);
257 if ($dar->rowCount() != 1) {
261 if ($dar && $dar->valid()) {
262 $row = $dar->current();
263 $build = new HudsonBuild($row['job_url'].'/'.$build_id.'/');
264 echo '<strong>' . _("Build performed on:") . '</strong> ' . $build->getBuildTime() . '<br />';
265 echo '<strong>' . _("Status") . _(": ") . '</strong> ' . $build->getResult();
267 echo '<span class="error">'._("Error: Hudson object not found.").'</span>';
271 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
272 $job_name = $params['val'];
273 $group_id = $params['group_id'];
274 $dar = $job_dao->searchByJobName($job_name, $group_id);
276 $row = $dar->current();
278 $job = new HudsonJob($row['job_url']);
279 $job_id = $row['job_id'];
283 $html .= ' <td colspan="2">';
284 $html .= ' '.$job->getName().': <img src="'.$job->getStatusIcon().'" />';
290 if ($job->hasBuilds()) {
291 $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>';
292 $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>';
293 $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>';
295 $html .= ' <li>'. _("No build found for this job.") . '</li>';
299 $html .= ' <td class="widget_lastbuilds_weather">';
300 $html .= _("Weather Report:").'<img src="'.$job->getWeatherReportIcon().'" align="middle" />';
305 } catch (Exception $e) {
308 echo '<span class="error">'._("Error: Hudson object not found.").'</span>';
315 require_once 'hudson.class.php';
316 $controler = new hudson();
317 $controler->process();
320 function role_get(&$params) {
321 $role =& $params['role'];
324 $right = new PluginSpecificRoleSetting($role, 'plugin_hudson_read');
325 $right->SetAllowedValues(array('0', '1'));
326 $right->SetDefaultValues(array('Admin' => '1',
327 'Senior Developer' => '1',
328 'Junior Developer' => '1',
330 'Support Tech' => '1'));
334 function role_normalize(&$params) {
335 $role =& $params['role'];
336 $new_sa =& $params['new_sa'];
337 $new_pa =& $params['new_pa'];
339 $projects = $role->getLinkedProjects();
340 foreach ($projects as $p) {
341 $role->normalizePermsForSection($new_pa, 'plugin_hudson_read', $p->getID());
346 function role_translate_strings(&$params) {
347 $right = new PluginSpecificRoleSetting($role, 'plugin_hudson_read');
348 $right->setDescription(_('Hudson access'));
349 $right->setValueDescriptions(array('0' => _('No access'),
350 '1' => _('Full access')));
354 function role_has_permission(&$params) {
355 $value = $params['value'];
357 switch ($params['section']) {
358 case 'plugin_hudson_read':
360 switch ($params['action']) {
363 $params['result'] |= ($value >= 1);
371 function role_get_setting(&$params) {
372 $role = $params['role'];
373 $reference = $params['reference'];
374 $value = $params['value'];
376 switch ($params['section']) {
377 case 'plugin_hudson_read':
379 if ($role->hasPermission('project_admin', $reference)) {
380 $params['result'] = 1;
382 $params['result'] = $value;
389 function list_roles_by_permission(&$params) {
390 switch ($params['section']) {
391 case 'plugin_hudson_read':
393 switch ($params['action']) {
396 $params['qpa'] = db_construct_qpa($params['qpa'], ' AND perm_val >= 1') ;