Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

[VERY][WIP] Introduce collaborators #457

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/DoctrineMigrations/Version20150320230132.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20150320230132 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('CREATE TABLE collaboration (bundle_id INT NOT NULL, developer_id INT NOT NULL, INDEX IDX_DA3AE323F1FAD9D3 (bundle_id), INDEX IDX_DA3AE32364DD9267 (developer_id), PRIMARY KEY(bundle_id, developer_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
$this->addSql('ALTER TABLE collaboration ADD CONSTRAINT FK_DA3AE323F1FAD9D3 FOREIGN KEY (bundle_id) REFERENCES bundle (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE collaboration ADD CONSTRAINT FK_DA3AE32364DD9267 FOREIGN KEY (developer_id) REFERENCES owner (id) ON DELETE CASCADE');
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('DROP TABLE collaboration');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private function updateContributors(Bundle $bundle)
}
$bundle->setContributors($contributors);


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be reverted

if ($this->logger) {
$this->logger->info(sprintf('%d contributor(s) have been retrieved for bundle %s', count($contributors), $bundle->getName()));
}
Expand Down
13 changes: 13 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Entity/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ class Bundle
*/
protected $contributors;

/**
* Developers who are the Repo collabs
*
* @ORM\ManyToMany(targetEntity="Developer", inversedBy="collaboratedBundles")
* @ORM\JoinTable(name="collaboration",
* joinColumns={@ORM\JoinColumn(name="bundle_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="developer_id", referencedColumnName="id", onDelete="CASCADE")}
* )
*
* @var Collection
*/
protected $collaborators;

/**
* Number of GitHub followers
*
Expand Down
42 changes: 42 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Entity/Developer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Knp\Bundle\KnpBundlesBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;

Expand Down Expand Up @@ -57,6 +58,15 @@ class Developer extends Owner implements UserInterface
*/
private $contributionBundles;

/**
* Bundles this User is collaborating
*
* @ORM\ManyToMany(targetEntity="Bundle", mappedBy="collaborators")
*
* @var Collection
*/
private $collaboratedBundles;

/**
* @ORM\OneToMany(targetEntity="Activity", mappedBy="developer", fetch="EXTRA_LAZY", cascade={"persist"})
*
Expand All @@ -79,6 +89,7 @@ public function __construct()
$this->organizations = new ArrayCollection();
$this->recommendedBundles = new ArrayCollection();
$this->contributionBundles = new ArrayCollection();
$this->collaboratedBundles = new ArrayCollection();
$this->favoriteBundles = new ArrayCollection();

parent::__construct();
Expand Down Expand Up @@ -252,6 +263,37 @@ public function getContributionBundles($page = null, $limit = 15)
return $paginator->getCurrentPageResults();
}

/**
* Add collaborated Bundle
*
* @param Bundle $collaboratedBundle
*/
public function addCollaboratedBundle(Bundle $collaboratedBundle)
{
$this->collaboratedBundles[] = $collaboratedBundle;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is broken: you only update the inverse side of the relation, which means that Doctrine will not persist this change.

And does the relation actually need to be bidirectional ? Unidirectional relations are much easier to reason about and to handle.

}

/**
* Remove collaborated Bundle
*
* @param Bundle $collaboratedBundle
*/
public function removeCollaboratedBundle(Bundle $collaboratedBundle)
{
$this->collaboratedBundles->removeElement($collaboratedBundle);
}

/**
* Get collaborated bundles

*
* @return \Traversable
*/
public function getCollaboratedBundles()
{
return $this->collaboratedBundles;
}

/**
* @param Activity $activity
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Github/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,24 @@ public function getContributorNames(Bundle $bundle)
return $names;
}

public function getCollaboratorsNames(Bundle $bundle)
{
try {
$collaborators = $this->github->api('repo')->collaborators()->all();
} catch(RuntimeException $e) {
return array();
}

$names = array();
foreach ($collaborators as $collaborator) {
if ($bundle->getOwnerName() != $collaborator['login']) {
$names[] = $collaborator['login'];
}
}

return $names;
}

/**
* @param Bundle $bundle
*
Expand Down
95 changes: 95 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Tests/Github/RepoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,101 @@ public function shouldNotUpdateVersionsHistoryWithWrongData()
$this->assertNull($bundle->getVersionsHistory());
}

/**
* @test
*/
public function shouldReturnContributorsNames()
{
$bundle = new Bundle('KnpLabs/KnpMenuBundle');

$contributors = [
['login' => 'KnpLabs'],
['login' => 'akovalyov'],
['login' => 'john_doe'],
];


$githubApiMock = $this->getMockBuilder('Github\Api\Repo')
->disableOriginalConstructor()
->getMock()
;
$githubApiMock->expects($this->at(0))
->method('contributors')
->will($this->returnValue($contributors))
;

$githubMock = $this->getMock('Github\Client');
$githubMock->expects($this->at(0))
->method('api')
->with('repo')
->will($this->returnValue($githubApiMock))
;
$output = $this->getMock('Symfony\Component\Console\Output\OutputInterface');

$repoManager = $this->getMockBuilder('Knp\Bundle\KnpBundlesBundle\Git\RepoManager')
->disableOriginalConstructor()
->getMock()
;

$repo = new Repo($githubMock, $output, $repoManager, new EventDispatcher(), $this->getOwnerManagerMock());

$results = $repo->getContributorNames($bundle);
$this->assertTrue(count($results) === 2);
$this->assertFalse(array_search('KnpLabs', $results));
}

/**
* @test
*/
public function shouldReturnCollaboratorsNames()
{
$bundle = new Bundle('KnpLabs/KnpMenuBundle');

$collaborators = [
['login' => 'KnpLabs'],
['login' => 'akovalyov'],
['login' => 'john_doe'],
];


$collaboratorsMock = $this->getMockBuilder('Github\Api\Repository\Collaborators')
->disableOriginalConstructor()
->getMock();

$collaboratorsMock
->expects($this->once())
->method('all')
->will($this->returnValue($collaborators));

$githubApiMock = $this->getMockBuilder('Github\Api\Repo')
->disableOriginalConstructor()
->getMock()
;
$githubApiMock->expects($this->at(0))
->method('collaborators')
->will($this->returnValue($collaboratorsMock))
;

$githubMock = $this->getMock('Github\Client');
$githubMock->expects($this->at(0))
->method('api')
->with('repo')
->will($this->returnValue($githubApiMock))
;
$output = $this->getMock('Symfony\Component\Console\Output\OutputInterface');

$repoManager = $this->getMockBuilder('Knp\Bundle\KnpBundlesBundle\Git\RepoManager')
->disableOriginalConstructor()
->getMock()
;

$repo = new Repo($githubMock, $output, $repoManager, new EventDispatcher(), $this->getOwnerManagerMock());

$results = $repo->getCollaboratorsNames($bundle);
$this->assertTrue(count($results) === 2);
$this->assertFalse(array_search('KnpLabs', $results));
}

protected function getRepo($httpClient = null)
{
$github = new \Github\Client();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @author Leszek Prabucki <leszek.prabucki@knplabs.com>
*/
class RepoTest extends \PHPUnit_Framework_TestCase
class TravisTest extends \PHPUnit_Framework_TestCase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably submit this separately

{
/**
* @test
Expand Down