Selaa lähdekoodia

Reinstate repo validation, remove two-step validation, should be fetched by JS

If JS is disabled, we can just store the package w/o the confirmation screen
Jordi Boggiano 13 vuotta sitten
vanhempi
commit
dc977de349

+ 19 - 8
src/Packagist/WebBundle/Controller/WebController.php

@@ -56,16 +56,26 @@ class WebController extends Controller
     public function submitPackageAction()
     {
         $package = new Package;
+        $package->setRepositoryProvider($this->get('packagist.repository_provider'));
         $form = $this->createForm(new PackageType, $package);
 
         $request = $this->getRequest();
         if ($request->getMethod() == 'POST') {
             $form->bindRequest($request);
-            $children = $form->getChildren();
-            if($children['repository']->isValid()) {
-                $this->get('session')->set('repository', $package->getRepository());
-
-                return new RedirectResponse($this->generateUrl('confirm'));
+            if ($form->isValid()) {
+                try {
+                    $user = $this->getUser();
+                    $package->addMaintainers($user);
+                    $em = $this->get('doctrine')->getEntityManager();
+                    $em->persist($package);
+                    $em->flush();
+
+                    $this->get('session')->setFlash('success', $package->getName().' has been added to the package list, the repository will be parsed for releases in a bit.');
+                    return new RedirectResponse($this->generateUrl('home'));
+                } catch (\Exception $e) {
+                    $this->get('logger')->crit($e->getMessage(), array('exception', $e));
+                    $this->get('session')->setFlash('error', $package->getName().' could not be saved.');
+                }
             }
         }
 
@@ -74,15 +84,16 @@ class WebController extends Controller
 
     /**
      * @Template()
-     * @Route("/submit/confirm", name="confirm")
+     * @Route("/submit/fetch-info", name="submit.fetch_info", defaults={"_format"="json"})
      */
-    public function confirmPackageAction()
+    public function fetchInfoAction()
     {
+        // TODO refactor, this must validate then retrive the name and return that as json, or just return the errors
         $session = $this->get('session');
         $em = $this->getDoctrine()->getEntityManager();
         $package = new Package;
 
-        if($repository = $session->get('repository')) {
+        if ($repository = $session->get('repository')) {
             $session->remove('repository');
             $package->setRepository($repository);
             $package->fromProvider($this->get('packagist.repository_provider'));

+ 25 - 34
src/Packagist/WebBundle/Entity/Package.php

@@ -13,7 +13,6 @@
 namespace Packagist\WebBundle\Entity;
 
 use Packagist\WebBundle\Repository\RepositoryProviderInterface;
-
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;
 use Symfony\Component\Validator\ExecutionContext;
@@ -25,7 +24,7 @@ use Doctrine\Common\Collections\ArrayCollection;
  *     name="package",
  *     uniqueConstraints={@ORM\UniqueConstraint(name="name_idx", columns={"name"})}
  * )
- * @Assert\Callback(methods={"isPackageUnique"})
+ * @Assert\Callback(methods={"isPackageUnique","isRepositoryValid"})
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
 class Package
@@ -41,7 +40,6 @@ class Package
      * Unique package name
      *
      * @ORM\Column
-     * @Assert\NotBlank()
      */
     private $name;
 
@@ -110,54 +108,37 @@ class Package
         return json_encode($data);
     }
 
-/*
+    public function setRepositoryProvider(RepositoryProviderInterface $provider)
+    {
+        $this->repositoryProvider = $provider;
+    }
+
     public function isRepositoryValid(ExecutionContext $context)
     {
         $propertyPath = $context->getPropertyPath() . '.repository';
         $context->setPropertyPath($propertyPath);
 
-        if (!preg_match('#^git://.+|https?://github.com/[^/]+/[^/]+(?:\.git)?$#', $this->repository)) {
-            $context->addViolation('This is not a valid git repository url', array(), null);
-            return;
-        }
-
-        if (!preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $this->repository, $match)) {
-            $context->addViolation('Only GitHub repositories are supported at the moment', array(), null);
-            // TODO handle other types of git repos, and later svn/hg too
+        $repo = $this->repositoryClass;
+        if (!$repo) {
+            $context->addViolation('No valid/supported repository was found at the given URL', array(), null);
             return;
         }
+        try {
+            $information = $repo->getComposerInformation($repo->getRootIdentifier());
+        } catch (\UnexpectedValueException $e) {}
+        // TODO use more specialized exception for repos
 
-        // handle github repositories
-        $owner = $match[1];
-        $repository = $match[2];
-
-        $repoData = json_decode(file_get_contents('http://github.com/api/v2/json/repos/show/'.$owner.'/'.$repository), true);
-        if (!$repoData) {
-            $context->addViolation('Could not fetch information from this repository (if GitHub is down, please try again later)', array(), null);
-            return;
-        }
-
-        $masterData = json_decode(file_get_contents('https://raw.github.com/'.$owner.'/'.$repository.'/master/composer.json'), true);
-        if ($masterData['name'] !== $this->name) {
-            $context->addViolation('The repository\'s composer.json information does not match the given package name, '.$masterData['name'].' found.', array(), null);
+        if (!isset($information['name']) || !$information['name']) {
+            $context->addViolation('The package name was not be found, your composer.json file must be invalid or missing in your master branch/trunk.', array(), null);
             return;
         }
     }
-*/
 
     public function isPackageUnique(ExecutionContext $context)
     {
         // TODO check for uniqueness of package name
     }
 
-    public function fromProvider(RepositoryProviderInterface $provider)
-    {
-        $repo = $provider->getRepository($this->repository);
-        $composerFile = $repo->getComposerInformation('master');
-
-        $this->setName($composerFile['name']);
-    }
-
     /**
      * Get id
      *
@@ -236,6 +217,16 @@ class Package
     public function setRepository($repository)
     {
         $this->repository = $repository;
+
+        try {
+            $this->repositoryClass = $repo = $this->repositoryProvider->getRepository($this->repository);
+            if (!$repo) {
+                return;
+            }
+            $information = $repo->getComposerInformation($repo->getRootIdentifier());
+            $this->setName($information['name']);
+        } catch (\UnexpectedValueException $e) {}
+        // TODO use more specialized exception for repos
     }
 
     /**