0

Final Class Yiisoft\YiiDevTool\App\Command\Release\WhatCommand

InheritanceYiisoft\YiiDevTool\App\Command\Release\WhatCommand » Symfony\Component\Console\Command\Command
Uses TraitsYiisoft\YiiDevTool\App\Component\GitHubTokenAware

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( ?\Yiisoft\YiiDevTool\App\Command\Release\GitHubReleaseInspectorInterface $gitHub null )
$gitHub ?\Yiisoft\YiiDevTool\App\Command\Release\GitHubReleaseInspectorInterface

                public function __construct(private ?GitHubReleaseInspectorInterface $gitHub = null)
{
    parent::__construct();
}

            
configure() protected method

protected void configure ( )

                protected function configure(): void
{
    $this
        ->setName('release:what')
        ->setDescription('Find packages that are ready to release');
}

            
execute() protected method

protected integer execute ( \Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output )
$input \Symfony\Component\Console\Input\InputInterface
$output \Symfony\Component\Console\Output\OutputInterface

                protected function execute(InputInterface $input, OutputInterface $output): int
{
    $this->initPackageList();
    $ready = [];
    $blocked = [];
    $candidates = [];
    foreach ($this->packageList->getInstalledAndEnabledPackages() as $package) {
        if (!$package->isGitRepositoryCloned()) {
            continue;
        }
        try {
            $candidate = $this->inspectLocalCandidate($package);
        } catch (Throwable $e) {
            $blocked[] = [$package->getName(), '', $e->getMessage()];
            continue;
        }
        if ($candidate === null) {
            continue;
        }
        $candidates[$package->getName()] = $candidate;
    }
    $defaultBranchRequests = [];
    foreach ($candidates as $candidate) {
        $key = $candidate['remoteKey'];
        $defaultBranchRequests[$key] ??= [
            'vendor' => $candidate['vendor'],
            'repository' => $candidate['repository'],
        ];
    }
    $defaultBranches = [];
    $remoteError = null;
    if ($defaultBranchRequests !== []) {
        try {
            $defaultBranches = $this->getGitHub()->getDefaultBranches($defaultBranchRequests);
        } catch (Throwable $e) {
            $remoteError = $e->getMessage();
        }
    }
    $detailRequests = [];
    foreach ($candidates as $packageName => $candidate) {
        if ($remoteError !== null) {
            $candidates[$packageName]['problems'][] = $remoteError;
            continue;
        }
        $key = $candidate['remoteKey'];
        if (!isset($defaultBranches[$key])) {
            $candidates[$packageName]['problems'][] = 'GitHub default branch result is missing.';
            continue;
        }
        if ($candidate['head'] !== $defaultBranches[$key]['sha']) {
            $candidates[$packageName]['problems'][] = sprintf(
                'HEAD does not match origin/%s.',
                $defaultBranches[$key]['branch'],
            );
            continue;
        }
        if ($candidate['problems'] !== []) {
            continue;
        }
        if (isset($detailRequests[$key])) {
            $detailRequests[$key]['issues'] = array_values(array_unique([
                ...$detailRequests[$key]['issues'],
                ...$candidate['issues'],
            ]));
        } else {
            $detailRequests[$key] = [
                'vendor' => $candidate['vendor'],
                'repository' => $candidate['repository'],
                'sha' => $candidate['head'],
                'issues' => $candidate['issues'],
            ];
        }
    }
    $detailResults = [];
    if ($detailRequests !== []) {
        try {
            $detailResults = $this->getGitHub()->inspect($detailRequests);
        } catch (Throwable $e) {
            foreach ($candidates as $packageName => $candidate) {
                if (isset($detailRequests[$candidate['remoteKey']])) {
                    $candidates[$packageName]['problems'][] = $e->getMessage();
                }
            }
        }
    }
    foreach ($candidates as $packageName => $candidate) {
        $key = $candidate['remoteKey'];
        if ($candidate['problems'] === [] && isset($detailResults[$key])) {
            array_push($candidate['problems'], ...$this->checkRemote($candidate, $detailResults[$key]));
        } elseif ($candidate['problems'] === [] && isset($detailRequests[$key])) {
            $candidate['problems'][] = 'GitHub inspection result is missing.';
        }
        $row = [$packageName, $candidate['version'], implode("\n", $candidate['summary'])];
        if ($candidate['problems'] === []) {
            $ready[] = $row;
        } else {
            $blocked[] = [
                $packageName,
                $candidate['version'],
                implode("\n", $candidate['problems']),
            ];
        }
    }
    $this->render($output, $ready, $blocked);
    return $blocked === [] ? Command::SUCCESS : Command::FAILURE;
}

            
getAppRootDir() protected method

protected string getAppRootDir ( )

                protected function getAppRootDir(): string
{
    return rtrim($this->getApplication()->getRootDir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}

            
getGitHubToken() public method
public string getGitHubToken ( boolean $validate true )
$validate boolean

                public function getGitHubToken(bool $validate = true): string
{
    if ($this->gitHubToken !== null) {
        return $this->gitHubToken;
    }
    $io = $this->getIO();
    $tokenFile = $this->getAppRootDir() . 'config/github.token';
    if (!file_exists($tokenFile)) {
        $io->error([
            "There's no $tokenFile.",
            '<href=https://github.com/settings/tokens>Please create a GitHub token</> and put it there.',
        ]);
        exit(Command::FAILURE);
    }
    $token = trim(file_get_contents($tokenFile));
    if (empty($token)) {
        $io->error([
            "$tokenFile exists but is empty.",
            '<href=https://github.com/settings/tokens>Please create a GitHub token</> and put it there.',
        ]);
        exit(Command::FAILURE);
    }
    if (!$validate) {
        return $token;
    }
    // Test the token by making an authenticated request
    $client = new Client();
    $client->authenticate($token, null, AuthMethod::ACCESS_TOKEN);
    try {
        $client->currentUser()->show();
    } catch (Exception $e) {
        $io->error([
            "Failed to authenticate with GitHub using the provided token from $tokenFile.",
            '<href=https://github.com/settings/tokens>Please make sure the token is valid '
            . 'and has the required permissions</>.',
            'Error: ' . $e->getMessage(),
        ]);
        exit(Command::FAILURE);
    }
    $this->gitHubToken = $token;
    return $token;
}

            
getIO() protected method

protected Yiisoft\YiiDevTool\App\Component\Console\OutputManager getIO ( )

                protected function getIO(): OutputManager
{
    if ($this->io === null) {
        throw new RuntimeException('IO is not initialized.');
    }
    return $this->io;
}

            
initialize() protected method

protected void initialize ( \Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output )
$input \Symfony\Component\Console\Input\InputInterface
$output \Symfony\Component\Console\Output\OutputInterface

                protected function initialize(InputInterface $input, OutputInterface $output): void
{
    $this->io = new OutputManager(new YiiDevToolStyle($input, $output));
}