Skip to content

Commit

Permalink
Roll into teams comand
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Sep 27, 2024
1 parent 72c020b commit 6cc6413
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 135 deletions.
1 change: 0 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ protected function getCommands()
$commands[] = new Command\Team\TeamDeleteCommand();
$commands[] = new Command\Team\TeamGetCommand();
$commands[] = new Command\Team\TeamListCommand();
$commands[] = new Command\Team\TeamListByProjectCommand();
$commands[] = new Command\Team\TeamUpdateCommand();
$commands[] = new Command\Team\Project\TeamProjectAddCommand();
$commands[] = new Command\Team\Project\TeamProjectDeleteCommand();
Expand Down
39 changes: 38 additions & 1 deletion src/Command/Team/TeamCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace Platformsh\Cli\Command\Team;

use GuzzleHttp\Exception\BadResponseException;
use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Console\ProgressMessage;
use Platformsh\Cli\Exception\NoOrganizationsException;
use Platformsh\Client\Exception\ApiResponseException;
use Platformsh\Client\Model\Organization\Organization;
use Platformsh\Client\Model\Project;
use Platformsh\Client\Model\Team\ProjectTeamAccess;
use Platformsh\Client\Model\Team\Team;
use Platformsh\Client\Model\Team\TeamProjectAccess;
use Symfony\Component\Console\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -143,7 +147,7 @@ public function validateTeamInput(InputInterface $input, Organization $organizat
protected function loadTeams(Organization $organization, $fetchAllPages = true, $params = [])
{
$httpClient = $this->api()->getHttpClient();
$options = ['query' => ['filter[organization_id]' => $organization->id, 'sort' => 'label'] + $params];
$options = ['query' => array_merge(['filter[organization_id]' => $organization->id, 'sort' => 'label'], $params)];
$url = '/teams';
/** @var Team[] $teams */
$teams = [];
Expand All @@ -162,6 +166,39 @@ protected function loadTeams(Organization $organization, $fetchAllPages = true,
return $teams;
}

/**
* Loads the information of teams that have access to a single project.
*
* @param Project $project The project.
*
* @return array<string, array{team_id: string, granted_at: string}>
*/
protected function loadTeamsOnProject(Project $project)
{
$httpClient = $this->api()->getHttpClient();
$url = $project->getUri() . '/team-access';
$info = [];
$progress = new ProgressMessage($this->stdErr);
$pageNumber = 1;
do {
if ($pageNumber > 1) {
$progress->showIfOutputDecorated(sprintf('Loading project teams (page %d)...', $pageNumber));
}
try {
$data = $httpClient->get($url)->json();
} catch (BadResponseException $e) {
throw ApiResponseException::create($e->getRequest(), $e->getResponse(), $e);
}
foreach ($data['items'] as $item) {
$info[$item['team_id']] = $item;
}
$progress->done();
$url = isset($data['_links']['next']['href']) ? $data['_links']['next']['href'] : null;
$pageNumber++;
} while ($url);
return $info;
}

/**
* Presents an interactive choice to pick a team in the organization.
*
Expand Down
129 changes: 0 additions & 129 deletions src/Command/Team/TeamListByProjectCommand.php

This file was deleted.

29 changes: 26 additions & 3 deletions src/Command/Team/TeamListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TeamListCommand extends TeamCommandBase
'project_permissions' => 'Permissions',
'created_at' => 'Created at',
'updated_at' => 'Updated at',
'granted_at' => 'Granted at',
];
private $defaultColumns = ['id', 'label', 'member_count', 'project_count', 'project_permissions'];

Expand All @@ -32,6 +33,7 @@ protected function configure()
->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'The number of items to display per page. Use 0 to disable pagination.')
->addOption('sort', null, InputOption::VALUE_REQUIRED, 'A team property to sort by', 'label')
->addOption('reverse', null, InputOption::VALUE_NONE, 'Sort in reverse order')
->addOption('all', 'A', InputOption::VALUE_NONE, 'List all teams in the organization (regardless of a selected project)')
->addOrganizationOptions(true);
PropertyFormatter::configureInput($this->getDefinition());
Table::configureInput($this->getDefinition(), $this->tableHeader, $this->defaultColumns);
Expand Down Expand Up @@ -62,6 +64,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
$params['page[size]'] = $count;
}

// Fetch teams for a specific project.
$project = false;
if (!$input->getOption('all')) {
if ($this->hasSelectedProject()) {
$project = $this->getSelectedProject();
} elseif ($currentProject = $this->getCurrentProject(true)) {
$project = $currentProject;
}
if ($project) {
$onProject = $this->loadTeamsOnProject($project);
$params['filter[id][in]'] = implode(',', array_keys($onProject));
}
}

$teams = $this->loadTeams($organization, $fetchAllPages, $params);

$executable = $this->config()->get('application.executable');
Expand Down Expand Up @@ -93,20 +109,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
'project_permissions' => $rolesUtil->formatPermissions($team->project_permissions, $machineReadable),
'created_at' => $formatter->format($team->created_at, 'created_at'),
'updated_at' => $formatter->format($team->created_at, 'updated_at'),
'granted_at' => isset($onProject[$team->id]) ? $formatter->format($onProject[$team->id]['granted_at'], 'granted_at') : '',
];
$rows[] = $row;
}

if (!$machineReadable) {
$this->stdErr->writeln(sprintf('Teams in the organization %s:', $this->api()->getOrganizationLabel($organization)));
if ($project) {
$this->stdErr->writeln(sprintf('Teams with access to the project %s:', $this->api()->getProjectLabel($project)));
} else {
$this->stdErr->writeln(sprintf('Teams in the organization %s:', $this->api()->getOrganizationLabel($organization)));
}
}

$table->render($rows, $this->tableHeader, $this->defaultColumns);

if (!$machineReadable) {
$this->stdErr->writeln('');
$this->stdErr->writeln(\sprintf('To list teams with access to one project, run: <info>%s team:list-by-project</info>', $executable));
$this->stdErr->writeln(\sprintf('To list all team projects, run: <info>%s team:projects</info>', $executable));
if ($project) {
$this->stdErr->writeln(\sprintf('To list all teams in the organization, run: <info>%s teams --all</info>', $executable));
}
$this->stdErr->writeln(\sprintf('To list team projects, run: <info>%s team:projects</info>', $executable));
$this->stdErr->writeln(\sprintf('To list team users, run: <info>%s team:users</info>', $executable));
$this->stdErr->writeln(\sprintf('To see all team commands run: <info>%s list team</info>', $executable));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/User/UserListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$organization = $this->api()->getOrganizationById($project->getProperty('organization'));
if (in_array('teams', $organization->capabilities) && $organization->hasLink('members')) {
$this->stdErr->writeln('');
$this->stdErr->writeln("To list teams with access to the project, run: <info>$executable team:list-by-project</info>");
$this->stdErr->writeln(sprintf("To list teams with access to the project, run: <info>$executable teams -p %s</info>", $project->id));
}
}
}
Expand Down

0 comments on commit 6cc6413

Please sign in to comment.