Skip to content

Commit

Permalink
chore: release version 1.20.0
Browse files Browse the repository at this point in the history
  • Loading branch information
K.I.T.T committed May 27, 2022
1 parent 2ce1070 commit 74def39
Show file tree
Hide file tree
Showing 18 changed files with 410 additions and 12 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.20.0](https://github.com/AmazeeLabs/silverback-mono/compare/@-amazeelabs/silverback_gatsby@1.19.5...@-amazeelabs/silverback_gatsby@1.20.0) (2022-05-27)


### Bug Fixes

* better naming for trigger ([9a78e52](https://github.com/AmazeeLabs/silverback-mono/commit/9a78e52a36687d737552b24fdda99c9dc255ecd8))


### Features

* periodic build ([3038e08](https://github.com/AmazeeLabs/silverback-mono/commit/3038e08bb1d3ed08a0c4d9886df870f30dbc7afb))





## [1.19.5](https://github.com/AmazeeLabs/silverback-mono/compare/@-amazeelabs/silverback_gatsby@1.19.4...@-amazeelabs/silverback_gatsby@1.19.5) (2022-05-12)


Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ query MainMenu {

The `@menu` directive also takes an optional `max_level` argument. It can be
used to restrict the number of levels a type will include, which in turn can
optimize caching and Gatsby build times.
optimize caching and Gatsby build times.
In many cases, the main page layout only displays the first level of menu items.
When a new page is created and attached to the third level, Gatsby will still
re-render all pages, because the menu that is used in the header changed. By
Expand Down Expand Up @@ -295,3 +295,22 @@ access control and use it for the "Preview" environment on Gatsby cloud, so
unpublished content can be previewed. Another sensible case would be to create a
"build" user that has access to published content and block anonymous access to
Drupal entirely.

## Trigger a build

There are multiple ways to trigger a Gatsby build:
- on entity save
- via the Drupal UI or Drush.

### On entity save

On the _Build_ tab of the schema configuration, check the _Trigger a build on entity save_ checkbox.

### Drupal UI

On the same _Build_ tab, click the _Gatsby Build_ button.

### Drush

This command can be configured in the system cron.
`drush silverback-gatsby:build [server_id]`
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "amazeelabs/silverback_gatsby",
"type": "drupal-module",
"version": "1.19.5",
"version": "1.20.0",
"description": "Bridge module between Gatsby and Drupal.",
"homepage": "https://silverback.netlify.app",
"license": "GPL-2.0+",
Expand Down
1 change: 1 addition & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ services:
arguments:
- '@entity_type.manager'
- '@plugin.manager.graphql.schema'
- '@silverback_gatsby.update_trigger'
tags:
- { name: drush.command }
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@-amazeelabs/silverback_gatsby",
"version": "1.19.5",
"version": "1.20.0",
"main": "index.js",
"scripts": {
"version": "sync-composer-version",
Expand All @@ -16,5 +16,5 @@
"repository": "git@github.com:AmazeeLabs/silverback_gatsby.git",
"branch": "main"
},
"gitHead": "c45047d9f948ad5c47976d307d088abb2d378951"
"gitHead": "78cebd6b4f85a05d86c2c924e6363bee4e603d06"
}
5 changes: 5 additions & 0 deletions silverback_gatsby.links.task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
entity.graphql_server.build_form:
route_name: entity.graphql_server.build_form
base_route: entity.graphql_server.edit_form
title: Build
weight: -1
23 changes: 23 additions & 0 deletions silverback_gatsby.module
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ function silverback_gatsby_entity_update(EntityInterface $entity) {
function silverback_gatsby_entity_delete(EntityInterface $entity) {
_silverback_gatsby_entity_event($entity);
}

/**
* Implements hook_entity_type_alter().
*/
function silverback_gatsby_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
foreach ($entity_types as $entity_type) {
if ($entity_type->id() === 'graphql_server') {
if (!$entity_type->hasHandlerClass('build')) {
$entity_type->setHandlerClass(
'build',
Drupal\silverback_gatsby\GraphQL\Build::class
);
}
if (!$entity_type->getFormClass('build')) {
$entity_type->setFormClass(
'build',
Drupal\silverback_gatsby\GraphQL\Build::class
);
}
}
}
}
2 changes: 2 additions & 0 deletions silverback_gatsby.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trigger a gatsby build:
title: 'Trigger a Gatsby Build'
9 changes: 9 additions & 0 deletions silverback_gatsby.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
entity.graphql_server.build_form:
path: '/admin/config/graphql/servers/build/{graphql_server}'
defaults:
_entity_form: 'graphql_server.build'
_title: 'Build'
requirements:
_permission: 'administer graphql configuration+trigger a gatsby build'
options:
_admin_route: TRUE
5 changes: 4 additions & 1 deletion silverback_gatsby.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ services:

silverback_gatsby.update_trigger:
class: Drupal\silverback_gatsby\GatsbyUpdateTrigger
arguments: ['@http_client', '@messenger', '@entity_type.manager']
arguments:
- '@http_client'
- '@messenger'
- '@entity_type.manager'

silverback_gatsby.update_tracker:
class: Drupal\silverback_gatsby\GatsbyUpdateTracker
Expand Down
24 changes: 23 additions & 1 deletion src/Commands/SilverbackGatsbyCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\silverback_gatsby\GatsbyUpdateTriggerInterface;
use Drupal\silverback_gatsby\GraphQL\ComposableSchema;
use Drush\Commands\DrushCommands;

Expand All @@ -22,11 +23,17 @@ class SilverbackGatsbyCommands extends DrushCommands {

protected EntityTypeManagerInterface $entityTypeManager;
protected PluginManagerInterface $schemaPluginManager;
protected GatsbyUpdateTriggerInterface $updateTrigger;

public function __construct(EntityTypeManagerInterface $entityTypeManager, PluginManagerInterface $schemaPluginManager) {
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
PluginManagerInterface $schemaPluginManager,
GatsbyUpdateTriggerInterface $updateTrigger
) {
parent::__construct();
$this->entityTypeManager = $entityTypeManager;
$this->schemaPluginManager = $schemaPluginManager;
$this->updateTrigger = $updateTrigger;
}

/**
Expand Down Expand Up @@ -64,4 +71,19 @@ public function schemaExport($folder = '../generated') {
file_put_contents($path, implode("\n", $definition));
}
}

/**
* Trigger a Gatsby build for a given GraphQL server.
*
* @param string $server
* The server id.
*
* @command silverback-gatsby:build
* @aliases sgb
* @usage silverback-gatsby:build [server_id]
*/
public function triggerBuild($server) {
$this->updateTrigger->triggerLatestBuild($server);
}

}
7 changes: 6 additions & 1 deletion src/GatsbyUpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public function handle(string $feedClassName, $context) {
}
}

// If the configuration is not set, assume TRUE.
$trigger = TRUE;
if (array_key_exists('build_trigger_on_save', $config[$schema_id])) {
$trigger = $config[$schema_id]['build_trigger_on_save'] === 1;
}
foreach ($schema->getExtensions() as $extension) {
if ($extension instanceof SilverbackGatsbySchemaExtension) {
foreach ($extension->getFeeds() as $feed) {
Expand All @@ -89,7 +94,7 @@ public function handle(string $feedClassName, $context) {
&& $updates = $feed->investigateUpdate($context, $account)
) {
foreach ($updates as $update) {
$this->gatsbyUpdateTracker->track($server->id(), $update->type, $update->id);
$this->gatsbyUpdateTracker->track($server->id(), $update->type, $update->id, $trigger);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/GatsbyUpdateTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function clear() : void {
/**
* {@inheritDoc}
*/
public function track(string $server, string $type, string $id) : int {
public function track(string $server, string $type, string $id, bool $trigger = TRUE) : int {
if (isset($this->tracked[$server]) && isset($this->tracked[$server][$type]) && in_array($id,$this->tracked[$server][$type])) {
return $this->latestBuild($server);
}
Expand All @@ -51,7 +51,9 @@ public function track(string $server, string $type, string $id) : int {
'uid' => $this->currentUser->id(),
'timestamp' => time(),
])->execute();
$this->trigger->trigger($server, $buildId);
if ($trigger) {
$this->trigger->trigger($server, $buildId);
}
return $buildId;
}

Expand Down
70 changes: 70 additions & 0 deletions src/GatsbyUpdateTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\graphql\Entity\ServerInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class GatsbyUpdateTrigger implements GatsbyUpdateTriggerInterface {

use StringTranslationTrait;

protected array $buildIds = [];
protected Client $httpClient;
protected MessengerInterface $messenger;
Expand Down Expand Up @@ -44,6 +49,71 @@ public function trigger(string $server, int $id) : void {
}
}

/**
* {@inheritDoc}
*/
public function triggerLatestBuild(string $server) : TranslatableMarkup {
$servers = $this->entityTypeManager
->getStorage('graphql_server')
->loadByProperties(['name' => $server]);

if (empty($servers)) {
$message = $this->t('No server found with id @server_id.', [
'@server_id' => $server,
]);
$this->messenger->addError($message);
return $message;
}

$serverEntity = reset($servers);
if ($serverEntity instanceof ServerInterface) {
// No dependency injection, prevent circular reference.
/** @var \Drupal\silverback_gatsby\GatsbyUpdateTrackerInterface $updateTracker */
$updateTracker = \Drupal::service('silverback_gatsby.update_tracker');
$latestBuildId = $updateTracker->latestBuild($serverEntity->id());
if (!$this->isFrontendLatestBuild($latestBuildId, $serverEntity)) {
$message = $this->t('Triggering a build for server @server_id.', [
'@server_id' => $server,
]);
$this->messenger->addStatus($message);
$this->trigger($serverEntity->id(), $latestBuildId);
}
else {
$message = $this->t('Build is already up-to-date for server @server_id.', [
'@server_id' => $server,
]);
$this->messenger->addStatus($message);
}
}

return $message;
}

/**
* Check on the frontend if the latest build already occurred.
*
* If the build url is not configured, presume false so the build
* will still happen.
*
* @param string $latestBuildId
* @param \Drupal\graphql\Entity\ServerInterface $serverEntity
*
* @return bool
*/
protected function isFrontendLatestBuild(int $latestBuildId, ServerInterface $serverEntity) {
$result = FALSE;
$configuration = $serverEntity->get('schema_configuration')[$serverEntity->get('schema')];
if (!empty($configuration['build_url'])) {
$response = $this->httpClient->get($configuration['build_url'] . '/build.json');
if ($response->getStatusCode() === 200) {
$content = json_decode($response->getBody()->getContents(), TRUE);
$buildId = array_key_exists('drupalBuildId', $content) ? (int) $content['drupalBuildId'] : 0;
$result = $latestBuildId === $buildId;
}
}
return $result;
}

protected function getWebhook($server_id) {
$server = $this->entityTypeManager
->getStorage('graphql_server')
Expand Down
16 changes: 16 additions & 0 deletions src/GatsbyUpdateTriggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\silverback_gatsby;

use Drupal\Core\StringTranslation\TranslatableMarkup;

interface GatsbyUpdateTriggerInterface {

/**
Expand All @@ -14,6 +16,20 @@ interface GatsbyUpdateTriggerInterface {
*/
public function trigger(string $server, int $id) : void;

/**
* Trigger a build for a given server based on latest build id.
*
* Compares the latest build id with the one on the frontend to not
* trigger unnecessary builds.
*
* @param string $server
* The server id.
*
* @return TranslatableMarkup
* The resut message.
*/
public function triggerLatestBuild(string $server) : TranslatableMarkup;

/**
* Send out notifications about potential updates to all Gatsby servers.
*/
Expand Down
Loading

0 comments on commit 74def39

Please sign in to comment.