-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathValidateConfigCommands.php
109 lines (98 loc) · 3.62 KB
/
ValidateConfigCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace Usher\Robo\Plugin\Commands;
use DrupalFinder\DrupalFinder;
use Robo\Exception\TaskException;
use Robo\Result;
use Robo\Tasks;
use Usher\Robo\Plugin\Traits\GitHubStatusTrait;
/**
* Robo commands related to validating Drupal configuration state.
*/
class ValidateConfigCommands extends Tasks
{
use GitHubStatusTrait;
/**
* The name of the GitHub status check, if set.
*
* @var string
*/
protected const GITHUB_STATUS_CHECK_NAME = 'ci/configuration-check';
/**
* Drupal root directory.
*
* @var string
*/
protected $drupalRoot;
/**
* Class constructor.
*/
public function __construct()
{
// Treat this command like bash -e and exit as soon as there's a failure.
$this->stopOnFail();
// Find Drupal root path.
$drupalFinder = new DrupalFinder();
$drupalFinder->locateRoot(getcwd());
$this->drupalRoot = $drupalFinder->getDrupalRoot();
}
/**
* Validate Drupal configuration status.
*
* @param string $siteDirs
* A comma separated list of Drupal site directories.
* @option set-pr-status
* Use this flag in Tugboat environments to set the GitHub status check
* on the associated pull request.
* @aliases vdc
*
* @throws \Robo\Exception\TaskException
*/
public function validateDrupalConfig(
string $siteDirs = 'default',
array $options = ['set-pr-status' => false],
): Result {
$this->io()->title('validate drupal configuration.');
['set-pr-status' => $setPrStatus] = $options;
if ($setPrStatus) {
$this->setGitHubStatusPending(gitHubCheckName: self::GITHUB_STATUS_CHECK_NAME);
}
$result = null;
$sites = explode(separator: ',', string: $siteDirs);
foreach ($sites as $siteDir) {
// Clear the "config" cache bin before we verify config status to
// improve the accuracy of this check.
// @see https://github.com/drush-ops/drush/pull/3861#issuecomment-453767694
// @see https://www.drush.org/11.x/commands/cache_clear/
$result = $this->taskExec("$this->drupalRoot/../vendor/bin/drush cache:clear bin config")
->dir("$this->drupalRoot/sites/$siteDir/")
->run();
$result = $this->taskExec("$this->drupalRoot/../vendor/bin/drush config:status --format=json")
->dir("$this->drupalRoot/sites/$siteDir/")
->printOutput(false)
->run();
$drushOutput = trim($result->getOutputData());
$configJson = json_decode($drushOutput, null, 512, JSON_THROW_ON_ERROR);
if (!is_array($configJson) || $configJson !== []) {
$this->say($drushOutput);
if ($setPrStatus) {
$this->setGitHubStatusError(
gitHubCheckName: self::GITHUB_STATUS_CHECK_NAME,
gitHubCheckDescription: 'Drupal config validation failed!'
);
}
throw new TaskException(
$this,
'Drupal database configuration does not match the tracked file system configuration.'
);
}
}
$this->say('Drupal database configuration matches the tracked file system configuration.');
if ($setPrStatus) {
$this->setGitHubStatusSuccess(
gitHubCheckName: self::GITHUB_STATUS_CHECK_NAME,
gitHubCheckDescription: 'Drupal config validation passed!'
);
}
return $result;
}
}