From f344cf617e3b46ac1a5bdb3fa8714c255b47a450 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Thu, 10 Oct 2019 11:41:19 +0100 Subject: [PATCH] Support setting git_sha with HEROKU_SLUG_COMMIT environment variable --- src/Events/Metadata.php | 6 ++++++ tests/Unit/Events/MetadataTest.php | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Events/Metadata.php b/src/Events/Metadata.php index 1f5e23a3..f7044ba4 100644 --- a/src/Events/Metadata.php +++ b/src/Events/Metadata.php @@ -17,6 +17,7 @@ use function dirname; use function explode; use function file_exists; +use function getenv; use function gethostname; use function is_readable; use function is_string; @@ -124,6 +125,11 @@ private function rootPackageGitSha() : string return $revisionShaConfiguration; } + $herokuSlugCommit = getenv('HEROKU_SLUG_COMMIT'); + if (is_string($herokuSlugCommit) && $herokuSlugCommit !== '') { + return $herokuSlugCommit; + } + return explode('@', Versions::getVersion(Versions::ROOT_PACKAGE_NAME))[1]; } diff --git a/tests/Unit/Events/MetadataTest.php b/tests/Unit/Events/MetadataTest.php index 6b9cd65e..a8814b7f 100644 --- a/tests/Unit/Events/MetadataTest.php +++ b/tests/Unit/Events/MetadataTest.php @@ -20,6 +20,8 @@ use function gethostname; use function json_decode; use function json_encode; +use function putenv; +use function uniqid; /** @covers \Scoutapm\Events\Metadata */ final class MetadataTest extends TestCase @@ -117,4 +119,22 @@ static function ($package, $version) { json_decode(json_encode(new Metadata($time, $config)), true) ); } + + /** @throws Exception */ + public function testHerokuSlugCommitOverridesTheGitSha() : void + { + $testHerokuSlugCommit = uniqid('testHerokuSlugCommit', true); + + putenv('HEROKU_SLUG_COMMIT=' . $testHerokuSlugCommit); + + self::assertSame( + $testHerokuSlugCommit, + json_decode(json_encode(new Metadata( + new DateTimeImmutable('now', new DateTimeZone('UTC')), + Config::fromArray([]) + )), true)['ApplicationEvent']['event_value']['git_sha'] + ); + + putenv('HEROKU_SLUG_COMMIT'); + } }