Exemple #1
0
 /**
  * @todo use github api instead of relying on request parameters
  *
  * @param Project $project
  */
 protected function doInspect(Project $project)
 {
     try {
         $request = $this->client->get('/repos/' . $project->getFullName());
         $response = $request->send();
     } catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
         $this->logger->error($e->getMessage());
         return false;
     }
     $infos = $response->json();
     # @todo @slug
     $project->setSlug(preg_replace('/[^a-z0-9\\-]/', '-', strtolower($infos['full_name'])));
     $providerData = ['id' => $infos['id'], 'owner_login' => $infos['owner']['login'], 'full_name' => $infos['full_name'], 'url' => $infos['url'], 'clone_url' => $infos['clone_url'], 'ssh_url' => $infos['ssh_url'], 'keys_url' => $infos['keys_url'], 'hooks_url' => $infos['hooks_url'], 'contents_url' => $infos['contents_url'], 'private' => $infos['private']];
     $project->setProviderName($this->provider->getName());
     $project->setProviderData($providerData);
     $project->setFullName($infos['full_name']);
     $project->setName($infos['name']);
     $project->setDockerBaseImage('symfony2:latest');
     $project->setIsPrivate($infos['private']);
     $project->setGitUrl($infos['private'] ? $infos['ssh_url'] : $infos['clone_url']);
     if (isset($infos['organization'])) {
         $this->logger->info('attaching project\'s organization', ['organization' => $infos['organization']['login']]);
         $rp = $this->doctrine->getRepository('Model:Organization');
         $org = $rp->findOneBy(['name' => $infos['organization']['login'], 'providerName' => $this->provider->getName()]);
         if (null === $org) {
             $this->logger->info('organization not found, creating', ['organization' => $infos['organization']['login']]);
             $orgKeys = $this->sshKeysGenerator->generate();
             $org = new Organization();
             $org->setName($infos['organization']['login']);
             $org->setPublicKey($orgKeys['public']);
             $org->setPrivateKey($orgKeys['private']);
             $org->setProviderName($this->provider->getName());
         }
         $project->setOrganization($org);
     } else {
         $this->logger->info('project has no organization, skipping');
     }
 }
Exemple #2
0
 /**
  * @param Project
  */
 protected function doInspect(Project $project)
 {
     try {
         $response = $this->client->get('2.0/repositories/' . $project->getProviderData('full_slug'))->send();
     } catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
         $this->logger->error($e->getMessage());
         return false;
     }
     $infos = $response->json();
     # @todo @slug
     $project->setSlug(preg_replace('/[^a-z0-9\\-]/', '-', strtolower($infos['full_name'])));
     $providerData = ['id' => $infos['full_name'], 'owner_login' => $infos['owner']['username'], 'full_name' => sprintf('%s/%s', $infos['owner']['username'], $infos['name']), 'full_slug' => $infos['full_name'], 'private' => $infos['is_private'], 'url' => $infos['links']['self']['href']];
     foreach ($infos['links']['clone'] as $link) {
         switch ($link['name']) {
             case 'https':
                 $providerData['clone_url'] = $link['href'];
                 break;
             case 'ssh':
                 $providerData['ssh_url'] = $link['href'];
                 break;
         }
     }
     $project->setProviderName($this->provider->getName());
     $project->setProviderData($providerData);
     $project->setFullName($providerData['full_name']);
     $project->setName($infos['name']);
     $project->setIsPrivate($providerData['private']);
     $project->setGitUrl($providerData['private'] ? $providerData['ssh_url'] : $providerData['clone_url']);
     $project->setDockerBaseImage('stage1/symfony2');
     if (strpos($infos['owner']['links']['self']['href'], '.0/teams')) {
         $orgName = $infos['owner']['username'];
         $this->logger->info('attaching project\'s organization', ['organization' => $orgName]);
         $rp = $this->doctrine->getRepository('Model:Organization');
         $org = $rp->findOneBy(['name' => $orgName, 'providerName' => $this->provider->getName()]);
         if (null === $org) {
             $this->logger->info('organization not found, creating', ['organization' => $orgName]);
             $orgKeys = $this->sshKeysGenerator->generate();
             $org = new Organization();
             $org->setName($orgName);
             $org->setPublicKey($orgKeys['public']);
             $org->setPrivateKey($orgKeys['private']);
             $org->setProviderName($this->provider->getName());
         }
         $project->setOrganization($org);
     } else {
         $this->logger->info('project has no organization, skipping');
     }
 }
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $repository = $this->getContainer()->get('doctrine')->getRepository('Model:Project');
     $em = $this->getContainer()->get('doctrine')->getManager();
     foreach ($repository->findByProviderName('github') as $project) {
         $this->message($output, $project, 'using access token <info>' . $project->getUsers()->first()->getProviderAccessToken('github') . '</info>');
         try {
             $githubInfos = $this->getGithubInfos($project);
         } catch (\Exception $e) {
             $output->writeln('<error>Could not fetch infos</error>');
             continue;
         }
         $providerData = $project->getProviderData();
         if (strlen($providerData['hooks_url']) === 0) {
             $this->message($output, $project, 'fixing hooks url');
             $providerData['hooks_url'] = $githubInfos['hooks_url'];
         }
         if (strlen($project->getDockerBaseImage()) === 0) {
             $this->message($output, $project, 'fixing base image');
             $project->setDockerBaseImage('symfony2:latest');
         }
         if (strlen($providerData['url']) === 0) {
             $this->message($output, $project, 'fixing github url');
             $providerData['url'] = 'https://api.github.com/repos/' . $project->getFullName();
         }
         if (!array_key_exists('private', $providerData) || !is_bool($providerData['private'])) {
             $this->message($output, $project, 'fixing private status');
             $providerData['private'] = $githubInfos['private'];
         }
         if (strlen($providerData['contents_url']) === 0) {
             $this->message($output, $project, 'fixing contents url');
             if (isset($githubInfos['contents_url'])) {
                 $providerData['contents_url'] = $githubInfos['contents_url'];
             }
         }
         if (null === $project->getOrganization() && isset($githubInfos['organization'])) {
             $this->message($output, $project, 'fixing organization');
             $orgKeys = $this->getContainer()->get('app_core.ssh_keys_generator')->generate();
             $org = new Organization();
             $org->setName($githubInfos['organization']['login']);
             $org->setGithubId($githubInfos['organization']['id']);
             $org->setPublicKey($orgKeys['public']);
             $org->setPrivateKey($orgKeys['private']);
             $project->setOrganization($org);
         }
         if (!$project->getSettings() || strlen($project->getSettings()->getPolicy()) === 0) {
             $this->message($output, $project, 'fixing build policy');
             $settings = $project->getSettings() ?: new ProjectSettings();
             $settings->setPolicy(ProjectSettings::POLICY_ALL);
             $settings->setProject($project);
             $em->persist($settings);
         }
         try {
             $githubHookUrl = $this->getContainer()->get('router')->generate('app_core_hooks_github', [], true);
             $githubHookUrl = str_replace('http://localhost', 'http://stage1.io', $githubHookUrl);
             $provider = $this->getContainer()->get('app_core.provider.github');
             $client = $provider->configureClientForProject($project);
             if (strlen($providerData['hook_id']) === 0) {
                 $this->message($output, $project, 'adding webhooks');
                 $request = $client->post($providerData['hooks_url']);
                 $request->setBody(json_encode(['name' => 'web', 'active' => true, 'events' => ['push', 'pull_request'], 'config' => ['url' => $githubHookUrl, 'content_type' => 'json']]), 'application/json');
                 $response = $request->send();
                 $installedHook = $response->json();
                 $providerData['hook_id'] = $installedHook['id'];
             } else {
                 $request = $client->get([$providerData['hooks_url'], ['id' => $providerData['hook_id']]]);
                 $response = $request->send();
                 $installedHook = $response->json()[0];
                 if (count($installedHook['events']) === 1) {
                     $this->message($output, $project, 'adding pull_request webhook');
                     $request = $client->patch($installedHook['url']);
                     $request->setBody(json_encode(['add_events' => ['pull_request']]));
                     $request->send();
                 }
             }
         } catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
             $output->writeln('<error>could not check webhook</error>');
         }
         $project->setProviderData($providerData);
         $em->persist($project);
     }
     $em->flush();
 }