|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Controller; |
| 4 | + |
| 5 | +use App\Service\CircleCiClientInterface; |
| 6 | +use App\Service\GitLabClientInterface; |
| 7 | +use Bitbucket\Client as BitbucketClient; |
| 8 | +use Packagist\Api\Client as PackagistClient; |
| 9 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 10 | +use Symfony\Component\HttpFoundation\Response; |
| 11 | + |
| 12 | +final class HealthController extends AbstractController |
| 13 | +{ |
| 14 | + // sample repo taken from https://github.com/PUGX/badge-poser/issues/337 |
| 15 | + private const BITBUCKET_HEALTHCHECK_WORKSPACE = 'wirbelwild'; |
| 16 | + private const BITBUCKET_HEALTHCHECK_REPO = 'kiwa-core'; |
| 17 | + |
| 18 | + public function health( |
| 19 | + PackagistClient $packagistClient, |
| 20 | + CircleCiClientInterface $circleCiClient, |
| 21 | + GitLabClientInterface $gitlabClient, |
| 22 | + BitbucketClient $bitbucketClient, |
| 23 | + ): Response { |
| 24 | + $packagistPopular = $packagistClient->popular(1); |
| 25 | + $packagistSuccessRequest = \count($packagistPopular) > 0 && !empty($packagistPopular[0]->getName()) && $packagistPopular[0]->getDownloads() > 0; |
| 26 | + |
| 27 | + $circleciHealth = \json_decode($circleCiClient->health()->getContent()); |
| 28 | + $circleciSuccessRequest = !empty($circleciHealth) && !empty($circleciHealth->id); |
| 29 | + |
| 30 | + $gitlabHealth = $gitlabClient->health(); |
| 31 | + $gitlabSuccessRequest = !empty($gitlabHealth) && !empty($gitlabHealth['id']); |
| 32 | + |
| 33 | + $bitbucketHealth = $bitbucketClient->repositories()->workspaces(self::BITBUCKET_HEALTHCHECK_WORKSPACE)->show(self::BITBUCKET_HEALTHCHECK_REPO); |
| 34 | + $bitbucketSuccessRequest = !empty($bitbucketHealth) && !empty($bitbucketHealth['full_name']); |
| 35 | + |
| 36 | + $response = $this->render( |
| 37 | + 'health/index.txt.twig', |
| 38 | + [ |
| 39 | + 'packagist' => $packagistSuccessRequest, |
| 40 | + 'circleci' => $circleciSuccessRequest, |
| 41 | + 'gitlab' => $gitlabSuccessRequest, |
| 42 | + 'bitbucket' => $bitbucketSuccessRequest, |
| 43 | + ] |
| 44 | + ); |
| 45 | + $response->headers->set('Content-Type', 'text/plain'); |
| 46 | + $response->setMaxAge(0); |
| 47 | + $response->setSharedMaxAge(0); |
| 48 | + |
| 49 | + return $response; |
| 50 | + } |
| 51 | +} |
0 commit comments