diff --git a/src/Client.php b/src/Client.php index c81303aa..b1321cca 100644 --- a/src/Client.php +++ b/src/Client.php @@ -311,6 +311,8 @@ public function notify(Report $report, callable $callback = null) /** * Notify Bugsnag of a deployment. * + * @deprecated This function is being deprecated in favour of `build`. + * * @param string|null $repository the repository from which you are deploying the code * @param string|null $branch the source control branch from which you are deploying * @param string|null $revision the source control revision you are currently deploying @@ -318,6 +320,21 @@ public function notify(Report $report, callable $callback = null) * @return void */ public function deploy($repository = null, $branch = null, $revision = null) + { + $this->build($repository, $revision); + } + + /** + * Notify Bugsnag of a build. + * + * @param string|null $repository the repository from which you are deploying the code + * @param string|null $revision the source control revision you are currently deploying + * @param string|null $provider the provider of the source control for the build + * @param string|null $builderName the name of who or what is making the build + * + * @return void + */ + public function build($repository = null, $revision = null, $provider = null, $builderName = null) { $data = []; @@ -325,15 +342,19 @@ public function deploy($repository = null, $branch = null, $revision = null) $data['repository'] = $repository; } - if ($branch) { - $data['branch'] = $branch; - } - if ($revision) { $data['revision'] = $revision; } - $this->http->deploy($data); + if ($provider) { + $data['provider'] = $provider; + } + + if ($builderName) { + $data['builder'] = $builderName; + } + + $this->http->sendBuildReport($data); } /** diff --git a/src/Configuration.php b/src/Configuration.php index 42662e49..f14195fe 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -13,6 +13,13 @@ class Configuration */ const SESSION_ENDPOINT = 'https://sessions.bugsnag.com'; + /** + * The default build endpoint. + * + * @var string + */ + const BUILD_ENDPOINT = 'https://build.bugsnag.com'; + /** * The Bugnsag API Key. * @@ -129,6 +136,13 @@ class Configuration */ protected $sessionEndpoint = self::SESSION_ENDPOINT; + /** + * The endpoint to deliver build notifications to. + * + * @var string + */ + protected $buildEndpoint; + /** * Create a new config instance. * @@ -568,4 +582,32 @@ public function shouldCaptureSessions() { return $this->autoCaptureSessions; } + + /** + * Sets the build endpoint. + * + * @param string $endpoint the build endpoint + * + * @return $this + */ + public function setBuildEndpoint($endpoint) + { + $this->buildEndpoint = $endpoint; + + return $this; + } + + /** + * Returns the build endpoint. + * + * @return string + */ + public function getBuildEndpoint() + { + if (isset($this->buildEndpoint)) { + return $this->buildEndpoint; + } + + return self::BUILD_ENDPOINT; + } } diff --git a/src/HttpClient.php b/src/HttpClient.php index c52df5fc..cc865e15 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -72,6 +72,8 @@ public function queue(Report $report) /** * Notify Bugsnag of a deployment. * + * @deprecated This method should no longer be used in favour of sendBuildReport. + * * @param array $data the deployment information * * @return void @@ -91,6 +93,60 @@ public function deploy(array $data) $this->guzzle->post('deploy', ['json' => $data]); } + /** + * Notify Bugsnag of a build. + * + * @param array $buildInfo the build information + * + * @return void + */ + public function sendBuildReport(array $buildInfo) + { + $app = $this->config->getAppData(); + + $data = []; + $sourceControl = []; + if (isset($buildInfo['repository'])) { + $sourceControl['repository'] = $buildInfo['repository']; + } + + if (isset($buildInfo['provider'])) { + $sourceControl['provider'] = $buildInfo['provider']; + } + + if (isset($buildInfo['revision'])) { + $sourceControl['revision'] = $buildInfo['revision']; + } + + if (!empty($sourceControl)) { + $data['sourceControl'] = $sourceControl; + } + + if (isset($buildInfo['builder'])) { + $data['builderName'] = $buildInfo['builder']; + } else { + $data['builderName'] = Utils::getBuilderName(); + } + + if (isset($buildInfo['buildTool'])) { + $data['buildTool'] = $buildInfo['buildTool']; + } else { + $data['buildTool'] = 'bugsnag-php'; + } + + $data['releaseStage'] = $app['releaseStage']; + + if (isset($app['version'])) { + $data['appVersion'] = $app['version']; + } + + $data['apiKey'] = $this->config->getApiKey(); + + $endpoint = $this->config->getBuildEndpoint(); + + $this->guzzle->post($endpoint, ['json' => $data]); + } + /** * Deliver everything on the queue to Bugsnag. * diff --git a/src/Utils.php b/src/Utils.php new file mode 100644 index 00000000..5529061e --- /dev/null +++ b/src/Utils.php @@ -0,0 +1,43 @@ +guzzle->expects($this->once())->method('post')->with($this->equalTo('deploy'), $this->equalTo(['json' => ['releaseStage' => 'production', 'apiKey' => 'example-api-key']])); + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); $this->client->deploy(); } - public function testDeployWorksWithgReleaseStage() + public function testDeployWorksWithReleaseStage() { - $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('deploy'), $this->equalTo(['json' => ['releaseStage' => 'staging', 'apiKey' => 'example-api-key']])); + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['releaseStage' => 'staging', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); @@ -396,7 +396,7 @@ public function testDeployWorksWithgReleaseStage() public function testDeployWorksWithAppVersion() { - $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('deploy'), $this->equalTo(['json' => ['releaseStage' => 'production', 'appVersion' => '1.1.0', 'apiKey' => 'example-api-key']])); + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['releaseStage' => 'production', 'appVersion' => '1.1.0', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); @@ -407,7 +407,7 @@ public function testDeployWorksWithAppVersion() public function testDeployWorksWithRepository() { - $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('deploy'), $this->equalTo(['json' => ['repository' => 'foo', 'releaseStage' => 'production', 'apiKey' => 'example-api-key']])); + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['sourceControl' => ['repository' => 'foo'], 'releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); @@ -416,7 +416,7 @@ public function testDeployWorksWithRepository() public function testDeployWorksWithBranch() { - $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('deploy'), $this->equalTo(['json' => ['branch' => 'master', 'releaseStage' => 'production', 'apiKey' => 'example-api-key']])); + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); @@ -425,7 +425,7 @@ public function testDeployWorksWithBranch() public function testDeployWorksWithRevision() { - $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('deploy'), $this->equalTo(['json' => ['revision' => 'bar', 'releaseStage' => 'production', 'apiKey' => 'example-api-key']])); + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['sourceControl' => ['revision' => 'bar'], 'releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); @@ -434,7 +434,7 @@ public function testDeployWorksWithRevision() public function testDeployWorksWithEverything() { - $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('deploy'), $this->equalTo(['json' => ['repository' => 'baz', 'branch' => 'develop', 'revision' => 'foo', 'releaseStage' => 'development', 'appVersion' => '1.3.1', 'apiKey' => 'example-api-key']])); + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['sourceControl' => ['repository' => 'baz', 'revision' => 'foo'], 'releaseStage' => 'development', 'appVersion' => '1.3.1', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); @@ -444,6 +444,94 @@ public function testDeployWorksWithEverything() $this->client->deploy('baz', 'develop', 'foo'); } + public function testBuildWorksOutOfTheBox() + { + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); + + $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); + + $this->client->build(); + } + + public function testBuildWorksWithReleaseStage() + { + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['releaseStage' => 'staging', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); + + $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); + + $this->config->setReleaseStage('staging'); + + $this->client->build(); + } + + public function testBuildWorksWithAppVersion() + { + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['releaseStage' => 'production', 'appVersion' => '1.1.0', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); + + $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); + + $this->config->setAppVersion('1.1.0'); + + $this->client->build(); + } + + public function testBuildWorksWithRepository() + { + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['sourceControl' => ['repository' => 'foo'], 'releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); + + $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); + + $this->client->build('foo'); + } + + public function testBuildWorksWithProvider() + { + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['sourceControl' => ['provider' => 'github'], 'releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); + + $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); + + $this->client->build(null, null, 'github'); + } + + public function testBuildWorksWithRevision() + { + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['sourceControl' => ['revision' => 'bar'], 'releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); + + $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); + + $this->client->build(null, 'bar'); + } + + public function testBuildWorksWithBuilderName() + { + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['builderName' => 'me', 'releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php']])); + + $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); + + $this->client->build(null, null, null, 'me'); + } + + public function testBuildWorksWithBuildTool() + { + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['releaseStage' => 'production', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php', 'builderName' => exec('whoami')]])); + + $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); + + $this->client->build(null, null, null, null); + } + + public function testBuildWorksWithEverything() + { + $this->guzzle->expects($this->once())->method('post')->with($this->equalTo('https://build.bugsnag.com'), $this->equalTo(['json' => ['builderName' => 'me', 'sourceControl' => ['repository' => 'baz', 'revision' => 'foo', 'provider' => 'github'], 'releaseStage' => 'development', 'appVersion' => '1.3.1', 'apiKey' => 'example-api-key', 'buildTool' => 'bugsnag-php']])); + + $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle); + + $this->config->setReleaseStage('development'); + $this->config->setAppVersion('1.3.1'); + + $this->client->build('baz', 'foo', 'github', 'me'); + } + public function testSeverityReasonUnmodified() { $this->client = new Client($this->config = new Configuration('example-api-key'), null, $this->guzzle);