Skip to content
This repository has been archived by the owner on May 21, 2020. It is now read-only.

Commit

Permalink
增加sentry通知 #2891
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Feb 25, 2017
1 parent 762b554 commit df14b6c
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/AutoComplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace plugin\build\docs {

/**
* @property \Miaoxing\Build\Service\SentryNotification $sentryNotification
*/
class AutoComplete
{
}
}

namespace {

/**
* @return \plugin\build\docs\AutoComplete
*/
function wei()
{
}
}
31 changes: 31 additions & 0 deletions resources/views/sentryNotifications/issue-tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Message: <?= $payload['message'] ?>


### Request

URL: <?= $http['url'] ?>

Method: <?= $http['method'] ?>

Query: <?= $http['query_string'] ?: '-' ?>


### User

<?php foreach ($user as $key => $value) : ?>
<?= $key == 'id' ? 'ID' : ucfirst($key) ?>: <?= $value , "\n" ?>
<?php endforeach ?>

<?php if ($payload['event']['extra']) : ?>
### Additional Data

```php
<?= var_export($payload['event']['extra']), "\n" ?>
```
<?php endif ?>

View the event: <?= $payload['url'] ?>

---

**关闭issue后记得关闭sentry的event**
15 changes: 15 additions & 0 deletions src/Controller/SentryNotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Miaoxing\Build\Controller;

use Wei\BaseController;

class SentryNotifications extends BaseController
{
public function createAction()
{
$ret = wei()->sentryNotification->createIssue($this->request->getContent());

return $this->response->json($ret);
}
}
91 changes: 91 additions & 0 deletions src/Service/SentryNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Miaoxing\Build\Service;

use miaoxing\plugin\BaseService;
use Wei\Http;
use Wei\Request;
use Wei\RetTrait;
use Wei\View;

/**
* @method Http http(array $options)
* @property Request $request
* @property View $view
*/
class SentryNotification extends BaseService
{
use RetTrait;

protected $githubToken = '';

protected $githubRepo = '';

protected $assignees = '';

protected $issueLabels = ['bug'];

protected function guestAssignees(array $payload)
{
if (!isset($payload['event']['sentry.interfaces.Exception'])) {
return $this->assignees;
}

$value = $payload['event']['sentry.interfaces.Exception']['values'][0];
list($file, $line) = explode(':', $value['module']);

exec(sprintf('git log -1 --pretty=%%an -L %s,%s:%s', $line, $line, $file), $output);
if (isset($output[0])) {
return $output[0];
}

return $this->assignees;
}

/**
* @param string $payload
* @return array
*/
public function createIssue($payload)
{
$payload = json_decode($payload, true);
if (!$payload || !isset($payload['message'])) {
return $this->err('Invalid payload');
}

$title = sprintf('[%s]告警: %s', date('y-m-d'), $payload['message']);

$http = $payload['event']['sentry.interfaces.Http'];
$user = $payload['event']['sentry.interfaces.User'];
$body = $this->view->render('sentryNotifications/issue-tpl.php', [
'payload' => $payload,
'http' => $http,
'user' => $user,
]);

$assignees = $this->guestAssignees($payload);

$http = $this->http([
'url' => 'https://api.github.com/repos/' . $this->githubRepo . '/issues',
'method' => 'post',
'dataType' => 'json',
'userAgent' => 'Wei/0.9.X',
'errorLevel' => 'debug',
'headers' => [
'Authorization' => 'token ' . $this->githubToken,
],
'data' => json_encode([
'title' => $title,
'body' => $body,
'assignees' => [$assignees],
'labels' => $this->issueLabels,
]),
]);

if ($http->isSuccess()) {
return $this->suc(['http' => $http]);
} else {
return $this->err(['http' => $http]);
}
}
}

0 comments on commit df14b6c

Please sign in to comment.