This repository has been archived by the owner on May 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |