Simon Vieille
a291a03d89
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
149 lines
4.4 KiB
PHP
Executable file
149 lines
4.4 KiB
PHP
Executable file
#!/usr/bin/php
|
|
<?php
|
|
|
|
require __DIR__.'/../vendor/autoload.php';
|
|
|
|
use Plugin\Factory\EmailFactory;
|
|
use Plugin\Factory\TwigFactory;
|
|
use Plugin\Loader\EnvVarLoader;
|
|
use Plugin\Pipeline\Evaluation;
|
|
use Symfony\Component\Mailer\Exception\TransportException;
|
|
use Twig\Error\SyntaxError;
|
|
|
|
$build = EnvVarLoader::buildArray([
|
|
'workspace' => 'CI_WORKSPACE',
|
|
'repo' => [
|
|
'full_name' => 'CI_REPO',
|
|
'owner' => 'CI_REPO_OWNER',
|
|
'name' => 'CI_REPO_NAME',
|
|
'url' => 'CI_REPO_URL',
|
|
],
|
|
'commit' => [
|
|
'sha' => 'CI_COMMIT_SHA',
|
|
'ref' => 'CI_COMMIT_REF',
|
|
'branch' => 'CI_COMMIT_BRANCH',
|
|
'source_branch' => 'CI_COMMIT_SOURCE_BRANCH',
|
|
'target_branch' => 'CI_COMMIT_TARGET_BRANCH',
|
|
'tag' => 'CI_COMMIT_TAG',
|
|
'pull_request' => 'CI_COMMIT_PULL_REQUEST',
|
|
'pull_request_labels' => 'CI_COMMIT_PULL_REQUEST_LABELS',
|
|
'tag' => 'CI_COMMIT_TAG',
|
|
'message' => 'CI_COMMIT_MESSAGE',
|
|
'author' => 'CI_COMMIT_AUTHOR',
|
|
'author_email' => 'CI_COMMIT_AUTHOR_EMAIL',
|
|
'author_avatar' => 'CI_COMMIT_AUTHOR_AVATAR',
|
|
'prerelease' => 'CI_COMMIT_PRERELEASE',
|
|
],
|
|
'prev_commit' => [
|
|
'sha' => 'CI_PREV_COMMIT_SHA',
|
|
'ref' => 'CI_PREV_COMMIT_REF',
|
|
'branch' => 'CI_PREV_COMMIT_BRANCH',
|
|
'source_branch' => 'CI_PREV_COMMIT_SOURCE_BRANCH',
|
|
'target_branch' => 'CI_PREV_COMMIT_TARGET_BRANCH',
|
|
'message' => 'CI_PREV_COMMIT_MESSAGE',
|
|
'author' => 'CI_PREV_COMMIT_AUTHOR',
|
|
'author_email' => 'CI_PREV_COMMIT_AUTHOR_EMAIL',
|
|
'author_avatar' => 'CI_PREV_COMMIT_AUTHOR_AVATAR',
|
|
'url' => 'CI_PREV_COMMIT_URL',
|
|
],
|
|
'pipeline' => [
|
|
'number' => 'CI_PIPELINE_NUMBER',
|
|
'parent' => 'CI_PIPELINE_PARENT',
|
|
'event' => 'CI_PIPELINE_EVENT',
|
|
'url' => 'CI_PIPELINE_URL',
|
|
'deploy_target' => 'CI_PIPELINE_DEPLOY_TARGET',
|
|
'status' => 'CI_PIPELINE_STATUS',
|
|
'created_at' => 'CI_PIPELINE_CREATED',
|
|
'stared_at' => 'CI_PIPELINE_STARTED',
|
|
'finished_at' => 'CI_PIPELINE_FINISHED',
|
|
],
|
|
'prev_pipeline' => [
|
|
'number' => 'CI_PREV_PIPELINE_NUMBER',
|
|
'parent' => 'CI_PREV_PIPELINE_PARENT',
|
|
'event' => 'CI_PREV_PIPELINE_EVENT',
|
|
'url' => 'CI_PREV_PIPELINE_URL',
|
|
'deploy_target' => 'CI_PREV_PIPELINE_DEPLOY_TARGET',
|
|
'status' => 'CI_PREV_PIPELINE_STATUS',
|
|
'created_at' => 'CI_PREV_PIPELINE_CREATED',
|
|
'stared_at' => 'CI_PREV_PIPELINE_STARTED',
|
|
'finished_at' => 'CI_PREV_PIPELINE_FINISHED',
|
|
],
|
|
'workflow' => [
|
|
'name' => 'WORKFLOW_NAME',
|
|
],
|
|
'step' => [
|
|
'name' => 'CI_STEP_NAME',
|
|
'number' => 'CI_STEP_NUMBER',
|
|
'status' => 'CI_STEP_STATUS',
|
|
'start_at' => 'CI_STEP_STARTED',
|
|
'finished_at' => 'CI_STEP_FINISHED',
|
|
'url' => 'CI_STEP_URL',
|
|
],
|
|
]);
|
|
|
|
$config = EnvVarLoader::buildArray([
|
|
'dsn' => 'PLUGIN_DSN',
|
|
'from' => 'PLUGIN_FROM',
|
|
'recipients' => 'PLUGIN_RECIPIENTS',
|
|
'is_recipients_only' => 'PLUGIN_RECIPIENTS_ONLY',
|
|
'attachments' => 'PLUGIN_ATTACHMENTS',
|
|
'evaluate' => 'PLUGIN_EVALUATE',
|
|
'content' => 'PLUGIN_CONTENT',
|
|
'is_debug' => 'PLUGIN_DEBUG',
|
|
], [
|
|
'PLUGIN_RECIPIENTS_ONLY' => true,
|
|
'PLUGIN_DEBUG' => false,
|
|
]);
|
|
|
|
function writeln(...$values)
|
|
{
|
|
foreach ($values as $value) {
|
|
echo sprintf("%s\n", $value);
|
|
}
|
|
}
|
|
|
|
function handleError($section, Exception $e)
|
|
{
|
|
writeln(
|
|
sprintf('ERROR - %s', $section),
|
|
$e->getMessage()
|
|
);
|
|
|
|
exit(1);
|
|
}
|
|
|
|
if ($config['is_debug']) {
|
|
writeln(
|
|
'Debug',
|
|
'=====',
|
|
'Config',
|
|
json_encode($config, JSON_PRETTY_PRINT),
|
|
'Environment',
|
|
json_encode($_SERVER, JSON_PRETTY_PRINT),
|
|
'====='
|
|
);
|
|
}
|
|
|
|
$twig = (new TwigFactory())->create();
|
|
$emailFactory = new EmailFactory($twig, $config, $build);
|
|
$evaluation = new Evaluation($twig);
|
|
|
|
try {
|
|
if (!empty($config['evaluate']) && !$evaluation->isTrue($config['evaluate'], $build)) {
|
|
writeln('Evaluation returns false.', 'Program aborted!');
|
|
} else {
|
|
$emailFactory
|
|
->createMailer($config)
|
|
->send($emailFactory->createEmail($config, $build))
|
|
;
|
|
|
|
writeln('Email sent!');
|
|
}
|
|
} catch (SyntaxError $e) {
|
|
handleError('Syntax error', $e);
|
|
} catch (TransportException $e) {
|
|
handleError('Transport error', $e);
|
|
} catch (\Exception $e) {
|
|
handleError('Generic error', $e);
|
|
}
|