diff --git a/PHPCI/Controller/ProjectController.php b/PHPCI/Controller/ProjectController.php index 56dc9ed0..dbeee05f 100644 --- a/PHPCI/Controller/ProjectController.php +++ b/PHPCI/Controller/ProjectController.php @@ -62,7 +62,7 @@ class ProjectController extends \PHPCI\Controller /** * View a specific project. */ - public function view($projectId) + public function view($projectId, $branch = '') { $project = $this->projectStore->getById($projectId); @@ -72,18 +72,20 @@ class ProjectController extends \PHPCI\Controller $per_page = 10; $page = $this->getParam('p', 1); - $builds = $this->getLatestBuildsHtml($projectId, (($page - 1) * $per_page)); + $builds = $this->getLatestBuildsHtml($projectId, urldecode($branch), (($page - 1) * $per_page)); $pages = $builds[1] == 0 ? 1 : ceil($builds[1] / $per_page); if ($page > $pages) { throw new NotFoundException('Page with number: ' . $page . ' not found'); } - $this->view->builds = $builds[0]; - $this->view->total = $builds[1]; - $this->view->project = $project; - $this->view->page = $page; - $this->view->pages = $pages; + $this->view->builds = $builds[0]; + $this->view->total = $builds[1]; + $this->view->project = $project; + $this->view->branch = urldecode($branch); + $this->view->branches = $this->projectStore->getKnownBranches($projectId); + $this->view->page = $page; + $this->view->pages = $pages; $this->config->set('page_title', $project->getTitle()); @@ -93,16 +95,20 @@ class ProjectController extends \PHPCI\Controller /** * Create a new pending build for a project. */ - public function build($projectId) + public function build($projectId, $branch = '') { /* @var \PHPCI\Model\Project $project */ $project = $this->projectStore->getById($projectId); + if (empty($branch)) { + $branch = $project->getBranch(); + } + if (empty($project)) { throw new NotFoundException('Project with id: ' . $projectId . ' not found'); } - $build = $this->buildService->createBuild($project, null, null, $_SESSION['user']->getEmail()); + $build = $this->buildService->createBuild($project, null, urldecode($branch), $_SESSION['user']->getEmail()); header('Location: '.PHPCI_URL.'build/view/' . $build->getId()); exit; @@ -127,18 +133,27 @@ class ProjectController extends \PHPCI\Controller /** * AJAX get latest builds. */ - public function builds($projectId) + public function builds($projectId, $branch = '') { - $builds = $this->getLatestBuildsHtml($projectId); + $builds = $this->getLatestBuildsHtml($projectId, urldecode($branch)); die($builds[0]); } /** - * Render latest builds for project as HTML table. - */ - protected function getLatestBuildsHtml($projectId, $start = 0) + * Render latest builds for project as HTML table. + * + * @param $projectId + * @param string $branch A urldecoded branch name. + * @param int $start + * @return array + */ + protected function getLatestBuildsHtml($projectId, $branch = '', $start = 0) { $criteria = array('project_id' => $projectId); + if (!empty($branch)) { + $criteria['branch'] = $branch; + } + $order = array('id' => 'DESC'); $builds = $this->buildStore->getWhere($criteria, 10, $start, array(), $order); $view = new b8\View('BuildsTable'); diff --git a/PHPCI/Store/ProjectStore.php b/PHPCI/Store/ProjectStore.php index 0596d0c9..6f81b8f4 100644 --- a/PHPCI/Store/ProjectStore.php +++ b/PHPCI/Store/ProjectStore.php @@ -9,6 +9,7 @@ namespace PHPCI\Store; +use b8\Database; use PHPCI\Store\Base\ProjectStoreBase; /** @@ -19,5 +20,23 @@ use PHPCI\Store\Base\ProjectStoreBase; */ class ProjectStore extends ProjectStoreBase { - // This class has been left blank so that you can modify it - changes in this file will not be overwritten. + public function getKnownBranches($projectId) + { + $query = 'SELECT DISTINCT branch from build WHERE project_id = :pid'; + $stmt = Database::getConnection('read')->prepare($query); + $stmt->bindValue(':pid', $projectId); + + if ($stmt->execute()) { + $res = $stmt->fetchAll(\PDO::FETCH_ASSOC); + + $map = function ($item) { + return $item['branch']; + }; + $rtn = array_map($map, $res); + + return $rtn; + } else { + return array(); + } + } } diff --git a/PHPCI/View/Project/view.phtml b/PHPCI/View/Project/view.phtml index 3aeb75a7..704a6ba5 100644 --- a/PHPCI/View/Project/view.phtml +++ b/PHPCI/View/Project/view.phtml @@ -1,4 +1,23 @@ -