Final Class Yiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand
| Inheritance | Yiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand » Symfony\Component\Console\Command\Command |
|---|---|
| Uses Traits | Yiisoft\YiiDevTool\App\Component\GitHubTokenAware |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| getGitHubToken() | Yiisoft\YiiDevTool\App\Component\GitHubTokenAware |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| configure() | Yiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand | |
| execute() | Yiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand | |
| getAppRootDir() | Use this method to get a root directory of the tool. | Yiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand |
| getIO() | Yiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand | |
| initialize() | Yiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand |
Method Details
| 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();
}
| 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;
}
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;
}
| 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;
}
| 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;
}
| 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));
}
Signup or Login in order to comment.