Skip to content

Commit 6e9ef3a

Browse files
Merge pull request #865 from PUGX/health-page
added APIs health page
2 parents b199995 + 75108a7 commit 6e9ef3a

File tree

8 files changed

+102
-4
lines changed

8 files changed

+102
-4
lines changed

config/routes.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ home:
1212
defaults:
1313
repository: "phpunit/phpunit"
1414

15+
health:
16+
path: /health
17+
controller: App\Controller\HealthController::health
18+
1519
show:
1620
path: /show/{repository}
1721
controller: App\Controller\HomeController::index
@@ -36,10 +40,14 @@ search_packagist:
3640

3741
# https://circleci.com/api/v1.1/project/github/PUGX/badge-poser?filter=completed&limit=1
3842
# https://circleci.com/api/v1.1/project/github/PUGX/badge-poser/tree/release%2Fv3.0.0?filter=completed&limit=1
39-
circleci_api:
43+
circleci_api_repository:
4044
path: /api/v1.1/project/github/{repository}/tree/{branch}
4145
host: "circleci.com"
4246
schemes: [https]
4347
requirements:
4448
repository: "[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+"
4549
branch: ".+"
50+
circleci_api_health:
51+
path: /api/v2/me
52+
host: "circleci.com"
53+
schemes: [https]

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ services:
3333
env_file:
3434
- ./.env
3535
- ./.env.local
36+
- ./.env.test
3637
working_dir: /application
3738
command: |
3839
/bin/sh -c "
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

src/Service/CircleCiClient.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,42 @@ public function __construct(private UrlGeneratorInterface $router, private HttpC
1414
{
1515
}
1616

17+
/**
18+
* @throws TransportExceptionInterface
19+
*/
20+
public function health(): ResponseInterface
21+
{
22+
$circleCiApiUrl = $this->router->generate('circleci_api_health');
23+
24+
return $this->httpClient->request(
25+
Request::METHOD_GET,
26+
$circleCiApiUrl,
27+
[
28+
'headers' => [
29+
'Accept' => 'application/json',
30+
'Circle-Token' => $this->circleToken,
31+
],
32+
]
33+
);
34+
}
35+
1736
/**
1837
* @throws TransportExceptionInterface
1938
*/
2039
public function getBuilds(string $repository, string $branch = 'master'): ResponseInterface
2140
{
22-
$circleCiApiUrl = $this->router->generate('circleci_api', ['repository' => $repository, 'branch' => \urlencode($branch)]);
41+
$circleCiApiUrl = $this->router->generate('circleci_api_repository', ['repository' => $repository, 'branch' => \urlencode($branch)]);
2342

2443
return $this->httpClient->request(
2544
Request::METHOD_GET,
2645
$circleCiApiUrl,
2746
[
2847
'headers' => [
2948
'Accept' => 'application/json',
30-
'Content-type' => 'application/json',
49+
'Content-Type' => 'application/json',
50+
'Circle-Token' => $this->circleToken,
3151
],
3252
'query' => [
33-
'circle-token' => $this->circleToken,
3453
'filter' => 'completed',
3554
'limit' => '1',
3655
],

src/Service/CircleCiClientInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66

77
interface CircleCiClientInterface
88
{
9+
public function health(): ResponseInterface;
10+
911
public function getBuilds(string $repository, string $branch = 'master'): ResponseInterface;
1012
}

src/Service/GitLabClient.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ public function __construct(private Client $gitlabClient, private string $gitLab
1212
$this->gitlabClient->authenticate($this->gitLabToken, Client::AUTH_HTTP_TOKEN);
1313
}
1414

15+
public function health(): array
16+
{
17+
$repositoryData = $this->gitlabClient->users()->user();
18+
if (!\is_array($repositoryData)) {
19+
throw new RepositoryDataNotValid('Repository data not valid: '.\json_encode($repositoryData));
20+
}
21+
22+
return $repositoryData;
23+
}
24+
1525
public function show(string $project): array
1626
{
1727
$repositoryData = $this->gitlabClient->projects()->show($project);

src/Service/GitLabClientInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
interface GitLabClientInterface
66
{
7+
/** @return array<string, mixed> */
8+
public function health(): array;
9+
710
/** @return array<string, mixed> */
811
public function show(string $project): array;
912
}

templates/health/index.txt.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
packagist: {{ packagist ? 'OK' : 'KO' }}
2+
circleci: {{ circleci ? 'OK' : 'KO' }}
3+
gitlab: {{ gitlab ? 'OK' : 'KO' }}
4+
bitbucket: {{ bitbucket ? 'OK' : 'KO' }}

0 commit comments

Comments
 (0)