From 03986732d3863b7cdfaeb4009ee030ae9277022d Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Sat, 9 Mar 2024 21:38:26 -0300 Subject: [PATCH 1/8] Adds a Buggregator service --- app/Services/Buggregator.php | 40 ++++++++++++++++++++++++++++ app/Shell/GitHubDockerTags.php | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 app/Services/Buggregator.php create mode 100644 app/Shell/GitHubDockerTags.php diff --git a/app/Services/Buggregator.php b/app/Services/Buggregator.php new file mode 100644 index 00000000..bc5a2d76 --- /dev/null +++ b/app/Services/Buggregator.php @@ -0,0 +1,40 @@ + 'smtp_port', + 'prompt' => 'What is the SMTP port?', + 'default' => '1025', + ], + [ + 'shortname' => 'var_dumper_port', + 'prompt' => 'What is the VarDumper server port?', + 'default' => '9912', + ], + [ + 'shortname' => 'monolog_port', + 'prompt' => 'What is the Monolog port?', + 'default' => '9913', + ], + ]; + + protected $dockerRunTemplate = '-p "${:port}":8000 \ + -p "${:smtp_port}":1025 \ + -p "${:var_dumper_port}":9912 \ + -p "${:monolog_port}":9913 \ + --network-alias dump-server \ + "${:organization}"/"${:image_name}":"${:tag}"'; +} diff --git a/app/Shell/GitHubDockerTags.php b/app/Shell/GitHubDockerTags.php new file mode 100644 index 00000000..ea77c406 --- /dev/null +++ b/app/Shell/GitHubDockerTags.php @@ -0,0 +1,48 @@ +getToken(); + + return $this->guzzle + ->get($this->buildTagsUrl(), [ + 'headers' => [ + 'Authorization' => "Bearer {$token}", + ], + ]) + ->getBody(); + } + + public function getTags(): Collection + { + return collect(json_decode($this->getTagsResponse(), true)['tags']); + } + + protected function getToken(): string + { + $image = $this->service->imageName(); + + $response = $this->guzzle->get("https://ghcr.io/token?" . http_build_query([ + 'scope' => "repository:{$image}:pull", + ])); + + if ($response->getStatusCode() !== 200) { + throw new RuntimeException("Something went wrong getting the Token from GitHub's registry."); + } + + return json_decode($response->getBody(), true)['token']; + } + + protected function tagsUrlTemplate(): string + { + return 'https://%s/v2/%s/tags/list'; + } +} From d1e798c4c77381bef50bd0ce8c84ee16b85f7d4a Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Sat, 9 Mar 2024 21:44:33 -0300 Subject: [PATCH 2/8] Adds the network alias for ease calling this service from other containers on the same network --- app/Services/Buggregator.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Services/Buggregator.php b/app/Services/Buggregator.php index bc5a2d76..705775e3 100644 --- a/app/Services/Buggregator.php +++ b/app/Services/Buggregator.php @@ -29,12 +29,17 @@ class Buggregator extends BaseService 'prompt' => 'What is the Monolog port?', 'default' => '9913', ], + [ + 'shortname' => 'network_alias', + 'prompt' => 'What network alias to you want to assign to this container? This alias can be used by other services on the same network.', + 'default' => 'dump-server', + ], ]; protected $dockerRunTemplate = '-p "${:port}":8000 \ -p "${:smtp_port}":1025 \ -p "${:var_dumper_port}":9912 \ -p "${:monolog_port}":9913 \ - --network-alias dump-server \ + --network-alias "${:network_alias}" \ "${:organization}"/"${:image_name}":"${:tag}"'; } From 24db3d4a7f1345c3b27a61ba00a0fecd69423ec2 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Sat, 9 Mar 2024 21:55:32 -0300 Subject: [PATCH 3/8] Tweaks the alias and resolving the latest tag --- app/Services/Buggregator.php | 2 +- app/Shell/GitHubDockerTags.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Services/Buggregator.php b/app/Services/Buggregator.php index 705775e3..be517996 100644 --- a/app/Services/Buggregator.php +++ b/app/Services/Buggregator.php @@ -32,7 +32,7 @@ class Buggregator extends BaseService [ 'shortname' => 'network_alias', 'prompt' => 'What network alias to you want to assign to this container? This alias can be used by other services on the same network.', - 'default' => 'dump-server', + 'default' => 'buggregator', ], ]; diff --git a/app/Shell/GitHubDockerTags.php b/app/Shell/GitHubDockerTags.php index ea77c406..e8d2a229 100644 --- a/app/Shell/GitHubDockerTags.php +++ b/app/Shell/GitHubDockerTags.php @@ -26,6 +26,11 @@ public function getTags(): Collection return collect(json_decode($this->getTagsResponse(), true)['tags']); } + public function getLatestTag(): string + { + return $this->getTags()->first(); + } + protected function getToken(): string { $image = $this->service->imageName(); From 8919bcc544e313676f539544d1b202c5f04a089b Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Sat, 9 Mar 2024 22:34:18 -0300 Subject: [PATCH 4/8] Lint --- app/Shell/GitHubDockerTags.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Shell/GitHubDockerTags.php b/app/Shell/GitHubDockerTags.php index e8d2a229..0e6a8ae8 100644 --- a/app/Shell/GitHubDockerTags.php +++ b/app/Shell/GitHubDockerTags.php @@ -35,7 +35,7 @@ protected function getToken(): string { $image = $this->service->imageName(); - $response = $this->guzzle->get("https://ghcr.io/token?" . http_build_query([ + $response = $this->guzzle->get('https://ghcr.io/token?' . http_build_query([ 'scope' => "repository:{$image}:pull", ])); From b11caee2e718f5abc3ad17e93d87a0c3590595cb Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Sat, 9 Mar 2024 22:36:04 -0300 Subject: [PATCH 5/8] Lint --- app/Shell/GitHubDockerTags.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/Shell/GitHubDockerTags.php b/app/Shell/GitHubDockerTags.php index 0e6a8ae8..c0176c51 100644 --- a/app/Shell/GitHubDockerTags.php +++ b/app/Shell/GitHubDockerTags.php @@ -8,6 +8,16 @@ class GitHubDockerTags extends DockerTags { + public function getTags(): Collection + { + return collect(json_decode($this->getTagsResponse(), true)['tags']); + } + + public function getLatestTag(): string + { + return $this->getTags()->first(); + } + protected function getTagsResponse(): StreamInterface { $token = $this->getToken(); @@ -21,16 +31,6 @@ protected function getTagsResponse(): StreamInterface ->getBody(); } - public function getTags(): Collection - { - return collect(json_decode($this->getTagsResponse(), true)['tags']); - } - - public function getLatestTag(): string - { - return $this->getTags()->first(); - } - protected function getToken(): string { $image = $this->service->imageName(); From dca0ce9754ada774633f4a441782a95881de467f Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Sun, 10 Mar 2024 11:46:29 -0300 Subject: [PATCH 6/8] Adds tests for the GitHub tags --- app/Shell/GitHubDockerTags.php | 4 ++- tests/Feature/GitHubTagsTest.php | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/GitHubTagsTest.php diff --git a/app/Shell/GitHubDockerTags.php b/app/Shell/GitHubDockerTags.php index c0176c51..b4956650 100644 --- a/app/Shell/GitHubDockerTags.php +++ b/app/Shell/GitHubDockerTags.php @@ -37,7 +37,9 @@ protected function getToken(): string $response = $this->guzzle->get('https://ghcr.io/token?' . http_build_query([ 'scope' => "repository:{$image}:pull", - ])); + ]), [ + "http_errors" => false, + ]); if ($response->getStatusCode() !== 200) { throw new RuntimeException("Something went wrong getting the Token from GitHub's registry."); diff --git a/tests/Feature/GitHubTagsTest.php b/tests/Feature/GitHubTagsTest.php new file mode 100644 index 00000000..6ad9f302 --- /dev/null +++ b/tests/Feature/GitHubTagsTest.php @@ -0,0 +1,59 @@ +mockImagesResponseHandler()); + $client = new Client(['handler' => $handlerStack]); + + /** @var DockerTags $dockerTags */ + $dockerTags = M::mock(GitHubDockerTags::class, [$client, app(Buggregator::class)])->makePartial(); + + $this->assertEquals('latest', $dockerTags->getLatestTag()); + } + + /** @test */ + function it_throws_exception_when_token_request_fails() + { + $handlerStack = HandlerStack::create($this->mockImagesResponseHandler(false)); + $client = new Client(['handler' => $handlerStack]); + + /** @var DockerTags $dockerTags */ + $dockerTags = M::mock(GitHubDockerTags::class, [$client, app(Buggregator::class)])->makePartial(); + + $this->expectException(RuntimeException::class); + + $dockerTags->getLatestTag(); + } + + private function mockImagesResponseHandler($tokenWorks = true) + { + return new MockHandler([ + new Response($tokenWorks ? 200 : 400, [], json_encode([ + 'token' => 'fake-token', + ])), + new Response(200, [], json_encode([ + 'tags' => [ + 'latest', + '1.0.0', + '1.0.0.rc-1', + ], + ])), + ]); + } +} From 594c4e305795609971746978dad73f11de576936 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Sun, 10 Mar 2024 11:47:57 -0300 Subject: [PATCH 7/8] Lint --- app/Shell/GitHubDockerTags.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Shell/GitHubDockerTags.php b/app/Shell/GitHubDockerTags.php index b4956650..b9a64e8e 100644 --- a/app/Shell/GitHubDockerTags.php +++ b/app/Shell/GitHubDockerTags.php @@ -38,7 +38,7 @@ protected function getToken(): string $response = $this->guzzle->get('https://ghcr.io/token?' . http_build_query([ 'scope' => "repository:{$image}:pull", ]), [ - "http_errors" => false, + 'http_errors' => false, ]); if ($response->getStatusCode() !== 200) { From eb4ebd47506de166d9045941b6d75159ce8875e9 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Sun, 10 Mar 2024 11:51:52 -0300 Subject: [PATCH 8/8] Lint --- tests/Feature/GitHubTagsTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/Feature/GitHubTagsTest.php b/tests/Feature/GitHubTagsTest.php index 6ad9f302..a7d2b3db 100644 --- a/tests/Feature/GitHubTagsTest.php +++ b/tests/Feature/GitHubTagsTest.php @@ -3,7 +3,6 @@ namespace Tests\Feature; use App\Services\Buggregator; -use App\Shell\DockerTags; use App\Shell\GitHubDockerTags; use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; @@ -21,7 +20,7 @@ function it_fetches_latest_tag() $handlerStack = HandlerStack::create($this->mockImagesResponseHandler()); $client = new Client(['handler' => $handlerStack]); - /** @var DockerTags $dockerTags */ + /** @var GitHubDockerTags $dockerTags */ $dockerTags = M::mock(GitHubDockerTags::class, [$client, app(Buggregator::class)])->makePartial(); $this->assertEquals('latest', $dockerTags->getLatestTag()); @@ -33,7 +32,7 @@ function it_throws_exception_when_token_request_fails() $handlerStack = HandlerStack::create($this->mockImagesResponseHandler(false)); $client = new Client(['handler' => $handlerStack]); - /** @var DockerTags $dockerTags */ + /** @var GitHubDockerTags $dockerTags */ $dockerTags = M::mock(GitHubDockerTags::class, [$client, app(Buggregator::class)])->makePartial(); $this->expectException(RuntimeException::class);