3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
4 * Copyright (c) 2010 Alcatel-Lucent
5 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
6 * Copyright 2013, Franck Villaume - TrivialDev
8 * This file is a part of Fusionforge.
10 * Fusionforge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * Fusionforge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
24 require_once 'common/mvc/Views.class.php';
25 /*require_once('common/include/HTTPRequest.class.php');
26 require_once 'common/user/UserManager.class.php';
27 require_once 'common/project/ProjectManager.class.php';*/
28 //require_once('common/reference/CrossReferenceFactory.class.php');
30 require_once 'HudsonJob.class.php';
31 require_once 'common/dao/CodendiDataAccess.class.php';
32 require_once 'PluginHudsonJobDao.class.php';
34 class hudsonViews extends Views {
36 function hudsonViews(&$controler, $view=null) {
37 $request =& HTTPRequest::instance();
38 $this->View($controler, $view, array('title'=>$this->_getTitle(),'group' => $request->get('group_id'), 'toptab' => 'hudson'));
44 // echo $this->_getHelp();
45 // echo '<h1>'.$this->_getTitle().'</h1>';
48 function _getTitle() {
49 // return _("Continuous Integration");
53 function _getHelp($section = '', $questionmark = false) {
54 if (trim($section) !== '' && $section{0} !== '#') {
55 $section = '#'.$section;
60 $help_label = _("Help");
62 return '<b><a href="javascript:help_window(\''.get_server_url().'help/guide/ContinuousIntegrationIntroduction.html'.$section.'\');">'.$help_label.'</a></b>';
70 function projectOverview() {
71 $request =& HTTPRequest::instance();
72 $group_id = $request->get('group_id');
73 $user = UserManager::instance()->getCurrentUser();
75 $this->_display_jobs_table($group_id);
76 if ($user->isMember($request->get('group_id'), 'A')) {
77 $this->_display_add_job_form($group_id);
79 $this->_display_iframe();
80 $this->_hide_iframe();
83 function job_details() {
84 $myfile=fopen('/tmp/jobdetails','a');
85 fwrite($myfile,"\n J'ai fait job detail");
86 $request =& HTTPRequest::instance();
87 $group_id = $request->get('group_id');
88 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
89 if ($request->exist('job_id')) {
90 $job_id = $request->get('job_id');
91 $dar = $job_dao->searchByJobID($job_id);
92 } elseif ($request->exist('job')) {
93 // used for references (job #MyJob or job #myproject:MyJob)
94 $job_name = $request->get('job');
95 $dar = $job_dao->searchByJobName($job_name, $group_id);
98 $row = $dar->current();
99 /*fwrite($myfile,"appel de Cross REf Factory avec :".$row['name']);
100 $crossref_fact= new CrossReferenceFactory($row['name'], 'hudson_job', $group_id);
101 $crossref_fact->fetchDatas();
102 if ($crossref_fact->getNbReferences() > 0) {
103 echo '<b> '._('References').'</b>';
104 $crossref_fact->DisplayCrossRefs();
106 $this->_display_iframe($row['job_url']);
107 fwrite($myfile,"display iframe de ".$row['job_url']);
109 echo '<span class="error">'._("Error: Hudson object not found.").'</span>';
113 function last_build() {
114 $request =& HTTPRequest::instance();
115 $group_id = $request->get('group_id');
116 $job_id = $request->get('job_id');
118 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
119 $dar = $job_dao->searchByJobID($job_id);
121 $row = $dar->current();
122 $this->_display_iframe($row['job_url'].'/lastBuild/');
124 echo '<span class="error">'._("Error: Hudson object not found.").'</span>';
128 function build_number() {
129 $request =& HTTPRequest::instance();
130 $group_id = $request->get('group_id');
131 if ($request->exist('build')) {
132 $build_id = $request->get('build');
134 $build_id = $request->get('build_id');
136 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
137 if ($request->exist('job_id')) {
138 $job_id = $request->get('job_id');
139 $dar = $job_dao->searchByJobID($job_id);
140 } elseif ($request->exist('job')) {
141 // used for references (build #MyJob/175 or job #myproject:MyJob/175 where 175 is the build number required)
142 $job_name = $request->get('job');
143 $dar = $job_dao->searchByJobName($job_name, $group_id);
145 // used for references (build #175 where 175 is the build number required)
146 // If no job or project is specified, we check if there is only one job associated to the current project and we assume it is this job.
147 $dar = $job_dao->searchByGroupID($group_id);
148 if ($dar->rowCount() != 1) {
153 if ($dar && $dar->valid()) {
154 $row = $dar->current();
155 // $crossref_fact= new CrossReferenceFactory($row['name'].'/'.$build_id, 'hudson_build', $group_id);
156 // $crossref_fact->fetchDatas();
157 // if ($crossref_fact->getNbReferences() > 0) {
158 // echo '<b> '._('References').'</b>';
159 // $crossref_fact->DisplayCrossRefs();
161 $this->_display_iframe($row['job_url'].'/'.$build_id.'/');
163 echo '<span class="error">'._("Error: Hudson object not found.").'</span>';
167 function last_test_result() {
168 $request =& HTTPRequest::instance();
169 $group_id = $request->get('group_id');
170 $job_id = $request->get('job_id');
171 $user = UserManager::instance()->getCurrentUser();
173 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
174 $dar = $job_dao->searchByJobID($job_id);
176 $row = $dar->current();
177 $this->_display_iframe($row['job_url'].'/lastBuild/testReport/');
179 echo '<span class="error">'._("Error: Hudson object not found.").'</span>';
183 function test_trend() {
184 $request =& HTTPRequest::instance();
185 $group_id = $request->get('group_id');
186 $job_id = $request->get('job_id');
187 $user = UserManager::instance()->getCurrentUser();
189 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
190 $dar = $job_dao->searchByJobID($job_id);
192 $row = $dar->current();
193 $this->_display_iframe($row['job_url'].'/test/?width=800&height=600&failureOnly=false');
195 echo '<span class="error">'._("Error: Hudson object not found.").'</span>';
200 $request =& HTTPRequest::instance();
201 $group_id = $request->get('group_id');
202 $job_id = $request->get('job_id');
203 $user = UserManager::instance()->getCurrentUser();
204 if ($user->isMember($group_id, 'A')) {
206 $project_manager = ProjectManager::instance();
207 $project = $project_manager->getProject($group_id);
209 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
210 $dar = $job_dao->searchByJobID($job_id);
212 $row = $dar->current();
214 echo '<a href="/plugins/hudson/?group_id='.$group_id.'">'._("Back to jobs list").'</a>';
216 echo '<h3>'._("Edit Job").'</h3>';
217 echo ' <form method="post">';
219 echo ' <label for="new_hudson_job_url">'._("Job URL:").'</label>';
220 echo ' <input id="new_hudson_job_url" name="new_hudson_job_url" type="text" value="'.$row['job_url'].'" size="64" />';
223 echo ' <span class="legend">'._("eg: http://myCIserver/hudson/job/myJob").'</span>';
226 echo ' <label for="new_hudson_job_name">'._("Job name:").'</label>';
227 echo ' <input id="new_hudson_job_name" name="new_hudson_job_name" type="text" value="'.$row['name'].'" size="32" />';
230 echo ' <span class="legend">'.vsprintf(_("Name (with no space) used to make a reference to this job. Eg: job #%s"), array($row['name'])).'</span>';
232 if ($project->usesService('svn')) {
234 echo ' <label for="new_hudson_use_svn_trigger">';
235 echo sprintf(_("Trigger a build after %s commits:"), 'Subversion');
237 if ($row['use_svn_trigger'] == 1) {
238 $checked = ' checked="checked" ';
242 echo ' <input id="new_hudson_use_svn_trigger" name="new_hudson_use_svn_trigger" type="checkbox" '.$checked.' />';
245 if ($project->usesService('cvs')) {
247 echo ' <label for="new_hudson_use_cvs_trigger">';
248 echo sprintf(_("Trigger a build after %s commits:"), 'CVS');
250 if ($row['use_cvs_trigger'] == 1) {
251 $checked = ' checked="checked" ';
255 echo ' <input id="new_hudson_use_cvs_trigger" name="new_hudson_use_cvs_trigger" type="checkbox" '.$checked.' />';
258 if ($project->usesService('svn') || $project->usesService('cvs')) {
260 echo ' <label for="new_hudson_trigger_token">'._("with (optional) token:").'</label>';
261 echo ' <input id="new_hudson_trigger_token" name="new_hudson_trigger_token" type="text" value="'.$row['token'].'" size="32" />';
265 echo ' <input type="hidden" name="group_id" value="'.$group_id.'" />';
266 echo ' <input type="hidden" name="job_id" value="'.$job_id.'" />';
267 echo ' <input type="hidden" name="action" value="update_job" />';
268 echo ' <input type="submit" value="'._("Update job").'" />';
281 function _display_jobs_table($group_id) {
282 $request =& HTTPRequest::instance();
283 $group_id = $request->get('group_id');
284 $user = UserManager::instance()->getCurrentUser();
285 $job_dao = new PluginHudsonJobDao(CodendiDataAccess::instance());
286 $dar = $job_dao->searchByGroupID($group_id);
288 if ($dar && $dar->valid()) {
290 $project_manager = ProjectManager::instance();
291 $project = $project_manager->getProject($group_id);
293 echo '<table id="jobs_table">';
294 echo ' <tr class="boxtable">';
295 echo ' <th class="boxtitle"> </th>';
296 echo ' <th class="boxtitle">'._("Job").'</th>';
297 echo ' <th class="boxtitle">'._("Last Success").'</th>';
298 echo ' <th class="boxtitle">'._("Last Failure").'</th>';
299 echo ' <th class="boxtitle">'._("RSS").'</th>';
300 if ($project->usesService('svn')) {
301 echo ' <th class="boxtitle">'._("SVN trigger").'</th>';
303 if ($project->usesService('cvs')) {
304 echo ' <th class="boxtitle">'._("CVS trigger").'</th>';
306 if ($user->isMember($request->get('group_id'), 'A')) {
307 echo ' <th class="boxtitle">'._("Actions").'</th>';
312 while ($dar->valid()) {
313 $row = $dar->current();
315 $class="boxitemalt bgcolor-white";
317 $class="boxitem bgcolor-grey";
319 echo ' <tr class="'. $class .'">';
322 $job = new HudsonJob($row['job_url']);
324 echo ' <td><img src="'.$job->getStatusIcon().'" alt="'.$job->getStatus().'" title="'.$job->getStatus().'" /></td>';
325 // function toggle_iframe is in script plugins/hudson/www/scripts/hudson_tab.js
326 echo ' <td class="boxitem"><a href="'.$job->getUrl().'" onclick="toggle_iframe(this); return false;" title="'.vsprintf(_("Show job %s"), array($row['name'])).'">'.$row['name'].'</a></td>';
327 if ($job->getLastSuccessfulBuildNumber() != '') {
328 echo ' <td><a href="'.$job->getLastSuccessfulBuildUrl().'" onclick="toggle_iframe(this); return false;" title="'.vsprintf(_("Show build #%s of job %s"), array($job->getLastSuccessfulBuildNumber(), $row['name'])).'">'._("build").' #'.$job->getLastSuccessfulBuildNumber().'</a></td>';
330 echo ' <td> </td>';
332 if ($job->getLastFailedBuildNumber() != '') {
333 echo ' <td><a href="'.$job->getLastFailedBuildUrl().'" onclick="toggle_iframe(this); return false;" title="'.vsprintf(_("Show build #%s of job %s"), array($job->getLastFailedBuildNumber(), $row['name'])).'">'._("build").' #'.$job->getLastFailedBuildNumber().'</a></td>';
335 echo ' <td> </td>';
337 echo ' <td align="center"><a href="'.$job->getUrl().'/rssAll" onclick="toggle_iframe(this); return false;"><img src="'.$this->getControler()->getIconsPath().'rss_feed.png" alt="'.vsprintf(_("RSS feed of all builds for %s job"), array($row['name'])).'" title="'.vsprintf(_("RSS feed of all builds for %s job"), array($row['name'])).'" /></a></td>'."\n";
339 if ($project->usesService('svn')) {
340 if ($row['use_svn_trigger'] == 1) {
341 echo ' <td align="center"><img src="'.$this->getControler()->getIconsPath().'server_lightning.png" alt="'._("SVN commit will trigger a build").'" title="'._("SVN commit will trigger a build").'" /></td>';
343 echo ' <td> </td>';
346 if ($project->usesService('cvs')) {
347 if ($row['use_cvs_trigger'] == 1) {
348 echo ' <td align="center"><img src="'.$this->getControler()->getIconsPath().'server_lightning.png" alt="'._("CVS commit will trigger a build").'" title="'._("CVS commit will trigger a build").'" /></td>';
350 echo ' <td> </td>';
354 } catch (Exception $e) {
355 echo ' <td><img src="'.$this->getControler()->getIconsPath().'link_error.png" alt="'.$e->getMessage().'" title="'.$e->getMessage().'" /></td>';
357 if ($project->usesService('svn')) { $nb_columns++; }
358 if ($project->usesService('cvs')) { $nb_columns++; }
359 echo ' <td colspan="'.$nb_columns.'"><span class="error">'.$e->getMessage().'</span></td>';
362 if ($user->isMember($request->get('group_id'), 'A')) {
365 echo ' <span class="job_action">';
366 echo ' <a href="?action=edit_job&group_id='.$group_id.'&job_id='.$row['job_id'].'"><img src="'.$this->getControler()->getIconsPath().'edit.png" alt="'._("Edit this job").'" title="'._("Edit this job").'" />';
370 echo ' <span class="job_action">';
371 echo ' <a href="?action=delete_job&group_id='.$group_id.'&job_id='.$row['job_id'].'" onclick="return confirm(';
372 echo "'" . vsprintf(_("Are you sure you want to delete Job %s from project %s?"), array($row['name'], $project->getUnixName())) . "'";
373 echo ');"><img src="'.$this->getControler()->getIconsPath().'cross.png" alt="'._("Delete this job").'" title="'._("Delete this job").'" />';
386 echo '<p>'._('No Hudson jobs associated with this project.');
387 if ($user->isMember($request->get('group_id'), 'A')) {
388 echo _('To add a job, select the link just below.');
394 function _display_add_job_form($group_id) {
395 $project_manager = ProjectManager::instance();
396 $project = $project_manager->getProject($group_id);
398 echo '<a href="#" onclick="jQuery(\'#hudson_add_job\').slideToggle(); return false;">' . getIcon("ic/add.png") . ' '._("Add job").'</a>';
399 echo ' '.$this->_getHelp('HudsonService', true);
400 echo '<div id="hudson_add_job" style="display:none" >';
401 echo ' <form action="">'."\n";
402 echo ' <label for="hudson_job_url">'._("Job URL:").'</label>'."\n";
403 echo ' <input id="hudson_job_url" name="hudson_job_url" type="url" size="64" placeholder="http://myCIserver/hudson/job/myJob" required="required" pattern="https?://.+" title="http[s]://myCIserver/hudson/job/myjob" />'."\n";
404 echo ' <input type="hidden" name="group_id" value="'.$group_id.'" />'."\n";
405 echo ' <input type="hidden" name="action" value="add_job" />'."\n";
407 echo ' <span class="legend">'._("eg: http://myCIserver/hudson/job/myJob").'</span>'."\n";
410 if ($project->usesService('svn')) {
411 echo sprintf(_("Trigger a build after %s commits:"), '');
412 if ($project->usesService('svn')) {
413 echo ' <label for="hudson_use_svn_trigger">Subversion</label>';
414 echo ' <input id="hudson_use_svn_trigger" name="hudson_use_svn_trigger" type="checkbox" />';
416 if ($project->usesService('cvs')) {
417 echo ' <label for="hudson_use_cvs_trigger">CVS</label>';
418 echo ' <input id="hudson_use_cvs_trigger" name="hudson_use_cvs_trigger" type="checkbox" />';
422 echo ' <label for="hudson_trigger_token">'._("with (optional) token:").'</label>';
423 echo ' <input id="hudson_trigger_token" name="hudson_trigger_token" type="text" size="32" />';
427 echo ' <input type="submit" value="Add job" />';
430 //echo "<script type=\"text/javascript\">jQuery('#hudson_add_job').slideToggle();</script>\n";
433 function _display_iframe($url = '') {
434 echo '<div id="hudson_iframe_div">';
435 htmlIframe($url, array('id' => 'hudson_iframe', 'class' => 'iframe_service'));
439 function _hide_iframe() {
440 echo "<script type=\"text/javascript\">jQuery('hudson_iframe_div').slideToggle();</script>";