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

Basic configuration

Basic API
Webform handler to create a new project
parent a5d84052
Branches
Tags
No related merge requests found
gitlab_api.settings:
title: GitLab
parent: system.admin_config_system
route_name: gitlab_api.settings
weight: 10
gitlab_api.settings:
path: '/admin/config/system/settings'
defaults:
_title: 'GitLab'
_form: 'Drupal\gitlab_api\Form\Settings'
requirements:
_permission: 'administer site configuration'
services:
gitlab_api.api:
class: Drupal\gitlab_api\Api
arguments: ['@config.factory']
<?php
namespace Drupal\gitlab_api;
use Drupal\Core\Config\ConfigFactoryInterface;
use Gitlab\Client;
use Gitlab\Exception\InvalidArgumentException;
/**
* Class Api
*
* @package Drupal\gitlab_api
*/
class Api {
/**
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* @var \Gitlab\Client
*/
protected $client;
/**
* Api constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->config = $config_factory->get('gitlab_api.settings');
}
/**
* Initialize client and authenticate session.
*/
protected function init() {
if (!isset($this->client)) {
$this->client = Client::create($this->config->get('url'))
->authenticate($this->config->get('token'), Client::AUTH_URL_TOKEN);
}
}
/**
* @param string $namespace
* @param string $project_name
*
* @return string
*/
public function createProject($namespace, $project_name): string {
$this->init();
if (!$this->client->namespaces()->show($namespace)) {
throw new InvalidArgumentException('Invalid namespace');
}
return $this->client->projects()->create($project_name, [
'namespace' => $namespace,
]);
}
}
<?php
namespace Drupal\gitlab_api\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure GitLab API settings for this site.
*/
class Settings extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'gitlab_api_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['gitlab_api.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['url'] = [
'#type' => 'textfield',
'#title' => $this->t('URL'),
'#default_value' => $this->config('gitlab_api.settings')->get('url'),
];
$form['token'] = [
'#type' => 'textfield',
'#title' => $this->t('Authentication token'),
'#default_value' => $this->config('gitlab_api.settings')->get('token'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('gitlab_api.settings')
->set('url', $form_state->getValue('url'))
->set('token', $form_state->getValue('token'))
->save();
parent::submitForm($form, $form_state);
}
}
......@@ -2,8 +2,15 @@
namespace Drupal\gitlab_api\Plugin\WebformHandler;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\gitlab_api\Api;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionConditionsValidatorInterface;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Webform submission handler to create a new GitLab project.
......@@ -21,6 +28,92 @@ use Drupal\webform\WebformSubmissionInterface;
*/
class CreateProject extends WebformHandlerBase {
/**
* @var \Drupal\gitlab_api\Api
*/
protected $api;
/**
* CreateProject constructor.
*
* @param array $configuration
* @param $plugin_id
* @param $plugin_definition
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\webform\WebformSubmissionConditionsValidatorInterface $conditions_validator
* @param \Drupal\gitlab_api\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) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger_factory, $config_factory, $entity_type_manager, $conditions_validator);
$this->api = $api;
}
/**
* {@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'),
$container->get('config.factory'),
$container->get('entity_type.manager'),
$container->get('webform_submission.conditions_validator'),
$container->get('gitlab_api.api')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$field_names = array_keys(\Drupal::service('entity_field.manager')->getBaseFieldDefinitions('webform_submission'));
$excluded_data = array_combine($field_names, $field_names);
return [
'namespace' => '',
'pattern' => '',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$webform = $this->getWebform();
$form['namespace'] = [
'#type' => 'textfield',
'#title' => $this->t('Namespace'),
'#required' => TRUE,
'#default_value' => $this->configuration['namespace'],
];
$form['pattern'] = [
'#type' => 'textfield',
'#title' => $this->t('Project name pattern'),
'#required' => TRUE,
'#default_value' => $this->configuration['pattern'],
];
$this->elementTokenValidate($form);
return $this->setSettingsParents($form);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->applyFormStateToConfiguration($form_state);
}
/**
* {@inheritdoc}
*/
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
$state = $webform_submission->getWebform()->getSetting('results_disabled') ?
WebformSubmissionInterface::STATE_COMPLETED :
......@@ -28,7 +121,8 @@ class CreateProject extends WebformHandlerBase {
if ($this->configuration['states'] && in_array($state, $this->configuration['states'], TRUE)) {
$data = $webform_submission->getData();
$data['gitlab_project'] = '';
$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();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment