Skip to content

Commit

Permalink
Added upstream:list name filter, organization field (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeslaDethray authored Aug 6, 2017
1 parent 8a14e8b commit a20082e
Show file tree
Hide file tree
Showing 40 changed files with 1,715 additions and 306 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
# Change Log
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org)

### Changed
- Updated the name of the `longname` field output by `upstream:list` to `label`. (#1747)
- Updated the name of the `longname` field output by `upstream:info` to `label`. (#1747)
- Upstreams of types `core` and `custom` are the only ones which appear by default when using `upstream:list`. (#1747)
- The `--org` option of the `site:list` command now defaults to `"all"` instead of `null`, but its behavior is unchanged. (#1747)

### Added
- Added a `machine_name` field to the output of `upstream:list`. (#1747)
- Added a `organization` field to the output of `upstream:list`. (#1747)
- Added a `machine_name` field to the output of `upstream:info`. (#1747)
- Added a `organization` field to the output of `upstream:info`. (#1747)
- Added a `repository_url` field to the output of `upstream:info`. (#1747)
- Added a `org:upstream:list` command to list the upstreams of a specific organization. (#1747)
- Added an `--org` option to `upstream:list` to list the upstreams of a specific organization. (#1747)
- Added an `--all` option to list upstreams of all types in the output of `upstream:list`. (#1747)
- Added a `--framework` option to `upstream:list` to filter the list by framework. (#1747)
- Added a `--name` option to `upstream:list` to filter the list by name regex. (#1747)

### Removed
- Removed the `category` field from the output of `upsteram:info`. (#1747)

### Fixed
- The `org` option of `site:create` now works with machine names and labels as well as UUIDs. (#1747)

## 1.4.1 - 2017-07-17
### Fixed
- Corrected the help text of `import:site`/`site:import` to call the params params rather than options. (#1718)
Expand Down
33 changes: 33 additions & 0 deletions src/Collections/OrganizationUpstreams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Pantheon\Terminus\Collections;

use Pantheon\Terminus\Models\OrganizationUpstream;

/**
* Class OrganizationUpstreams
* @package Pantheon\Terminus\Collections
*/
class OrganizationUpstreams extends OrganizationOwnedCollection
{
public static $pretty_name = 'upstreams';
/**
* @var string
*/
protected $collected_class = OrganizationUpstream::class;
/**
* @var string
*/
protected $url = 'organizations/{organization_id}/upstreams';

/**
* Filters an array of Upstreams by their label
*
* @param string $regex Non-delimited PHP regex to filter site names by
* @return Upstreams
*/
public function filterByName($regex = '(.*)')
{
return $this->filterByRegex('label', $regex);
}
}
9 changes: 3 additions & 6 deletions src/Collections/Sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function fetch(array $arg_options = [])

if (!$options['team_only']) {
$memberships = $this->getUser()->getOrganizationMemberships()->fetch()->all();
if (!is_null($org_id = $options['org_id']) && ($org_id != 'all')) {
if (!is_null($org_id = $options['org_id'])) {
$memberships = array_filter($memberships, function ($membership) use ($org_id) {
return $membership->id == $org_id;
});
Expand Down Expand Up @@ -102,17 +102,14 @@ public function fetch(array $arg_options = [])
}

/**
* Filters an array of sites by whether the user is an organizational member
* Filters the members of this collection by their names
*
* @param string $regex Non-delimited PHP regex to filter site names by
* @return Sites
*/
public function filterByName($regex = '(.*)')
{
return $this->filter(function ($site) use ($regex) {
preg_match("~$regex~", $site->get('name'), $matches);
return !empty($matches);
});
return $this->filterByRegex('name', $regex);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/Collections/TerminusCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,29 @@ public function fetch(array $options = [])
* Filters the members of this collection
*
* @param callable $filter Filter function
* TODO: This should not affect the original member array, but a copy should be kept and manipulated.
*/
public function filter(callable $filter)
{
$this->models = array_filter($this->getMembers(), $filter);
return $this;
}

/**
* Filters the models by a regex checked against a specific attribute
*
* @param string $attribute Name of the attribute to apply the regex filter to
* @param string $regex Non-delimited PHP regex to filter site names by
* @return TerminusCollection
*/
public function filterByRegex($attribute, $regex = '(.*)')
{
return $this->filter(function ($model) use ($attribute, $regex) {
preg_match("~$regex~", $model->get($attribute), $matches);
return !empty($matches);
});
}

/**
* Retrieves the model of the given ID
*
Expand Down
48 changes: 46 additions & 2 deletions src/Collections/Upstreams.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

namespace Pantheon\Terminus\Collections;

use Pantheon\Terminus\Exceptions\TerminusNotFoundException;
use Pantheon\Terminus\Friends\OrganizationsInterface;
use Pantheon\Terminus\Friends\OrganizationsTrait;
use Pantheon\Terminus\Models\Upstream;

/**
* Class Upstreams
* @package Pantheon\Terminus\Collections
*/
class Upstreams extends UserOwnedCollection
class Upstreams extends UserOwnedCollection implements OrganizationsInterface
{
use OrganizationsTrait;

public static $pretty_name = 'upstreams';
/**
* @var string
Expand All @@ -18,5 +23,44 @@ class Upstreams extends UserOwnedCollection
/**
* @var string
*/
protected $url = 'products';
protected $url = 'users/{user_id}/upstreams';

/**
* Adds a model to this collection
*
* @param object $model_data Data to feed into attributes of new model
* @param array $options Data to make properties of the new model
* @return TerminusModel
*/
public function add($model_data, array $options = [])
{
$model = parent::add($model_data, $options);
if (!empty($org_id = $model_data->organization_id)) {
try {
$model->setOrganization($this->getOrganizationMemberships()->get($org_id)->getOrganization());
} catch (TerminusNotFoundException $e) {
// Do nothing
}
}
return $model;
}

/**
* Filters an array of Upstreams by their label
*
* @param string $regex Non-delimited PHP regex to filter site names by
* @return Upstreams
*/
public function filterByName($regex = '(.*)')
{
return $this->filterByRegex('label', $regex);
}

/**
* @return UserOrganizationMemberships
*/
public function getOrganizationMemberships()
{
return $this->getUser()->getOrganizationMemberships();
}
}
42 changes: 42 additions & 0 deletions src/Commands/Org/Upstream/ListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Pantheon\Terminus\Commands\Org\Upstream;

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Pantheon\Terminus\Commands\Upstream\ListCommand as UserListCommand;

/**
* Class ListCommand
* @package Pantheon\Terminus\Commands\Org\Upstream
*/
class ListCommand extends UserListCommand
{
/**
* Displays the list of upstreams belonging to an organization.
*
* @command org:upstream:list
* @aliases org:upstreams
*
* @field-labels
* id: ID
* label: Name
* machine_name: Machine Name
* category: Category
* type: Type
* framework: Framework
* @param string $organization Organization name, label, or ID
* @option all Show all upstreams
* @option framework Framework filter
* @option name Name filter
* @return RowsOfFields
*
* @usage Displays the list of upstreams accessible to the currently logged-in user.
* @usage --all Displays upstreams of all types, including product.
* @usage --framework=<framework> Displays a list of accessible upstreams using the <framework> framework.
* @usage --name=<regex> Displays a list of accessible upstreams with a name that matches <regex>.
*/
public function listOrgUpstreams($organization, $options = ['all' => false, 'framework' => null, 'name' => null,])
{
return $this->listUpstreams(array_merge($options, ['org' => $organization,]));
}
}
9 changes: 5 additions & 4 deletions src/Commands/Remote/SSHBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Pantheon\Terminus\Site\SiteAwareTrait;
use Pantheon\Terminus\Exceptions\TerminusException;
use Pantheon\Terminus\Exceptions\TerminusProcessException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\ProcessUtils;

/**
Expand Down Expand Up @@ -84,6 +85,9 @@ protected function executeCommand(array $command_args)

/**
* Determine whether the use of a tty is appropriate for the current command.
*
* @param InputInterface $input
* @return bool|null
*/
protected function useTty($input)
{
Expand All @@ -94,11 +98,8 @@ protected function useTty($input)
// If we are in interactive mode (or at least the user did not
// specify -n / --no-interaction), then also prevent the use
// of a tty if stdout is redirected.
if (function_exists('posix_isatty') && !posix_isatty(STDOUT)) {
return false;
}
// Otherwise, let the local machine helper decide whether to use a tty.
return null;
return (function_exists('posix_isatty') && !posix_isatty(STDOUT)) ? false : null;
}

/**
Expand Down
21 changes: 11 additions & 10 deletions src/Commands/Site/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ class ListCommand extends SiteCommand
* frozen: Is Frozen?
* @return RowsOfFields
*
* @option team Team-only filter
* @option owner Owner filter; "me" or user UUID
* @option org Organization filter; "all" or an organization's name, label, or ID
* @option name Name filter
* @option org Organization filter; "all" or an organization's name, label, or ID
* @option owner Owner filter; "me" or user UUID
* @option team Team-only filter
*
* @usage Displays the list of all sites accessible to the currently logged-in user.
* @usage --team Displays the list of sites of which the currently logged-in user is a member of the team.
* @usage --owner=<user> Displays the list of accessible sites owned by the user with UUID <user>.
* @usage --owner=me Displays the list of sites owned by the currently logged-in user.
* @usage --name=<regex> Displays a list of accessible sites with a name that matches <regex>.
* @usage --org=<org> Displays a list of accessible sites associated with the <org> organization.
* @usage --org=all Displays a list of accessible sites associated with any organization of which the currently logged-in is a member.
* @usage --name=<regex> Displays a list of accessible sites with a name that matches <regex>.
* @usage --owner=<user> Displays the list of accessible sites owned by the user with UUID <user>.
* @usage --owner=me Displays the list of sites owned by the currently logged-in user.
* @usage --team Displays the list of sites of which the currently logged-in user is a member of the team.
*/
public function index($options = ['team' => false, 'owner' => null, 'org' => null, 'name' => null,])
public function index($options = ['name' => null, 'org' => 'all', 'owner' => null, 'team' => false,])
{
$user = $this->session()->getUser();
$this->sites()->fetch(
[
'org_id' => isset($options['org']) ? $options['org'] : null,
'org_id' => (isset($options['org']) && ($options['org'] !== 'all')) ? $user->getOrganizationMemberships()->get($options['org'])->getOrganization()->id : null,
'team_only' => isset($options['team']) ? $options['team'] : false,
]
);
Expand All @@ -52,7 +53,7 @@ public function index($options = ['team' => false, 'owner' => null, 'org' => nul
}
if (isset($options['owner']) && !is_null($owner = $options['owner'])) {
if ($owner == 'me') {
$owner = $this->session()->getUser()->id;
$owner = $user->id;
}
$this->sites->filterByOwner($owner);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Site/Upstream/SetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function set($site_name, $upstream_id)
{
$site = $this->getSite($site_name);
$upstream = $this->session()->getUser()->getUpstreams()->get($upstream_id);
$msg_params = ['site' => $site->getName(), 'upstream' => $upstream->get('longname'),];
$msg_params = ['site' => $site->getName(), 'upstream' => $upstream->get('label'),];

$this->log()->warning('This functionality is experimental. Do not use this on production sites.');
if (!$this->confirm('Are you sure you want change the upstream for {site} to {upstream}?', $msg_params)) {
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/Upstream/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class InfoCommand extends TerminusCommand
*
* @field-labels
* id: ID
* longname: Name
* category: Category
* label: Name
* machine_name: Machine Name
* type: Type
* framework: Framework
* upstream: URL
* author: Author
* repository_url: URL
* description: Description
* organization: Organization
* @return PropertyList
*
* @usage <upstream> Displays information about the <upstream> upstream.
Expand Down
Loading

0 comments on commit a20082e

Please sign in to comment.