Skip to content
Snippets Groups Projects
Commit 3962ef81 authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Properly create new project and dispatch event for other modules to hook into this

parent 0aa51cdb
No related branches found
No related tags found
No related merge requests found
......@@ -44,20 +44,24 @@ class Api {
/**
* @param string $namespace
* @param string $project_name
* @param string $path
* @param string $name
*
* @return string
* @return array
*/
public function createProject($namespace, $project_name): string {
public function createProject($namespace, $path, $name): array {
$this->init();
if (!$this->client->namespaces()->show($namespace)) {
$gitlab_namespace = $this->client->namespaces()->show($namespace);
if (!$gitlab_namespace) {
throw new InvalidArgumentException('Invalid namespace');
}
return $this->client->projects()->create($project_name, [
'namespace' => $namespace,
$project = $this->client->projects()->create($name, [
'namespace_id' => $gitlab_namespace['id'],
'path' => $path,
]);
return $project;
}
}
<?php
namespace Drupal\gitlab_api\Event;
use Symfony\Component\EventDispatcher\Event;
/**
* Class CreateProject Event
*
* @package Drupal\gitlab_api\Event
*/
class CreateProject extends Event {
/**
* @var array
*/
protected $project;
/**
* CreateProject Event constructor.
*/
public function __construct(array $project) {
$this->project = $project;
}
/**
* @return array
*/
public function getProject() {
return $this->project;
}
}
<?php
namespace Drupal\gitlab_api;
/**
* Contains all events thrown by the wayfinding module.
*/
final class GitLabEvents {
const CreateProject = 'create project';
}
......@@ -3,14 +3,18 @@
namespace Drupal\gitlab_api\Plugin\WebformHandler;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\gitlab_api\Api;
use Drupal\gitlab_api\Event\CreateProject as CreateProjectEvent;
use Drupal\gitlab_api\GitLabEvents;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionConditionsValidatorInterface;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Webform submission handler to create a new GitLab project.
......@@ -33,6 +37,11 @@ class CreateProject extends WebformHandlerBase {
*/
protected $api;
/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* CreateProject constructor.
*
......@@ -44,13 +53,14 @@ class CreateProject extends WebformHandlerBase {
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\webform\WebformSubmissionConditionsValidatorInterface $conditions_validator
* @param \Drupal\gitlab_api\Api $api
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, WebformSubmissionConditionsValidatorInterface $conditions_validator, Api $api) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, WebformSubmissionConditionsValidatorInterface $conditions_validator, Api $api, EventDispatcherInterface $event_dispatcher) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger_factory, $config_factory, $entity_type_manager, $conditions_validator);
$this->api = $api;
$this->eventDispatcher = $event_dispatcher;
}
/**
* {@inheritdoc}
*/
......@@ -63,27 +73,32 @@ class CreateProject extends WebformHandlerBase {
$container->get('config.factory'),
$container->get('entity_type.manager'),
$container->get('webform_submission.conditions_validator'),
$container->get('gitlab_api.api')
$container->get('gitlab_api.api'),
$container->get('event_dispatcher')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$field_names = array_keys(\Drupal::service('entity_field.manager')->getBaseFieldDefinitions('webform_submission'));
$excluded_data = array_combine($field_names, $field_names);
public function defaultConfiguration(): array {
return [
'namespace' => '',
'pattern' => '',
'path_pattern' => '',
'name_pattern' => '',
'field_gitlab_url' => '',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$webform = $this->getWebform();
$fields = [];
foreach ($webform->getElementsInitializedAndFlattened() as $item) {
$fields[$item['#webform_key']] = $item['#title'];
}
$form['namespace'] = [
'#type' => 'textfield',
......@@ -91,11 +106,24 @@ class CreateProject extends WebformHandlerBase {
'#required' => TRUE,
'#default_value' => $this->configuration['namespace'],
];
$form['pattern'] = [
$form['path_pattern'] = [
'#type' => 'textfield',
'#title' => $this->t('Project path pattern'),
'#required' => TRUE,
'#default_value' => $this->configuration['path_pattern'],
];
$form['name_pattern'] = [
'#type' => 'textfield',
'#title' => $this->t('Project name pattern'),
'#required' => TRUE,
'#default_value' => $this->configuration['pattern'],
'#default_value' => $this->configuration['name_pattern'],
];
$form['field_gitlab_url'] = [
'#type' => 'select',
'#title' => $this->t('Field to store GitLab URL'),
'#options' => $fields,
'#required' => TRUE,
'#default_value' => $this->configuration['field_gitlab_url'],
];
$this->elementTokenValidate($form);
......@@ -119,12 +147,17 @@ class CreateProject extends WebformHandlerBase {
WebformSubmissionInterface::STATE_COMPLETED :
$webform_submission->getState();
if ($this->configuration['states'] && in_array($state, $this->configuration['states'], TRUE)) {
$data = $webform_submission->getData();
$project_name = $token_value = $this->replaceTokens($this->configuration['pattern'], $webform_submission);
$data['gitlab_project'] = $this->api->createProject($this->configuration['namespace'], $project_name);
$webform_submission->setData($data);
$webform_submission->save();
$path = $this->replaceTokens($this->configuration['path_pattern'], $webform_submission);
$name = $this->replaceTokens($this->configuration['name_pattern'], $webform_submission);
$project = $this->api->createProject($this->configuration['namespace'], $path, $name);
$webform_submission->set($this->configuration['field_gitlab_url'], $project);
try {
$webform_submission->save();
} catch (EntityStorageException $e) {
// TODO: handle exception.
}
$event = new CreateProjectEvent($project);
$this->eventDispatcher->dispatch(GitLabEvents::CreateProject, $event);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment