Пример #1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $cwd = getcwd() . '/' . $input->getOption('working-dir');
     $timeout = (int) $input->getOption('timeout');
     /** @var QuestionHelper $question */
     $question = $this->getHelperSet()->get('question');
     /** @var Config $config */
     $config = new Config($cwd);
     try {
         if (!$config->read()) {
             $output->writeln('<fg=red>No previous config found, initializing.</fg=red>');
         } else {
             $output->writeln('<fg=yellow>Previous config found.</fg=yellow>');
             $output->writeln('<fg=yellow>Loading existing config...</fg=yellow> <fg=green>done</fg=green>');
         }
     } catch (GrenadeRuntimeException $e) {
         $output->writeln('<fg=red>failed</fg=red>');
         $output->writeln('<fg=red>An error occured while parsing .grenade.json file.</fg=red>');
         return -1;
     }
     $github = new Client();
     /** @var Repo $repoApi */
     $repoApi = $github->api('repo');
     /** @var CurrentUser $userApi */
     $userApi = $github->api('me');
     /** @var Organization $orgsApi */
     $orgsApi = $github->api('organization');
     /** @var Git $git */
     $git = new Git(null, $timeout);
     $failures = 0;
     $githubUsername = $question->ask($input, $output, new Question('<fg=green>Your Github account:</fg=green> '));
     while (true) {
         $githubPassword = $question->ask($input, $output, (new Question('<fg=green>Your Github password:</fg=green> '))->setHidden(true));
         $github->authenticate($githubUsername, $githubPassword, Client::AUTH_HTTP_PASSWORD);
         try {
             $userApi->emails()->all();
             break;
         } catch (\Github\Exception\RuntimeException $e) {
             $output->writeln(sprintf('<fg=red>%s</fg=red>', $e->getMessage()));
         }
         if (++$failures >= 3) {
             $output->writeln('<fg=red>Too many connection failures, please check your credentials.</fg=red>');
             return -1;
         }
     }
     if (!file_exists($cwd . '/bundles')) {
         $output->writeln('<fg=red>Bundles folder does not exist, some repositories should exist.</fg=red>');
         return -1;
     }
     foreach ($config->walkProjects() as $projectAlias => $projectConfig) {
         foreach ($config->walkBundles($projectAlias) as $bundleAlias => $bundleRepository) {
             $output->writeln(sprintf('<fg=green>Uploading <fg=cyan>%s</fg=cyan>.</fg=green>', $bundleAlias));
             $bundleRepositoryPath = $cwd . '/bundles/' . $bundleAlias;
             if (($organization = $config->getBundleRemoteOrganization($projectAlias, $bundleAlias)) !== null) {
                 $repositoriesList = $orgsApi->repositories($organization);
             } else {
                 $repositoriesList = $userApi->repositories();
             }
             $repositoryInfo = null;
             foreach ($repositoriesList as $remoteRepositoryInfo) {
                 if ($remoteRepositoryInfo['name'] == $bundleAlias) {
                     $repositoryInfo = $remoteRepositoryInfo;
                     break;
                 }
             }
             $git->setWorkingDirectory($bundleRepositoryPath);
             if ($repositoryInfo === null) {
                 $output->writeln(sprintf('<fg=green>Creating a new repository <fg=cyan>%s</fg=cyan>.</fg=green>', $bundleAlias));
                 $repositoryInfo = $repoApi->create($bundleAlias, sprintf('[READONLY] %s mirror repository', $config->getBundleName($projectAlias, $bundleAlias)), '', true, $organization);
                 if (isset($repositoryInfo['ssh_url'])) {
                     $git->remote()->add('origin', $repositoryInfo['ssh_url']);
                 }
             }
             if (!isset($repositoryInfo['ssh_url'])) {
                 throw new RuntimeException('The repository info has not returned a git URL.');
             }
             $git->pushToMirror('origin');
         }
     }
 }
Пример #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $cwd = getcwd() . '/' . $input->getOption('working-dir');
     $timeout = (int) $input->getOption('timeout');
     /** @var FormatterHelper $formatter */
     $formatter = $this->getHelperSet()->get('formatter');
     /** @var Config $config */
     $config = new Config($cwd);
     try {
         if (!$config->read()) {
             $output->writeln('<fg=red>No previous config found, please run grenade:config</fg=red>');
             return -1;
         }
         $output->writeln('<fg=yellow>Previous config found.</fg=yellow>');
         $output->writeln('<fg=yellow>Loading existing config...</fg=yellow> <fg=green>done</fg=green>');
     } catch (GrenadeRuntimeException $e) {
         $output->writeln('<fg=red>failed</fg=red>');
         $output->writeln('<fg=red>An error occured while parsing .grenade.json file.</fg=red>');
         return -1;
     }
     if (!file_exists($cwd . '/repositories')) {
         $output->writeln('<fg=yellow>Repositories folder does not exist, repositories should already be cloned.</fg=yellow>');
         mkdir($cwd . '/repositories', 0755, true);
     }
     if (!file_exists($cwd . '/bundles')) {
         mkdir($cwd . '/bundles', 0755, true);
     }
     /** @var GitSubtreeProgressBarHelper $progressBarHelper */
     $progressBarHelper = new GitSubtreeProgressBarHelper($output);
     foreach ($config->walkProjects() as $projectName => $projectRemote) {
         $repositoryPath = $cwd . '/repositories/' . $projectName;
         $originalGit = new Git($repositoryPath, $timeout);
         $bundleGit = new Git(null, $timeout);
         if (!file_exists($repositoryPath)) {
             $output->writeln(sprintf('<fg=green>Cloning <fg=cyan>%s</fg=cyan> repository.</fg=green>', $projectName));
             $originalGit->setWorkingDirectory($cwd . '/repositories/');
             $process = $originalGit->cloneRepository($projectRemote, $projectName);
             if (!$process->isSuccessful()) {
                 throw new CommandFailureException($process);
             }
             $originalGit->setWorkingDirectory($repositoryPath);
         } else {
             $output->writeln(sprintf('<fg=green>Fetching <fg=cyan>%s</fg=cyan> repository.</fg=green>', $projectName));
             $process = $originalGit->fetch();
             if (!$process->isSuccessful()) {
                 throw new CommandFailureException($process);
             }
         }
         foreach ($config->walkBundles($projectName) as $repositoryAlias => $repositoryConfig) {
             $output->writeln(sprintf('<fg=green>Analyzing bundle <fg=cyan>%s</fg=cyan>...</fg=green>', $repositoryConfig['name']));
             foreach ($originalGit->branchList(false, true, 'origin') as $branchAlias => $branchName) {
                 try {
                     $process = $originalGit->revParse()->verify($branchName);
                     if ($process->isSuccessful() && $config->compareBundleBranchHead($projectName, $repositoryAlias, $branchName, $process->getOutput())) {
                         $output->writeln(sprintf('  <fg=green>Branch <fg=cyan>%s</fg=cyan> up to date, skipping...</fg=green>', $branchName));
                         continue;
                     }
                     $output->writeln(sprintf('  <fg=green>Exporting branch <fg=cyan>%s</fg=cyan>...</fg=green>', $branchName));
                     $process = $originalGit->revParse()->verify($this->derivedBranch($repositoryAlias, $branchAlias));
                     $bundleRepositoryPath = $cwd . '/bundles/' . $repositoryAlias;
                     if (!$process->isSuccessful() || !file_exists($bundleRepositoryPath)) {
                         $output->writeln(sprintf('  <fg=green>Splitting into a new <fg=cyan>%s</fg=cyan> branch.</fg=green>', $this->derivedBranch($repositoryAlias, $branchAlias)));
                         $progressBarHelper->reset();
                         $process = $originalGit->subtree()->split($this->derivedBranch($repositoryAlias, $branchAlias), $repositoryConfig['path'], null, 'origin/' . $branchAlias, false, null, $progressBarHelper);
                         $progressBarHelper->finish();
                         if ($progressBarHelper->updatesCount() <= 0) {
                             $output->writeln(sprintf('  <fg=green>Fast-forward update for branch <fg=cyan>%s</fg=cyan>.</fg=green>', $branchAlias));
                         } else {
                             if (!$process->isSuccessful()) {
                                 throw new CommandFailureException($process);
                             }
                         }
                         $bundleGit->setWorkingDirectory($bundleRepositoryPath);
                         if (!file_exists($bundleRepositoryPath)) {
                             $output->writeln(sprintf('  <fg=green>Initializing git repository for <fg=cyan>%s</fg=cyan> bundle.</fg=green>', $repositoryConfig['name']));
                             mkdir($bundleRepositoryPath, 0755, true);
                             $bundleGit->init();
                         }
                         $output->writeln(sprintf('  <fg=green>Pulling branch <fg=cyan>%s</fg=cyan> in the bundle repository.</fg=green>', $this->derivedBranch($repositoryAlias, $branchAlias)));
                         $bundleGit->pull($repositoryPath, $this->derivedBranch($repositoryAlias, $branchAlias));
                     } else {
                         $output->writeln(sprintf('  <fg=green>Updating the <fg=cyan>%s</fg=cyan> bundle\'s code into the <fg=cyan>%s</fg=cyan> branch.</fg=green>', $repositoryConfig['name'], $this->derivedBranch($repositoryAlias, $branchAlias)));
                         $progressBarHelper->reset();
                         $process = $originalGit->subtree()->push($bundleRepositoryPath, $this->derivedBranch($repositoryAlias, $branchAlias), $repositoryConfig['path'], $progressBarHelper);
                         $progressBarHelper->finish();
                         if (!$process->isSuccessful()) {
                             throw new CommandFailureException($process);
                         }
                     }
                     $process = $originalGit->revParse()->verify($branchName);
                     if ($process->isSuccessful()) {
                         $config->updateBundleBranch($projectName, $repositoryAlias, $branchName, $process->getOutput());
                     }
                 } catch (CommandFailureException $e) {
                     $output->writeln($formatter->formatBlock(['An error occurred while exporting subtrees.', $e->__toString()], 'error'));
                 } catch (RuntimeException $e) {
                     $output->writeln($formatter->formatBlock(['An error occurred while exporting subtrees.', $e->__toString()], 'error'));
                 }
             }
         }
     }
     $config->save();
     return 0;
 }