Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/Api/GitlabClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace mglaman\DrupalOrg;

use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Storage\Psr6CacheStorage;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
use mglaman\DrupalOrg\GitlabRequest as Request;
use mglaman\DrupalOrgCli\Cache;

class GitlabClient
{

/**
* @var \GuzzleHttp\Client
*/
protected \GuzzleHttp\Client $client;

/**
* @var string
*/
public const API_URL = 'https://git.drupalcode.org/api/v4/';

public function __construct()
{
$stack = HandlerStack::create();
$stack->push(
new CacheMiddleware(
new PublicCacheStrategy(
new Psr6CacheStorage(Cache::getCache())
)
),
'cache'
);

$this->client = new \GuzzleHttp\Client(
[
'base_uri' => self::API_URL,
'cookies' => true,
'handler' => $stack,
'headers' => [
'User-Agent' => 'DrupalOrgCli/0.0.1',
'Accept' => 'application/json',
'Accept-Encoding' => '*',
],
]
);
}

/**
* @param Request $request
*
* @return \mglaman\DrupalOrg\Response
* @throws \Exception
*/
public function request(Request $request): Response
{
$res = $this->client->request('GET', $request->getUrl());
if ($res->getStatusCode() === 200) {
return new Response($res->getBody()->getContents());
}

throw new \Exception('Error code', $res->getStatusCode());
}

public function getProject(string $machineName): ?\stdClass
{
$request = new Request(
'projects',
[
'search' => $machineName,
]
);
$res = $this->request($request);
foreach ($res->getAll() as $project) {
if ($project->name === $machineName) {
return $project;
}
}
return null;
}

public function getProjectBranches(int $projectId): array
{
$request = new Request(
'projects/' . $projectId . '/repository/branches'
);
$res = $this->request($request);
return array_column($res->getAll()->getArrayCopy(), 'name');
}
}
9 changes: 9 additions & 0 deletions src/Api/GitlabRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace mglaman\DrupalOrg;

class GitlabRequest extends Request
{
protected string $baseUri = GitlabClient::API_URL;

}
8 changes: 8 additions & 0 deletions src/Api/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public function getList(): \ArrayObject
{
return new \ArrayObject($this->get('list'));
}

/**
* @return \ArrayObject<int, object>
*/
public function getAll(): \ArrayObject
{
return new \ArrayObject($this->response);
}
}
1 change: 1 addition & 0 deletions src/Cli/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function getCommands(): array
$commands[] = new Command\Project\Link();
$commands[] = new Command\Project\Kanban();
$commands[] = new Command\Project\ProjectIssues();
$commands[] = new Command\Project\ProjectClone();
$commands[] = new Command\Project\Releases();
$commands[] = new Command\Project\ReleaseNotes();
$commands[] = new Command\TravisCi\ListBuilds();
Expand Down
88 changes: 88 additions & 0 deletions src/Cli/Command/Project/ProjectClone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace mglaman\DrupalOrgCli\Command\Project;

use mglaman\DrupalOrg\GitlabClient;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Process\Process;

class ProjectClone extends ProjectCommandBase
{

protected function configure(): void
{
$this
->setName('project:clone')
->setAliases(['pc'])
->addArgument('project', InputArgument::REQUIRED, 'project ID')
->addOption(
'branch',
'b',
InputOption::VALUE_OPTIONAL,
'Branch to clone'
)
->addOption(
'method',
'm',
InputOption::VALUE_OPTIONAL,
'Method for cloning (ssh or https)',
'ssh'
)
->setDescription('Clone a project\'s repository.');
}

/**
* {@inheritdoc}
*/
protected function execute(
InputInterface $input,
OutputInterface $output
): int {
$method = mb_strtolower($this->stdIn->getOption('method'));
if (!in_array($method, ['ssh', 'https'], true)) {
$this->stdErr->writeln('<error>Unexpected method ' . $method . '.</error>');
$this->stdErr->writeln('Allowed values are: ssh, https');
return 1;
}

$gitlabClient = new GitlabClient();
$project = $gitlabClient->getProject($this->projectName);
$branches = $gitlabClient->getProjectBranches($project->id);
$defaultBranch = reset($branches);

$branch = $this->stdIn->getOption('branch');
if (null === $branch) {
$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
"Which branch do you want to clone? [$defaultBranch]",
$branches,
$defaultBranch
);
$branch = $helper->ask($input, $output, $question);
}
if (!in_array($branch, $branches, true)) {
$this->stdErr->writeln('<error>Unexpected branch ' . $branch . '.</error>');
$this->stdErr->writeln('Existing branches are: ' . implode(', ', $branches));
return 2;
}

$cloneUrl = $method === 'ssh' ? $project->ssh_url_to_repo : $project->http_url_to_repo;

// Clone the repository.
$process = new Process(['git', 'clone', '--branch', $branch, $cloneUrl]);
$process->run();
if (!$process->isSuccessful()) {
$this->stdErr->writeln('<error>The clone process failed.</error>');
$this->stdErr->writeln($process->getErrorOutput());
return 3;
}

$this->stdOut->writeln('<info>Clone successful!</info>');
$this->stdOut->writeln("<comment>$ cd $this->projectName</comment>");
return 0;
}
}