Skip to content
Snippets Groups Projects
Commit ade03bf9 authored by Marcelo Vani's avatar Marcelo Vani
Browse files

#3507409 Refactored code, moved github controller to its own module.

parent db6bc532
No related branches found
No related tags found
1 merge request!12Refactored code, moved github controller to its own module.
Showing
with 165 additions and 71 deletions
......@@ -47,9 +47,17 @@ INSTALLATION
Use composer to make sure you will have all dependencies.
`composer require drupal/config_pr:^1.0`
You need to enable the module for the repo client that you require, these are the available options:
- Config Pull Request Github Support
- Config Pull Request Gitlab Support
- Config Pull Request BitBucket Support (Work in progress)
then use composer require for the dev libraries that you need i.e. For Gitlab
`composer require m4tthumphrey/php-gitlab-api:^11.14 guzzlehttp/guzzle:^7.8 http-interop/http-factory-guzzle:^1.2`
then use composer require for the dev libraries that you need i.e. For Github
`composer require m4tthumphrey/php-gitlab-api:^11.14 guzzlehttp/guzzle:^7.8 http-interop/http-factory-guzzle:^1.2`
CONFIGURATION
-------------
......@@ -61,7 +69,7 @@ CONFIGURATION
3. Make sure you set the right permissions to allow the token to be used to create new branches and pull requests.
4. Navigate to the module settings page,
/admin/config/development/configuration/pull_request/settings and add the
repo owner name and repo name. Normally these are found on the repo Url.
repo user name and repo name. Normally these are found on the repo Url.
i.e. https://github.com/marcelovani/captcha_keypad
the username = marcelovani and repo name = captcha_keypad
......
......@@ -4,15 +4,3 @@ services:
arguments: ['@messenger']
tags:
- { name: service_collector, tag: config_pr.repo_controller, call: addController }
config_pr.repo_controller.github:
class: Drupal\config_pr\RepoControllers\GithubController
arguments: ['@messenger']
tags:
- { name: config_pr.repo_controller }
config_pr.repo_controller.github_enterprise:
class: Drupal\config_pr\RepoControllers\GithubEnterpriseController
arguments: ['@messenger']
tags:
- { name: config_pr.repo_controller }
Config Pull Request Github
==========================
This is a sub-module for Config Pull Request.
It adds support for Github.
Dependencies
============
- php-http/guzzle7-adapter:^1.0
- knplabs/github-api:^3.16
Installation
============
Use composer to make sure you will have all dependencies.
`composer require php-http/guzzle7-adapter:^1.0 knplabs/github-api:^3.16`
{
"name": "drupal/config_pr_config_pr_github",
"description": "Github controller for Config Pull Request module.",
"type": "drupal-module",
"license": "GPL-2.0+",
"require": {
"php": ">=8.0",
"config_pr/config_pr": "^1.0",
"php-http/guzzle7-adapter": "^1.0",
"knplabs/github-api": "^3.16"
}
}
\ No newline at end of file
name: 'Config Pull Request Github Support'
type: module
description: 'Github controllers for Config Pull Request module.'
package: Administration
core_version_requirement: ">=8"
configure: config_pr.settings
dependencies:
- config_pr:config_pr
<?php
/**
* @file
* Module file for Config pull request Github.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Alter the settings form for Config pull request settings.
*/
function config_pr_github_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id !== 'config_pr_settings_form' || !isset($form['repo'])) {
return;
}
if ($form['repo']['repo_controller']['#default_value'] === 'config_pr_github.repo_controller.github_enterprise') {
$form['repo']['repo_url']['#title'] = t('Repository URL');
$form['repo']['repo_url']['#description'] = t('Enter the URL for the Github enterprise repository.');
$form['repo']['repo_url']['#required'] = TRUE;
}
}
services:
config_pr_github.repo_controller.github:
class: Drupal\config_pr_github\RepoControllers\GithubController
arguments: ['@messenger']
tags:
- { name: config_pr.repo_controller }
config_pr_github.repo_controller.github_enterprise:
class: Drupal\config_pr_github\RepoControllers\GithubEnterpriseController
arguments: ['@messenger']
tags:
- { name: config_pr.repo_controller }
<?php
namespace Drupal\config_pr\RepoControllers;
namespace Drupal\config_pr_github\RepoControllers;
use Github\Api\AbstractApi;
......
<?php
namespace Drupal\config_pr\RepoControllers;
namespace Drupal\config_pr_github\RepoControllers;
use Drupal\config_pr\RepoControllerInterface;
use Drupal\config_pr\RepoControllerTrait;
......@@ -38,7 +38,7 @@ class GithubController implements RepoControllerInterface {
* @var string
* The controller id.
*/
protected $controllerId = 'config_pr.repo_controller.github';
protected $controllerId = 'config_pr_github.repo_controller.github';
/**
* Holds the Github client instance.
......@@ -69,7 +69,9 @@ class GithubController implements RepoControllerInterface {
* {@inheritdoc}
*/
public function authenticate(): bool {
$this->client = new \Github\Client();
if (empty($this->client)) {
$this->client = new \Github\Client();
}
$this->client->authenticate(
$this->getAuthToken(),
NULL,
......@@ -146,6 +148,10 @@ class GithubController implements RepoControllerInterface {
return $this->branches;
}
public function setClient($client) {
$this->client = $client;
}
/**
* @inheritDoc
*/
......
<?php
namespace Drupal\config_pr\RepoControllers;
namespace Drupal\config_pr_github\RepoControllers;
use Github\Client;
use Github\HttpClient\Builder;
use Http\Discovery\Psr18ClientDiscovery;
/**
* Class to define the Github Enterprise controller.
......@@ -25,15 +26,32 @@ class GithubEnterpriseController extends GithubController {
* @var string
* The controller id.
*/
protected $controllerId = 'config_pr.repo_controller.github_enterprise';
protected $controllerId = 'config_pr_github.repo_controller.github_enterprise';
/**
* {@inheritdoc}
*/
public function authenticate(): bool {
$repo_url = \Drupal::service('config.factory')->get('config_pr.settings')->get('repo.repo_url');
$this->client = new Client(NULL, NULL, $repo_url);
$this->client->authenticate($this->getAuthToken(), NULL, Github\AuthMethod::ACCESS_TOKEN);
// Discover PSR-18 client
$httpClient = Psr18ClientDiscovery::find();
$builder = new Builder($httpClient);
$this->client = new \Github\Client($builder, NULL, $repo_url);
$this->client->authenticate(
$this->getAuthToken(),
NULL,
\Github\AuthMethod::ACCESS_TOKEN
);
// Check if we can get project details.
if ($this->getProjectDetails()) {
return TRUE;
}
return FALSE;
}
}
......@@ -4,12 +4,13 @@ Config Pull Request GitLab
This is a sub-module for Config Pull Request.
It adds support for GitLab.
Installation
============
Use composer to make sure you will have all dependencies.
`composer require m4tthumphrey/php-gitlab-api:^11`
Dependencies
============
- m4tthumphrey/php-gitlab-api:^11
- guzzlehttp/guzzle:^7.8
- http-interop/http-factory-guzzle:^1.2
Installation
============
Use composer to make sure you will have all dependencies.
`composer require m4tthumphrey/php-gitlab-api:^11 guzzlehttp/guzzle:^7.8 http-interop/http-factory-guzzle:^1.2`
......@@ -4,8 +4,8 @@
"type": "drupal-module",
"license": "GPL-2.0+",
"require": {
"php": ">=8.0",
"config_pr/config_pr": "^1.0",
"php": ">=8.0",
"config_pr/config_pr": "^1.0",
"m4tthumphrey/php-gitlab-api": "^11",
"guzzlehttp/guzzle": "^7.8",
"http-interop/http-factory-guzzle": "^1.2"
......
......@@ -15,30 +15,10 @@ function config_pr_gitlab_form_alter(&$form, FormStateInterface $form_state, $fo
return;
}
// Add repo url field.
$form['repo']['repo_url'] = [
'#type' => 'textfield',
'#title' => t('Repository URL'),
'#description' => t('Enter the URL for the self managed repository.'),
'#default_value' => \Drupal::config('config_pr.settings')->get('repo.repo_url') ?? '',
'#states' => [
'visible' => [
':input[name="repo_controller"]' => [
'value' => 'config_pr_gitlab.repo_controller.gitlab_self_managed',
],
],
'required' => [
':input[name="repo_controller"]' => [
'value' => 'config_pr_gitlab.repo_controller.gitlab_self_managed',
],
],
],
];
// Remove owner/repo name fields.
if ($form['repo']['repo_controller']['#default_value'] === 'config_pr_gitlab.repo_controller.gitlab_self_managed') {
$form['repo']['repo_owner']['#access'] = FALSE;
$form['repo']['repo_name']['#access'] = FALSE;
$form['repo']['repo_url']['#title'] = t('Repository URL');
$form['repo']['repo_url']['#description'] = t('Enter the URL for the self managed repository.');
$form['repo']['repo_url']['#required'] = TRUE;
}
}
......@@ -2,15 +2,8 @@
namespace Drupal\config_pr_gitlab\RepoControllers;
use Gitlab\Client;
use Gitlab\HttpClient\Builder;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle7\Client as GuzzleAdapter;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Http\Message\StreamFactory\GuzzleStreamFactory;
use Psr\Http\Client\ClientInterface;
/**
* Class to define the Gitlab self managed controller.
......@@ -45,7 +38,8 @@ class GitlabSelfManagedController extends GitlabController {
$httpClient = Psr18ClientDiscovery::find();
$builder = new Builder($httpClient);
$this->client = new \Gitlab\Client($builder, null, $repo_url);
$this->client = new \Gitlab\Client($builder);
$this->client->setUrl($repo_url);
$this->client->authenticate(
$this->getAuthToken(),
......
......@@ -96,23 +96,11 @@ class ConfigPrSettingsForm extends ConfigFormBase {
$form['repo']['repo_url'] = [
'#type' => 'textfield',
'#title' => $this->t('Enterprise repository URL'),
'#description' => $this->t('Enter the Github Enterprise URL.'),
'#default_value' => $this->config('config_pr.settings')->get('repo.repo_url') ?? $repo_info['repo_url'],
'#states' => [
'visible' => [
':input[name="repo_controller"]' => [
'value' => 'config_pr.repo_controller.github_enterprise',
],
],
'required' => [
':input[name="repo_controller"]' => [
'value' => 'config_pr.repo_controller.github_enterprise',
],
],
],
'#title' => $this->t('Repository URL (Optional)'),
'#description' => $this->t('The repo URL is required when using self hosted repositories.'),
'#default_value' => $this->config('config_pr.settings')->get('repo.repo_url') ?? '',
'#required' => FALSE,
];
$form['repo']['repo_owner'] = [
'#type' => 'textfield',
'#title' => $this->t('Repo owner name'),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment