|
|
package monkey
|
|
|
|
|
|
import (
|
|
|
"autogo/common"
|
|
|
"autogo/controllers"
|
|
|
"autogo/dbsql"
|
|
|
"autogo/models"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"net/http"
|
|
|
"strings"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/spf13/cast"
|
|
|
)
|
|
|
|
|
|
type anomaly struct {
|
|
|
TaskId int `json:"task_id"`
|
|
|
Udid string `json:"udid"`
|
|
|
Anomalies []string `json:"anomalies"`
|
|
|
CoverdPages []string `json:"coverd_pages"`
|
|
|
Logs []string `json:"logs"`
|
|
|
}
|
|
|
|
|
|
// @Tags Monkey相关 /api/monkey/v2/
|
|
|
// @Summary 上报Monkey异常
|
|
|
// @Description 上报Monkey异常
|
|
|
// @accept x-www-form-urlencoded
|
|
|
// @Param task_id formData int true "任务id"
|
|
|
// @Param crash_log_list formData string false "要更新的任务状态"
|
|
|
// @Param activity_name_info formData string false "要更新的任务状态"
|
|
|
// @Param device_name formData string false "要更新的任务状态"
|
|
|
// @Param status formData string false "要更新的任务状态"
|
|
|
// @Success 200 {object} models.Response "返回更新后的任务信息"
|
|
|
// @Router /api/monkey/v2/task/store_anomaly [post]
|
|
|
func StoreMonkeyAnomaly(c *gin.Context) {
|
|
|
rsp := controllers.NewResponse()
|
|
|
|
|
|
var ano anomaly
|
|
|
err := c.ShouldBindJSON(&ano)
|
|
|
if err != nil {
|
|
|
c.JSON(http.StatusOK, rsp.Error("json解析失败: "+err.Error()))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
var res models.MonkeyAnomaly
|
|
|
res.TaskId = cast.ToInt(ano.TaskId)
|
|
|
if res.TaskId < 1 {
|
|
|
rsp.Error("task_id error:" + c.PostForm("task_id"))
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
return
|
|
|
}
|
|
|
if len(ano.Anomalies) > 0 {
|
|
|
buf, _ := json.Marshal(ano.Anomalies)
|
|
|
res.Anomalies = ano.Anomalies
|
|
|
res.AnomalyInfos = string(buf)
|
|
|
}
|
|
|
if len(ano.CoverdPages) > 0 {
|
|
|
res.CoverdPages = strings.Join(ano.CoverdPages, ",")
|
|
|
}
|
|
|
res.Udid = ano.Udid
|
|
|
if len(ano.Logs) > 0 {
|
|
|
res.Logs = strings.Join(ano.Logs, ",")
|
|
|
}
|
|
|
|
|
|
db, err := dbsql.GetConn(dbsql.DSN)
|
|
|
if err != nil {
|
|
|
rsp.Error(err.Error())
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
return
|
|
|
}
|
|
|
defer dbsql.Close(db)
|
|
|
db.Table(res.TableName()).Model(models.MonkeyAnomaly{}).Create(&res)
|
|
|
if res.ID < 1 {
|
|
|
rsp.Error("保存Monkey异常失败")
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
rsp.Success()
|
|
|
rsp.Data = res
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
|
|
|
go createTbBug(res)
|
|
|
}
|
|
|
|
|
|
// @Tags Monkey相关 /api/monkey/v2/
|
|
|
// @Summary 获取Monkey异常信息
|
|
|
// @Description 通过任务id获取Monkey异常信息
|
|
|
// @accept x-www-form-urlencoded
|
|
|
// @Param task_id query int true "任务id"
|
|
|
// @Success 200 {object} models.Response "返回任务结果"
|
|
|
// @Router /api/monkey/v2/task/get_anomalies [get]
|
|
|
func GetMonkeyAnomaliesByTaskId(c *gin.Context) {
|
|
|
rsp := controllers.NewResponse()
|
|
|
|
|
|
var list []models.MonkeyAnomaly
|
|
|
db, err := dbsql.GetConn(dbsql.DSN)
|
|
|
if err != nil {
|
|
|
rsp.Error(err.Error())
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
return
|
|
|
}
|
|
|
defer dbsql.Close(db)
|
|
|
db.Where("task_id = ?", c.Query("task_id")).Find(&list)
|
|
|
for i, _ := range list {
|
|
|
list[i].JsonReady()
|
|
|
}
|
|
|
|
|
|
rsp.Success()
|
|
|
rsp.Data = list
|
|
|
c.JSON(http.StatusOK, rsp)
|
|
|
}
|
|
|
|
|
|
func createTbBug(ano models.MonkeyAnomaly) {
|
|
|
// 暂时忽略anr提单
|
|
|
if strings.Contains(ano.AnomalyInfos, "ANR in") {
|
|
|
fmt.Println("[Debug]", "anr异常将跳过提单逻辑")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
db, err := dbsql.GetConn(dbsql.DSN)
|
|
|
if err != nil {
|
|
|
fmt.Println("[createTbBug]", err)
|
|
|
return
|
|
|
}
|
|
|
defer dbsql.Close(db)
|
|
|
|
|
|
var task models.MonkeyTask
|
|
|
db.Model(models.MonkeyTask{}).Where("id = ?", ano.TaskId).Last(&task)
|
|
|
if task.Id < 1 {
|
|
|
fmt.Println("[createTbBug]", "没有找到monkey任务,id -", ano.TaskId)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
bug_type := "Other"
|
|
|
if strings.Contains(ano.AnomalyInfos, "FATAL EXCEPTION") {
|
|
|
bug_type = "EXCEPTION"
|
|
|
}
|
|
|
|
|
|
content := ""
|
|
|
content += "**关联Monkey报告:** [点击查看](http://qa.flatincbr.work/#/monkey/result/" + cast.ToString(ano.TaskId) + ")" + "\n\n<br>\n\n"
|
|
|
content += "**上报时间:**" + ano.CreateTime.Format("2006-01-02 15:04:05") + "\n\n<br>\n\n"
|
|
|
content += "**应用版本:**" + task.Version + "\n\n<br>\n\n"
|
|
|
content += "**信息预览:**" + "\n\n"
|
|
|
|
|
|
title := "【Monkey测试】"
|
|
|
for i, v := range ano.Anomalies {
|
|
|
for ii, vv := range strings.Split(v, "\n") {
|
|
|
content += "> " + vv + "\n"
|
|
|
switch bug_type {
|
|
|
case "EXCEPTION":
|
|
|
// 取第一行和第三行内容拼接
|
|
|
if i == 0 && ii == 0 {
|
|
|
_strs := strings.Split(vv, "): ")
|
|
|
if len(_strs) > 1 {
|
|
|
title += _strs[1] + " - "
|
|
|
}
|
|
|
} else if i == 0 && ii == 2 {
|
|
|
_strs := strings.Split(vv, "): ")
|
|
|
if len(_strs) > 1 {
|
|
|
title += _strs[1]
|
|
|
}
|
|
|
}
|
|
|
default:
|
|
|
if i == 0 && ii == 0 {
|
|
|
title += "未知异常 - " + vv
|
|
|
}
|
|
|
}
|
|
|
if ii > 10 {
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
content += "> " + "——————————————————————" + "\n"
|
|
|
}
|
|
|
content += "> " + "(...更多请查看Monkey报告)" + "\n"
|
|
|
content += "\n"
|
|
|
|
|
|
bug := common.NewBug("crushu") //task.Project
|
|
|
|
|
|
bug.SetTitle(title).
|
|
|
SetPlatform(task.Platform).
|
|
|
SetContent(content).
|
|
|
SetCategory(common.BugCategory_Anomaly)
|
|
|
|
|
|
id, err := bug.Create()
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
return
|
|
|
}
|
|
|
fmt.Println("缺陷创建成功:", "https://www.teambition.com/task/"+id)
|
|
|
|
|
|
db.Table(ano.TableName()).Model(models.MonkeyAnomaly{}).Where("id = ?", ano.ID).Update("bug_id", id)
|
|
|
}
|