1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-09 22:49:31 +01:00
psalm/src/Psalm/Internal/ExecutionEnvironment/BuildInfoCollector.php

322 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Internal\ExecutionEnvironment;
2019-12-02 01:16:21 +01:00
use Psalm\SourceControl\Git\CommitInfo;
use Psalm\SourceControl\Git\GitInfo;
2021-06-08 04:55:21 +02:00
use function explode;
2021-12-03 21:07:25 +01:00
use function file_get_contents;
use function json_decode;
use function str_replace;
use function strpos;
use function strtotime;
/**
* Environment variables collector for CI environment.
*
* @author Kitamura Satoshi <with.no.parachute@gmail.com>
*/
class BuildInfoCollector
{
/**
* Environment variables.
*
* Overwritten through collection process.
*
* @var array
*/
protected $env;
/**
* Read environment variables.
*
* @var array
*/
protected $readEnv = [];
public function __construct(array $env)
{
$this->env = $env;
}
// API
/**
* Collect environment variables.
*/
public function collect() : array
{
$this->readEnv = [];
$this
->fillTravisCi()
->fillCircleCi()
->fillAppVeyor()
2019-04-14 20:49:09 +02:00
->fillJenkins()
->fillScrutinizer()
->fillGithubActions();
return $this->readEnv;
}
// internal method
/**
* Fill Travis CI environment variables.
*
* "TRAVIS", "TRAVIS_JOB_ID" must be set.
*
* @return $this
*
2019-11-11 16:11:42 +01:00
* @psalm-suppress PossiblyUndefinedStringArrayOffset
*/
protected function fillTravisCi() : self
{
if (isset($this->env['TRAVIS']) && $this->env['TRAVIS'] && isset($this->env['TRAVIS_JOB_ID'])) {
2019-03-25 01:48:08 +01:00
$this->readEnv['CI_JOB_ID'] = $this->env['TRAVIS_JOB_ID'];
$this->env['CI_NAME'] = 'travis-ci';
// backup
$this->readEnv['TRAVIS'] = $this->env['TRAVIS'];
$this->readEnv['TRAVIS_JOB_ID'] = $this->env['TRAVIS_JOB_ID'];
$this->readEnv['CI_NAME'] = $this->env['CI_NAME'];
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
2020-08-23 16:32:07 +02:00
$this->readEnv['TRAVIS_TAG'] = $this->env['TRAVIS_TAG'] ?? '';
2019-03-25 01:48:08 +01:00
$repo_slug = (string) $this->env['TRAVIS_REPO_SLUG'];
if ($repo_slug) {
$slug_parts = explode('/', $repo_slug);
$this->readEnv['CI_REPO_OWNER'] = $slug_parts[0];
$this->readEnv['CI_REPO_NAME'] = $slug_parts[1];
}
2019-08-20 04:45:24 +02:00
$pr_slug = (string) ($this->env['TRAVIS_PULL_REQUEST_SLUG'] ?? '');
2019-03-25 01:48:08 +01:00
if ($pr_slug) {
$slug_parts = explode('/', $pr_slug);
$this->readEnv['CI_PR_REPO_OWNER'] = $slug_parts[0];
$this->readEnv['CI_PR_REPO_NAME'] = $slug_parts[1];
}
$this->readEnv['CI_PR_NUMBER'] = $this->env['TRAVIS_PULL_REQUEST'];
2019-03-25 02:33:07 +01:00
$this->readEnv['CI_BRANCH'] = $this->env['TRAVIS_BRANCH'];
}
return $this;
}
/**
* Fill CircleCI environment variables.
*
* "CIRCLECI", "CIRCLE_BUILD_NUM" must be set.
*
* @return $this
*/
protected function fillCircleCi() : self
{
if (isset($this->env['CIRCLECI']) && $this->env['CIRCLECI'] && isset($this->env['CIRCLE_BUILD_NUM'])) {
$this->env['CI_BUILD_NUMBER'] = $this->env['CIRCLE_BUILD_NUM'];
$this->env['CI_NAME'] = 'circleci';
// backup
$this->readEnv['CIRCLECI'] = $this->env['CIRCLECI'];
$this->readEnv['CIRCLE_BUILD_NUM'] = $this->env['CIRCLE_BUILD_NUM'];
$this->readEnv['CI_NAME'] = $this->env['CI_NAME'];
2019-03-25 01:48:08 +01:00
$this->readEnv['CI_PR_REPO_OWNER'] = $this->env['CIRCLE_PR_USERNAME'] ?? null;
$this->readEnv['CI_PR_REPO_NAME'] = $this->env['CIRCLE_PR_REPONAME'] ?? null;
$this->readEnv['CI_REPO_OWNER'] = $this->env['CIRCLE_PROJECT_USERNAME'] ?? null;
$this->readEnv['CI_REPO_NAME'] = $this->env['CIRCLE_PROJECT_REPONAME'] ?? null;
$this->readEnv['CI_PR_NUMBER'] = $this->env['CIRCLE_PR_NUMBER'] ?? null;
2019-03-25 02:33:07 +01:00
$this->readEnv['CI_BRANCH'] = $this->env['CIRCLE_BRANCH'] ?? null;
}
return $this;
}
/**
* Fill AppVeyor environment variables.
*
* "APPVEYOR", "APPVEYOR_BUILD_NUMBER" must be set.
*
2019-11-11 16:11:42 +01:00
* @psalm-suppress PossiblyUndefinedStringArrayOffset
*
* @return $this
*/
protected function fillAppVeyor() : self
{
if (isset($this->env['APPVEYOR']) && $this->env['APPVEYOR'] && isset($this->env['APPVEYOR_BUILD_NUMBER'])) {
2019-03-25 01:48:08 +01:00
$this->readEnv['CI_BUILD_NUMBER'] = $this->env['APPVEYOR_BUILD_NUMBER'];
$this->readEnv['CI_JOB_ID'] = $this->env['APPVEYOR_JOB_NUMBER'];
$this->readEnv['CI_BRANCH'] = $this->env['APPVEYOR_REPO_BRANCH'];
$this->readEnv['CI_PR_NUMBER'] = $this->env['APPVEYOR_PULL_REQUEST_NUMBER'] ?? '';
$this->env['CI_NAME'] = 'AppVeyor';
// backup
$this->readEnv['APPVEYOR'] = $this->env['APPVEYOR'];
$this->readEnv['APPVEYOR_BUILD_NUMBER'] = $this->env['APPVEYOR_BUILD_NUMBER'];
$this->readEnv['APPVEYOR_JOB_NUMBER'] = $this->env['APPVEYOR_JOB_NUMBER'];
$this->readEnv['APPVEYOR_REPO_BRANCH'] = $this->env['APPVEYOR_REPO_BRANCH'];
$this->readEnv['CI_NAME'] = $this->env['CI_NAME'];
2019-03-25 01:48:08 +01:00
2019-08-20 04:45:24 +02:00
$repo_slug = (string) ($this->env['APPVEYOR_REPO_NAME'] ?? '');
2019-03-25 01:48:08 +01:00
if ($repo_slug) {
$slug_parts = explode('/', $repo_slug);
$this->readEnv['CI_REPO_OWNER'] = $slug_parts[0];
$this->readEnv['CI_REPO_NAME'] = $slug_parts[1];
}
$pr_slug = (string) ($this->env['APPVEYOR_PULL_REQUEST_HEAD_REPO_NAME'] ?? '');
2019-03-25 01:48:08 +01:00
if ($pr_slug) {
$slug_parts = explode('/', $pr_slug);
$this->readEnv['CI_PR_REPO_OWNER'] = $slug_parts[0];
$this->readEnv['CI_PR_REPO_NAME'] = $slug_parts[1];
}
2019-03-25 02:33:07 +01:00
2019-03-25 02:35:43 +01:00
$this->readEnv['CI_BRANCH'] = $this->env['APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH']
?? $this->env['APPVEYOR_REPO_BRANCH'];
}
return $this;
}
/**
* Fill Jenkins environment variables.
*
* "JENKINS_URL", "BUILD_NUMBER" must be set.
*
* @return $this
*/
protected function fillJenkins() : self
{
if (isset($this->env['JENKINS_URL']) && isset($this->env['BUILD_NUMBER'])) {
2019-03-25 01:48:08 +01:00
$this->readEnv['CI_BUILD_NUMBER'] = $this->env['BUILD_NUMBER'];
$this->readEnv['CI_BUILD_URL'] = $this->env['JENKINS_URL'];
$this->env['CI_NAME'] = 'jenkins';
// backup
$this->readEnv['BUILD_NUMBER'] = $this->env['BUILD_NUMBER'];
$this->readEnv['JENKINS_URL'] = $this->env['JENKINS_URL'];
$this->readEnv['CI_NAME'] = $this->env['CI_NAME'];
}
return $this;
}
2019-04-14 20:49:09 +02:00
/**
* Fill Scrutinizer environment variables.
*
* "JENKINS_URL", "BUILD_NUMBER" must be set.
*
2019-11-11 16:11:42 +01:00
* @psalm-suppress PossiblyUndefinedStringArrayOffset
*
* @return $this
2019-04-14 20:49:09 +02:00
*/
protected function fillScrutinizer() : self
{
if (isset($this->env['SCRUTINIZER']) && $this->env['SCRUTINIZER']) {
$this->readEnv['CI_JOB_ID'] = $this->env['SCRUTINIZER_INSPECTION_UUID'];
$this->readEnv['CI_BRANCH'] = $this->env['SCRUTINIZER_BRANCH'];
$this->readEnv['CI_PR_NUMBER'] = $this->env['SCRUTINIZER_PR_NUMBER'] ?? '';
// backup
$this->readEnv['CI_NAME'] = 'Scrutinizer';
2019-08-20 04:45:24 +02:00
$repo_slug = (string) ($this->env['SCRUTINIZER_PROJECT'] ?? '');
2019-04-14 20:49:09 +02:00
if ($repo_slug) {
$slug_parts = explode('/', $repo_slug);
if ($this->readEnv['CI_PR_NUMBER']) {
$this->readEnv['CI_PR_REPO_OWNER'] = $slug_parts[1];
$this->readEnv['CI_PR_REPO_NAME'] = $slug_parts[2];
} else {
$this->readEnv['CI_REPO_OWNER'] = $slug_parts[1];
$this->readEnv['CI_REPO_NAME'] = $slug_parts[2];
}
}
}
return $this;
}
/**
* Fill Github Actions environment variables.
*
* @return $this
* @psalm-suppress PossiblyUndefinedStringArrayOffset
*/
protected function fillGithubActions(): BuildInfoCollector
{
if (isset($this->env['GITHUB_ACTIONS'])) {
$this->env['CI_NAME'] = 'github-actions';
$this->env['CI_JOB_ID'] = $this->env['GITHUB_ACTIONS'];
$githubRef = (string) $this->env['GITHUB_REF'];
2021-12-03 21:07:25 +01:00
if (strpos($githubRef, 'refs/heads/') !== false) {
$githubRef = str_replace('refs/heads/', '', $githubRef);
} elseif (strpos($githubRef, 'refs/tags/') !== false) {
$githubRef = str_replace('refs/tags/', '', $githubRef);
}
$this->env['CI_BRANCH'] = $githubRef;
$this->readEnv['GITHUB_ACTIONS'] = $this->env['GITHUB_ACTIONS'];
$this->readEnv['GITHUB_REF'] = $this->env['GITHUB_REF'];
$this->readEnv['CI_NAME'] = $this->env['CI_NAME'];
2019-12-03 06:44:19 +01:00
$this->readEnv['CI_BRANCH'] = $this->env['CI_BRANCH'];
2019-12-03 06:26:19 +01:00
$slug_parts = explode('/', (string) $this->env['GITHUB_REPOSITORY']);
$this->readEnv['CI_REPO_OWNER'] = $slug_parts[0];
$this->readEnv['CI_REPO_NAME'] = $slug_parts[1];
if (isset($this->env['GITHUB_EVENT_PATH'])) {
2021-12-03 21:07:25 +01:00
$event_json = file_get_contents((string) $this->env['GITHUB_EVENT_PATH']);
/** @var array */
2021-12-03 21:07:25 +01:00
$event_data = json_decode($event_json, true);
2019-12-02 01:16:21 +01:00
2019-12-02 04:15:19 +01:00
if (isset($event_data['head_commit'])) {
/**
* @var array{
* id: string,
* author: array{name: string, email: string},
* committer: array{name: string, email: string},
* message: string,
* timestamp: string
* }
*/
$head_commit_data = $event_data['head_commit'];
$gitinfo = new GitInfo(
$githubRef,
(new CommitInfo())
->setId($head_commit_data['id'])
->setAuthorName($head_commit_data['author']['name'])
->setAuthorEmail($head_commit_data['author']['email'])
->setCommitterName($head_commit_data['committer']['name'])
->setCommitterEmail($head_commit_data['committer']['email'])
->setMessage($head_commit_data['message'])
2021-12-03 21:07:25 +01:00
->setDate(strtotime($head_commit_data['timestamp'])),
2019-12-02 04:15:19 +01:00
[]
);
$this->readEnv['git'] = $gitinfo->toArray();
}
if ($this->env['GITHUB_EVENT_PATH'] === 'pull_request') {
$this->readEnv['CI_PR_NUMBER'] = $event_data['number'];
}
}
}
return $this;
}
}