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,
            ]);
        }
    }
}
相关推荐
和你看星星1 小时前
我把代码排查流程做成了一个 Codex Skill
前端
excel1 小时前
AI 冲击下的前端发展指引:从工具到价值的重塑
前端
文心快码BaiduComate1 小时前
提升组织级AI Coding质量:电商搜索项目实践
前端·后端·程序员
excel1 小时前
AI 时代前端转型:模型训练才是未来的核心竞争力
前端
放下华子我只抽RuiKe51 小时前
FastAPI 全栈后端(四):认证与授权
开发语言·前端·javascript·python·深度学习·react.js·fastapi
持敬chijing2 小时前
Web渗透之前后端漏洞-文件包含漏洞
前端·安全·web安全·网络安全·网络攻击模型·安全威胁分析
CV艺术家2 小时前
前端免费高效的接入天气组件(天气网),控制组件的样式
前端
hunterandroid2 小时前
RecyclerView 进阶:DiffUtil 与列表更新
前端
_codeOH2 小时前
Vue 3 vs React 19:框架还在卷,核心原理就这些
前端·vue.js
the_answer2 小时前
CSS 新时代:浏览器原生能力如何重塑前端开发范式
前端