Example #1
0
 public function postGitHub(HttpFoundation\Request $request)
 {
     $signature = $request->headers->get('X-Hub-Signature');
     $rawContent = $request->getContent();
     $hookContent = json_decode($rawContent, true);
     $watchedRepo = $this->watchedReposRepository->getById($hookContent['repository']['id']);
     if ($signature !== 'sha1=' . hash_hmac('sha1', $rawContent, $watchedRepo->secret)) {
         return new HttpFoundation\Response('You\'re not GitHub', 403);
     }
     //If a field called hook exists in the content sent
     //to us then we'll say it's installed.
     if (isset($hookContent['hook'])) {
         return new HttpFoundation\Response('Hook Installed', 202);
     }
     $filters = ['modified', 'removed'];
     //Create an array of [filename => [editors...]]
     $fileEditors = [];
     foreach ($hookContent['commits'] as $commit) {
         foreach ($filters as $filter) {
             foreach ($commit[$filter] as $file) {
                 if (!array_key_exists($file, $fileEditors)) {
                     $fileEditors[$file] = [];
                 }
                 if (!in_array($commit['committer']['name'], $fileEditors[$file])) {
                     $fileEditors[$file][] = $commit['committer']['name'];
                 }
             }
         }
     }
     $patchModels = $this->githubRepo->getChangePatches($hookContent['repository']['full_name'], $hookContent['before'], $hookContent['after'], $fileEditors, $filters);
     $this->emailerService->send('*****@*****.**', $patchModels);
     //202 means accepted but processing hasn't started yet. Perhaps we
     //could offload the work from the server to some other worker process.
     return new HttpFoundation\Response('Hello GitHub', 202);
 }
 /**
  * @param HttpFoundation\Request $request
  * @return HttpFoundation\Response
  */
 public function postGitHub(HttpFoundation\Request $request)
 {
     $rawContent = $request->getContent();
     $repoContent = json_decode($rawContent, true);
     $watchedRepo = $this->watchedReposRepository->createNew($repoContent['name']);
     //This should come from the OAuth token.
     $user = $request->get('user');
     $success = $this->githubRepo->installHook($user, $watchedRepo, $this->baseUrl);
     if ($success) {
         return $this->watchedReposRepository->save($watchedRepo) ? new HttpFoundation\JsonResponse($watchedRepo, 201) : new HttpFoundation\Response('Failed to Save', 507);
     }
     return new HttpFoundation\Response('GitHub Request Failed', 502);
 }
Example #3
0
 public function getGitHubAuthorize(HttpFoundation\Request $request)
 {
     $code = $request->get('code');
     $state = $request->get('state');
     if (is_null($code) || is_null($state)) {
         return new HttpFoundation\Response('Invalid GitHub Request Params', 400);
     }
     $user = $this->github->getUserFromOAuth($code, $state);
     //Check to see if we already have this user. If so then set their
     //ID so we update the user instead of creating a new one.
     $dbUser = $this->userRepo->getAll(['githubId' => $user->githubId], 1);
     if (!empty($dbUser)) {
         $user->id = $dbUser[0]->id;
     }
     if ($this->userRepo->save($user)) {
         //Add an access token to the user for this one time so that
         //they have something to use to contact our service again.
         $token = $this->tokenGenerator->createAccessToken('codemana', $user->id, 'user', true);
         return new HttpFoundation\JsonResponse(['user' => $user, 'token' => $token]);
         //TODO: The user no longer comes with any repositories. The front end is expected to fetch those separately.
     }
     return new HttpFoundation\Response('Failed Login', 500);
 }