/** * Create a new pending build for a project. */ public function build($projectId) { /* @var \PHPCI\Model\Project $project */ $project = $this->projectStore->getById($projectId); if (empty($project)) { throw new NotFoundException('Project with id: ' . $projectId . ' not found'); } $build = new Build(); $build->setProjectId($projectId); $build->setCommitId('Manual'); $build->setStatus(Build::STATUS_NEW); $build->setBranch($project->getBranch()); $build->setCreated(new \DateTime()); $build->setCommitterEmail($_SESSION['user']->getEmail()); $build = $this->buildStore->save($build); header('Location: ' . PHPCI_URL . 'build/view/' . $build->getId()); exit; }
/** * Pulls all pending builds from the database and runs them. */ protected function execute(InputInterface $input, OutputInterface $output) { $parser = new Parser(); $yaml = file_get_contents(APPLICATION_PATH . 'PHPCI/config.yml'); $this->settings = $parser->parse($yaml); $token = $this->settings['phpci']['github']['token']; if (!$token) { $this->logger->error(Lang::get('no_token')); return; } $buildStore = Factory::getStore('Build'); $this->logger->addInfo(Lang::get('finding_projects')); $projectStore = Factory::getStore('Project'); $result = $projectStore->getWhere(); $this->logger->addInfo(Lang::get('found_n_projects', count($result['items']))); foreach ($result['items'] as $project) { $http = new HttpClient('https://api.github.com'); $commits = $http->get('/repos/' . $project->getReference() . '/commits', array('access_token' => $token)); $last_commit = $commits['body'][0]['sha']; $last_committer = $commits['body'][0]['commit']['committer']['email']; $message = $commits['body'][0]['commit']['message']; $this->logger->info(Lang::get('last_commit_is', $project->getTitle(), $last_commit)); if ($project->getLastCommit() != $last_commit && $last_commit != "") { $this->logger->info(Lang::get('adding_new_build')); $build = new Build(); $build->setProjectId($project->getId()); $build->setCommitId($last_commit); $build->setStatus(Build::STATUS_NEW); $build->setBranch($project->getBranch()); $build->setCreated(new \DateTime()); $build->setCommitMessage($message); if (!empty($last_committer)) { $build->setCommitterEmail($last_committer); } $buildStore->save($build); $project->setLastCommit($last_commit); $projectStore->save($project); } } $this->logger->addInfo(Lang::get('finished_processing_builds')); }
/** * Pulls all pending builds from the database and runs them. */ protected function execute(InputInterface $input, OutputInterface $output) { $parser = new Parser(); $yaml = file_get_contents(APPLICATION_PATH . 'PHPCI/config.yml'); $this->settings = $parser->parse($yaml); $token = $this->settings['phpci']['github']['token']; if (!$token) { $this->logger->error("No github token found"); exit; } $buildStore = Factory::getStore('Build'); $this->logger->addInfo("Finding projects to poll"); $projectStore = Factory::getStore('Project'); $result = $projectStore->getWhere(); $this->logger->addInfo(sprintf("Found %d projects", count($result['items']))); foreach ($result['items'] as $project) { $http = new HttpClient('https://api.github.com'); $commits = $http->get('/repos/' . $project->getReference() . '/commits', array('access_token' => $token)); $last_commit = $commits['body'][0]['sha']; $last_committer = $commits['body'][0]['commit']['committer']['email']; $this->logger->info("Last commit to github for " . $project->getTitle() . " is " . $last_commit); if ($project->getLastCommit() != $last_commit && $last_commit != "") { $this->logger->info("Last commit is different from database, adding new build for " . $project->getTitle()); $build = new Build(); $build->setProjectId($project->getId()); $build->setCommitId($last_commit); $build->setStatus(Build::STATUS_NEW); $build->setBranch($project->getBranch()); $build->setCreated(new \DateTime()); if (!empty($last_committer)) { $build->setCommitterEmail($last_committer); } $buildStore->save($build); $project->setLastCommit($last_commit); $projectStore->save($project); } } $this->logger->addInfo("Finished processing builds"); }
/** * @covers PHPUnit::execute */ public function testExecute_CreateDuplicateBuild() { $build = new Build(); $build->setId(1); $build->setProjectId(101); $build->setCommitId('abcde'); $build->setStatus(Build::STATUS_FAILED); $build->setLog('Test'); $build->setBranch('example_branch'); $build->setStarted(new \DateTime()); $build->setFinished(new \DateTime()); $build->setCommitMessage('test'); $build->setCommitterEmail('*****@*****.**'); $build->setExtra(json_encode(array('item1' => 1001))); $returnValue = $this->testedService->createDuplicateBuild($build); $this->assertNotEquals($build->getId(), $returnValue->getId()); $this->assertEquals($build->getProjectId(), $returnValue->getProjectId()); $this->assertEquals($build->getCommitId(), $returnValue->getCommitId()); $this->assertNotEquals($build->getStatus(), $returnValue->getStatus()); $this->assertEquals(Build::STATUS_NEW, $returnValue->getStatus()); $this->assertNull($returnValue->getLog()); $this->assertEquals($build->getBranch(), $returnValue->getBranch()); $this->assertNotEquals($build->getCreated(), $returnValue->getCreated()); $this->assertNull($returnValue->getStarted()); $this->assertNull($returnValue->getFinished()); $this->assertEquals('test', $returnValue->getCommitMessage()); $this->assertEquals('*****@*****.**', $returnValue->getCommitterEmail()); $this->assertEquals($build->getExtra('item1'), $returnValue->getExtra('item1')); }
protected function createBuild($projectId, $commitId, $branch, $committer, $commitMessage, $extra = null) { // Check if a build already exists for this commit ID: $builds = $this->buildStore->getByProjectAndCommit($projectId, $commitId); if ($builds['count']) { return true; } // If not, create a new build job for it: $build = new Build(); $build->setProjectId($projectId); $build->setCommitId($commitId); $build->setStatus(Build::STATUS_NEW); $build->setLog(''); $build->setCreated(new \DateTime()); $build->setBranch($branch); $build->setCommitterEmail($committer); $build->setCommitMessage($commitMessage); if (!is_null($extra)) { $build->setExtra(json_encode($extra)); } $build = BuildFactory::getBuild($this->buildStore->save($build)); // Send a status postback if the build type provides one: $build->sendStatusPostback(); return true; }
/** * Create a build using an existing build as a template: */ public function rebuild($buildId) { $copy = BuildFactory::getBuildById($buildId); if (empty($copy)) { throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.'); } $build = new Build(); $build->setProjectId($copy->getProjectId()); $build->setCommitId($copy->getCommitId()); $build->setStatus(Build::STATUS_NEW); $build->setBranch($copy->getBranch()); $build->setCreated(new \DateTime()); $build->setCommitterEmail($copy->getCommitterEmail()); $build->setCommitMessage($copy->getCommitMessage()); $build->setExtra(json_encode($copy->getExtra())); $build = $this->buildStore->save($build); header('Location: ' . PHPCI_URL . 'build/view/' . $build->getId()); exit; }