diff --git a/README.md b/README.md index 11b3f91..efac765 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,17 @@ print_r(\Diffy\Screenshot::create(132, 'production')); See examples.php file for more examples. +## Environment + +By default the client talks to production (`app.diffy.website`). To use a different +Diffy environment (e.g. staging), set the `DIFFY_API_HOST` environment variable to its hostname: + +```bash +export DIFFY_API_HOST=staging.diffy.website +``` + +API calls then go to `https:///api/`. Remember to use an API key created in that environment. + ## Documentation See the [Diffy REST API Swagger](https://app.diffy.website/rest). diff --git a/examples.php b/examples.php index 81bf762..0e982b4 100644 --- a/examples.php +++ b/examples.php @@ -41,3 +41,14 @@ //$data[] = ['file'=> fopen(__DIR__.'/1280.png', 'r'), 'url'=> '/', 'breakpoint'=> 1280]; // //print_r(\Diffy\Screenshot::createCustomScreenshot($project_id, $data, $screenshotName)); + +// Register a user with a coupon, then create an API key with the returned token. +//$registration = \Diffy\User::create('Jane Doe', 'jane@example.com', $password, 'claude-code?utm_source=claude-code', 'CLAUDE2026'); +//\Diffy\Diffy::setApiToken($registration['token']); +//print_r(\Diffy\Key::create('my laptop')); + +// Send a password reset email. +//\Diffy\User::sendResetPasswordEmail('jane@example.com'); + +// Send feedback on a diff. +//\Diffy\Feedback::create($diffId, 'useful', 'Caught a header overflow at 640px.', 'my-client'); diff --git a/src/Diffy.php b/src/Diffy.php index 6c17c9a..6f519b4 100644 --- a/src/Diffy.php +++ b/src/Diffy.php @@ -80,7 +80,29 @@ public static function setApiToken($apiToken) */ public static function getApiBaseUrl() { - return self::$baseUrl; + $host = self::getHostFromEnv(); + + return $host ? 'https://' . $host . '/api/' : self::$baseUrl; + } + + /** + * @return string Base URL for links to the Diffy UI. + */ + public static function getUiBaseUrl() + { + $host = self::getHostFromEnv(); + + return $host ? 'https://' . $host . '/#/' : self::$uiBaseUrl; + } + + /** + * @return string|null Hostname from DIFFY_API_HOST env variable (e.g. to use staging instead of production). + */ + protected static function getHostFromEnv() + { + $host = trim((string) getenv('DIFFY_API_HOST'), " \t\n\r\0\x0B/"); + + return $host !== '' ? $host : null; } /** diff --git a/src/Feedback.php b/src/Feedback.php new file mode 100644 index 0000000..5541a9f --- /dev/null +++ b/src/Feedback.php @@ -0,0 +1,40 @@ + $diffId, + 'rating' => $rating, + 'comment' => $comment, + 'source' => $source, + 'clientVersion' => $clientVersion, + ]); + } +} diff --git a/src/Project.php b/src/Project.php index bd73427..e38d149 100644 --- a/src/Project.php +++ b/src/Project.php @@ -92,6 +92,10 @@ public static function compare(int $projectId, $params = []) $arguments['commitSha'] = $params['commitSha']; } + if (isset($params['mergeRequestIid'])) { + $arguments['mergeRequestIid'] = $params['mergeRequestIid']; + } + return Diffy::request('POST', 'projects/' . $projectId . '/compare', $arguments); } diff --git a/src/Screenshot.php b/src/Screenshot.php index 44f8bff..c5a4cd5 100644 --- a/src/Screenshot.php +++ b/src/Screenshot.php @@ -204,31 +204,6 @@ public static function createUpload(int $projectId, array $upload) return Diffy::multipartRequest('POST', 'projects/' . $projectId . '/create-custom-snapshot', $data); } - /** - * Create Screenshot from browserstack. - * - * @param int $projectId - * @param array $screenshots - * - * @return mixed - * - * @throws InvalidArgumentsException - */ - public static function createBrowserStackScreenshot(int $projectId, array $screenshots) - { - if (empty($projectId)) { - throw new InvalidArgumentsException('Project ID can not be empty'); - } - - if (empty($screenshots)) { - throw new InvalidArgumentsException('Screenshots list can not be empty'); - } - - return Diffy::request('POST', 'projects/' . $projectId . '/create-browser-stack-screenshot', [ - 'screenshots' => $screenshots, - ]); - } - /** * Create Screenshot with custom files. * diff --git a/src/User.php b/src/User.php index f970a9a..199b136 100644 --- a/src/User.php +++ b/src/User.php @@ -11,12 +11,14 @@ class User * @param string $email * @param string $password * @param string $location Used internally for tracking where user is coming from, utm_* tags. + * @param string $coupon Optional coupon code applied to the new account. A coupon that can not be applied + * does not stop the registration, check `coupon.applied` in the response. * * @return mixed * * @throws InvalidArgumentsException */ - public static function create(string $name, string $email, string $password, string $location = '') + public static function create(string $name, string $email, string $password, string $location = '', string $coupon = '') { if (empty($name)) { throw new InvalidArgumentsException('Name can not be empty'); @@ -30,11 +32,35 @@ public static function create(string $name, string $email, string $password, str throw new InvalidArgumentsException('Password can not be empty'); } - return Diffy::request('POST', 'register', [ + $data = [ 'name' => $name, 'email' => $email, 'password' => $password, 'location' => $location, - ]); + ]; + + if (!empty($coupon)) { + $data['coupon'] = $coupon; + } + + return Diffy::request('POST', 'register', $data); + } + + /** + * Send the email with a link to (re)set the user's password. + * + * @param string $email + * + * @return mixed + * + * @throws InvalidArgumentsException + */ + public static function sendResetPasswordEmail(string $email) + { + if (empty($email)) { + throw new InvalidArgumentsException('Email can not be empty'); + } + + return Diffy::request('POST', 'reset-password-email', ['email' => $email]); } }