Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates deploy to build API #454

Merged
merged 15 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,29 +311,50 @@ 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
*
* @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 = [];

if ($repository) {
$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);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
56 changes: 56 additions & 0 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should default to whoami or a php cross platform equivalent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to add shell calls in the base notifier, it's much safer to use wrapped commands in higher level libs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? If this isnt automated very few people will set it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want:

get_current_user();

instead of shell executing whoami. That function will give you the owner of the file, which is maybe better than nothing.

Something you could do if the user has installed and not disabled the functions from posix extension, is:

posix_getpwuid(posix_geteuid())['name'];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoami works across all windows post server-2003 & unix-like systems. I'll add a check on the response to see if it returned correctly and fallback to get_current_user if not.

} 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.
*
Expand Down
43 changes: 43 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Bugsnag;

class Utils
{
/**
* Checks whether the given function name is available.
*
* @param string $func the function name
*
* @return bool
*/
public static function functionAvailable($func)
{
$disabled = explode(',', ini_get('disable_functions'));

return function_exists($func) && !in_array($func, $disabled);
}

/**
* Gets the current user's identity for build reporting.
*
* @return string
*/
public static function getBuilderName()
{
$builderName = null;
if (self::functionAvailable('exec')) {
$output = [];
$success = 0;
exec('whoami', $output, $success);
if ($success == 0) {
$builderName = $output[0];
}
}
if (is_null($builderName)) {
$builderName = get_current_user();
}

return $builderName;
}
}
104 changes: 96 additions & 8 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,16 @@ public function testCanManuallyFlush()

public function testDeployWorksOutOfTheBox()
{
$this->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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

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