drupalorg-1.0.x-dev/src/Plugin/QueueWorker/DrupalOrgProjectActivityWebhookQueueWorker.php
src/Plugin/QueueWorker/DrupalOrgProjectActivityWebhookQueueWorker.php
<?php
namespace Drupal\drupalorg\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\drupalorg\ProjectService;
use Drupal\drupalorg\Traits\GitLabClientTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines 'drupalorg_project_activity_webhook_queue_worker' queue worker.
*
* Run via `drush` like this:
* `drush queue:run drupalorg_project_activity_webhook_queue_worker`.
*
* @QueueWorker(
* id = "drupalorg_project_activity_webhook_queue_worker",
* title = @Translation("Project Activity Webhook Queue Worker")
* )
*/
class DrupalOrgProjectActivityWebhookQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
use GitLabClientTrait;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('logger.factory')->get('drupalorg'),
$container->get('drupalorg.project_service'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected LoggerInterface $logger,
protected ProjectService $projectService,
protected EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public function processItem($data) {
if (!$this->validateItem($data)) {
return FALSE;
}
if ($data['event_name'] == 'repository_update') {
$this->processRepositoryUpdate($data);
}
}
/**
* Process the project update item.
*
* @param array $data
* Data coming from the webhook.
*/
protected function processRepositoryUpdate(array $data): void {
try {
$gitlab_client = $this->getGitLabClient();
$gitlab_project = $gitlab_client->projects()->show($data['project_id']);
if ($project_node = $this->projectService->getProjectByRepositoryPath($gitlab_project['path_with_namespace'])) {
$this->projectService->updateLogo($project_node, $gitlab_project['avatar_url'] ?? '');
}
}
catch (\Throwable $e) {
$this->logger->error('Could not process project @id update. Code @code. (At @at) Message: @message', [
'@id' => $data['project_id'],
'@code' => $e->getCode(),
'@at' => $e->getFile() . ':' . $e->getLine(),
'@message' => $e->getMessage(),
]);
}
}
/**
* Validate item array to make sure all key elements are there.
*
* @param array $data
* Item to validate.
*
* @return bool
* Whether the item was valid or not.
*/
protected function validateItem(array $data) {
if (empty($data['event_name'])) {
return FALSE;
}
$allowed_events = [
'repository_update',
];
if (!in_array($data['event_name'], $allowed_events)) {
return FALSE;
}
if ($data['event_name'] === 'repository_update') {
if (empty($data['project_id'])) {
return FALSE;
}
}
return TRUE;
}
}
