public function getRepositories() { $repositories = []; $endpoint = $this->getUser() ? sprintf($this->endpoints['public_repositories'], $this->getUser()) : sprintf($this->endpoints['repositories']); $authenticationPart = $this->getAuthenticationPart(); do { /** @var Response $response */ $response = $this->client->get($endpoint, $authenticationPart); if (200 !== $response->getStatusCode()) { throw new AdapterException(sprintf('Response is not OK for "%s"', $endpoint)); } $chunk = json_decode($response->getBody()->getContents(), true); foreach ($chunk as $definition) { $repository = new GitHubRepository($definition['full_name'], $definition['description'], $definition['clone_url']); $repository->setSize($definition['size']); $repository->setPrivate($definition['private']); $repository->setUpdatedAt(new \DateTime($definition['updated_at'])); $repository->setRemote($this); $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_remote.repository_discovery', $repository, 'discovery', null, ['definition' => $definition])); $repositories[] = $repository; } $endpoint = null; $headerLinks = $response->getHeader('link'); if (count($headerLinks)) { $links = explode(',', $headerLinks[0]); foreach ($links as $link) { $matches = []; if (preg_match('/^<([^>]+)>; rel="next"$/', trim($link), $matches)) { $endpoint = $matches[1]; } } } } while (isset($chunk) && is_array($chunk) && count($chunk) > 0 && $endpoint); return $repositories; }
/** * @return RepositoryInterface[] */ public function getRepositories() { $owner = $this->getUser(); $repositories = []; $page = 1; do { $response = $this->sendClientRequest('get', sprintf($this->endpoints['repositories'], $owner . '?' . http_build_query(['page' => $page, 'format' => 'json'])), [], true); $chunk = json_decode($response->getBody()->getContents(), true); foreach ($chunk['values'] as $definition) { $clone = null; foreach ($definition['links']['clone'] as $link) { if ('https' === $link['name']) { $clone = $link['href']; break; } } $repository = new BitBucketRepository($definition['full_name'], $definition['description'], $clone); $repository->setSize($definition['size']); $repository->setPrivate($definition['is_private']); $repository->setUpdatedAt(new \DateTime($definition['updated_on'])); $repository->setRemote($this); $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_remote.repository_discovery', $repository, 'discovery', null, ['definition' => $definition])); $repositories[] = $repository; } $page++; } while (isset($chunk) && is_array($chunk) && isset($chunk['next']) && count($chunk['values']) > 0); return $repositories; }
/** * @param RemoteInterface $remote * @param string $destination * @param array $options */ public function cloneRemote(RemoteInterface $remote, $destination, array $options = null) { $options = array_merge($this->defaultOptions, $options ?: []); $this->getEmitter()->emit(GitRemoteEvent::prepare('git_guardian.pre_clone_remote', $remote, ['destination' => $destination, 'options' => $options ?: []])); $repositories = $remote->getRepositories(); $fs = new Filesystem(); if (!$fs->exists($destination)) { $fs->mkdir($destination, 0770); } foreach ($repositories as $repository) { try { $this->cloneRepository($repository, $destination, $options); } catch (GitProcessException $e) { $this->getEmitter()->emit(GitRepositoryEvent::prepare('git_guardian.exception_repository', $repository, 'error', null, ['destination' => $destination, 'options' => $options ?: [], 'exception' => $e])); } } $this->getEmitter()->emit(GitRemoteEvent::prepare('git_guardian.post_clone_remote', $remote, ['destination' => $destination, 'options' => $options ?: []])); }