Skip to content

Commit

Permalink
Add self info command for build tools. (#341)
Browse files Browse the repository at this point in the history
* Add self info command for build tools.

* Don't fatally error if a token is missing.

* Minor: spelling fixes.

Co-authored-by: Fatima Sarah Khalid <[email protected]>
  • Loading branch information
rvtraveller and sugaroverflow authored Dec 3, 2020
1 parent de2d8c0 commit 0ae99c3
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/API/GitHub/GitHubAPITrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function setCredentials(CredentialProviderInterface $credentials_provider
$tokenKey = $this->tokenKey();
$token = $credentials_provider->fetch($tokenKey);
if (!$token) {
throw new \Exception('Could not determine authentication token for GitHub serivces. Please set ' . $tokenKey);
throw new \Exception('Could not determine authentication token for GitHub services. Please set ' . $tokenKey);
}
$this->setToken($token);
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/GitLab/GitLabAPITrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function setCredentials(CredentialProviderInterface $credentials_provider
$tokenKey = $this->tokenKey();
$token = $credentials_provider->fetch($tokenKey);
if (!$token) {
throw new \Exception('Could not determine authentication token for GitLab serivces. Please set ' . $tokenKey);
throw new \Exception('Could not determine authentication token for GitLab services. Please set ' . $tokenKey);
}
$this->setToken($token);
}
Expand Down
90 changes: 90 additions & 0 deletions src/Commands/SelfInfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Terminus Plugin that contain a collection of commands useful during
* the build step on a [Pantheon](https://www.pantheon.io) site that uses
* a Git PR workflow.
*
* See README.md for usage information.
*/

namespace Pantheon\TerminusBuildTools\Commands;

use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Pantheon\TerminusBuildTools\ServiceProviders\CIProviders\CIProvider;
use Pantheon\TerminusBuildTools\ServiceProviders\RepositoryProviders\BaseGitProvider;
use Pantheon\TerminusBuildTools\ServiceProviders\RepositoryProviders\GitHub\GitHubProvider;
use Pantheon\TerminusBuildTools\ServiceProviders\RepositoryProviders\GitProvider;
use Pantheon\TerminusBuildTools\ServiceProviders\SiteProviders\SiteProvider;

/**
* Self Info Command
*/
class SelfInfoCommand extends BuildToolsBase
{

/**
* Show information about the Terminus Build Tools local environment.
*
* @field-labels
* build_tools_directory: Build Tools Installation Directory
* supported_providers: Providers with Tokens On Your System
* releases: Releases
* @return PropertyList
*
* @command build:self:info
*/
public function info()
{
$buildToolsDirectory = str_replace('/src/Commands', '', dirname(__FILE__));

$providerManager = $this->providerManager();

$providers = $providerManager->availableProviders();
foreach ($providers as $provider) {
$providerClass = new \ReflectionClass($provider);
if ($providerClass->implementsInterface(GitProvider::class)) {
$this->createGitProvider($provider);
}
elseif ($providerClass->implementsInterface(CIProvider::class)) {
$this->createCIProvider($provider);
}
elseif ($providerClass->implementsInterface(SiteProvider::class)) {
$this->createSiteProvider($provider);
}
}

$supportedProviders = [];
foreach ($providerManager->getProviders() as $provider) {
try {
$credentialRequests = $provider->credentialRequests();
foreach ($credentialRequests as $credentialRequest) {
$this->providerManager()->credentialManager()->addRequest($credentialRequest);
$provider->setCredentials($this->providerManager()->credentialManager());
}
$secretValues = $provider->getSecretValues();
$validProvider = TRUE;
foreach ($secretValues as $value) {
if (strlen($value) > 1) {
continue;
}
else {
$validProvider = FALSE;
}
}
if ($validProvider) {
$supportedProviders[] = $provider->getServiceName();
}
}
catch (\Exception $e) {
// Some providers like to throw a fatal error if they don't have credentials.
// Catch it here and just keep oging, since they shouldn't be on our list anyway.
}
}

return new PropertyList([
'build_tools_directory' => $buildToolsDirectory,
'supported_providers' => implode(', ', $supportedProviders),
'releases' => 'https://github.com/pantheon-systems/terminus-build-tools-plugin/releases',
]);
}
}
6 changes: 6 additions & 0 deletions src/ServiceProviders/CIProviders/BaseCIProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ protected function findIntersecting($existing, $vars)
}
return $intersecting;
}

public function getSecretValues() {
return [
'token' => $this->token()
];
}
}
3 changes: 3 additions & 0 deletions src/ServiceProviders/ProviderEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function token($key = false)
if (!$key) {
$key = $this->token_key;
}
if (!isset($this[$key])) {
return NULL;
}
return $this[$key];
}

Expand Down
9 changes: 7 additions & 2 deletions src/ServiceProviders/ProviderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(CredentialManager $credential_manager, Config $confi
$this->config = $config;
}

protected function availableProviders()
public function availableProviders()
{
return [
'\Pantheon\TerminusBuildTools\ServiceProviders\CIProviders\CircleCI\CircleCIProvider',
Expand Down Expand Up @@ -92,7 +92,7 @@ public function createProvider($providerClass, $expectedInterface)
return $this->initializeProvider($provider);
}

protected function initializeProvider($provider)
public function initializeProvider($provider)
{
if ($provider instanceof LoggerAwareInterface) {
$provider->setLogger($this->logger);
Expand Down Expand Up @@ -130,4 +130,9 @@ public function validateCredentials()
}
}
}

public function getProviders()
{
return $this->providers;
}
}
9 changes: 9 additions & 0 deletions src/ServiceProviders/SiteProviders/PantheonProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,13 @@ public function addPublicKey(CIState $ci_env, $publicKey)
// Add the public key to Pantheon.
$this->session()->getUser()->getSSHKeys()->addKey($publicKey);
}

/**
* Retrieves this providers tokens.
*/
public function getSecretValues() {
return [
'token' => $this->machineToken
];
}
}

0 comments on commit 0ae99c3

Please sign in to comment.