Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
fix: make sentry release work without git (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
atymic authored and antonmedv committed Jul 14, 2019
1 parent f74cbe2 commit 2fe3999
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
/.idea/
composer.lock
67 changes: 39 additions & 28 deletions recipe/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ static function () {
$now = date('c');

$defaultConfig = [
'version' => getReleaseGitRef(),
'version' => null,
'refs' => [],
'ref' => null,
'commits' => getGitCommitsRefs(),
'commits' => null,
'url' => null,
'date_released' => $now,
'date_deploy_started' => $now,
'date_deploy_finished' => $now,
'sentry_server' => 'https://sentry.io',
'previous_commit' => null,
'environment' => get('symfony_env', 'prod'),
'deploy_name' => null
'deploy_name' => null,
];

if (releaseIsGitDirectory()) {
$config['version'] = getReleaseGitRef();
$config['commits'] = getGitCommitsRefs();
}

$config = array_merge($defaultConfig, (array) get('sentry'));
array_walk(
$config,
Expand Down Expand Up @@ -70,7 +75,7 @@ static function (&$value) use ($config) {
'refs' => $config['refs'],
'ref' => $config['ref'],
'url' => $config['url'],
'commits' => array_slice($config['commits'], 0), // reset keys to serialize as array in json
'commits' => array_slice($config['commits'] ?? [], 0), // reset keys to serialize as array in json
'dateReleased' => $config['date_released'],
'projects' => $config['projects'],
'previousCommit' => $config['previous_commit'],
Expand Down Expand Up @@ -130,12 +135,17 @@ static function (&$value) use ($config) {
}
);

function releaseIsGitDirectory()
{
return (bool) run('cd {{release_path}} && git rev-parse --git-dir > /dev/null 2>&1 && echo 1');
}

function getReleaseGitRef(): Closure
{
return static function ($config = []): string {
cd('{{release_path}}');

if(isset($config['git_version_command'])){
if (isset($config['git_version_command'])) {
return trim(run($config['git_version_command']));
}

Expand All @@ -160,33 +170,34 @@ function getGitCommitsRefs(): Closure
}

cd('{{release_path}}');

try {
$result = run(sprintf('git rev-list --pretty="%s" %s', 'format:%H#%an#%ae#%at', $commitRange));
$lines = array_filter(
$result = run(sprintf('git rev-list --pretty="%s" %s', 'format:%H#%an#%ae#%at', $commitRange));
$lines = array_filter(
// limit number of commits for first release with many commits
array_map('trim', array_slice(explode("\n", $result), 0, 200)),
static function (string $line): bool {
return !empty($line) && strpos($line, 'commit') !== 0;
}
);

return array_map(
static function (string $line): array {
list($ref, $authorName, $authorEmail, $timestamp) = explode('#', $line);

return [
'id' => $ref,
'author_name' => $authorName,
'author_email' => $authorEmail,
'timestamp' => date(DateTime::ATOM, (int) $timestamp),
];
},
$lines
);
array_map('trim', array_slice(explode("\n", $result), 0, 200)),
static function (string $line): bool {
return !empty($line) && strpos($line, 'commit') !== 0;
}
);

return array_map(
static function (string $line): array {
list($ref, $authorName, $authorEmail, $timestamp) = explode('#', $line);

return [
'id' => $ref,
'author_name' => $authorName,
'author_email' => $authorEmail,
'timestamp' => date(DateTime::ATOM, (int) $timestamp),
];
},
$lines
);

} catch (\Deployer\Exception\RuntimeException $e) {
writeln($e->getMessage());
return [];
writeln($e->getMessage());
return [];
}
};
}

0 comments on commit 2fe3999

Please sign in to comment.