Exemplo n.º 1
0
function rest_post()
{
    global $projectid;
    if (isset($_POST['newbuildgroup'])) {
        // Create a new buildgroup
        $BuildGroup = new BuildGroup();
        $BuildGroup->SetProjectId($projectid);
        $name = htmlspecialchars(pdo_real_escape_string($_POST['newbuildgroup']));
        // Avoid creating a group that uses one of the default names.
        if ($name == "Nightly" || $name == "Experimental" || $name == "Continuous") {
            echo_error("You cannot create a group named 'Nightly','Experimental' or 'Continuous'");
            return;
        }
        $type = htmlspecialchars(pdo_real_escape_string($_POST['type']));
        $BuildGroup->SetName($name);
        $BuildGroup->SetType($type);
        $BuildGroup->Save();
        // Respond with a JSON representation of this new buildgroup
        $response = array();
        $response['id'] = $BuildGroup->GetId();
        $response['name'] = $BuildGroup->GetName();
        $response['autoremovetimeframe'] = $BuildGroup->GetAutoRemoveTimeFrame();
        echo json_encode($response);
        return;
    }
    if (isset($_POST['newLayout'])) {
        // Update the order of the buildgroups for this project.
        $inputRows = $_POST['newLayout'];
        if (count($inputRows) > 0) {
            // Remove old build group layout for this project.
            global $CDASH_DB_TYPE;
            if (isset($CDASH_DB_TYPE) && $CDASH_DB_TYPE == "pgsql") {
                // We use a subquery here because postgres doesn't support
                // JOINs in a DELETE statement.
                $sql = "\n          DELETE FROM buildgroupposition WHERE buildgroupid IN\n            (SELECT bgp.buildgroupid FROM buildgroupposition AS bgp\n             LEFT JOIN buildgroup AS bg ON (bgp.buildgroupid = bg.id)\n             WHERE bg.projectid = '{$projectid}')";
            } else {
                $sql = "\n          DELETE bgp FROM buildgroupposition AS bgp\n          LEFT JOIN buildgroup AS bg ON (bgp.buildgroupid = bg.id)\n          WHERE bg.projectid = '{$projectid}'";
            }
            pdo_query($sql);
            add_last_sql_error("manageBuildGroup::newLayout::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("API::buildgroup::newLayout::INSERT", $projectid);
        }
        return;
    }
    if (isset($_POST['builds'])) {
        // Move builds to a new group.
        $group = $_POST['group'];
        if ($group['id'] < 1) {
            echo_error("Please select a group for these builds");
            return;
        }
        $builds = $_POST['builds'];
        if (array_key_exists('expected', $_POST)) {
            $expected = $_POST['expected'];
        } else {
            $expected = 0;
        }
        foreach ($builds as $buildinfo) {
            $groupid = pdo_real_escape_numeric($group['id']);
            $Build = new Build();
            $buildid = pdo_real_escape_numeric($buildinfo['id']);
            $Build->Id = $buildid;
            $Build->FillFromId($Build->Id);
            $prevgroupid = $Build->GroupId;
            // Change the group for this build.
            pdo_query("UPDATE build2group SET groupid='{$groupid}'\n         WHERE groupid='{$prevgroupid}' AND buildid='{$buildid}'");
            // Delete any previous rules
            pdo_query("\n        DELETE FROM build2grouprule\n        WHERE groupid='{$prevgroupid}' AND buildtype='{$Build->Type}' AND\n              buildname='{$Build->Name}' AND siteid='{$Build->SiteId}'");
            // Add the new rule
            pdo_query("\n        INSERT INTO build2grouprule\n          (groupid,buildtype,buildname,siteid,expected,starttime,endtime)\n        VALUES\n          ('{$groupid}','{$Build->Type}','{$Build->Name}','{$Build->SiteId}',\n           '{$expected}','1980-01-01 00:00:00','1980-01-01 00:00:00')");
        }
    }
    if (isset($_POST['nameMatch'])) {
        // Define a BuildGroup by Build name.
        $group = $_POST['group'];
        $groupid = $group['id'];
        if ($groupid < 1) {
            echo_error("Please select a BuildGroup to define.");
            return;
        }
        $nameMatch = "%" . htmlspecialchars(pdo_real_escape_string($_POST["nameMatch"])) . "%";
        $type = htmlspecialchars(pdo_real_escape_string($_POST["type"]));
        $sql = "INSERT INTO build2grouprule (groupid, buildtype, buildname, siteid)\n       VALUES ('{$groupid}', '{$type}', '{$nameMatch}', '-1')";
        if (!pdo_query($sql)) {
            echo_error(pdo_error());
        }
    }
    if (isset($_POST['dynamic']) && !empty($_POST['dynamic'])) {
        // Add a build row to a dynamic group
        $groupid = pdo_real_escape_numeric($_POST['dynamic']['id']);
        if (empty($_POST['buildgroup'])) {
            $parentgroupid = 0;
        } else {
            $parentgroupid = pdo_real_escape_numeric($_POST['buildgroup']['id']);
        }
        if (empty($_POST['site'])) {
            $siteid = 0;
        } else {
            $siteid = pdo_real_escape_numeric($_POST['site']['id']);
        }
        if (empty($_POST['match'])) {
            $match = "";
        } else {
            $match = "%" . htmlspecialchars(pdo_real_escape_string($_POST['match'])) . "%";
        }
        $sql = "INSERT INTO build2grouprule (groupid, buildname, siteid, parentgroupid)\n       VALUES ('{$groupid}', '{$match}', '{$siteid}', '{$parentgroupid}')";
        if (!pdo_query($sql)) {
            echo_error(pdo_error());
        } else {
            // Respond with a JSON representation of this new rule.
            $response = array();
            $response['match'] = htmlspecialchars(pdo_real_escape_string($_POST['match']));
            $response['siteid'] = $siteid;
            if ($siteid > 0) {
                $response['sitename'] = htmlspecialchars(pdo_real_escape_string($_POST['site']['name']));
            } else {
                $response['sitename'] = 'Any';
            }
            $response['parentgroupid'] = $parentgroupid;
            if ($parentgroupid > 0) {
                $response['parentgroupname'] = htmlspecialchars(pdo_real_escape_string($_POST['buildgroup']['name']));
            } else {
                $response['parentgroupname'] = 'Any';
            }
            echo json_encode($response);
            return;
        }
    }
}