Final Class Yiisoft\YiiDevTool\App\Command\Github\ForksRepositoriesCommand
| Inheritance | Yiisoft\ |
|---|---|
| Uses Traits | Yiisoft\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| getGitHubToken() | Yiisoft\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| configure() | Yiisoft\ |
|
| execute() | Yiisoft\ |
|
| getAppRootDir() | Use this method to get a root directory of the tool. | Yiisoft\ |
| getIO() | Yiisoft\ |
|
| initialize() | Yiisoft\ |
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 ( \ | ||
| $input | \ |
|
| $output | \ |
|
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 ( 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;
}
| protected Yiisoft\ |
protected function getIO(): OutputManager
{
if ($this->io === null) {
throw new RuntimeException('IO is not initialized.');
}
return $this->io;
}
| protected mixed initialize ( \ | ||
| $input | \ |
|
| $output | \ |
|
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->io = new OutputManager(new YiiDevToolStyle($input, $output));
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.