Skip to content

Commit

Permalink
Merge pull request KnpLabs#239 from cursedcoder/feature/OrgsAndDevs
Browse files Browse the repository at this point in the history
Split developers and organizations
  • Loading branch information
stloyd committed Sep 13, 2012
2 parents c502910 + f91441f commit 07d6b22
Show file tree
Hide file tree
Showing 93 changed files with 2,450 additions and 1,599 deletions.
1 change: 1 addition & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ default:
bootstrap: ""
env: test
debug: true
bootstrap: ''
Behat\MinkExtension\Extension:
base_url: 'http://knpbundles.local/app_test.php'
default_session: symfony2
4 changes: 2 additions & 2 deletions src/Knp/Bundle/KnpBundlesBundle/Badge/BadgeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function show(Bundle $bundle, $type = 'long', $regenerate = false)
$this->cacheDir
);

$filename = sprintf('%s/badges/%s/%s-%s.png', $relativePath, $type, $bundle->getUsername(), $bundle->getName());
$filename = sprintf('%s/badges/%s/%s-%s.png', $relativePath, $type, $bundle->getOwnerName(), $bundle->getName());
if (!$this->filesystem->exists($filename) || false !== $regenerate) {
$this->generate($bundle);
}
Expand Down Expand Up @@ -206,7 +206,7 @@ protected function setFont(ImagineInterface $imagine, $font, $size, $color = '8c
*/
protected function getBadgeFile(Bundle $bundle, $type = self::LONG)
{
return $this->cacheDir.'/badges/'.$type.'/'.$bundle->getUsername().'-'.$bundle->getName().'.png';
return $this->cacheDir.'/badges/'.$type.'/'.$bundle->getOwnerName().'-'.$bundle->getName().'.png';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$badgeGenerator->generate($bundle);
++$badgesCount;
} catch (ImageNotSavedException $e) {
$output->writeln('<error>Error occured during an image saving for '.$bundle->getUsername().'-'.$bundle->getName().' </error>');
$output->writeln('<error>Error occured during an image saving for '.$bundle->getOwnerName().'-'.$bundle->getName().' </error>');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,5 @@ protected function execute(InputInterface $input, OutputInterface $output)
$bundles = $updater->searchNewBundles();
$updater->createMissingBundles($bundles);
$updater->updateBundlesData();
$updater->updateUsers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$indexer = $this->getContainer()->get('knp_bundles.indexer.solr');

if ($bundleName) {
list($username, $name) = explode('/', $bundleName);
$bundles = array($doctrine->getRepository('Knp\\Bundle\\KnpBundlesBundle\\Entity\\Bundle')->findOneByUsernameAndName($username, $name));
list($ownerName, $name) = explode('/', $bundleName);
$bundles = array($doctrine->getRepository('Knp\\Bundle\\KnpBundlesBundle\\Entity\\Bundle')->findOneByOwnerNameAndName($ownerName, $name));
} elseif ($force) {
$bundles = $doctrine->getRepository('Knp\\Bundle\\KnpBundlesBundle\\Entity\\Bundle')->findAll();
} else {
Expand Down
41 changes: 0 additions & 41 deletions src/Knp/Bundle/KnpBundlesBundle/Command/KbUpdateUsersCommand.php

This file was deleted.

141 changes: 141 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Command/MigrateUsersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Knp\Bundle\KnpBundlesBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Github\Exception\ApiLimitExceedException;

/**
* @todo Remove this command on next week or so
*/
class MigrateUsersCommand extends ContainerAwareCommand
{
private $owners;
private $github;

private $ownerTable = <<<EOF
CREATE TABLE owner (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(127) NOT NULL, fullName VARCHAR(255) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, avatarUrl VARCHAR(255) DEFAULT NULL, url VARCHAR(255) DEFAULT NULL, location VARCHAR(255) DEFAULT NULL, createdAt DATETIME NOT NULL, score INT NOT NULL, discriminator VARCHAR(255) NOT NULL, company VARCHAR(255) DEFAULT NULL, UNIQUE INDEX name_unique (name), PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE organization_developer (organization_id INT NOT NULL, developer_id INT NOT NULL, INDEX IDX_5DCC519C32C8A3DE (organization_id), INDEX IDX_5DCC519C64DD9267 (developer_id), PRIMARY KEY(organization_id, developer_id)) ENGINE = InnoDB;
ALTER TABLE organization_developer ADD CONSTRAINT FK_5DCC519C32C8A3DE FOREIGN KEY (organization_id) REFERENCES owner (id) ON DELETE CASCADE;
ALTER TABLE organization_developer ADD CONSTRAINT FK_5DCC519C64DD9267 FOREIGN KEY (developer_id) REFERENCES owner (id) ON DELETE CASCADE;
ALTER TABLE bundle DROP FOREIGN KEY bundle_ibfk_1;
EOF;

private $afterMigration = <<<EOF
ALTER TABLE contribution DROP FOREIGN KEY FK_EA351E15A76ED395;
ALTER TABLE bundles_usage DROP FOREIGN KEY FK_1351C3D1D9BF196B;
DROP INDEX IDX_1351C3D1D9BF196B ON bundles_usage;
DROP INDEX user_id ON contribution;
DROP INDEX user_id ON bundle;
DROP INDEX full_name_unique ON bundle;
DROP TABLE user;
ALTER TABLE bundle CHANGE user_id owner_id INT NOT NULL, CHANGE username ownerName VARCHAR(127) NOT NULL;
ALTER TABLE bundle ADD CONSTRAINT FK_A57B32FD7E3C61F9 FOREIGN KEY (owner_id) REFERENCES owner (id);
CREATE INDEX IDX_A57B32FD7E3C61F9 ON bundle (owner_id);
CREATE UNIQUE INDEX full_name_unique ON bundle (ownerName, name);
ALTER TABLE bundles_usage DROP PRIMARY KEY;
ALTER TABLE bundles_usage CHANGE knpbundles_user_id knpbundles_owner_id INT NOT NULL;
ALTER TABLE bundles_usage ADD CONSTRAINT FK_1351C3D124476F28 FOREIGN KEY (knpbundles_owner_id) REFERENCES owner (id);
CREATE INDEX IDX_1351C3D124476F28 ON bundles_usage (knpbundles_owner_id);
ALTER TABLE bundles_usage ADD PRIMARY KEY (bundle_id, knpbundles_owner_id);
ALTER TABLE contribution DROP PRIMARY KEY;
ALTER TABLE contribution CHANGE user_id developer_id INT NOT NULL;
ALTER TABLE contribution ADD CONSTRAINT FK_EA351E1564DD9267 FOREIGN KEY (developer_id) REFERENCES owner (id) ON DELETE CASCADE;
CREATE INDEX IDX_EA351E1564DD9267 ON contribution (developer_id);
ALTER TABLE contribution ADD PRIMARY KEY (bundle_id, developer_id);
EOF;

protected function configure()
{
$this
->setDefinition(array())
->setName('kb:migrate:users')
->addOption('users', 'u', InputOption::VALUE_OPTIONAL, 'how many users migrate per one cycle', 10)
->addOption('remove-not-existing', 'rne', InputOption::VALUE_OPTIONAL, 'remove users which are not exist on github', false)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
/**
* @var $connection \Doctrine\DBAL\Connection
* @var $github \Github\Client
*/
$connection = $this->getContainer()->get('doctrine.orm.entity_manager')->getConnection();
$this->github = $github = $this->getContainer()->get('knp_bundles.github_client');

$usersPerCycle = $input->getOption('users');

$testFetch = $connection->fetchAssoc('SELECT * FROM user LIMIT 1');

if (!isset($testFetch['is_migrated'])) {
$connection->executeQuery($this->ownerTable);
$connection->executeQuery('ALTER TABLE user ADD is_migrated VARCHAR(10) NULL;');
}

do {
$oldUsers = $connection->fetchAll(sprintf('SELECT * FROM user WHERE is_migrated IS NULL LIMIT %d', $usersPerCycle));

foreach ($oldUsers as $oldUser) {
try {
$ghUser = $github->api('user')->show($oldUser['name']);
} catch (ApiLimitExceedException $e) {
throw $e;
} catch (\RuntimeException $e) {
sleep(10);
continue;
}

if (isset($ghUser['message']) && $ghUser['message'] == 'Not Found') {
if ($input->getOption('remove-not-existing')) {
$connection->executeQuery('DELETE user WHERE id = :user_id', array('user_id' => $oldUser['id']));
} else {
$connection->executeQuery('UPDATE user SET is_migrated = 1 WHERE id = :user_id', array('user_id' => $oldUser['id']));
}
continue;
}

$data = $oldUser;
$data['discriminator'] = $ghUser['type'] == 'Organization' ? 'organization' : 'developer';
$data = $this->migrateColumns($data, $ghUser);

$this->owners[] = $data;
}

$connection->beginTransaction();
try {
foreach ($this->owners as $ownerData) {
$connection->insert('owner', $ownerData);
$connection->executeQuery('UPDATE user SET is_migrated = 1 WHERE id = :user_id', array('user_id' => $ownerData['id']));
}

$connection->commit();

$output->writeln(sprintf('%d users has been successfully migrated', $usersPerCycle));
} catch (\Exception $e) {
$connection->rollback();
$connection->close();

throw $e;
}

$this->owners = array();
} while (count($oldUsers) > 0);

$connection->executeQuery($this->afterMigration);
}

private function migrateColumns($data, $ghUser)
{
$data['url'] = $data['blog'];
$data['avatarUrl'] = isset($ghUser['avatar_url']) ? $ghUser['avatar_url'] : null;

unset($data['blog'], $data['gravatarHash'], $data['is_migrated']);

return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function execute($msg)
$payload = $message->payload;
$bundle = $this->manager->getRepository('KnpBundlesBundle:Bundle')->findOneBy(array(
'name' => $payload->repository->name,
'username' => $payload->repository->owner->name
'ownerName' => $payload->repository->owner->name
));

if (!$bundle) {
Expand Down
24 changes: 12 additions & 12 deletions src/Knp/Bundle/KnpBundlesBundle/Consumer/UpdateBundleConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Knp\Bundle\KnpBundlesBundle\Git;
use Knp\Bundle\KnpBundlesBundle\Entity\Bundle;
use Knp\Bundle\KnpBundlesBundle\Entity\Score;
use Knp\Bundle\KnpBundlesBundle\Entity\User;
use Knp\Bundle\KnpBundlesBundle\Entity\UserManager;
use Knp\Bundle\KnpBundlesBundle\Entity\Owner;
use Knp\Bundle\KnpBundlesBundle\Entity\OwnerManager;
use Knp\Bundle\KnpBundlesBundle\Indexer\SolrIndexer;
use Knp\Bundle\KnpBundlesBundle\Travis\Travis;

Expand Down Expand Up @@ -43,9 +43,9 @@ class UpdateBundleConsumer implements ConsumerInterface
private $em;

/**
* @var Knp\Bundle\KnpBundlesBundle\Entity\UserManager
* @var Knp\Bundle\KnpBundlesBundle\Entity\OwnerManager
*/
private $users;
private $ownerManager;

/**
* @var Knp\Bundle\KnpBundlesBundle\Indexer\SolrIndexer
Expand All @@ -64,18 +64,18 @@ class UpdateBundleConsumer implements ConsumerInterface

/**
* @param ObjectManager $em
* @param UserManager $users
* @param OwnerManager $ownerManager
* @param Repo $githubRepoApi
* @param Travis $travis
* @param SolrIndexer $indexer
*/
public function __construct(ObjectManager $em, UserManager $users, Repo $githubRepoApi, Travis $travis, SolrIndexer $indexer)
public function __construct(ObjectManager $em, OwnerManager $ownerManager, Repo $githubRepoApi, Travis $travis, SolrIndexer $indexer)
{
$this->em = $em;
$this->users = $users;
$this->ownerManager = $ownerManager;
$this->githubRepoApi = $githubRepoApi;
$this->travis = $travis;
$this->users = $users;
$this->ownerManager = $ownerManager;
$this->indexer = $indexer;
}

Expand Down Expand Up @@ -187,7 +187,7 @@ private function updateContributors(Bundle $bundle)

$contributors = array();
foreach ($contributorNames as $contributorName) {
$contributors[] = $this->users->getOrCreate($contributorName);
$contributors[] = $this->ownerManager->getOrCreate($contributorName);
}
$bundle->setContributors($contributors);

Expand Down Expand Up @@ -220,9 +220,9 @@ private function updateKeywords(Bundle $bundle)
*/
protected function removeBundle(Bundle $bundle)
{
$user = $bundle->getUser();
if ($user instanceof User) {
$user->removeBundle($bundle);
$owner = $bundle->getOwner();
if ($owner instanceof Owner) {
$owner->removeBundle($bundle);
}

// remove bundle from search index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/
class BadgeController extends BaseController
{
public function showAction($username, $name, $type = 'long')
public function showAction($ownerName, $name, $type = 'long')
{
$bundle = $this->getBundleRepository()->findOneByUsernameAndName($username, $name);
$bundle = $this->getBundleRepository()->findOneByOwnerNameAndName($ownerName, $name);
if (!$bundle) {
throw new NotFoundHttpException(sprintf('The bundle "%s/%s" does not exist', $username, $name));
throw new NotFoundHttpException(sprintf('The bundle "%s/%s" does not exist', $ownerName, $name));
}

return $this->container->get('knp_bundles.badge_generator')->show($bundle, $type);
Expand Down
10 changes: 0 additions & 10 deletions src/Knp/Bundle/KnpBundlesBundle/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ protected function getPaginator(Query $query, $page, $limit = 10)
return $paginator;
}

protected function getBundleRepository()
{
return $this->getRepository('Bundle');
}

protected function getUserRepository()
{
return $this->getRepository('User');
}

protected function getRepository($class)
{
return $this->container->get('knp_bundles.entity_manager')->getRepository('Knp\\Bundle\\KnpBundlesBundle\\Entity\\'.$class);
Expand Down
Loading

0 comments on commit 07d6b22

Please sign in to comment.