diff --git a/README.md b/README.md index 5aeb777..c280c55 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,12 @@ or in case of custom environment with basic auth credentials diffy project:compare PROJECT_ID prod custom --env2Url="https://custom.example.com" --env2User="user" --env2Pass="password" ``` +or with existing screenshots + +```shell script +diffy project:compare PROJECT_ID existing existing --screenshot1=100 --screenshot2=101 +``` + Allowed environments are: prod, stage, dev, custom (long options: production, staging, development). #### Update project(s) diff --git a/src/Commands/ProjectCommand.php b/src/Commands/ProjectCommand.php index cb425f8..d2b9144 100644 --- a/src/Commands/ProjectCommand.php +++ b/src/Commands/ProjectCommand.php @@ -4,7 +4,9 @@ use Diffy\Diff; use Diffy\Diffy; +use Diffy\InvalidArgumentsException; use Diffy\Project; +use Diffy\Screenshot; use DiffyCli\Config; use GuzzleHttp\Exception\InvalidArgumentException; use Robo\Tasks; @@ -57,7 +59,7 @@ private function isValidJsonConfig(string $configurationPath): array * @param string $env2 Second environment to compare * @param array $options * - * @throws \Diffy\InvalidArgumentsException + * @throws InvalidArgumentsException * * @option env1Url Url of the first environment if custom environment * @option env1User Basic auth user for env1Url @@ -68,6 +70,8 @@ private function isValidJsonConfig(string $configurationPath): array * @option wait Wait for the diff to be completed * @option max-wait Maximum number of seconds to wait for the diff to be completed. * @option commit-sha GitHub commit SHA. + * @option screenshot1 First existing screenshot + * @option screenshot2 Second existing screenshot * * @usage project:compare 342 prod stage * Compare production and stage environments. @@ -81,8 +85,9 @@ public function createCompare( string $env1, string $env2, array $options = [ - 'wait' => false, 'max-wait' => 1200, 'commit-sha' => null, 'env1Url' => '', 'env1User' => null, 'env1Pass' => null, - 'env2Url' => '', 'env2User' => null, 'env2Pass' => null, 'name' => '' + 'wait' => false, 'max-wait' => 1200, 'commit-sha' => null, 'env1Url' => '', 'env1User' => null, + 'env1Pass' => null, 'env2Url' => '', 'env2User' => null, 'env2Pass' => null, 'name' => '', + 'screenshot1' => null, 'screenshot2' => null, ] ) { Diffy::setApiKey(Config::getConfig()['key']); @@ -95,7 +100,7 @@ public function createCompare( 'env1Pass' => $options['env1Pass'], 'env2Url' => $options['env2Url'], 'env2User' => $options['env2User'], - 'env2Pass' => $options['env2Pass'], + 'env2Pass' => $options['env2Pass'] ]; if (!empty($options['commit-sha']) && $options['commit-sha']) { @@ -118,7 +123,29 @@ public function createCompare( $params['env2'] = 'stage'; } - $diffId = Project::compare($projectId, $params); + if ($env1 === 'existing' || $env2 === 'existing') { + if ($env1 === 'existing' && empty($options['screenshot1'])) { + throw new InvalidArgumentsException('Set screenshot1 value'); + } elseif ($env2 === 'existing' && empty($options['screenshot2'])) { + throw new InvalidArgumentsException('Set screenshot2 value'); + } + + if ($env1 === 'existing') { + $screenshot1 = $options['screenshot1']; + } else { + $screenshot1 = Screenshot::create($projectId, $env1); + } + + if ($env2 === 'existing') { + $screenshot2 = $options['screenshot2']; + } else { + $screenshot2 = Screenshot::create($projectId, $env2); + } + + $diffId = Diff::create($projectId, $screenshot1, $screenshot2, $options['name']); + } else { + $diffId = Project::compare($projectId, $params); + } if (!empty($options['name'])) { Diff::updateName($diffId, $options['name']);