/** * @brief Creates new buildset. * * @param name Symbolic name of the build (e.g. branch name). * @param revision Revision to use for the new buildset. * * @returns Buildset object. */ public static function create($name, $revision) { $sql = 'INSERT INTO buildsets(name, revision, timestamp) ' . 'VALUES(?, ?, ?)'; $statement = DB::prepare($sql); $now = time(); if (!$statement || $statement->execute([$name, $revision, $now]) === false) { die("Failed to schedule buildset\n" . print_r(DB::errorInfo(), true)); } $buildsetid = DB::lastInsertId(); return new Buildset($buildsetid, $name, $revision, $now); }
/** * @brief Retrieves at most @p n most recent buildsets. * * @param n Maximum number of build instances to return. * * @returns Array of Buildset instances. */ public static function getLastN($n) { $sql = 'SELECT buildsetid, name, revision, timestamp FROM buildsets ' . "ORDER BY buildsetid DESC LIMIT {$n}"; $statement = DB::query($sql); if (!$statement) { die("Failed to list buildsets from the database\n" . print_r(DB::errorInfo(), true)); } $buildsetsinfo = $statement->fetchAll(); if ($buildsetsinfo === false) { die("Failed to fetch buildsets"); } $buildsets = []; foreach ($buildsetsinfo as $buildsetinfo) { array_push($buildsets, new Buildset($buildsetinfo['buildsetid'], $buildsetinfo['name'], $buildsetinfo['revision'], $buildsetinfo['timestamp'])); } return $buildsets; }
/** * @brief Performs query to obtain builds. * * @param sql SQL request, possibly with placeholders. * @param args Arguments to substitute in placeholders. * * @returns Array of Build. */ private static function getBuilds($sql, $args) { $statement = DB::prepare($sql); if (!$statement || !$statement->execute($args)) { die("Failed to list builds\n" . print_r(DB::errorInfo(), true)); } $buildsinfo = $statement->fetchAll(); if ($buildsinfo === false) { die("Failed to fetch builds\n"); } $builds = []; foreach ($buildsinfo as $buildinfo) { $revision = array_key_exists('revision', $buildinfo) ? $buildinfo['revision'] : null; array_push($builds, new Build($buildinfo['buildset'], $buildinfo['buildername'], $buildinfo['status'], $buildinfo['exitcode'], $revision)); } return $builds; }
/** * @brief Updates status and output of the build. * * @param status New status string. * @param output Multiline build output. */ public function setResult($status, $output, $exitcode) { $output = gzdeflate($output, 9); if ($output === false) { die("Failed to compress output."); } $sql = 'UPDATE builds SET status = ?, output = ?, exitcode = ? ' . 'WHERE buildset = ? AND buildername = ?'; $statement = DB::prepare($sql); if (!$statement || $statement->execute([$status, $output, $exitcode, $this->buildset, $this->buildername]) === false) { die("Failed to set build status to 'running'\n" . print_r(DB::errorInfo(), true)); } $this->status = $status; }