Skip to content

Commit 6606d4f

Browse files
author
Paweł Pasik
committed
Add support for --commit-message option in push:artifact
1 parent c864c3f commit 6606d4f

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

src/Command/Push/PushArtifactCommand.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ protected function configure(): void
4949
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Deprecated: Use no-push instead')
5050
->addOption('no-push', null, InputOption::VALUE_NONE, 'Do not push changes to Acquia Cloud')
5151
->addOption('no-commit', null, InputOption::VALUE_NONE, 'Do not commit changes. Implies no-push')
52+
->addOption(
53+
'commit-message',
54+
null,
55+
InputOption::VALUE_REQUIRED,
56+
'Custom commit message for artifact commit'
57+
)
5258
->addOption('no-clone', null, InputOption::VALUE_NONE, 'Do not clone repository. Implies no-commit and no-push')
5359
->addOption('destination-git-urls', 'u', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'The URL of your git repository to which the artifact branch will be pushed. Use multiple times for multiple URLs.')
5460
->addOption('destination-git-branch', 'b', InputOption::VALUE_REQUIRED, 'The destination branch to push the artifact to')
@@ -385,13 +391,47 @@ private function commit(Closure $outputCallback, string $artifactDir, string $co
385391

386392
private function generateCommitMessage(string $commitHash): array|string
387393
{
394+
// CLI option.
395+
if ($option = $this->input->getOption('commit-message')) {
396+
$this->logger->debug('Using custom commit message from --commit-message option.');
397+
return $this->sanitizeCommitMessage($option, $commitHash);
398+
}
399+
400+
// ENV fallback.
388401
if ($envVar = getenv('ACLI_PUSH_ARTIFACT_COMMIT_MSG')) {
389-
return $envVar;
402+
$this->logger->debug('Using commit message from ACLI_PUSH_ARTIFACT_COMMIT_MSG env var.');
403+
return $this->sanitizeCommitMessage($envVar, $commitHash);
390404
}
391405

406+
// Default behavior.
392407
return "Automated commit by Acquia CLI (source commit: $commitHash)";
393408
}
394409

410+
/**
411+
* Sanitize commit message.
412+
*/
413+
private function sanitizeCommitMessage(string $message, string $commitHash): string
414+
{
415+
// Remove control characters (security / log safety).
416+
$message = preg_replace('/[\x00-\x1F\x7F]/u', '', $message);
417+
418+
// Keep only first line (git subject best practice).
419+
$message = strtok($message, "\n");
420+
421+
// Trim whitespace.
422+
$message = trim($message);
423+
424+
// Limit length (avoid abuse / log issues).
425+
$message = mb_substr($message, 0, 255);
426+
427+
// Fallback if empty after sanitization.
428+
if ($message === '') {
429+
return "Automated commit by Acquia CLI (source commit: $commitHash)";
430+
}
431+
432+
return $message;
433+
}
434+
395435
/**
396436
* Push the artifact.
397437
*/

0 commit comments

Comments
 (0)