/** * Handles the request to show a repo. * * @param \StyleCI\StyleCI\Models\Repo $repo * * @return \Illuminate\Http\JsonResponse */ public function handleShow(Repo $repo) { $branch = Request::get('branch', $repo->default_branch); $analyses = $repo->analyses()->visible()->where('branch', $branch)->orderBy('created_at', 'desc')->paginate(50); $pagination = ['total' => $analyses->total(), 'count' => count($analyses->items()), 'per_page' => $analyses->perPage(), 'current_page' => $analyses->currentPage(), 'total_pages' => $analyses->lastPage(), 'links' => ['next_page' => $analyses->nextPageUrl(), 'previous_page' => $analyses->previousPageUrl()]]; return new JsonResponse(['data' => AutoPresenter::decorate($analyses->getCollection())->toArray(), 'pagination' => $pagination]); }
public function testPresentation() { $repo = Repo::create(['id' => '12345', 'user_id' => '4242', 'name' => 'Foo/Baz', 'default_branch' => 'lol', 'token' => str_repeat('a', 20)]); $presented = AutoPresenter::decorate($repo); $this->assertInstanceOf(RepoPresenter::class, $presented); $this->assertSame(12345, $presented->id); $this->assertSame(['id' => 12345, 'last_analysis' => null, 'name' => 'Foo/Baz', 'default_branch' => 'lol', 'link' => url('/repos/12345')], $presented->toArray()); }
/** * Notify collaborators of successful analyses. * * We only notify them if the previous analysis of this branch failed, or * if this was the first analysis of this branch. * * @param \StyleCI\StyleCI\Models\Analysis $analysis * @param \StyleCI\StyleCI\Models\Repo $repo * * @return void */ public function notifySuccess(Analysis $analysis, Repo $repo) { $previous = $repo->analyses()->where('branch', $analysis->branch)->where('id', '<', $analysis->id)->visible()->latest()->first(); if (!$previous) { $status = 'first'; } elseif ($previous->status > Analysis::PASSED) { $status = 'passed'; } else { return; } $mail = ['repo' => $repo->name, 'commit' => $analysis->message, 'branch' => $analysis->branch, 'link' => route('analysis', AutoPresenter::decorate($analysis)->id), 'subject' => "[{$repo->name}] Analysis Passed"]; foreach ($this->userRepository->collaborators($repo) as $user) { $mail['email'] = $user->email; $mail['name'] = AutoPresenter::decorate($user)->first_name; $this->mailer->queue(["emails.html.{$status}", "emails.text.{$status}"], $mail, function (Message $message) use($mail) { $message->to($mail['email'])->subject($mail['subject']); }); } }
/** * Define the route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * * @return void */ public function boot(Router $router) { parent::boot($router); $router->bind('analysis', function ($value) { $decoded = Hashids::connection('analyses')->decode($value); if (isset($decoded[0]) && is_numeric($decoded[0]) && ($analysis = Analysis::find($decoded[0]))) { return $analysis; } throw new NotFoundHttpException('Analysis not found.'); }); $router->bind('repo', function ($value) { if (is_numeric($value) && ($repo = Repo::find($value))) { return $repo; } throw new NotFoundHttpException('Repo not found.'); }); }
/** * Handles the request made to StyleCI by the GitHub API. * * @return \Illuminate\Http\JsonResponse */ public function handle() { $class = 'StyleCI\\StyleCI\\Events\\Repo\\GitHub\\GitHub' . ucfirst(camel_case(Request::header('X-GitHub-Event'))) . 'Event'; if (!class_exists($class)) { throw new BadRequestHttpException('Event not supported.'); } $data = Request::input(); $repo = Repo::find($data['repository']['id']); if (!$repo) { throw new BadRequestHttpException('Request integrity validation failed.'); } list($algo, $sig) = explode('=', Request::header('X-Hub-Signature')); $hash = hash_hmac($algo, Request::getContent(), $repo->token); if (!Str::equals($hash, $sig)) { throw new BadRequestHttpException('Request integrity validation failed.'); } event(new $class($repo, $data)); return new JsonResponse(['message' => 'Event successfully received.']); }
/** * Execute the console command. * * @return void */ public function handle() { $this->info('Checking all repos.'); $count = 0; $factory = app(ClientFactory::class); foreach (Repo::all() as $repo) { if ($repo->user === null) { $count++; $this->error("Orphaned repo: {$repo->id}, {$repo->name}"); } else { try { $name = explode('/', $repo->name); $factory->make($repo)->repo()->collaborators()->all($name[0], $name[1]); } catch (Exception $e) { $count++; $this->error("Bad repo: {$repo->id}, {$repo->name}"); } } } $this->info("Found {$count} bad/orphaned repos."); }
/** * Save all downloaded changes to the database for the given repo. * * @param \StyleCI\StyleCI\Models\Repo $repo * @param array $data * * @return void */ protected function syncWithDatabase(Repo $repo, array $data) { $modified = false; foreach (['name', 'default_branch'] as $property) { if ($repo->{$property} === $data[$property]) { continue; } $repo->{$property} = $data[$property]; $modified = true; } if ($modified) { $repo->save(); } }
/** * Handle the enable repo command. * * @param \StyleCI\StyleCI\Commands\Repo\EnableRepoCommand $command * * @return void */ public function handle(EnableRepoCommand $command) { $repo = Repo::create(['id' => $command->id, 'user_id' => $command->user->id, 'name' => $command->name, 'default_branch' => $command->branch, 'token' => bin2hex(random_bytes(10))]); event(new RepoWasEnabledEvent($repo)); }
/** * Find all repos a user can view. * * @param \StyleCI\StyleCI\Models\User $user * @param bool $admin * * @return \Illuminate\Database\Eloquent\Collection */ public function allByUser(User $user, $admin = false) { return Repo::whereIn('id', array_keys($this->repos->get($user, $admin)))->orderBy('name', 'asc')->get(); }