ApiController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Packagist\WebBundle\Controller;
  12. use Packagist\WebBundle\Package\Updater;
  13. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  19. /**
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class ApiController extends Controller
  23. {
  24. /**
  25. * @Template()
  26. * @Route("/packages.json", name="packages", defaults={"_format" = "json"})
  27. */
  28. public function packagesAction()
  29. {
  30. $packages = $this->get('doctrine')
  31. ->getRepository('Packagist\WebBundle\Entity\Package')
  32. ->getFullPackages();
  33. $data = array();
  34. foreach ($packages as $package) {
  35. $data[$package->getName()] = $package->toArray();
  36. }
  37. $response = new Response(json_encode($data), 200);
  38. $response->setSharedMaxAge(60);
  39. return $response;
  40. }
  41. /**
  42. * @Route("/api/github", name="github_postreceive", defaults={"_format" = "json"})
  43. * @Method({"POST"})
  44. */
  45. public function githubPostReceive(Request $request)
  46. {
  47. $payload = json_decode($request->request->get('payload'), true);
  48. if (!$payload or !isset($payload['repository']['url'])) {
  49. return new Response(json_encode(array('status' => 'error', 'message' => 'Missing or invalid payload',)), 406);
  50. }
  51. $username = $request->query->get('username');
  52. $apiToken = $request->query->get('apiToken');
  53. $doctrine = $this->get('doctrine');
  54. $user = $doctrine
  55. ->getRepository('Packagist\WebBundle\Entity\User')
  56. ->findOneBy(array('username' => $username, 'apiToken' => $apiToken));
  57. if (!$user) {
  58. return new Response(json_encode(array('status' => 'error', 'message' => 'Invalid credentials',)), 403);
  59. }
  60. if (! preg_match('~(github.com/[\w_\-\.]+/[\w_\-\.]+)$~', $payload['repository']['url'], $matches)) {
  61. return new Response(json_encode(array('status' => 'error', 'message' => 'Could not parse payload repository URL',)), 406);
  62. }
  63. $payloadRepositoryChunk = $matches[1];
  64. foreach ($user->getPackages() as $package) {
  65. if (false !== strpos($package->getRepository(), $payloadRepositoryChunk)) {
  66. // We found the package that was referenced.
  67. $updater = new Updater($doctrine);
  68. $updater->update($package);
  69. return new Response('{ "status": "success" }', 202);
  70. }
  71. }
  72. return new Response(json_encode(array('status' => 'error', 'message' => 'Could not find a package that matches this request (does user maintain the package?)',)), 404);
  73. }
  74. }