Yii2项目自动向GitLab上报Bug

Yii2 项目自动上报Bug

原理

yii2在程序报错时, 会执行指定action, 通过重写ErrorAction, 实现Bug自动提交至GitLab的issue

步骤

  • 配置SiteController中的actions方法

    复制代码
      public function actions()
      {
          return [
              'error' => [
                  'class' => 'app\helpers\web\ErrorAction',
              ],
          ];
      }
  • 重写ErrorAction, 位于app\helpers\web\ErrorAction, 并修改常量URL,PRIVATE_TOKEN和ASSIGNEE_ID

如何获取project_id和assignee_id见 WIKI

复制代码
namespace app\helpers\web;

use yii;
use yii\base\Action;
use yii\base\Exception;
use yii\base\UserException;
use yii\web\HttpException;

class ErrorAction extends \yii\web\ErrorAction
{
    const URL = '{host}/api/v3/projects/{project_id}/issues'; // host替换为主机地址, project_id为项目id
    const PRIVATE_TOKEN = 'tD3Te-ctECeGwEHH7-ec';
    const ASSIGNEE_ID = 21;

    public function run()
    {
        if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
            $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
        }

        if ($exception instanceof HttpException) {
            $code = $exception->statusCode;
        } else {
            $code = $exception->getCode();
        }
        if ($exception instanceof Exception) {
            $name = $exception->getName();
        } else {
            $name = $this->defaultName ?: Yii::t('yii', 'Error');
        }
        $preCode = $code;
        if ($code) {
            $name .= " (#$code)";
        }

        if ($exception instanceof UserException) {
            $message = $exception->getMessage();
        } else {
            $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
        }
        if ($code != '404') {
            //自动向GitLab提交Bug
            $url = self::URL;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'PRIVATE-TOKEN: '.self::PRIVATE_TOKEN,
            ));

            curl_setopt($ch, CURLOPT_POSTFIELDS, [
                'title' => $message,
                'description' => '<blockquote>'.Yii::$app->request->getReferrer().'</blockquote>'. '<blockquote>' . Yii::$app->request->absoluteUrl . '</blockquote><br/><pre>' . $exception . '</pre>',
                'assignee_id' => self::ASSIGNEE_ID,
                'labels' => '捕虫器,' . $name,
            ]);
            curl_setopt($ch, CURLOPT_HEADER, false);
            // Pass TRUE or 1 if you want to wait for and catch the response against the request made
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            // For Debug mode; shows up any error encountered during the operation
            curl_setopt($ch, CURLOPT_VERBOSE, false);
            $response = curl_exec($ch);
            curl_close($ch);
        }
        if (Yii::$app->getRequest()->getIsAjax() || strpos($_SERVER['REQUEST_URI'], '/api/') > -1) {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return [
                'message' => $message
            ];
        } else {
            return $this->controller->render($this->view ?: $this->id, [
                'name' => $name,
                'message' => $message,
                'exception' => $exception,
            ]);
        }
    }
}
相关推荐
F2E_Zhangmo3 小时前
基于cornerstone3D的dicom影像浏览器 第三章 拖拽seriesItem至displayer上显示第一张dicom
前端·javascript·cornerstone·cornerstone3d·cornerstonejs
gnip8 小时前
Jst执行上下文栈和变量对象
前端·javascript
excel8 小时前
🐣 最简单的卷积与激活函数指南(带示例)
前端
2501_930104048 小时前
Bug 排查日记:打造高效问题定位与解决的技术秘籍
bug
weixin_377634848 小时前
【YOLO】数据增强bug
yolo·bug
伍哥的传说8 小时前
还在为第三方包 bug 头疼?patch-package 让你轻松打补丁!
bug·开发效率·前端工具·第三方包bug·前端开发痛点·npm包修复·依赖包定制
黑客飓风8 小时前
Bug排查日记:从崩溃到修复的实战记录
log4j·bug
醉方休8 小时前
npm/pnpm软链接的优点和使用场景
前端·npm·node.js
拉不动的猪8 小时前
简单回顾下Weakmap在vue中为何不能去作为循环数据源,以及替代方案
前端·javascript·vue.js
How_doyou_do9 小时前
数据传输优化-异步不阻塞处理增强首屏体验
开发语言·前端·javascript