Skip to content
Open
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "todaydesign/craft-static-dusk",
"description": "Static site builder for Craft and AWS Code Pipelines",
"type": "craft-plugin",
"version": "1.1.0",
"version": "1.2.0",
"keywords": [
"craft",
"cms",
Expand All @@ -22,7 +22,7 @@
}
],
"require": {
"craftcms/cms": "^3.0.0-RC1"
"craftcms/cms": ">=3.0.0-RC1"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 4 additions & 2 deletions src/CraftStaticDusk.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Craft;
use craft\base\Plugin;
use craft\base\Model;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\UrlManager;
Expand Down Expand Up @@ -63,7 +64,7 @@ class CraftStaticDusk extends Plugin
*
* @var string
*/
public $schemaVersion = '1.0.1';
public string $schemaVersion = '1.2.0';

// Public Methods
// =========================================================================
Expand Down Expand Up @@ -174,7 +175,7 @@ function (PluginEvent $event) {
*
* @return \craft\base\Model|null
*/
protected function createSettingsModel()
protected function createSettingsModel(): ?Model
{
return new Settings();
}
Expand All @@ -194,4 +195,5 @@ protected function settingsHtml(): string
]
);
}

}
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
2 changes: 1 addition & 1 deletion src/models/BuildSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BuildSettings extends Model
*
* @return array
*/
public function rules()
public function rules(): array
{
return [
['someAttribute', 'string'],
Expand Down
2 changes: 1 addition & 1 deletion src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Settings extends Model
*
* @return array
*/
public function rules()
public function rules(): array
{
return [
['someAttribute', 'string'],
Expand Down
23 changes: 14 additions & 9 deletions src/templates/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
{% set siteIds = siteIds ?? craft.app.sites.getEditableSiteIds() %}

{% if selectedSiteId is not defined %}
{% if craft.request.getQuery('site') %}
{% set selectedSiteId = craft.request.getQuery('site') %}
{% if craft.app.request.getQueryParam('site') is numeric %}
{% set selectedSiteId = craft.app.request.getQueryParam('site') %}
{% else %}
{% set selectedSiteId = siteIds|first %}
{% endif %}
Expand All @@ -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;
}

}