0 follower

Final Class Yiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand

InheritanceYiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand » Symfony\Component\Console\Command\Command
Uses TraitsYiisoft\YiiDevTool\App\Component\GitHubTokenAware

Method Details

Hide inherited methods

configure() protected method

protected mixed configure ( )

                protected function configure()
{
    $this
        ->setAliases(['forks'])
        ->setName('github/forks')
        ->setDescription('Creating forks for repositories')
        ->addArgument('owner', InputArgument::REQUIRED, 'Repositories owner')
        ->addArgument('repositories', InputArgument::REQUIRED, 'upstream repositories');
    parent::configure();
}

            
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
{
    $ownerRepository = $input->getArgument('owner');
    $repositories = $input->getArgument('repositories');
    $targetRepositories = array_unique(explode(',', $repositories));
    $client = new Client();
    $client->authenticate($this->getGitHubToken(), null, AuthMethod::ACCESS_TOKEN);
    $forks = (new Forks($client));
    foreach ($targetRepositories as $repository) {
        try {
            // See https://developer.github.com/v3/repos/forks/
            $forks->create($ownerRepository, $repository);
            $output->write("<success>Successfully forked repository: $repository</success>" . PHP_EOL);
        } catch (GithubRuntimeException $e) {
            $output->write(
                [
                    "<error>Error when forking a repository $repository: {$e->getMessage()}</error>",
                    '<error>Check if the nickname of the owner and the name of the repository are correct</error>' . PHP_EOL,
                ],
                true
            );
        }
    }
    return Command::SUCCESS;
}

            
getAppRootDir() protected method

Use this method to get a root directory of the tool.

Commands and components can be moved as a result of refactoring, so you should not rely on their location in the file system.

protected string getAppRootDir ( )
return string

Path to the root directory of the tool WITH a TRAILING SLASH.

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

            
getGitHubToken() public method
public string getGitHubToken ( )

                public function getGitHubToken(): 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);
    }
    // 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 mixed 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)
{
    $this->io = new OutputManager(new YiiDevToolStyle($input, $output));
}