forked from Kitware/CDash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanageBuildGroup.php
514 lines (444 loc) · 17.2 KB
/
manageBuildGroup.php
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
/*=========================================================================
Program: CDash - Cross-Platform Dashboard System
Module: $Id$
Language: PHP
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
include("cdash/config.php");
require_once("cdash/pdo.php");
include_once("cdash/common.php");
include('login.php');
include('cdash/version.php');
include("models/project.php");
if ($session_OK)
{
@$db = pdo_connect("$CDASH_DB_HOST", "$CDASH_DB_LOGIN","$CDASH_DB_PASS");
pdo_select_db("$CDASH_DB_NAME",$db);
$userid = $_SESSION['cdash']['loginid'];
// Checks
if(!isset($userid) || !is_numeric($userid))
{
echo "Not a valid userid!";
return;
}
$xml = begin_XML_for_XSLT();
$xml .= "<backurl>user.php</backurl>";
$xml .= "<title>CDash - Build Groups</title>";
$xml .= "<menutitle>CDash</menutitle>";
$xml .= "<menusubtitle>Build Groups</menusubtitle>";
@$projectid = $_GET["projectid"];
if ($projectid != NULL)
{
$projectid = pdo_real_escape_numeric($projectid);
}
// If the projectid is not set and there is only one project we go directly to the page
if(!isset($projectid))
{
$project = pdo_query("SELECT id FROM project");
if(pdo_num_rows($project)==1)
{
$project_array = pdo_fetch_array($project);
$projectid = $project_array["id"];
}
}
@$submitAutoRemoveSettings = $_POST["submitAutoRemoveSettings"];
if($submitAutoRemoveSettings)
{
foreach($_POST as $key=>$value)
{
$value = pdo_real_escape_numeric($value);
if(substr($key, 0, 20) == 'autoremovetimeframe_' && is_numeric($value))
{
list(,$id) = explode('_',$key);
pdo_query("UPDATE buildgroup SET autoremovetimeframe='$value' WHERE id=".qnum($id));
}
}
}
@$show = $_GET["show"];
$role=0;
$user_array = pdo_fetch_array(pdo_query("SELECT admin FROM ".qid("user")." WHERE id='$userid'"));
if($projectid && is_numeric($projectid))
{
$user2project = pdo_query("SELECT role FROM user2project WHERE userid='$userid' AND projectid='$projectid'");
if(pdo_num_rows($user2project)>0)
{
$user2project_array = pdo_fetch_array($user2project);
$role = $user2project_array["role"];
}
}
if($user_array["admin"]!=1 && $role<=1)
{
echo "You don't have the permissions to access this page";
return;
}
$sql = "SELECT id,name FROM project";
if($user_array["admin"] != 1)
{
$sql .= " WHERE id IN (SELECT projectid AS id FROM user2project WHERE userid='$userid' AND role>0)";
}
$projects = pdo_query($sql);
while($project_array = pdo_fetch_array($projects))
{
$xml .= "<availableproject>";
$xml .= add_XML_value("id",$project_array['id']);
$xml .= add_XML_value("name",$project_array['name']);
if($project_array['id']==$projectid)
{
$xml .= add_XML_value("selected","1");
}
$xml .= "</availableproject>";
}
// check if we are changing the order of the build groups
if (isset($_POST['saveLayout']))
{
$inputRows = json_decode($_POST['saveLayout'], true);
if (count($inputRows) > 0)
{
// remove old build group layout for this project
pdo_query(
"DELETE bgp FROM buildgroupposition AS bgp
LEFT JOIN buildgroup AS bg ON (bgp.buildgroupid = bg.id)
WHERE bg.projectid = '$projectid'");
add_last_sql_error("manageBuildGroup::saveLayout::DELETE", $projectid);
// construct query to insert the new layout
$query = "INSERT INTO buildgroupposition (buildgroupid, position) VALUES ";
foreach ($inputRows as $inputRow)
{
$query .= "(" .
qnum(pdo_real_escape_numeric($inputRow["buildgroupid"])) . ", " .
qnum(pdo_real_escape_numeric($inputRow["position"])) . "), ";
}
// remove the trailing comma and space, then insert our new values
$query = rtrim($query, ", ");
pdo_query($query);
add_last_sql_error("manageOverview::saveLayout::INSERT", $projectid);
}
// since this is called by AJAX, we don't need to render the page below.
exit(0);
}
// If we should update the description
@$submitDescription = $_POST["submitDescription"];
if($submitDescription)
{
$Groupid = pdo_real_escape_numeric($_POST["groupid"]);
$Description = htmlspecialchars(pdo_real_escape_string($_POST["description"]));
$sql = "UPDATE buildgroup SET description='$Description' WHERE id='$Groupid'";
if(!pdo_query("$sql"))
{
echo pdo_error();
}
} // end submitDescription group
// If we should rename a group
@$Rename = $_POST["rename"];
if($Rename)
{
$Groupid = pdo_real_escape_numeric($_POST["groupid"]);
$Newname = htmlspecialchars(pdo_real_escape_string($_POST["newname"]));
$sql = "UPDATE buildgroup SET name='$Newname' WHERE id='$Groupid'";
if(!pdo_query("$sql"))
{
echo pdo_error();
}
} // end rename group
// If we should create a group
@$CreateGroup = $_POST["createGroup"];
if($CreateGroup)
{
$Name = htmlspecialchars(pdo_real_escape_string($_POST["name"]));
// Avoid creating a group that is Nightly, Experimental or Continuous
if($Name == "Nightly" || $Name == "Experimental" || $Name == "Continuous")
{
$xml .= add_XML_value("warning","You cannot create a group named 'Nightly','Experimental' or 'Continuous'");
}
else
{
// Find the last position available
$groupposition_array = pdo_fetch_array(pdo_query("SELECT bg.position,bg.starttime FROM buildgroup AS g, buildgroupposition AS bg
WHERE g.id=bg.buildgroupid AND g.projectid='$projectid'
AND bg.endtime='1980-01-01 00:00:00' ORDER BY bg.position DESC LIMIT 1"));
$newposition = $groupposition_array["position"]+1;
$starttime = '1980-01-01 00:00:00';
$endtime = '1980-01-01 00:00:00';
// Insert the new group
$sql = "INSERT INTO buildgroup (name,projectid,starttime,endtime,description) VALUES ('$Name','$projectid','$starttime','$endtime','')";
if(pdo_query("$sql"))
{
$newgroupid = pdo_insert_id("buildgroup");
// Create a new position for this group
pdo_query("INSERT INTO buildgroupposition (buildgroupid,position,starttime,endtime) VALUES ('$newgroupid','$newposition','$starttime','$endtime')");
}
else
{
echo pdo_error();
}
} // end not Nightly or Experimental or Continuous
} // end CreateGroup
// If we should delete a group
@$DeleteGroup = $_POST["deleteGroup"];
if($DeleteGroup)
{
$Groupid = pdo_real_escape_numeric($_POST["groupid"]);
// We delete all the build2grouprule associated with the group
pdo_query("DELETE FROM build2grouprule WHERE groupid='$Groupid'");
// We delete the buildgroup
pdo_query("DELETE FROM buildgroup WHERE id='$Groupid'");
// Restore the builds that were associated with this group
$oldbuilds = pdo_query("SELECT id,type FROM build WHERE id IN (SELECT buildid AS id FROM build2group WHERE groupid='$Groupid')");
echo pdo_error();
while($oldbuilds_array = pdo_fetch_array($oldbuilds))
{
// Move the builds
$buildid = $oldbuilds_array["id"];
$buildtype = $oldbuilds_array["type"];
// Find the group corresponding to the build type
$query = pdo_query("SELECT id FROM buildgroup WHERE name='$buildtype' AND projectid='$projectid'");
if(pdo_num_rows($query) == 0)
{
$query = pdo_query("SELECT id FROM buildgroup WHERE name='Experimental' AND projectid='$projectid'");
}
echo pdo_error();
$grouptype_array = pdo_fetch_array($query);
$grouptype = $grouptype_array["id"];
pdo_query("UPDATE build2group SET groupid='$grouptype' WHERE buildid='$buildid'");
echo pdo_error();
}
// We delete the buildgroupposition and update the position of the other groups
pdo_query("DELETE FROM buildgroupposition WHERE buildgroupid='$Groupid'");
$buildgroupposition = pdo_query("SELECT bg.buildgroupid FROM buildgroupposition as bg, buildgroup as g
WHERE g.projectid='$projectid' AND bg.buildgroupid=g.id ORDER BY bg.position ASC");
$p = 1;
while($buildgroupposition_array = pdo_fetch_array($buildgroupposition))
{
$buildgroupid = $buildgroupposition_array["buildgroupid"];
pdo_query("UPDATE buildgroupposition SET position='$p' WHERE buildgroupid='$buildgroupid'");
$p++;
}
} // end DeleteGroup
@$GlobalMove = $_POST["globalMove"];
@$ExpectedMove = $_POST["expectedMove"];
@$Movebuilds = $_POST["movebuilds"];
@$GroupSelection = $_POST["groupSelection"];
if($GlobalMove)
{
if($GroupSelection == 0)
{
$xml .= add_XML_value("warning","Please select a group to add these builds");
}
else
{
foreach($Movebuilds as $buildid)
{
// Find information about the build
$build_array = pdo_fetch_array(pdo_query("SELECT type,name,siteid FROM build WHERE id='$buildid'"));
$buildtype = $build_array['type'];
$buildname = $build_array['name'];
$siteid = $build_array['siteid'];
// Remove the group
$prevgroup = pdo_fetch_array(pdo_query("SELECT groupid FROM build2group WHERE buildid='$buildid'"));
$prevgroupid = $prevgroup["groupid"];
// Update the previous group
pdo_query("UPDATE build2group SET groupid='$GroupSelection' WHERE groupid='$prevgroupid' AND buildid='$buildid'");
// Delete any previous rules
pdo_query("DELETE FROM build2grouprule WHERE groupid='$prevgroupid' AND buildtype='$buildtype'
AND buildname='$buildname' AND siteid='$siteid'");
// Add the new rule
pdo_query("INSERT INTO build2grouprule(groupid,buildtype,buildname,siteid,expected,starttime,endtime)
VALUES ('$GroupSelection','$buildtype','$buildname','$siteid','$ExpectedMove','1980-01-01 00:00:00','1980-01-01 00:00:00')");
}
}
} // end GlobalMove
// Update summary email
if(isset($_POST["groupid"]))
{
$Groupid = $_POST["groupid"];
@$SummaryEmail = $_POST["summaryEmail"];
@$EmailCommitters = $_POST["emailCommitters"];
@$IncludeInSummary = $_POST["includeInSummary"];
if(!isset($SummaryEmail))
{
$SummaryEmail = 0;
}
if(!isset($EmailCommitters))
{
$EmailCommitters = 0;
}
if(!isset($IncludeInSummary))
{
$IncludeInSummary = 0;
}
$EmailCommitters = pdo_real_escape_numeric($EmailCommitters);
$IncludeInSummary = pdo_real_escape_numeric($IncludeInSummary);
$Groupid = pdo_real_escape_numeric($Groupid);
$SummaryEmail = pdo_real_escape_numeric($SummaryEmail);
$sql = "UPDATE buildgroup SET summaryemail='$SummaryEmail', ".
"emailcommitters='$EmailCommitters', ".
"includesubprojectotal='$IncludeInSummary' ".
"WHERE id='$Groupid'";
if(!pdo_query("$sql"))
{
echo pdo_error();
}
}
// define a group by build name
@$DefineByBuildName = $_POST["defineByBuildName"];
if($DefineByBuildName)
{
$Groupid = pdo_real_escape_numeric($_POST["groupBuildNameSelection"]);
if ($Groupid > 0)
{
$BuildNameMatch = "%" . htmlspecialchars(pdo_real_escape_string($_POST["buildNameMatch"])) . "%";
$BuildType = htmlspecialchars(pdo_real_escape_string($_POST["buildType"]));
$sql = "INSERT INTO build2grouprule (groupid, buildtype, buildname, siteid)
VALUES ('$Groupid', '$BuildType', '$BuildNameMatch', '-1')";
if(!pdo_query("$sql"))
{
echo pdo_error();
}
}
} // end define by build name
// delete the rules for a build group
@$DeleteWildcardRules = $_POST["deleteWildcardRules"];
if($DeleteWildcardRules)
{
$Groupid = pdo_real_escape_numeric($_POST["deleteWildcardRules"]);
if ($Groupid > 0)
{
$sql = "DELETE FROM build2grouprule WHERE groupid = '$Groupid'";
if(!pdo_query("$sql"))
{
echo pdo_error();
}
}
} // end delete build group rules
/** We start generating the XML here */
// Find the recent builds for this project
if($projectid>0)
{
$currentUTCTime = gmdate(FMT_DATETIME);
$beginUTCTime = gmdate(FMT_DATETIME,time()-3600*7*24); // 7 days
$sql = "";
if($show>0)
{
$sql = "AND g.id='$show'";
}
$builds = pdo_query("SELECT b.id,s.name AS sitename,b.name,b.type,g.name as groupname,g.id as groupid
FROM build AS b, build2group AS b2g,buildgroup AS g, buildgroupposition AS gp, site as s
WHERE b.starttime<'$currentUTCTime' AND b.starttime>'$beginUTCTime'
AND b.projectid='$projectid' AND b2g.buildid=b.id AND gp.buildgroupid=g.id AND b2g.groupid=g.id
AND s.id = b.siteid ".$sql." ORDER BY b.name ASC");
echo pdo_error();
$names = array();
while($build_array = pdo_fetch_array($builds))
{
// Avoid adding the same build twice
if(array_search($build_array['sitename'].$build_array['name'].$build_array['type'],$names) === FALSE)
{
$xml .= "<currentbuild>";
$xml .= add_XML_value("id",$build_array['id']);
$xml .= add_XML_value("name",$build_array['sitename']." ".$build_array['name']." [".$build_array['type']."] ".$build_array['groupname']);
$xml .= "</currentbuild>";
$names[] = $build_array['sitename'].$build_array['name'].$build_array['type'];
}
}
// Add expected builds
$builds = pdo_query(
"SELECT b.id ,s.name AS sitename, b.name, b.type, g.name as groupname,
g.id as groupid
FROM site AS s, build AS b, build2group AS b2g, buildgroup AS g,
build2grouprule AS b2gr
WHERE g.id = b2g.groupid AND b2g.buildid = b.id AND
b2gr.expected = 1 AND b2gr.groupid = g.id AND
g.endtime='1980-01-01 00:00:00' AND b.projectid='$projectid' AND
s.id = b.siteid ".$sql."
ORDER BY b.name ASC");
echo pdo_error();
while($build_array = pdo_fetch_array($builds))
{
$build_name = $build_array['sitename'].$build_array['name'].$build_array['type'];
// Avoid adding the same build twice
if(in_array($build_name ,$names) === FALSE)
{
$xml .= "<currentbuild>";
$xml .= add_XML_value("id",$build_array['id']);
$xml .= add_XML_value("name",$build_array['sitename']." ".$build_array['name']." [".$build_array['type']."] ".$build_array['groupname']." (expected)");
$xml .= "</currentbuild>";
$names[] = $build_name;
}
}
}
$Project = new Project();
$Project->Id = $projectid;
$buildgroups = $Project->GetBuildGroups();
foreach($buildgroups as $buildgroup)
{
$xml .= "<buildgroup>";
$xml .= add_XML_value('id',$buildgroup['id']);
$xml .= add_XML_value('name',$buildgroup['name']);
$xml .= add_XML_value('autoremovetimeframe',$buildgroup['autoremovetimeframe']);
$xml .= "</buildgroup>";
}
// If we have a project id
// WARNING: We should check for security here
if($projectid>0)
{
$project = pdo_query("SELECT id,name FROM project WHERE id='$projectid'");
$project_array = pdo_fetch_array($project);
$xml .= "<project>";
$xml .= add_XML_value("id",$project_array['id']);
$xml .= add_XML_value("name",$project_array['name']);
$xml .= add_XML_value("name_encoded",urlencode($project_array['name']));
// Display the current groups
$groups = pdo_query("SELECT g.id,g.name,g.description,g.summaryemail,g.emailcommitters,g.includesubprojectotal,
gp.position,g.starttime FROM buildgroup AS g, buildgroupposition AS gp
WHERE g.id=gp.buildgroupid AND g.projectid='$projectid'
AND g.endtime='1980-01-01 00:00:00' AND gp.endtime='1980-01-01 00:00:00'
ORDER BY gp.position ASC");
while($group_array = pdo_fetch_array($groups))
{
$xml .= "<group>";
if($show == $group_array['id'])
{
$xml .= add_XML_value("selected","1");
}
$xml .= add_XML_value("id",$group_array['id']);
$xml .= add_XML_value("name",$group_array['name']);
$xml .= add_XML_value("description",$group_array['description']);
$xml .= add_XML_value("summaryemail",$group_array['summaryemail']);
$xml .= add_XML_value("emailcommitters",$group_array['emailcommitters']);
$xml .= add_XML_value("includeinsummary",$group_array['includesubprojectotal']);
$xml .= add_XML_value("position",$group_array['position']);
$xml .= add_XML_value("startdate",$group_array['starttime']);
$xml .= "</group>";
}
$xml .= "</project>";
}
// Generate XML for the wildcard groups here
$wildcards = pdo_query("
SELECT bg.name, bg.id, b2gr.buildtype, b2gr.buildname
FROM build2grouprule AS b2gr, buildgroup AS bg
WHERE b2gr.buildname LIKE '\%%\%' AND b2gr.groupid = bg.id AND
bg.projectid='$projectid'");
echo pdo_error();
while($wildcard_array = pdo_fetch_array($wildcards))
{
$xml .= "<wildcard>";
$xml .= add_XML_value("groupname", $wildcard_array['name']);
$xml .= add_XML_value("groupid", $wildcard_array['id']);
$xml .= add_XML_value("buildtype", $wildcard_array['buildtype']);
$match = $wildcard_array["buildname"];
$match = str_replace("%", "", $match);
$xml .= add_XML_value("match", $match);
$xml .= "</wildcard>";
}
$xml .= "</cdash>";
// Now doing the xslt transition
generate_XSLT($xml,"manageBuildGroup");
} // end session OK
?>