Skip to content
Snippets Groups Projects

Issue #3445189 add Git integration

Open Ted Bowman requested to merge issue/aue-3445189:3445189-git into 1.0.x
Files
5
+ 77
0
<?php
namespace Drupal\aue_git;
use CzProject\GitPhp\Git;
use CzProject\GitPhp\GitException;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\package_manager\Event\PostApplyEvent;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\StatusCheckEvent;
use Drupal\package_manager\PathLocator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class GitCommitUpdater implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
StatusCheckEvent::class => 'onStatusCheck',
// PreApplyEvent::class => 'checkGitStatus',
// PostApplyEvent::class => 'onPostApply',
];
}
use StringTranslationTrait;
public function __construct(private PathLocator $pathLocator) {
}
/**
* Ensure that the Git repository exists and that working directory is clean
*
* @param \Drupal\package_manager\Event\StatusCheckEvent $event
*/
public function onStatusCheck(StatusCheckEvent $event): void {
if (in_array($event->stage->getType(), ['automatic_updates:unattended', 'automatic_updates:attended'])) {
$git = new Git();
$project_root = $this->pathLocator->getProjectRoot();
try {
$repo = $git->open($project_root);
}
catch (GitException $exception) {
// @TODO: Should this be an error or a warning?
$event->addWarning([t("No Git repository detected.")]);
return;
}
if($repo->hasChanges()) {
$changes = $repo->execute("status");
$imploded = implode("\n", $changes);
$event->addError([t($imploded)], t("Cannot move forward with Package Manager operations: There are uncommitted changes in Git."));
}
}
}
/**
* Make a Git commit after updating.
*
* @param \Drupal\package_manager\Event\PostApplyEvent $event
*/
public function onPostApply(PostApplyEvent $event): void {
if ($event->stage->getType() === 'automatic_updates:attended') {
$this->makeGitCommit($this->pathLocator->getProjectRoot());
}
}
private function makeGitCommit(string $projectRoot) {
}
}
Loading