Skip to content

Commit

Permalink
[#61] Removed --push in favour of --dry-run.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Mar 30, 2024
1 parent d5be94c commit 0d80cab
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 78 deletions.
5 changes: 0 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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"
Expand All @@ -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')
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/test-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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"
Expand All @@ -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')
Expand Down
51 changes: 21 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,27 @@ fully-configured [example in the DrevOps project](https://github.com/drevops/dre

## Options

Usage:
artifact [options] [--] <remote>

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
Expand Down
33 changes: 8 additions & 25 deletions src/Commands/ArtifactCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -261,23 +261,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* @param array<mixed> $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 {
Expand All @@ -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('<comment>Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.</comment>');
}
else {
$this->output->writeln('<comment>Cowardly refusing to push to remote. Use --push option to perform an actual push.</comment>');
$this->doPush();
}
$this->result = TRUE;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/Functional/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
22 changes: 11 additions & 11 deletions tests/phpunit/Functional/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -82,15 +82,15 @@ 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);
}

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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 0d80cab

Please sign in to comment.