Skip to content
Merged
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
117 changes: 61 additions & 56 deletions src/managers/StaticBuildManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,46 @@ public function getScheduledBuilds($site)
{
$settings = CraftStaticDusk::$plugin->getSettings();

$payload = [
'secret' => Craft::parseEnv($settings->webHookSecret),
'repo' => Craft::parseEnv($settings->gitRepo),
'ref' => Craft::parseEnv($settings->gitRef),
'envName' => Craft::parseEnv($settings->environmentName),
'site' => $site
];

$response = null;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => Craft::parseEnv($settings->webHookUrl) . '/scheduled',
if (Craft::parseEnv($settings->webHookType) === 'GH') {
$payload = [
'secret' => Craft::parseEnv($settings->webHookSecret),
'repo' => Craft::parseEnv($settings->gitRepo),
'ref' => Craft::parseEnv($settings->gitRef),
'envName' => Craft::parseEnv($settings->environmentName),
'site' => $site
];

$response = null;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => Craft::parseEnv($settings->webHookUrl) . '/scheduled',
// CURLOPT_URL => "http://host.docker.internal:3000/static-build/scheduled",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode((object)$payload),
CURLOPT_VERBOSE => TRUE,
));
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode((object)$payload),
CURLOPT_VERBOSE => TRUE,
));

$response = curl_exec($curl);
$response = curl_exec($curl);

if ($response === false) {
return [];
}
if ($response === false) {
return [];
}

$response = json_decode($response);
$response = json_decode($response);

curl_close($curl);
curl_close($curl);

if (property_exists($response, "responseObject")) {
if ($response !== null && property_exists($response, "responseObject")) {

// Convert unix milliseconds to seconds
$data = array_map(function ($build) {
$build->LaunchTime = $build->LaunchTime / 1000;
return $build;
}, $response->responseObject);
// Convert unix milliseconds to seconds
$data = array_map(function ($build) {
$build->LaunchTime = $build->LaunchTime / 1000;
return $build;
}, $response->responseObject);

return $response->responseObject;
return $response->responseObject;
}
}

return [];
Expand All @@ -69,38 +71,41 @@ public function getScheduledBuilds($site)
public function getBuildHistory($site)
{
$settings = CraftStaticDusk::$plugin->getSettings();
$ref = str_replace("refs/heads/", "", Craft::parseEnv($settings->gitRef));

$payload = [
'secret' => Craft::parseEnv($settings->webHookSecret),
'repo' => Craft::parseEnv($settings->gitRepo),
'ref' => $ref,
'site' => $site
];

$response = null;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => Craft::parseEnv($settings->webHookUrl) . '/history',

if (Craft::parseEnv($settings->webHookType) === 'GH') {
$ref = str_replace("refs/heads/", "", Craft::parseEnv($settings->gitRef));

$payload = [
'secret' => Craft::parseEnv($settings->webHookSecret),
'repo' => Craft::parseEnv($settings->gitRepo),
'ref' => $ref,
'site' => $site
];

$response = null;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => Craft::parseEnv($settings->webHookUrl) . '/history',
// CURLOPT_URL => "http://host.docker.internal:3000/static-build/history",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode((object)$payload),
CURLOPT_VERBOSE => TRUE,
));
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode((object)$payload),
CURLOPT_VERBOSE => TRUE,
));

$response = curl_exec($curl);
$response = curl_exec($curl);

if ($response === false) {
return [];
}
if ($response === false) {
return [];
}

$response = json_decode($response);
$response = json_decode($response);

curl_close($curl);
curl_close($curl);

if (property_exists($response, "responseObject")) {
return $response->responseObject;
if ($response !== null && property_exists($response, "responseObject")) {
return $response->responseObject;
}
}

return [];
Expand Down
19 changes: 12 additions & 7 deletions src/templates/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
{% set buildHistory = craft.craftStaticDusk.getBuildHistory(site.handle) %}

{% set isMissingEnvVariables = craft.craftStaticDusk.isMissingEnvVariables() %}
{% set isGhWebhook = craft.craftStaticDusk.isGhWebhook() %}

{# Content that should appear in the page header #}
{% set extraPageHeaderHtml %}
Expand All @@ -67,7 +68,7 @@
<li><h2><span data-icon="alert" aria-label="Error"></span><strong>Warning:</strong> Missing environment variables</h2>
</li>
<li>
Make sure that the environment variables <strong>webHookSecret, webHookUrl, gitRepo, gitRef</strong> and <strong>environmentName</strong> are set in the project.yaml and are correctly set in the CI
Make sure that the environment variables {% if isGhWebhook %}<strong>webHookSecret, webHookUrl, gitRepo, gitRef</strong> and <strong>environmentName</strong>{% else %}<strong>webHookSecret</strong>, <strong>webHookUrl</strong> and <strong>environmentName</strong>{% endif %} are set in the project.yaml and are correctly set in the CI
</li>
</ul>
{% endif %}
Expand Down Expand Up @@ -107,6 +108,7 @@
<br>
<br>

{% if isGhWebhook %}
<h2>When do you want to launch the build?</h2>
<div>
<input
Expand All @@ -132,8 +134,9 @@
</div>

<br>


{% else %}
<br>
{% endif %}

<form method="post" action="" class="launch_now">
{{ csrfInput() }}
Expand All @@ -142,6 +145,7 @@
<input type="submit" class="submit btn" value="Launch"/>
</form>

{% if isGhWebhook %}
<form method="post" action="" class="schedule" style="display: none">
{{ csrfInput() }}
{{ hiddenInput('siteHandle', site.handle) }}
Expand Down Expand Up @@ -215,10 +219,10 @@
<p>Please refresh the page to update the list</p>
<table id="types-craft-fields-Dropdown-options" class="editable fullwidth">
<thead>
<tr>
<th scope="col" class="code singleline-cell textual">Status</th>
<th scope="col" class="code singleline-cell textual">Start time</th>
</tr>
<tr>
<th scope="col" class="code singleline-cell textual">Status</th>
<th scope="col" class="code singleline-cell textual">Start time</th>
</tr>
</thead>
<tbody>

Expand All @@ -245,6 +249,7 @@
{% endfor %}
</tbody>
</table>
{% endif %}



Expand Down
35 changes: 28 additions & 7 deletions src/variables/CraftStaticDuskVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public function getBuildHistory($site)
return $results;
}

/**
* Whether scheduled builds and build history are available (GH webhook type only).
*
* @return bool
*/
public function isGhWebhook()
{
$settings = CraftStaticDusk::$plugin->getSettings();
return Craft::parseEnv($settings->webHookType) === 'GH';
}

/**
* Check if Environment variables are missing
*
Expand All @@ -65,20 +76,30 @@ public function getBuildHistory($site)
public function isMissingEnvVariables()
{
$settings = CraftStaticDusk::$plugin->getSettings();
$isGh = Craft::parseEnv($settings->webHookType) === 'GH';

$webHookSecret = Craft::parseEnv($settings->webHookSecret);
$gitRepo = Craft::parseEnv($settings->gitRepo);
$gitRef = Craft::parseEnv($settings->gitRef);
$environmentName = Craft::parseEnv($settings->environmentName);

$webHookUrl = Craft::parseEnv($settings->webHookUrl);

return (
$missing = (
empty($webHookSecret) || $webHookSecret === '$STATIC_BUILD_WEBHOOK_SECRET' ||
empty($gitRepo) || $gitRepo === '$STATIC_BUILD_GIT_REPO' ||
empty($gitRef) || $gitRef === '$STATIC_BUILD_GIT_REF' ||
empty($environmentName) || $environmentName === '$STATIC_BUILD_WEBHOOK_URL' ||

empty($webHookUrl) || $webHookUrl === '$STATIC_BUILD_WEBHOOK_URL'
);

if ($isGh) {
$gitRepo = Craft::parseEnv($settings->gitRepo);
$gitRef = Craft::parseEnv($settings->gitRef);
$environmentName = Craft::parseEnv($settings->environmentName);

$missing = $missing ||
empty($gitRepo) || $gitRepo === '$STATIC_BUILD_GIT_REPO' ||
empty($gitRef) || $gitRef === '$STATIC_BUILD_GIT_REF' ||
empty($environmentName) || $environmentName === '$ENVIRONMENT_NAME';
}

return $missing;
}

}