欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Yii2项目自动向GitLab上报Bug

Yii2项目自动向GitLab上报Bug

2025/6/14 8:49:57 来源:https://blog.csdn.net/zackslee/article/details/148528602  浏览:    关键词: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为项目idconst 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 madecurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// For Debug mode; shows up any error encountered during the operationcurl_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,]);}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词