Skip to content

Commit c7f3700

Browse files
authored
[DEVX-5268] Rebuild from vcs for evcs sites. (#2711)
* Rebuild from vcs for evcs sites. * Fixes to get rebuild working. * Fix response handling. * Fix linting.
1 parent f61cb52 commit c7f3700

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

src/Commands/Env/CodeRebuildCommand.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
use Pantheon\Terminus\Exceptions\TerminusException;
88
use Pantheon\Terminus\Site\SiteAwareInterface;
99
use Pantheon\Terminus\Site\SiteAwareTrait;
10+
use Pantheon\Terminus\Request\RequestAwareInterface;
11+
use Pantheon\Terminus\Request\RequestAwareTrait;
1012

1113
/**
1214
* Class CodeRebuildCommand.
1315
*
1416
* @package Pantheon\Terminus\Commands\Env
1517
*/
16-
class CodeRebuildCommand extends TerminusCommand implements SiteAwareInterface
18+
class CodeRebuildCommand extends TerminusCommand implements SiteAwareInterface, RequestAwareInterface
1719
{
1820
use SiteAwareTrait;
1921
use WorkflowProcessingTrait;
22+
use RequestAwareTrait;
2023

2124
/**
2225
* Moves code to the specified environment's runtime from the associated git branch, retriggering Composer builds for sites using Integrated Composer. (Not applicable for Test and Live environments which run on git tags made from the Dev environment's git history.)
@@ -40,6 +43,14 @@ public function rebuild(
4043
$site = $this->getSiteById($site_env);
4144
$env = $this->getEnv($site_env);
4245

46+
if ($site->isEvcs()) {
47+
if (($env->getName() === 'test' || $env->getName() === 'live') && !$site->isNodejs()) {
48+
// Rebuilding for test/live is only supported for Node.js sites.
49+
throw new TerminusException('Rebuilding for test/live is only supported for Node.js sites.');
50+
}
51+
return $this->rebuildFromVcs($site->get('id'), $env->getName());
52+
}
53+
4354
if ($env->getName() === 'test' || $env->getName() === 'live') {
4455
throw new TerminusException('Test and live are not valid environments for this command.');
4556
}
@@ -56,4 +67,51 @@ public function rebuild(
5667
$this->processWorkflow($workflow);
5768
$this->log()->notice($workflow->getMessage());
5869
}
70+
71+
/**
72+
* Rebuild from latest vcs event.
73+
*/
74+
protected function rebuildFromVcs(string $site_id, string $env)
75+
{
76+
$path = sprintf("%s/vcs/v1/site-details/%s/environments/%s/rebuild", $this->getBaseURI(), $site_id, $env);
77+
$response = $this->request()->request($path, [
78+
'method' => 'POST',
79+
'json' => [],
80+
'headers' => [
81+
'Authorization' => sprintf(
82+
'Bearer %s',
83+
$this->session()->get('session')
84+
),
85+
],
86+
]);
87+
if ($response->getStatusCode() !== 201) {
88+
throw new TerminusException(
89+
'Failed to rebuild from VCS for site {site} environment {env}. Status Code: {status_code}',
90+
['site' => $site_id, 'env' => $env, 'status_code' => $response->getStatusCode()]
91+
);
92+
}
93+
$this->log()->info(
94+
"Rebuild is now happening for site {site} environment {env}.",
95+
['site' => $site_id, 'env' => $env]
96+
);
97+
}
98+
99+
/**
100+
* Get API Base Uri.
101+
*/
102+
/**
103+
* Parses the base URI for requests.
104+
*
105+
* @return string
106+
*/
107+
private function getBaseURI()
108+
{
109+
$config = $this->getConfig();
110+
return sprintf(
111+
'%s://%s:%s',
112+
$config->get('protocol'),
113+
$config->get('host'),
114+
$config->get('port')
115+
);
116+
}
59117
}

0 commit comments

Comments
 (0)