From 0d80cabd4bd1ccc9866ed04c1dcd91902f5ff9b7 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Sat, 30 Mar 2024 15:07:49 +1100 Subject: [PATCH] [#61] Removed `--push` in favour of `--dry-run`. --- .circleci/config.yml | 5 -- .github/workflows/test-php.yml | 5 -- README.md | 51 ++++++++----------- src/Commands/ArtifactCommand.php | 33 +++--------- .../Functional/AbstractFunctionalTestCase.php | 4 +- tests/phpunit/Functional/GeneralTest.php | 22 ++++---- 6 files changed, 42 insertions(+), 78 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index f3173ef..9b9a8ab 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -97,7 +97,6 @@ jobs: --branch=mode--force-push--circleci--[branch] \ --mode=force-push \ --log=$HOME/report--mode--force-push.txt \ - --push \ -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') @@ -122,7 +121,6 @@ jobs: --branch=mode--force-push--circleci--[branch] \ --mode=force-push \ --log=$HOME/report--mode--force-push.txt \ - --push \ -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') @@ -171,7 +169,6 @@ jobs: --branch=mode--branch--circleci--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ --log=$HOME/report--mode--branch.txt \ - --push \ -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') @@ -188,7 +185,6 @@ jobs: --branch=mode--branch--circleci--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ --log=$HOME/report--mode--branch.txt \ - --push \ -vvv \ && { echo "Expected to fail as repeated pushes to the same branch are not allowed, but succeeded" >&2; exit 1; } || echo "Failed as expected" @@ -208,7 +204,6 @@ jobs: --branch=mode--branch--circleci--[branch]--[timestamp:Y-m-d_H-i-s] \ --mode=branch \ --log=$HOME/report--mode--branch.txt \ - --push \ -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') diff --git a/.github/workflows/test-php.yml b/.github/workflows/test-php.yml index 7215545..3f43ada 100644 --- a/.github/workflows/test-php.yml +++ b/.github/workflows/test-php.yml @@ -159,7 +159,6 @@ jobs: --branch=mode--force-push--gha--[branch] \ --mode=force-push \ --log=$HOME/report--mode--force-push.txt \ - --push \ -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') @@ -182,7 +181,6 @@ jobs: --branch=mode--force-push--gha--[branch] \ --mode=force-push \ --log=$HOME/report--mode--force-push.txt \ - --push \ -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') @@ -261,7 +259,6 @@ jobs: --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ --log=$HOME/report--mode--branch.txt \ - --push \ -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') @@ -284,7 +281,6 @@ jobs: --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ --log=$HOME/report--mode--branch.txt \ - --push \ -vvv \ && { echo "Expected to fail as repeated pushes to the same branch are not allowed, but succeeded" >&2; exit 1; } || echo "Failed as expected" @@ -295,7 +291,6 @@ jobs: --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i-s] \ --mode=branch \ --log=$HOME/report--mode--branch.txt \ - --push \ -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') diff --git a/README.md b/README.md index 854e6cc..7361ea6 100644 --- a/README.md +++ b/README.md @@ -117,36 +117,27 @@ fully-configured [example in the DrevOps project](https://github.com/drevops/dre ## Options - Usage: - artifact [options] [--] - - Arguments: - remote Path to the remote git repository. - - Options: - --branch[=BRANCH] Destination branch with optional tokens. [default: "[branch]"] - --gitignore=GITIGNORE Path to gitignore file to replace current .gitignore. - --message[=MESSAGE] Commit message with optional tokens. [default: "Deployment commit"] - --mode[=MODE] Mode of artifact build: branch, force-push or diff. Defaults to force-push. [default: "force-push"] - --no-cleanup Do not cleanup after run. - --now=NOW Internal value used to set internal time. - --push Push artifact to the remote repository. Defaults to FALSE. - --log=LOG Path to the log/report file. - --root=ROOT Path to the root for file path resolution. If not specified, current directory is used. - --show-changes Show changes made to the repo by the build in the output. - --src=SRC Directory where source repository is located. If not specified, root directory is used. - -h, --help Display this help message - -q, --quiet Do not output any message - -V, --version Display this application version - --ansi Force ANSI output - --no-ansi Disable ANSI output - -n, --no-interaction Do not ask any interactive question - -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug - - Help: - Assemble a code artifact from your codebase, remove unnecessary files, and push it into a separate Git repository. - -### Adding dependencies +| Name | Default value | Description | +|--------------------|----------------------|-----------------------------------------------------------------------------------------------| +| `--branch` | `"[branch]"` | Destination branch with optional tokens. | +| `--gitignore` | | Path to gitignore file to replace current `.gitignore`. | +| `--message` | `"Deployment commit"`| Commit message with optional tokens. | +| `--mode` | `"force-push"` | Mode of artifact build: branch, force-push or diff. | +| `--no-cleanup` | | Do not cleanup after run. | +| `--now` | | Internal value used to set internal time. | +| `--dry-run` | | Run without pushing to the remote repository. | +| `--log` | | Path to the log file. | +| `--root` | | Path to the root for file path resolution. Uses current directory if not specified. | +| `--show-changes` | | Show changes made to the repo by the build in the output. | +| `--src` | | Directory where source repository is located. Uses root directory if not specified. | +| `-h, --help` | | Display help for the given command. Displays help for the artifact command if no command given. | +| `-q, --quiet` | | Do not output any message. | +| `-V, --version` | | Display this application version. | +| `--ansi` | | Force ANSI output. Use `--no-ansi` to disable. | +| `-n, --no-interaction` | | Do not ask any interactive question. | +| `-v, --verbose` | | Increase the verbosity of messages: 1 for normal, 2 for more verbose, 3 for debug. | + +### Modifying artifact content `--gitignore` option allows to specify the path to the artifact's `.gitignore` file that replaces existing `.gitignore` (if any) during the build. Any files no diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index be51d80..aa0816e 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -88,9 +88,9 @@ class ArtifactCommand extends Command { protected string $message = ''; /** - * Flag to specify if push is required or should be using dry run. + * Flag to specify if using dry run. */ - protected bool $needsPush = FALSE; + protected bool $isDryRun = FALSE; /** * Flag to specify if cleanup is required to run after the build. @@ -181,7 +181,7 @@ protected function configure(): void { ) ->addOption('no-cleanup', NULL, InputOption::VALUE_NONE, 'Do not cleanup after run.') ->addOption('now', NULL, InputOption::VALUE_REQUIRED, 'Internal value used to set internal time.') - ->addOption('push', NULL, InputOption::VALUE_NONE, 'Push artifact to the remote repository') + ->addOption('dry-run', NULL, InputOption::VALUE_NONE, 'Run without pushing to the remote repository.') ->addOption('log', NULL, InputOption::VALUE_REQUIRED, 'Path to the log file.') ->addOption( 'root', @@ -261,23 +261,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int * @param array $options * Options. * - * @option $branch Destination branch with optional tokens. - * @option $debug Print debug information. - * @option $gitignore Path to gitignore file to replace current .gitignore. - * @option $message Commit message with optional tokens. - * @option $mode Mode of artifact build: branch, force-push or diff. - * Defaults to force-push. - * @option $now Internal value used to set internal time. - * @option $no-cleanup Do not cleanup after run. - * @option $push Push artifact to the remote repository. Defaults to FALSE. - * @option $report Path to the report file. - * @option $root Path to the root for file path resolution. If not - * specified, current directory is used. - * @option $show-changes Show changes made to the repo by the build in the - * output. - * @option $src Directory where source repository is located. If not - * specified, root directory is used. - * * @throws \Exception */ protected function processArtifact(string $remote, array $options): void { @@ -290,11 +273,11 @@ protected function processArtifact(string $remote, array $options): void { $this->showInfo(); $this->prepareArtifact(); - if ($this->needsPush) { - $this->doPush(); + if ($this->isDryRun) { + $this->output->writeln('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.'); } else { - $this->output->writeln('Cowardly refusing to push to remote. Use --push option to perform an actual push.'); + $this->doPush(); } $this->result = TRUE; } @@ -497,7 +480,7 @@ protected function resolveOptions(string $remote, array $options): void { // Resolve some basic options into properties. $this->showChanges = !empty($options['show-changes']); $this->needCleanup = empty($options['no-cleanup']); - $this->needsPush = !empty($options['push']); + $this->isDryRun = !empty($options['dry-run']); $this->logFile = empty($options['log']) ? '' : $this->fsGetAbsolutePath($options['log']); $this->now = empty($options['now']) ? time() : (int) $options['now']; $this->remoteName = self::GIT_REMOTE_NAME; @@ -553,7 +536,7 @@ protected function showInfo(): void { $lines[] = (' Remote repository: ' . $this->remoteUrl); $lines[] = (' Remote branch: ' . $this->destinationBranch); $lines[] = (' Gitignore file: ' . ($this->gitignoreFile ?: 'No')); - $lines[] = (' Will push: ' . ($this->needsPush ? 'Yes' : 'No')); + $lines[] = (' Will push: ' . ($this->isDryRun ? 'No' : 'Yes')); $lines[] = ('----------------------------------------------------------------------'); $this->output->writeln($lines); diff --git a/tests/phpunit/Functional/AbstractFunctionalTestCase.php b/tests/phpunit/Functional/AbstractFunctionalTestCase.php index be63379..a5c21b1 100644 --- a/tests/phpunit/Functional/AbstractFunctionalTestCase.php +++ b/tests/phpunit/Functional/AbstractFunctionalTestCase.php @@ -76,7 +76,7 @@ protected function setUp(): void { * Command output. */ protected function assertBuildSuccess(string $args = '', string $branch = 'testbranch', string $commit = 'Deployment commit'): string { - $output = $this->runBuild(sprintf('--push --branch=%s %s', $branch, $args)); + $output = $this->runBuild(sprintf('--branch=%s %s', $branch, $args)); $this->assertStringNotContainsString('[error]', $output); $this->assertStringContainsString(sprintf('Pushed branch "%s" with commit message "%s"', $branch, $commit), $output); $this->assertStringContainsString('Deployment finished successfully.', $output); @@ -99,7 +99,7 @@ protected function assertBuildSuccess(string $args = '', string $branch = 'testb * Command output. */ protected function assertBuildFailure(string $args = '', string $branch = 'testbranch', string $commit = 'Deployment commit'): string { - $output = $this->runBuild(sprintf('--push --branch=%s %s', $branch, $args), TRUE); + $output = $this->runBuild(sprintf('--branch=%s %s', $branch, $args), TRUE); $this->assertStringNotContainsString(sprintf('Pushed branch "%s" with commit message "%s"', $branch, $commit), $output); $this->assertStringNotContainsString('Deployment finished successfully.', $output); $this->assertStringContainsString('Deployment failed.', $output); diff --git a/tests/phpunit/Functional/GeneralTest.php b/tests/phpunit/Functional/GeneralTest.php index 3aa841c..fb86fbb 100644 --- a/tests/phpunit/Functional/GeneralTest.php +++ b/tests/phpunit/Functional/GeneralTest.php @@ -28,7 +28,7 @@ public function testCompulsoryParameter(): void { public function testInfo(): void { $this->gitCreateFixtureCommits(1); - $output = $this->runBuild(); + $output = $this->runBuild('--dry-run'); $this->assertStringContainsString('Artifact information', $output); $this->assertStringContainsString('Mode: force-push', $output); $this->assertStringContainsString('Source repository: ' . $this->src, $output); @@ -38,33 +38,33 @@ public function testInfo(): void { $this->assertStringContainsString('Will push: No', $output); $this->assertStringNotContainsString('Added changes:', $output); - $this->assertStringContainsString('Cowardly refusing to push to remote. Use --push option to perform an actual push.', $output); + $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); } public function testShowChanges(): void { $this->gitCreateFixtureCommits(1); - $output = $this->runBuild('--show-changes'); + $output = $this->runBuild('--show-changes --dry-run'); $this->assertStringContainsString('Added changes:', $output); - $this->assertStringContainsString('Cowardly refusing to push to remote. Use --push option to perform an actual push.', $output); + $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); } public function testNoCleanup(): void { $this->gitCreateFixtureCommits(1); - $output = $this->runBuild('--no-cleanup'); + $output = $this->runBuild('--no-cleanup --dry-run'); $this->assertGitCurrentBranch($this->src, $this->artifactBranch); - $this->assertStringContainsString('Cowardly refusing to push to remote. Use --push option to perform an actual push.', $output); + $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); } public function testDebug(): void { $this->gitCreateFixtureCommits(1); - $output = $this->runBuild('-vvv'); + $output = $this->runBuild('-vvv --dry-run'); $this->assertStringContainsString('Debug messages enabled', $output); $this->assertStringContainsString('Artifact information', $output); @@ -82,7 +82,7 @@ public function testDebug(): void { $this->assertStringContainsString('Gitignore file: No', $output); $this->assertStringContainsString('Push result: Success', $output); - $this->assertStringContainsString('Cowardly refusing to push to remote. Use --push option to perform an actual push.', $output); + $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); } @@ -90,7 +90,7 @@ public function testDebugLogFile(): void { $report = $this->src . DIRECTORY_SEPARATOR . 'report.txt'; $this->gitCreateFixtureCommits(1); - $commandOutput = $this->runBuild(sprintf('--log=%s', $report)); + $commandOutput = $this->runBuild(sprintf('--dry-run --log=%s', $report)); $this->assertStringContainsString('Debug messages enabled', $commandOutput); $this->assertStringContainsString('Artifact information', $commandOutput); @@ -131,11 +131,11 @@ public function testDebugLogFile(): void { public function testDebugDisabled(): void { $this->gitCreateFixtureCommits(1); - $output = $this->runBuild(); + $output = $this->runBuild('--dry-run'); $this->assertStringNotContainsString('Debug messages enabled', $output); - $this->assertStringContainsString('Cowardly refusing to push to remote. Use --push option to perform an actual push.', $output); + $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); }