Browse Source

Resync all package hooks when triggering a manual sync, and detect/update repo renames

Jordi Boggiano 6 years ago
parent
commit
92aae193fe

+ 2 - 2
src/Packagist/WebBundle/Entity/PackageRepository.php

@@ -99,9 +99,9 @@ class PackageRepository extends EntityRepository
             ->leftJoin('p.maintainers', 'm')
             ->where('m.id = :userId')
             ->andWhere('p.repository LIKE :repoUrl')
-            ->andWhere('p.autoUpdated != :synced')
+            ->orderBy('p.autoUpdated', 'ASC')
             ->getQuery()
-            ->setParameters(['userId' => $userId, 'repoUrl' => 'https://github.com/%', 'synced' => Package::AUTO_GITHUB_HOOK]);
+            ->setParameters(['userId' => $userId, 'repoUrl' => 'https://github.com/%']);
 
         return $query->getResult();
     }

+ 13 - 3
src/Packagist/WebBundle/Service/GitHubUserMigrationWorker.php

@@ -31,7 +31,7 @@ class GitHubUserMigrationWorker
 
     public function process(Job $job, SignalHandler $signal): array
     {
-        $em = $this->doctrine->getEntityManager();
+        $em = $this->doctrine->getManager();
         $id = $job->getPayload()['id'];
         $packageRepository = $em->getRepository(Package::class);
         $userRepository = $em->getRepository(User::class);
@@ -131,8 +131,18 @@ class GitHubUserMigrationWorker
 
             if (!$hasValidHook) {
                 $this->logger->debug('Creating hook');
-                $this->request($token, 'POST', 'repos/'.$repoKey.'/hooks', $hookData);
-                $changed = true;
+                $resp = $this->request($token, 'POST', 'repos/'.$repoKey.'/hooks', $hookData);
+                if ($resp->getStatusCode() === 201) {
+                    $hooks[] = json_decode((string) $resp->getBody(), true);
+                    $changed = true;
+                }
+            }
+
+            if (count($hooks) && !preg_match('{^https://api\.github\.com/repos/'.$repoKey.'/hooks/}', $hooks[0]['url'])) {
+                if (preg_match('https://api\.github\.com/repos/([^/]+/[^/]+)/hooks', $hooks[0]['url'], $match)) {
+                    $package->setRepository('https://github.com/'.$match[1]);
+                    $this->doctrine->getManager()->flush($package);
+                }
             }
         } catch (\GuzzleHttp\Exception\ClientException $e) {
             if ($msg = $this->isAcceptableException($e)) {