Skip to content

Commit

Permalink
Support setting git_sha with HEROKU_SLUG_COMMIT environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Oct 10, 2019
1 parent 0188a7d commit f344cf6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Events/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Events/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
}
}

0 comments on commit f344cf6

Please sign in to comment.