public function execute(InputInterface $input, OutputInterface $output) { $project = $this->findProject($input->getArgument('project_spec')); $branches = $project->getBranches()->map(function ($branch) { return $branch->getName(); })->toArray(); $provider = $this->getContainer()->get('app_core.provider.factory')->getProvider($project); foreach ($provider->getBranches($project) as $name) { if (array_search($name, $branches) !== false) { $output->writeln('skipping existing branch <info>' . $name . '</info>'); continue; } $output->writeln('importing branch <info>' . $name . '</info>'); $branch = new Branch(); $branch->setName($name); $branch->setProject($project); $project->addBranch($branch); $builds = $this->getContainer()->get('doctrine')->getRepository('Model:Build')->createQueryBuilder('b')->where('b.ref = ?1 AND b.project = ?2')->setParameters([1 => $branch->getName(), 2 => $project->getId()])->getQuery()->execute(); foreach ($builds as $build) { $branch->addBuild($build); $build->setBranch($branch); } if (count($builds) > 0) { $output->writeln(' - associated <info>' . count($builds) . '</info> builds'); } } $em = $this->getContainer()->get('doctrine')->getManager(); $em->persist($project); $em->flush(); }
/** * @param LifecycleEventArgs */ public function prePersist(LifecycleEventArgs $args) { $build = $args->getEntity(); if (!$build instanceof Build || $build->isPullRequest()) { return; } $em = $this->doctrine->getManager(); $branch = $this->doctrine->getRepository('Model:Branch')->findOneByProjectAndName($build->getProject(), $build->getRef()); if (!$branch) { $this->logger->info('creating non-existing branch', ['project' => $build->getProject()->getId(), 'branch' => $build->getRef()]); $branch = new Branch(); $branch->setName($build->getRef()); $branch->setProject($build->getProject()); $em->persist($branch); } $build->setBranch($branch); }
/** * @param Project $project * * @return boolean */ protected function doBranches(Project $project) { foreach ($this->provider->getBranches($project) as $name) { $branch = new Branch(); $branch->setName($name); $branch->setProject($project); $project->addBranch($branch); } return true; }