From 2c9011f8be3ccb115fdd1b45a2edda96ceaba8ee Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Tue, 19 Sep 2023 17:39:15 -0500 Subject: [PATCH 01/18] feat: added changelog to the newrelic deploy information --- src/Helpers/Changelog.php | 22 ++++++++++++++++++++++ src/NewRelic/NewRelicApi.php | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/Helpers/Changelog.php diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php new file mode 100644 index 0000000..128a183 --- /dev/null +++ b/src/Helpers/Changelog.php @@ -0,0 +1,22 @@ +client->post($this->constructUrl(), [ 'deployment' => [ 'revision' => $version, - 'changelog' => 'Not available right now', + 'changelog' => Changelog::read(), 'description' => 'Commit on ' . $environment, 'user' => 'Not available right now', ], From 38c484273b6e50ba2cd75790e414df7c47a1b3eb Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Wed, 20 Sep 2023 10:54:11 -0500 Subject: [PATCH 02/18] test: test case for newrelic deploy --- src/Helpers/Changelog.php | 9 +++++--- tests/Commands/CreateDeployCommandTest.php | 26 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index 128a183..f288f80 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -4,18 +4,21 @@ class Changelog { - protected const PATH = 'CHANGELOG.md'; + public static function path(): string + { + return app_path('CHANGELOG.md'); + } public static function exists(): bool { - return file_exists(self::PATH); + return file_exists(self::path()); } public static function read(): string { $content = 'Not available right now'; if (self::exists()) { - $content = file_get_contents(self::PATH); + $content = file_get_contents(self::path()); } return $content; } diff --git a/tests/Commands/CreateDeployCommandTest.php b/tests/Commands/CreateDeployCommandTest.php index a02169a..9c8ff78 100644 --- a/tests/Commands/CreateDeployCommandTest.php +++ b/tests/Commands/CreateDeployCommandTest.php @@ -2,6 +2,7 @@ namespace PlacetoPay\AppVersion\Tests\Commands; +use PlacetoPay\AppVersion\Helpers\Changelog; use PlacetoPay\AppVersion\Tests\Mocks\InteractsWithFakeClient; use PlacetoPay\AppVersion\Tests\TestCase; @@ -43,4 +44,29 @@ public function can_create_a_release_for_newrelic() $this->assertEquals($this->fakeClient->lastRequest()['headers'][0], 'X-Api-Key: ' . config('app-version.newrelic.api_key')); } + + public function test_create_newrelic_deploy_with_changelog(): void + { + file_put_contents(Changelog::path(), 'TEST'); + + $this->setNewRelicEnvironmentSetUp(); + $this->bindNewRelicFakeClient(); + $this->fakeClient->push('success_deploy'); + + $this->artisan('app-version:create-deploy')->assertExitCode(0); + + $this->fakeClient->assertLastRequestHas('deployment', [ + 'revision' => 'asdfg2', + 'changelog' => 'TEST', + 'description' => 'Commit on testing', + 'user' => 'Not available right now', + ]); + + $this->assertEquals( + $this->fakeClient->lastRequest()['headers'][0], + 'X-Api-Key: ' . config('app-version.newrelic.api_key') + ); + + unlink(Changelog::path()); + } } From 96b779881890a31f213d3ecf78a0efcd2f0cb4e8 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Wed, 20 Sep 2023 14:43:15 -0500 Subject: [PATCH 03/18] fix: Changelog path --- src/Helpers/Changelog.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index f288f80..15bba74 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -6,7 +6,7 @@ class Changelog { public static function path(): string { - return app_path('CHANGELOG.md'); + return base_path('CHANGELOG.md'); } public static function exists(): bool From 9a0ce9877859cb26596784910c475b56f5bfc49b Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Wed, 20 Sep 2023 15:37:37 -0500 Subject: [PATCH 04/18] fix: Changelog line breaks --- src/Helpers/Changelog.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index 15bba74..2c23f46 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -18,7 +18,8 @@ public static function read(): string { $content = 'Not available right now'; if (self::exists()) { - $content = file_get_contents(self::path()); + // The line breaks replacement is necessary to see it properly on the Newrelic panel + $content = str_replace("\n", '\n\n', file_get_contents(self::path())); } return $content; } From e60811bdb1a142fa7ad80bf5a329b09bfd472f62 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Wed, 20 Sep 2023 17:24:50 -0500 Subject: [PATCH 05/18] fix: changelog characters limit for newrelic --- src/Helpers/Changelog.php | 8 +++++--- src/NewRelic/NewRelicApi.php | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index 2c23f46..0a77bdd 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -14,12 +14,14 @@ public static function exists(): bool return file_exists(self::path()); } - public static function read(): string + public static function read(int $charactersLimit): string { $content = 'Not available right now'; if (self::exists()) { - // The line breaks replacement is necessary to see it properly on the Newrelic panel - $content = str_replace("\n", '\n\n', file_get_contents(self::path())); + $content = file_get_contents(self::path()); + if (strlen($content) > $charactersLimit) { + $content = substr($content, 0, $charactersLimit); + } } return $content; } diff --git a/src/NewRelic/NewRelicApi.php b/src/NewRelic/NewRelicApi.php index d079db8..96bf64d 100644 --- a/src/NewRelic/NewRelicApi.php +++ b/src/NewRelic/NewRelicApi.php @@ -21,6 +21,8 @@ class NewRelicApi */ private $applicationId; + private int $characterLimit = 4095; + public function __construct(HttpClient $client, string $apiKey, string $applicationId) { $this->client = $client; @@ -47,7 +49,7 @@ public function createDeploy(string $version, string $environment) return $this->client->post($this->constructUrl(), [ 'deployment' => [ 'revision' => $version, - 'changelog' => Changelog::read(), + 'changelog' => Changelog::read($this->characterLimit), 'description' => 'Commit on ' . $environment, 'user' => 'Not available right now', ], From 0e25f405696752f6943efdbfa46f9387a72afbcc Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Thu, 21 Sep 2023 16:16:09 -0500 Subject: [PATCH 06/18] fix: default charactersLimit value --- src/Helpers/Changelog.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index 0a77bdd..d38791d 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -14,12 +14,12 @@ public static function exists(): bool return file_exists(self::path()); } - public static function read(int $charactersLimit): string + public static function read(int $charactersLimit = 0): string { $content = 'Not available right now'; if (self::exists()) { $content = file_get_contents(self::path()); - if (strlen($content) > $charactersLimit) { + if ($charactersLimit > 0 && strlen($content) > $charactersLimit) { $content = substr($content, 0, $charactersLimit); } } From c20dbb7cfccaf9cea02f9c88d626a9a13ec7faea Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Mon, 25 Sep 2023 17:36:33 -0500 Subject: [PATCH 07/18] feat: send the last version in the changelog to newrelic --- src/Helpers/Changelog.php | 11 ++++-- src/NewRelic/NewRelicApi.php | 4 +-- tests/Commands/CreateDeployCommandTest.php | 42 ++++++++++++++++++++-- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index d38791d..93eff03 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -2,6 +2,9 @@ namespace PlacetoPay\AppVersion\Helpers; +/** + * The Changelog format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). + */ class Changelog { public static function path(): string @@ -14,15 +17,17 @@ public static function exists(): bool return file_exists(self::path()); } - public static function read(int $charactersLimit = 0): string + public static function read(): string { $content = 'Not available right now'; if (self::exists()) { $content = file_get_contents(self::path()); - if ($charactersLimit > 0 && strlen($content) > $charactersLimit) { - $content = substr($content, 0, $charactersLimit); + preg_match_all("/##\s\[\d/", $content, $matches, PREG_OFFSET_CAPTURE); + if (count($matches) === 1 && count($matches[0]) > 1) { + $content = substr($content, 0, $matches[0][1][1]); } } + return $content; } } diff --git a/src/NewRelic/NewRelicApi.php b/src/NewRelic/NewRelicApi.php index 96bf64d..d079db8 100644 --- a/src/NewRelic/NewRelicApi.php +++ b/src/NewRelic/NewRelicApi.php @@ -21,8 +21,6 @@ class NewRelicApi */ private $applicationId; - private int $characterLimit = 4095; - public function __construct(HttpClient $client, string $apiKey, string $applicationId) { $this->client = $client; @@ -49,7 +47,7 @@ public function createDeploy(string $version, string $environment) return $this->client->post($this->constructUrl(), [ 'deployment' => [ 'revision' => $version, - 'changelog' => Changelog::read($this->characterLimit), + 'changelog' => Changelog::read(), 'description' => 'Commit on ' . $environment, 'user' => 'Not available right now', ], diff --git a/tests/Commands/CreateDeployCommandTest.php b/tests/Commands/CreateDeployCommandTest.php index 9c8ff78..aaf5444 100644 --- a/tests/Commands/CreateDeployCommandTest.php +++ b/tests/Commands/CreateDeployCommandTest.php @@ -45,9 +45,45 @@ public function can_create_a_release_for_newrelic() $this->assertEquals($this->fakeClient->lastRequest()['headers'][0], 'X-Api-Key: ' . config('app-version.newrelic.api_key')); } - public function test_create_newrelic_deploy_with_changelog(): void + public function test_create_newrelic_deploy_with_last_changelog(): void { - file_put_contents(Changelog::path(), 'TEST'); + $changelogContent = '# Changelog + All notable changes to this project will be documented in this file. + + The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), + and this project adheres to [Semantic Versioning](https://semver.org/). + + ## [Unreleased] + + ## [0.0.2 (2023-09-25)](https://bitbucket.org/) + + ### Updated + - Change number 1. + - Change number 2. [TK-002](https://app.clickup.com/) + + ## [0.0.1 (2023-09-10)](https://bitbucket.org/) + + ### Fixed + + - Fix api parameters'; + + $expectedChangelogContent = '# Changelog + All notable changes to this project will be documented in this file. + + The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), + and this project adheres to [Semantic Versioning](https://semver.org/). + + ## [Unreleased] + + ## [0.0.2 (2023-09-25)](https://bitbucket.org/) + + ### Updated + - Change number 1. + - Change number 2. [TK-002](https://app.clickup.com/) + + '; + + file_put_contents(Changelog::path(), $changelogContent); $this->setNewRelicEnvironmentSetUp(); $this->bindNewRelicFakeClient(); @@ -57,7 +93,7 @@ public function test_create_newrelic_deploy_with_changelog(): void $this->fakeClient->assertLastRequestHas('deployment', [ 'revision' => 'asdfg2', - 'changelog' => 'TEST', + 'changelog' => $expectedChangelogContent, 'description' => 'Commit on testing', 'user' => 'Not available right now', ]); From fbc126deed31e484ab547a9c701f3e5db347d962 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Mon, 25 Sep 2023 17:48:35 -0500 Subject: [PATCH 08/18] refactor: code structure --- src/Helpers/Changelog.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index 93eff03..9e22234 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -21,11 +21,21 @@ public static function read(): string { $content = 'Not available right now'; if (self::exists()) { - $content = file_get_contents(self::path()); - preg_match_all("/##\s\[\d/", $content, $matches, PREG_OFFSET_CAPTURE); - if (count($matches) === 1 && count($matches[0]) > 1) { - $content = substr($content, 0, $matches[0][1][1]); - } + $content = self::getLastVersion(file_get_contents(self::path())); + } + + return $content; + } + + /** + * Returns the content up to the beginning of the second H2 of the Markdown + * that meets the following regular expression: /##\s\[\d/ . + */ + private static function getLastVersion(string $content): string + { + preg_match_all("/##\s\[\d/", $content, $matches, PREG_OFFSET_CAPTURE); + if (count($matches) === 1 && count($matches[0]) > 1) { + $content = substr($content, 0, $matches[0][1][1]); } return $content; From 79c695e1d5cec9fbe9e01b1b484ffbfc678f078f Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Mon, 2 Oct 2023 17:55:17 -0500 Subject: [PATCH 09/18] test: add tests to newrelic deploy --- src/Helpers/Changelog.php | 9 +- tests/Commands/CreateDeployCommandTest.php | 108 ++++++++++++++++++++- 2 files changed, 108 insertions(+), 9 deletions(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index 9e22234..e3d746f 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -7,6 +7,8 @@ */ class Changelog { + public const H2_REGEX = "/##\s\[\d/"; + public static function path(): string { return base_path('CHANGELOG.md'); @@ -19,7 +21,7 @@ public static function exists(): bool public static function read(): string { - $content = 'Not available right now'; + $content = 'Not Available'; if (self::exists()) { $content = self::getLastVersion(file_get_contents(self::path())); } @@ -28,12 +30,11 @@ public static function read(): string } /** - * Returns the content up to the beginning of the second H2 of the Markdown - * that meets the following regular expression: /##\s\[\d/ . + * Returns the content up to the beginning of the second H2 of the Markdown that meets the H2_REGEX. */ private static function getLastVersion(string $content): string { - preg_match_all("/##\s\[\d/", $content, $matches, PREG_OFFSET_CAPTURE); + preg_match_all(self::H2_REGEX, $content, $matches, PREG_OFFSET_CAPTURE); if (count($matches) === 1 && count($matches[0]) > 1) { $content = substr($content, 0, $matches[0][1][1]); } diff --git a/tests/Commands/CreateDeployCommandTest.php b/tests/Commands/CreateDeployCommandTest.php index aaf5444..c3e269b 100644 --- a/tests/Commands/CreateDeployCommandTest.php +++ b/tests/Commands/CreateDeployCommandTest.php @@ -11,7 +11,7 @@ class CreateDeployCommandTest extends TestCase use InteractsWithFakeClient; /** @test */ - public function can_create_a_release_for_sentry() + public function can_create_a_release_for_sentry(): void { $this->setSentryEnvironmentSetUp(); @@ -26,7 +26,7 @@ public function can_create_a_release_for_sentry() } /** @test */ - public function can_create_a_release_for_newrelic() + public function can_create_a_deploy_for_newrelic_without_changelog(): void { $this->setNewRelicEnvironmentSetUp(); @@ -37,15 +37,113 @@ public function can_create_a_release_for_newrelic() $this->fakeClient->assertLastRequestHas('deployment', [ 'revision' => 'asdfg2', - 'changelog' => 'Not available right now', + 'changelog' => 'Not Available', 'description' => 'Commit on testing', 'user' => 'Not available right now', ]); - $this->assertEquals($this->fakeClient->lastRequest()['headers'][0], 'X-Api-Key: ' . config('app-version.newrelic.api_key')); + $this->assertEquals( + $this->fakeClient->lastRequest()['headers'][0], + 'X-Api-Key: ' . config('app-version.newrelic.api_key') + ); + } + + /** @test */ + public function can_create_a_newrelic_deploy_with_wrong_changelog_name(): void + { + $changelogContent = '# Changelog + All notable changes to this project will be documented in this file. + + The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), + and this project adheres to [Semantic Versioning](https://semver.org/). + + ## [Unreleased] + + ## [0.0.2 (2023-09-25)](https://bitbucket.org/) + + ### Updated + - Change number 1. + '; + + $name = str_replace('CHANGELOG.md', 'changelog.md', Changelog::path()); + + file_put_contents($name, $changelogContent); + + $this->setNewRelicEnvironmentSetUp(); + $this->bindNewRelicFakeClient(); + $this->fakeClient->push('success_deploy'); + + $this->artisan('app-version:create-deploy')->assertExitCode(0); + + $this->fakeClient->assertLastRequestHas('deployment', [ + 'revision' => 'asdfg2', + 'changelog' => 'Not Available', + 'description' => 'Commit on testing', + 'user' => 'Not available right now', + ]); + + $this->assertEquals( + $this->fakeClient->lastRequest()['headers'][0], + 'X-Api-Key: ' . config('app-version.newrelic.api_key') + ); + + unlink($name); + } + + /** @test */ + public function can_create_a_newrelic_deploy_with_wrong_format(): void + { + $changelogContent = '# Changelog + All notable changes to this project will be documented in this file. + + The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), + and this project adheres to [Semantic Versioning](https://semver.org/). + + ## [Unreleased] + + ### 0.0.3 (2023-09-25)(https://bitbucket.org/) + + ### Updated + - Change number 1. + + ### [0.0.2 (2023-09-25)](https://bitbucket.org/) + + ### Updated + - Change number 1. + - Change number 2. [TK-002](https://app.clickup.com/) + + ## (0.0.1 (2023-09-10))(https://bitbucket.org/) + + ### Fixed + + - Fix api parameters + '; + + file_put_contents(Changelog::path(), $changelogContent); + + $this->setNewRelicEnvironmentSetUp(); + $this->bindNewRelicFakeClient(); + $this->fakeClient->push('success_deploy'); + + $this->artisan('app-version:create-deploy')->assertExitCode(0); + + $this->fakeClient->assertLastRequestHas('deployment', [ + 'revision' => 'asdfg2', + 'changelog' => $changelogContent, + 'description' => 'Commit on testing', + 'user' => 'Not available right now', + ]); + + $this->assertEquals( + $this->fakeClient->lastRequest()['headers'][0], + 'X-Api-Key: ' . config('app-version.newrelic.api_key') + ); + + unlink(Changelog::path()); } - public function test_create_newrelic_deploy_with_last_changelog(): void + /** @test */ + public function can_create_a_newrelic_deploy_with_last_changelog(): void { $changelogContent = '# Changelog All notable changes to this project will be documented in this file. From 4f76a0b0513b3024df1419f81185860c2ea121c2 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Tue, 3 Oct 2023 13:56:41 -0500 Subject: [PATCH 10/18] refactor: newrelic create deploy tests --- readme.md | 2 + src/Helpers/Changelog.php | 33 +++--- tests/Commands/CreateDeployCommandTest.php | 115 +++++---------------- tests/Mocks/CHANGELOG.md | 19 ++++ tests/Mocks/WRONG-CHANGELOG.md | 24 +++++ 5 files changed, 92 insertions(+), 101 deletions(-) create mode 100644 tests/Mocks/CHANGELOG.md create mode 100644 tests/Mocks/WRONG-CHANGELOG.md diff --git a/readme.md b/readme.md index dd987ea..bbf3485 100644 --- a/readme.md +++ b/readme.md @@ -142,3 +142,5 @@ To access the sha ```php PlacetoPay\AppVersion\VersionFile::readSha() ``` +### Note +The Newrelic deploy will read the CHANGELOG.md as long as it meets the following standard [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). \ No newline at end of file diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index e3d746f..5ca77eb 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -9,36 +9,45 @@ class Changelog { public const H2_REGEX = "/##\s\[\d/"; + public const DEFAULT_MESSAGE = 'Not Available'; + + public const FILENAME_REGEX = "/(?i)changelog(?-i)\.md/"; + public static function path(): string { - return base_path('CHANGELOG.md'); - } + $basePath = base_path(); + $files = scandir($basePath); - public static function exists(): bool - { - return file_exists(self::path()); + $matches = preg_grep(self::FILENAME_REGEX, $files); + if ($matches) { + return $basePath . DIRECTORY_SEPARATOR . array_pop($matches); + } + + return ''; } public static function read(): string { - $content = 'Not Available'; - if (self::exists()) { - $content = self::getLastVersion(file_get_contents(self::path())); + $path = self::path(); + if ($path) { + return self::getLastVersion(file_get_contents($path)); } - return $content; + return self::DEFAULT_MESSAGE; } /** - * Returns the content up to the beginning of the second H2 of the Markdown that meets the H2_REGEX. + * Returns the content from the first H2 to the beginning of the second H2 that meets the H2_REGEX. */ private static function getLastVersion(string $content): string { preg_match_all(self::H2_REGEX, $content, $matches, PREG_OFFSET_CAPTURE); if (count($matches) === 1 && count($matches[0]) > 1) { - $content = substr($content, 0, $matches[0][1][1]); + $firstH2Position = $matches[0][0][1]; + $secondH2Position = $matches[0][1][1]; + return substr($content, $firstH2Position, $secondH2Position - $firstH2Position); } - return $content; + return self::DEFAULT_MESSAGE; } } diff --git a/tests/Commands/CreateDeployCommandTest.php b/tests/Commands/CreateDeployCommandTest.php index c3e269b..6768d2f 100644 --- a/tests/Commands/CreateDeployCommandTest.php +++ b/tests/Commands/CreateDeployCommandTest.php @@ -2,7 +2,6 @@ namespace PlacetoPay\AppVersion\Tests\Commands; -use PlacetoPay\AppVersion\Helpers\Changelog; use PlacetoPay\AppVersion\Tests\Mocks\InteractsWithFakeClient; use PlacetoPay\AppVersion\Tests\TestCase; @@ -10,6 +9,19 @@ class CreateDeployCommandTest extends TestCase { use InteractsWithFakeClient; + protected function setUp(): void + { + parent::setUp(); + $this->expectedChangelog = '## [0.0.2 (2023-09-25)](https://bitbucket.org/) + +### Updated +- Change number 1. +- Change number 2. [TK-002](https://app.clickup.com/) + +'; + $this->expectedDefaultMessage = 'Not Available'; + } + /** @test */ public function can_create_a_release_for_sentry(): void { @@ -37,7 +49,7 @@ public function can_create_a_deploy_for_newrelic_without_changelog(): void $this->fakeClient->assertLastRequestHas('deployment', [ 'revision' => 'asdfg2', - 'changelog' => 'Not Available', + 'changelog' => $this->expectedDefaultMessage, 'description' => 'Commit on testing', 'user' => 'Not available right now', ]); @@ -51,23 +63,8 @@ public function can_create_a_deploy_for_newrelic_without_changelog(): void /** @test */ public function can_create_a_newrelic_deploy_with_wrong_changelog_name(): void { - $changelogContent = '# Changelog - All notable changes to this project will be documented in this file. - - The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - and this project adheres to [Semantic Versioning](https://semver.org/). - - ## [Unreleased] - - ## [0.0.2 (2023-09-25)](https://bitbucket.org/) - - ### Updated - - Change number 1. - '; - - $name = str_replace('CHANGELOG.md', 'changelog.md', Changelog::path()); - - file_put_contents($name, $changelogContent); + $changelogName = base_path('changelog.md'); + copy('tests/Mocks/CHANGELOG.md', $changelogName); $this->setNewRelicEnvironmentSetUp(); $this->bindNewRelicFakeClient(); @@ -77,7 +74,7 @@ public function can_create_a_newrelic_deploy_with_wrong_changelog_name(): void $this->fakeClient->assertLastRequestHas('deployment', [ 'revision' => 'asdfg2', - 'changelog' => 'Not Available', + 'changelog' => $this->expectedChangelog, 'description' => 'Commit on testing', 'user' => 'Not available right now', ]); @@ -87,39 +84,14 @@ public function can_create_a_newrelic_deploy_with_wrong_changelog_name(): void 'X-Api-Key: ' . config('app-version.newrelic.api_key') ); - unlink($name); + unlink($changelogName); } /** @test */ public function can_create_a_newrelic_deploy_with_wrong_format(): void { - $changelogContent = '# Changelog - All notable changes to this project will be documented in this file. - - The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - and this project adheres to [Semantic Versioning](https://semver.org/). - - ## [Unreleased] - - ### 0.0.3 (2023-09-25)(https://bitbucket.org/) - - ### Updated - - Change number 1. - - ### [0.0.2 (2023-09-25)](https://bitbucket.org/) - - ### Updated - - Change number 1. - - Change number 2. [TK-002](https://app.clickup.com/) - - ## (0.0.1 (2023-09-10))(https://bitbucket.org/) - - ### Fixed - - - Fix api parameters - '; - - file_put_contents(Changelog::path(), $changelogContent); + $changelogName = base_path('CHANGELOG.md'); + copy('tests/Mocks/WRONG-CHANGELOG.md', $changelogName); $this->setNewRelicEnvironmentSetUp(); $this->bindNewRelicFakeClient(); @@ -129,7 +101,7 @@ public function can_create_a_newrelic_deploy_with_wrong_format(): void $this->fakeClient->assertLastRequestHas('deployment', [ 'revision' => 'asdfg2', - 'changelog' => $changelogContent, + 'changelog' => $this->expectedDefaultMessage, 'description' => 'Commit on testing', 'user' => 'Not available right now', ]); @@ -139,49 +111,14 @@ public function can_create_a_newrelic_deploy_with_wrong_format(): void 'X-Api-Key: ' . config('app-version.newrelic.api_key') ); - unlink(Changelog::path()); + unlink($changelogName); } /** @test */ public function can_create_a_newrelic_deploy_with_last_changelog(): void { - $changelogContent = '# Changelog - All notable changes to this project will be documented in this file. - - The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - and this project adheres to [Semantic Versioning](https://semver.org/). - - ## [Unreleased] - - ## [0.0.2 (2023-09-25)](https://bitbucket.org/) - - ### Updated - - Change number 1. - - Change number 2. [TK-002](https://app.clickup.com/) - - ## [0.0.1 (2023-09-10)](https://bitbucket.org/) - - ### Fixed - - - Fix api parameters'; - - $expectedChangelogContent = '# Changelog - All notable changes to this project will be documented in this file. - - The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - and this project adheres to [Semantic Versioning](https://semver.org/). - - ## [Unreleased] - - ## [0.0.2 (2023-09-25)](https://bitbucket.org/) - - ### Updated - - Change number 1. - - Change number 2. [TK-002](https://app.clickup.com/) - - '; - - file_put_contents(Changelog::path(), $changelogContent); + $changelogName = base_path('CHANGELOG.md'); + copy('tests/Mocks/CHANGELOG.md', $changelogName); $this->setNewRelicEnvironmentSetUp(); $this->bindNewRelicFakeClient(); @@ -191,7 +128,7 @@ public function can_create_a_newrelic_deploy_with_last_changelog(): void $this->fakeClient->assertLastRequestHas('deployment', [ 'revision' => 'asdfg2', - 'changelog' => $expectedChangelogContent, + 'changelog' => $this->expectedChangelog, 'description' => 'Commit on testing', 'user' => 'Not available right now', ]); @@ -201,6 +138,6 @@ public function can_create_a_newrelic_deploy_with_last_changelog(): void 'X-Api-Key: ' . config('app-version.newrelic.api_key') ); - unlink(Changelog::path()); + unlink($changelogName); } } diff --git a/tests/Mocks/CHANGELOG.md b/tests/Mocks/CHANGELOG.md new file mode 100644 index 0000000..3e6bf58 --- /dev/null +++ b/tests/Mocks/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/). + +## [Unreleased] + +## [0.0.2 (2023-09-25)](https://bitbucket.org/) + +### Updated +- Change number 1. +- Change number 2. [TK-002](https://app.clickup.com/) + +## [0.0.1 (2023-09-10)](https://bitbucket.org/) + +### Fixed + +- Fix api parameters \ No newline at end of file diff --git a/tests/Mocks/WRONG-CHANGELOG.md b/tests/Mocks/WRONG-CHANGELOG.md new file mode 100644 index 0000000..0b59c90 --- /dev/null +++ b/tests/Mocks/WRONG-CHANGELOG.md @@ -0,0 +1,24 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/). + +## [Unreleased] + +## [0.0.3 (2023-10-02)](https://bitbucket.org/) + +### Added +- Feature number 1. + +### [0.0.2 (2023-09-25)](https://bitbucket.org/) + +### Updated +- Change number 1. +- Change number 2. [TK-002](https://app.clickup.com/) + +## 0.0.1(https://bitbucket.org/) + +### Fixed + +- Fix api parameters \ No newline at end of file From 82c54e238d75994293b50dd1c46f40fda1e6f5b7 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Tue, 3 Oct 2023 14:38:30 -0500 Subject: [PATCH 11/18] build: github actions --- .github/workflows/package.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/workflows/package.yml diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml new file mode 100644 index 0000000..73ed58f --- /dev/null +++ b/.github/workflows/package.yml @@ -0,0 +1,29 @@ +name: Laravel Package + +on: + push: + branches: [ "feature/newrelic_deploy", "master" ] + pull_request: + branches: [ "feature/newrelic_deploy", "master" ] + +jobs: + laravel-tests: + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: PHP Setup + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + extensions: mbstring, dom, fileinfo, curl + coverage: xdebug + + - name: Install Dependencies + run: composer install -q --ignore-platform-reqs --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist + + - name: Execute tests (Unit and Feature tests) via PHPUnit + run: vendor/bin/phpunit \ No newline at end of file From 3592278520d1e5293ce1dc2dec33c4a5d9dbde26 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Tue, 3 Oct 2023 14:44:42 -0500 Subject: [PATCH 12/18] build: composer install on github actions --- .github/workflows/package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index 73ed58f..da17856 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -23,7 +23,7 @@ jobs: coverage: xdebug - name: Install Dependencies - run: composer install -q --ignore-platform-reqs --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist + run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist - name: Execute tests (Unit and Feature tests) via PHPUnit run: vendor/bin/phpunit \ No newline at end of file From d61496bad725934d4d706a44585b2b615e9204f5 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Tue, 3 Oct 2023 14:49:36 -0500 Subject: [PATCH 13/18] build: github actions for master branch --- .github/workflows/package.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index da17856..c48db1d 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -2,9 +2,9 @@ name: Laravel Package on: push: - branches: [ "feature/newrelic_deploy", "master" ] + branches: [ "master" ] pull_request: - branches: [ "feature/newrelic_deploy", "master" ] + branches: [ "master" ] jobs: laravel-tests: From d0e457fea90dbdf76c5c71a2816895f9aa590751 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Tue, 3 Oct 2023 16:36:08 -0500 Subject: [PATCH 14/18] build: PHP CS Fixer on github actions --- .github/workflows/package.yml | 3 +++ src/Console/Commands/CreateVersionFile.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index c48db1d..0b017e6 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -25,5 +25,8 @@ jobs: - name: Install Dependencies run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist + - name: PHP CS Fixer + run: vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php + - name: Execute tests (Unit and Feature tests) via PHPUnit run: vendor/bin/phpunit \ No newline at end of file diff --git a/src/Console/Commands/CreateVersionFile.php b/src/Console/Commands/CreateVersionFile.php index a72dd83..8b692a3 100644 --- a/src/Console/Commands/CreateVersionFile.php +++ b/src/Console/Commands/CreateVersionFile.php @@ -40,7 +40,7 @@ public function handle(Factory $validator): int VersionFile::generate([ 'sha' => $options['sha'] ?? exec('git rev-parse HEAD'), - 'time' => $options['time'] ?? date('c'), + 'time' => $options['time'] ?? date('c'), 'branch' => $options['branch'] ?? exec('git symbolic-ref -q --short HEAD'), 'version' => $options['tag'] ?? exec('git describe --tags'), ]); From 7e12508797c8d65573e7fa63a31f6663cadb6f9d Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Fri, 6 Oct 2023 17:13:51 -0500 Subject: [PATCH 15/18] build: run GitHub actions on push/pull_request for any branch --- .github/workflows/package.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index 0b017e6..7499028 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -1,11 +1,6 @@ name: Laravel Package -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - +on: [push, pull_request] jobs: laravel-tests: From 31611bb576e64cc28f4b04425febfde4037c7635 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Wed, 11 Oct 2023 12:33:45 -0500 Subject: [PATCH 16/18] feat: improved H2 regex to recognize v1.2.3 as a valid version --- src/Helpers/Changelog.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index 5ca77eb..0a72525 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -7,7 +7,7 @@ */ class Changelog { - public const H2_REGEX = "/##\s\[\d/"; + public const H2_REGEX = "/^##\s\[v?(\d+\.){2}\d+.*]/m"; public const DEFAULT_MESSAGE = 'Not Available'; @@ -42,7 +42,7 @@ public static function read(): string private static function getLastVersion(string $content): string { preg_match_all(self::H2_REGEX, $content, $matches, PREG_OFFSET_CAPTURE); - if (count($matches) === 1 && count($matches[0]) > 1) { + if (count($matches) === 2 && count($matches[0]) > 1) { $firstH2Position = $matches[0][0][1]; $secondH2Position = $matches[0][1][1]; return substr($content, $firstH2Position, $secondH2Position - $firstH2Position); From 4f8ad31568a660e546ac9ac22c41106af2f2e0b9 Mon Sep 17 00:00:00 2001 From: Kevin Vargas Date: Tue, 17 Oct 2023 09:14:40 -0500 Subject: [PATCH 17/18] build: add PHP CS Fixer flags --- .github/workflows/package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index 7499028..618a8c9 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -21,7 +21,7 @@ jobs: run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist - name: PHP CS Fixer - run: vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php + run: vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --stop-on-violation --using-cache=no - name: Execute tests (Unit and Feature tests) via PHPUnit run: vendor/bin/phpunit \ No newline at end of file From da02a498a7ff3ee7ffead40f7e8bdd7507c415de Mon Sep 17 00:00:00 2001 From: David Valbuena Date: Thu, 26 Oct 2023 10:14:17 -0500 Subject: [PATCH 18/18] update scopes of methods --- src/Helpers/Changelog.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Helpers/Changelog.php b/src/Helpers/Changelog.php index 0a72525..0b35d91 100644 --- a/src/Helpers/Changelog.php +++ b/src/Helpers/Changelog.php @@ -13,7 +13,18 @@ class Changelog public const FILENAME_REGEX = "/(?i)changelog(?-i)\.md/"; - public static function path(): string + public static function read(): string + { + $path = self::findFilePath(); + + if ($path) { + return self::getLastVersion(file_get_contents($path)); + } + + return self::DEFAULT_MESSAGE; + } + + private static function findFilePath(): string { $basePath = base_path(); $files = scandir($basePath); @@ -26,16 +37,6 @@ public static function path(): string return ''; } - public static function read(): string - { - $path = self::path(); - if ($path) { - return self::getLastVersion(file_get_contents($path)); - } - - return self::DEFAULT_MESSAGE; - } - /** * Returns the content from the first H2 to the beginning of the second H2 that meets the H2_REGEX. */