Permalink
Browse files

Big refactoring:

- refactor code of Updater,
- introduce activities instead of texts,
- removed useless exception,
- refatored creation of bundle into one BundleManager,
- fixed and simplified creation of owners for background processes,
- removed unused tags from bundle
- fixed incomplete tests!
  • Loading branch information...
1 parent ed464e7 commit 9378f4638c7f7596da4d5ae87e7ba3be728f897b @stloyd stloyd committed Oct 5, 2012
Showing with 1,597 additions and 830 deletions.
  1. +17 −0 CHANGELOG.markdown
  2. +34 −0 app/DoctrineMigrations/Version20121010101749.php
  3. +1 −6 src/Knp/Bundle/KnpBundlesBundle/Command/KbAddBundleCommand.php
  4. +0 −1 src/Knp/Bundle/KnpBundlesBundle/Command/KbAddSearchedBundlesCommand.php
  5. +1 −3 src/Knp/Bundle/KnpBundlesBundle/Command/KbPopulateCommand.php
  6. +1 −3 src/Knp/Bundle/KnpBundlesBundle/Command/KbUpdateBundlesCommand.php
  7. +3 −8 src/Knp/Bundle/KnpBundlesBundle/Command/KbUpdateTrendsCommand.php
  8. +66 −0 src/Knp/Bundle/KnpBundlesBundle/Command/MigrateCommitsCommand.php
  9. +6 −7 src/Knp/Bundle/KnpBundlesBundle/Consumer/UpdateBundleConsumer.php
  10. +19 −39 src/Knp/Bundle/KnpBundlesBundle/Controller/BundleController.php
  11. +41 −41 src/Knp/Bundle/KnpBundlesBundle/DataFixtures/ORM/Data.php
  12. +315 −0 src/Knp/Bundle/KnpBundlesBundle/Entity/Activity.php
  13. +85 −147 src/Knp/Bundle/KnpBundlesBundle/Entity/Bundle.php
  14. +58 −110 src/Knp/Bundle/KnpBundlesBundle/Entity/Developer.php
  15. +49 −7 src/Knp/Bundle/KnpBundlesBundle/Entity/Owner.php
  16. +15 −16 src/Knp/Bundle/KnpBundlesBundle/Github/Organization.php
  17. +20 −19 src/Knp/Bundle/KnpBundlesBundle/Github/Owner.php
  18. +68 −59 src/Knp/Bundle/KnpBundlesBundle/Github/Repo.php
  19. +167 −0 src/Knp/Bundle/KnpBundlesBundle/Manager/BundleManager.php
  20. +24 −17 src/Knp/Bundle/KnpBundlesBundle/{Entity → Manager}/OwnerManager.php
  21. +82 −0 src/Knp/Bundle/KnpBundlesBundle/Repository/ActivityRepository.php
  22. +0 −15 src/Knp/Bundle/KnpBundlesBundle/Repository/BundleRepository.php
  23. +37 −5 src/Knp/Bundle/KnpBundlesBundle/Repository/OwnerRepository.php
  24. +11 −2 src/Knp/Bundle/KnpBundlesBundle/Resources/config/services.yml
  25. +0 −7 src/Knp/Bundle/KnpBundlesBundle/Resources/doc/api.markdown
  26. +27 −2 src/Knp/Bundle/KnpBundlesBundle/Resources/public/css/styles.css
  27. +2 −2 src/Knp/Bundle/KnpBundlesBundle/Resources/views/Bundle/add.html.twig
  28. +1 −1 src/Knp/Bundle/KnpBundlesBundle/Resources/views/Bundle/bigList.html.twig
  29. +24 −0 src/Knp/Bundle/KnpBundlesBundle/Resources/views/Bundle/list.html.twig
  30. +10 −9 src/Knp/Bundle/KnpBundlesBundle/Resources/views/Bundle/show.html.twig
  31. +20 −0 src/Knp/Bundle/KnpBundlesBundle/Resources/views/Developer/show.html.twig
  32. +8 −13 src/Knp/Bundle/KnpBundlesBundle/Security/Core/User/UserProvider.php
  33. +23 −5 src/Knp/Bundle/KnpBundlesBundle/Tests/Commands/KbUpdateTrendsCommandTest.php
  34. +87 −72 src/Knp/Bundle/KnpBundlesBundle/Tests/Github/RepoTest.php
  35. +1 −0 src/Knp/Bundle/KnpBundlesBundle/Tests/Github/fixtures/encoded-composer.json
  36. +1 −1 src/Knp/Bundle/KnpBundlesBundle/Tests/Security/Core/User/UserProviderTest.php
  37. +43 −65 src/Knp/Bundle/KnpBundlesBundle/Tests/Updater/UpdaterTest.php
  38. +18 −7 src/Knp/Bundle/KnpBundlesBundle/Travis/Travis.php
  39. +63 −0 src/Knp/Bundle/KnpBundlesBundle/Twig/Extension/BundleUtilsExtension.php
  40. +12 −3 src/Knp/Bundle/KnpBundlesBundle/Twitterer/TrendingBundleTwitterer.php
  41. +0 −8 src/Knp/Bundle/KnpBundlesBundle/Updater/Exception/UserNotFoundException.php
  42. +137 −130 src/Knp/Bundle/KnpBundlesBundle/Updater/Updater.php
View
@@ -1,3 +1,20 @@
+* 2012-10-08
+ * Added `Activity` entity and replace all text based ones with new approach
+ in `Bundle` and `Developer` entities,
+ * Added `OwnerRepository#findOneByUniqueFields()` to lookup for users in database,
+ * Added new methods to `BundleUtilsExtension`: `#bundleActivityIcon`
+ and `#bundleActivityMessage`,
+ * Added `BundleManager` for easier creation of bundles,
+ * Moved `OwnerManager` into own namespace,
+ * Moved methods `#getGithubId()`, `#getSensioId()` from `Developer` entity
+ to `Owner` entity
+ * Replace dependency of `Github\Organization` from `OrganizationRepository`
+ to `OwnerManager`,
+ * Replaced dependency of `Update` from `OwnerManager` to `BundleManager`,
+ * Removed `Updater#setUp` method, bundles are now loaded via `Pagerfanta`,
+ * `UpdateBundleConsumer` no longer removes bundle on API failure,
+ * Removed unused exception `UserNotFoundException`
+
* 2012-10-03
* Removed `js` format from web API, we still support: `json` and `html`,
* Moved code related to web API into controllers and removed all view templates
@@ -0,0 +1,34 @@
+<?php
+
+namespace Application\Migrations;
+
+use Doctrine\DBAL\Migrations\AbstractMigration,
+ Doctrine\DBAL\Schema\Schema;
+
+/**
+ * Auto-generated Migration: Please modify to your need!
+ */
+class Version20121010101749 extends AbstractMigration
+{
+ public function up(Schema $schema)
+ {
+ // this up() migration is autogenerated, please modify it to your needs
+ $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
+
+ $this->addSql("CREATE TABLE activities (id INT AUTO_INCREMENT NOT NULL, bundle_id INT DEFAULT NULL, developer_id INT DEFAULT NULL, type INT NOT NULL, state INT NOT NULL, createdAt DATETIME NOT NULL, message VARCHAR(255) DEFAULT NULL, author VARCHAR(255) DEFAULT NULL, bundleName VARCHAR(255) DEFAULT NULL, bundleOwnerName VARCHAR(255) DEFAULT NULL, INDEX IDX_B5F1AFE5F1FAD9D3 (bundle_id), INDEX IDX_B5F1AFE564DD9267 (developer_id), INDEX type_state (type, state), INDEX created_at (createdAt), PRIMARY KEY(id)) ENGINE = InnoDB");
+ $this->addSql("ALTER TABLE activities ADD CONSTRAINT FK_B5F1AFE5F1FAD9D3 FOREIGN KEY (bundle_id) REFERENCES bundle (id)");
+ $this->addSql("ALTER TABLE activities ADD CONSTRAINT FK_B5F1AFE564DD9267 FOREIGN KEY (developer_id) REFERENCES owner (id)");
+ $this->addSql("ALTER TABLE owner ADD lastCommitAt DATE DEFAULT NULL");
+ $this->addSql("ALTER TABLE bundle DROP lastCommits, DROP tags");
+ }
+
+ public function down(Schema $schema)
+ {
+ // this down() migration is autogenerated, please modify it to your needs
+ $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
+
+ $this->addSql("DROP TABLE activities");
+ $this->addSql("ALTER TABLE bundle ADD lastCommits LONGTEXT NOT NULL, ADD tags LONGTEXT NOT NULL");
+ $this->addSql("ALTER TABLE owner DROP lastCommitAt");
+ }
+}
@@ -34,18 +34,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
/* @var $updater Updater */
$updater = $container->get('knp_bundles.updater');
+ $updater->setOutput($output);
if (!$input->getOption('no-publish')) {
// manually set RabbitMQ producer
$updater->setBundleUpdateProducer($container->get('old_sound_rabbit_mq.update_bundle_producer'));
}
- $updater->setOutput($output);
- $updater->setUp();
-
$updater->addBundle($input->getArgument('bundleName'));
-
- $em = $this->getContainer()->get('knp_bundles.entity_manager');
- $em->flush();
}
}
@@ -34,7 +34,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
/* @var $updater Updater */
$updater = $this->getContainer()->get('knp_bundles.updater');
$updater->setOutput($output);
- $updater->setUp();
$bundles = $updater->searchNewBundles();
$updater->createMissingBundles($bundles);
@@ -36,15 +36,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
/* @var $updater Updater */
$updater = $container->get('knp_bundles.updater');
+ $updater->setOutput($output);
if (!$input->getOption('no-publish')) {
// manually set RabbitMQ producer
$updater->setBundleUpdateProducer($container->get('old_sound_rabbit_mq.update_bundle_producer'));
}
- $updater->setOutput($output);
- $updater->setUp();
-
$bundles = $updater->searchNewBundles();
$updater->createMissingBundles($bundles);
$updater->updateBundlesData();
@@ -33,15 +33,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
/* @var $updater Updater */
$updater = $container->get('knp_bundles.updater');
+ $updater->setOutput($output);
if (!$input->getOption('no-publish')) {
// manually set RabbitMQ producer
$updater->setBundleUpdateProducer($container->get('old_sound_rabbit_mq.update_bundle_producer'));
}
- $updater->setOutput($output);
- $updater->setUp();
-
$updater->updateBundlesData();
$em = $container->get('knp_bundles.entity_manager');
@@ -30,19 +30,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
$em = $this->getContainer()->get('knp_bundles.entity_manager');
if ($em->getConnection()->getDatabasePlatform()->getName() == 'sqlite') {
- $output->writeln(sprintf('[%s] This command can\'t be executed on <error>SQLite</error>!', $this->currentTime()));
+ $output->writeln(sprintf('[%s] This command can\'t be executed on <error>SQLite</error>!', date('d-m-y H:i:s')));
return 1;
}
$em->getConnection()->beginTransaction();
try {
$nbRows = $this->updateTrends();
- $output->writeln(sprintf('[%s] <info>%s</info> rows updated', $this->currentTime(), $nbRows));
+ $output->writeln(sprintf('[%s] <info>%s</info> rows updated', date('d-m-y H:i:s'), $nbRows));
$em->getConnection()->commit();
} catch (\Exception $e) {
- $output->writeln(sprintf('[%s] <error>Rollbacking</error> because of %s', $this->currentTime(), $e));
+ $output->writeln(sprintf('[%s] <error>Rollbacking</error> because of %s', date('d-m-y H:i:s'), $e));
$em->getConnection()->rollback();
$em->close();
@@ -89,9 +89,4 @@ private function updateTrends()
return $nbRows;
}
-
- private function currentTime()
- {
- return date('d-m-y H:i:s');
- }
}
@@ -0,0 +1,66 @@
+<?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 Symfony\Component\Console\Output\NullOutput;
+
+use Pagerfanta\Pagerfanta;
+use Pagerfanta\Adapter\DoctrineORMAdapter;
+
+use Knp\Bundle\KnpBundlesBundle\Entity\Bundle;
+use Knp\Bundle\KnpBundlesBundle\Github\Repo;
+
+/**
+ * @todo Remove this command on next week or so
+ */
+class MigrateCommitsCommand extends ContainerAwareCommand
+{
+ protected function configure()
+ {
+ $this
+ ->setDefinition(array())
+ ->setName('kb:migrate:commits')
+ ;
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ /** @var $repo Repo */
+ $em = $this->getContainer()->get('doctrine.orm.entity_manager');
+ $repo = $this->getContainer()->get('knp_bundles.github_repository_api');
+
+ $page = 1;
+ $pager = new Pagerfanta(new DoctrineORMAdapter($em->getRepository('KnpBundlesBundle:Bundle')->queryAllSortedBy('updatedAt'), false));
+ $pager
+ ->setMaxPerPage(100)
+ ->setCurrentPage($page, false, true)
+ ;
+
+ if (1 === $page) {
+ $this->output->writeln(sprintf('[%s] Loaded <comment>%d</comment> bundles from the DB', date('d-m-y H:i:s'), $pager->getNbResults()));
+ }
+
+ do {
+ /** @var $bundle Bundle */
+ foreach ($pager as $bundle) {
+ // Check that API not failed
+ if (!$repo->updateCommits($bundle)) {
+ // Sleep a while, and check again
+ sleep(60);
+
+ $repo->updateCommits($bundle);
+ }
+ }
+
+ $this->output->writeln(sprintf('[%s] Migrated %d from %d bundles', date('d-m-y H:i:s'), $page * 100, $pager->getNbResults()));
+
+ ++$page;
+ } while ($pager->haveToPaginate() && $pager->setCurrentPage($page, false, true));
+
+ return 0;
+ }
+}
@@ -7,8 +7,8 @@
use Knp\Bundle\KnpBundlesBundle\Entity\Bundle;
use Knp\Bundle\KnpBundlesBundle\Entity\Score;
use Knp\Bundle\KnpBundlesBundle\Entity\Owner;
-use Knp\Bundle\KnpBundlesBundle\Entity\OwnerManager;
use Knp\Bundle\KnpBundlesBundle\Indexer\SolrIndexer;
+use Knp\Bundle\KnpBundlesBundle\Manager\OwnerManager;
use Knp\Bundle\KnpBundlesBundle\Travis\Travis;
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
@@ -43,22 +43,22 @@ class UpdateBundleConsumer implements ConsumerInterface
private $em;
/**
- * @var Knp\Bundle\KnpBundlesBundle\Entity\OwnerManager
+ * @var OwnerManager
*/
private $ownerManager;
/**
- * @var Knp\Bundle\KnpBundlesBundle\Indexer\SolrIndexer
+ * @var SolrIndexer
*/
private $indexer;
/**
- * @var Knp\Bundle\KnpBundlesBundle\Github\Repo
+ * @var Repo
*/
private $githubRepoApi;
/**
- * @var Knp\Bundle\KnpBundlesBundle\Travis\Travis
+ * @var Travis
*/
private $travis;
@@ -137,9 +137,8 @@ public function execute(AMQPMessage $msg)
try {
if (!$this->githubRepoApi->update($bundle)) {
if ($this->logger) {
- $this->logger->warn(sprintf('Update failed, bundle "%s" will be removed', $bundle->getName()));
+ $this->logger->warn(sprintf('Update of "%s" failed', $bundle->getName()));
}
- $this->removeBundle($bundle);
return;
}
@@ -8,9 +8,9 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\SolariumAdapter;
+use Knp\Bundle\KnpBundlesBundle\Entity\Activity;
use Knp\Bundle\KnpBundlesBundle\Entity\Bundle;
use Knp\Bundle\KnpBundlesBundle\Entity\Developer;
-use Knp\Bundle\KnpBundlesBundle\Updater\Exception\UserNotFoundException;
class BundleController extends BaseController
{
@@ -44,10 +44,9 @@ public function searchAction(Request $request)
}
// Skip search if query matches exactly one bundle name, and such was found in database
- if (!$request->isXmlHttpRequest() && preg_match('/^[a-z0-9-]+\/[a-z0-9-\.]+$/i', $query)) {
+ if (!$request->isXmlHttpRequest() && preg_match('/^[a-z0-9-]+\/[a-z0-9-]+$/i', $query)) {
list($ownerName, $name) = explode('/', $query);
- /* @var $bundle Bundle */
$bundle = $this->getRepository('Bundle')->findOneBy(array('ownerName' => $ownerName, 'name' => $name));
if ($bundle) {
return $this->redirect($this->generateUrl('bundle_show', array('ownerName' => $ownerName, 'name' => $name, '_format' => $format)));
@@ -223,7 +222,8 @@ public function listAction(Request $request, $sort)
$this->highlightMenu('bundles');
- $owners = $this->getRepository('Developer')->findAllSortedBy('createdAt', 20);
+ $developers = $this->getRepository('Developer')->findAllSortedBy('createdAt', 20);
+ $activities = $this->getRepository('Activity')->findAllSortedBy('createdAt', 10);
$graphPeriod = $this->container->getParameter('knp_bundles.bundle.graph.main_page.period');
@@ -235,7 +235,8 @@ public function listAction(Request $request, $sort)
)
),
'bundles' => $paginator,
- 'developers' => $owners,
+ 'developers' => $developers,
+ 'activities' => $activities,
'sort' => $sort,
'sortLegends' => $this->sortLegends
));
@@ -301,7 +302,7 @@ public function addAction(Request $request)
if (!$error && ($request->isXmlHttpRequest() || 'POST' === $request->getMethod())) {
$bundle = trim(str_replace(array('http://github.com', 'https://github.com', '.git'), '', $bundle), '/');
- if (preg_match('/^[a-z0-9-]+\/[a-z0-9-\.]+$/i', $bundle)) {
+ if (preg_match('/^[a-z0-9-]+\/[a-z0-9-]+$/i', $bundle)) {
list($ownerName, $name) = explode('/', $bundle);
$url = $this->generateUrl('bundle_show', array('ownerName' => $ownerName, 'name' => $name));
@@ -317,22 +318,14 @@ public function addAction(Request $request)
if (!$error) {
/** @var $updater \Knp\Bundle\KnpBundlesBundle\Updater\Updater */
$updater = $this->get('knp_bundles.updater');
- $updater->setUp();
- try {
- $valid = $updater->addBundle($bundle, false, true);
-
- if ($valid) {
- if (!$request->isXmlHttpRequest()) {
- return $this->redirect($url);
- }
- $message = '<strong>Hey, friend!</strong> Thanks for adding <a href="'.$url.'">your bundle</a> to our database!';
- } else {
- $error = true;
- $message = 'Specified repo is not valid Symfony2 bundle!';
+ if ($updater->addBundle($bundle)) {
+ if (!$request->isXmlHttpRequest()) {
+ return $this->redirect($url);
}
- } catch (UserNotFoundException $e) {
+ $message = '<strong>Hey, friend!</strong> Thanks for adding <a href="'.$url.'">your bundle</a> to our database!';
+ } else {
$error = true;
- $message = 'Specified user was not found on GitHub.';
+ $message = 'Specified repository is not valid Symfony2 bundle!';
}
}
} else {
@@ -360,32 +353,19 @@ public function addAction(Request $request)
public function changeUsageStatusAction($ownerName, $name)
{
/* @var $bundle Bundle */
- $bundle = $this->getRepository('Bundle')->findOneByOwnerNameAndName($ownerName, $name);
+ $bundle = $this->getRepository('Bundle')->findOneBy(array('ownerName' => $ownerName, 'name' => $name));
if (!$bundle) {
throw new NotFoundHttpException(sprintf('The bundle "%s/%s" does not exist', $ownerName, $name));
}
- $params = array('ownerName' => $ownerName, 'name' => $name);
-
- if (!$owner = $this->get('security.context')->getToken()->getUser()) {
- return $this->redirect($this->generateUrl('bundle_show', $params));
- }
- $em = $this->get('doctrine')->getEntityManager();
-
- if ($owner->isUsingBundle($bundle)) {
- $bundle->updateScore(-5);
-
- $bundle->removeRecommender($owner);
- } else {
- $bundle->updateScore(5);
-
- $bundle->addRecommender($owner);
+ $url = $this->generateUrl('bundle_show', array('ownerName' => $ownerName, 'name' => $name));
+ if (!$developer = $this->get('security.context')->getToken()->getUser()) {
+ return $this->redirect($url);
}
- $em->persist($bundle);
- $em->flush();
+ $this->get('knp_bundles.bundle.manager')->manageBundleRecommendation($bundle, $developer);
- return $this->redirect($this->generateUrl('bundle_show', $params));
+ return $this->redirect($url);
}
public function searchByKeywordAction(Request $request, $slug)
Oops, something went wrong.

0 comments on commit 9378f46

Please sign in to comment.