Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DQA-5456: Hook pre-push replace lint-yaml with test-phpmd. #585

Merged
merged 4 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ toolkit:
pre-push:
commands:
- toolkit:test-phpcs
- toolkit:lint-yaml
- toolkit:test-phpmd
- toolkit:lint-php
- toolkit:opts-review
commands: [ ]
65 changes: 34 additions & 31 deletions src/TaskRunner/Commands/ToolCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,47 +86,50 @@ public function composerLockCheck(): int
public function optsReview()
{
$reviewOk = true;
if (file_exists('.opts.yml')) {
$project_id = $this->getConfig()->get('toolkit.project_id');
$forbiddenCommands = Website::projectConstraints($project_id);
if (empty($forbiddenCommands)) {
$this->io()->error('Failed to get constraints from the endpoint.');
return 1;
}
if (!file_exists('.opts.yml')) {
$this->say("The file 'opts.yml' was not found, skipping.");
return 0;
}
$project_id = $this->getConfig()->get('toolkit.project_id');
$forbiddenCommands = Website::projectConstraints($project_id);
if (empty($forbiddenCommands)) {
$this->io()->error('Failed to get constraints from the endpoint.');
return 1;
}

$parseOptsFile = Yaml::parseFile('.opts.yml');
if (empty($parseOptsFile['upgrade_commands'])) {
$this->say('The project is using default deploy instructions.');
return 0;
}
if (empty($parseOptsFile['upgrade_commands']['default']) && empty($parseOptsFile['upgrade_commands']['append'])) {
$this->say("Your structure for the 'upgrade_commands' is invalid.\nSee the documentation at https://webgate.ec.europa.eu/fpfis/wikis/display/MULTISITE/Pipeline+configuration+and+override");
return 1;
}
$parseOptsFile = Yaml::parseFile('.opts.yml');
if (empty($parseOptsFile['upgrade_commands'])) {
$this->say('The project is using default deploy instructions.');
return 0;
}
if (empty($parseOptsFile['upgrade_commands']['default']) && empty($parseOptsFile['upgrade_commands']['append'])) {
$this->say("Your structure for the 'upgrade_commands' is invalid.\nSee the documentation at https://webgate.ec.europa.eu/fpfis/wikis/display/MULTISITE/Pipeline+configuration+and+override");
return 1;
}

foreach ($parseOptsFile['upgrade_commands'] as $key => $commands) {
foreach ($commands as $command) {
$command = str_replace('\\', '', $command);
$parsedCommand = preg_split("/[\s;&|]/", $command, 0, PREG_SPLIT_NO_EMPTY);
foreach ($forbiddenCommands as $forbiddenCommand) {
if ($key == 'default') {
foreach ($parseOptsFile['upgrade_commands'] as $key => $commands) {
foreach ($commands as $command) {
$command = str_replace('\\', '', $command);
$parsedCommand = preg_split("/[\s;&|]/", $command, 0, PREG_SPLIT_NO_EMPTY);
foreach ($forbiddenCommands as $forbiddenCommand) {
if ($key == 'default') {
if (in_array($forbiddenCommand, $parsedCommand)) {
$this->say("The command '$command' is not allowed. Please remove it from 'upgrade_commands' section.");
$reviewOk = false;
}
} else {
foreach ($command as $subCommand) {
$parsedCommand = preg_split("/[\s;&|]/", $subCommand, 0, PREG_SPLIT_NO_EMPTY);
if (in_array($forbiddenCommand, $parsedCommand)) {
$this->say("The command '$command' is not allowed. Please remove it from 'upgrade_commands' section.");
$this->say("The command '$subCommand' is not allowed. Please remove it from 'upgrade_commands' section.");
$reviewOk = false;
}
} else {
foreach ($command as $subCommand) {
$parsedCommand = preg_split("/[\s;&|]/", $subCommand, 0, PREG_SPLIT_NO_EMPTY);
if (in_array($forbiddenCommand, $parsedCommand)) {
$this->say("The command '$subCommand' is not allowed. Please remove it from 'upgrade_commands' section.");
$reviewOk = false;
}
}
}
}
}
}
}

if (!$reviewOk) {
$this->io()->error("Failed the '.opts.yml' file review. Please contact the QA team.");
return 1;
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/commands/git-hooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
- contains: |
[Simulator] Simulating Exec('./vendor/bin/run toolkit:test-phpcs')
[Simulator] Running ./vendor/bin/run toolkit:test-phpcs
[Simulator] Simulating Exec('./vendor/bin/run toolkit:lint-yaml')
[Simulator] Running ./vendor/bin/run toolkit:lint-yaml
[Simulator] Simulating Exec('./vendor/bin/run toolkit:test-phpmd')
[Simulator] Running ./vendor/bin/run toolkit:test-phpmd
[Simulator] Simulating Exec('./vendor/bin/run toolkit:lint-php')
[Simulator] Running ./vendor/bin/run toolkit:lint-php
[Simulator] Simulating Exec('./vendor/bin/run toolkit:opts-review')
Expand Down
22 changes: 19 additions & 3 deletions tests/fixtures/commands/opts-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@
toolkit:
project_id: toolkit
expectations:
- contains: |
> Review 'opts.yml' file - Ok.
- contains: "> The file 'opts.yml' was not found, skipping."

- command: 'toolkit:opts-review'
sample: opts.yml
configuration:
toolkit:
project_id: toolkit
expectations:
- contains: "> Review 'opts.yml' file - Ok."

- command: 'toolkit:opts-review'
sample: config.yml
configuration:
toolkit:
project_id: toolkit
expectations:
- contains: "> The project is using default deploy instructions."

- command: 'toolkit:opts-review'
sample: opts-broken-structure.yml
configuration:
toolkit:
project_id: toolkit
expectations:
- contains: |
> Review 'opts.yml' file - Ok.
> Your structure for the 'upgrade_commands' is invalid.
See the documentation at https://webgate.ec.europa.eu/fpfis/wikis/display/MULTISITE/Pipeline+configuration+and+override

- command: 'toolkit:opts-review'
sample: opts-broken.yml
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/samples/sample-opts-broken-structure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
upgrade_commands:
- ./vendor/bin/drush cache-rebuild