From 4d8a9aa5cb4cc51ff1cf92c75f43eba0ee94b4ed Mon Sep 17 00:00:00 2001 From: zhaoke Date: Fri, 5 Aug 2022 15:02:16 +0800 Subject: [PATCH] * Add command submit unit bug. --- internal/command/action/bug.go | 22 ++-- internal/pkg/domain/testing.go | 5 +- internal/pkg/helper/zentao/bug.go | 127 ++++++++++++++++++++- internal/pkg/helper/zentao/case.go | 52 ++++++++- res/server/en/messages.json | 4 + res/server/zh/messages.json | 4 + ui/src/components/Table.vue | 2 +- ui/src/locales/en-US.ts | 1 + ui/src/locales/zh-CN.ts | 1 + ui/src/views/result/FormBug.vue | 2 +- ui/src/views/result/TabPageResult.vue | 7 ++ ui/src/views/script/FormSyncFromZentao.vue | 27 ++++- 12 files changed, 230 insertions(+), 24 deletions(-) diff --git a/internal/command/action/bug.go b/internal/command/action/bug.go index 67e8ab13..0897a5e5 100644 --- a/internal/command/action/bug.go +++ b/internal/command/action/bug.go @@ -2,19 +2,20 @@ package action import ( "fmt" + "os" + "strconv" + "strings" + commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts" commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain" analysisHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/analysis" configHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/config" - "github.com/easysoft/zentaoatf/internal/pkg/helper/zentao" + zentaoHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/zentao" i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118" logUtils "github.com/easysoft/zentaoatf/pkg/lib/log" stdinUtils "github.com/easysoft/zentaoatf/pkg/lib/stdin" stringUtils "github.com/easysoft/zentaoatf/pkg/lib/string" "github.com/fatih/color" - "os" - "strconv" - "strings" ) var ( @@ -46,6 +47,13 @@ func CommitBug(files []string, productId int, noNeedConfirm bool) { lines := make([]string, 0) for _, cs := range report.FuncResult { if cs.Status != commConsts.PASS { + lines = append(lines, fmt.Sprintf("%d. %s %s", cs.Id, cs.Title, coloredStatus(cs.Status.String()))) + ids = append(ids, strconv.Itoa(cs.Id)) + } + } + + for _, cs := range report.UnitResult { + if cs.Status != "pass" { lines = append(lines, fmt.Sprintf("%d. %s %s", cs.Id, cs.Title, coloredStatus(cs.Status))) ids = append(ids, strconv.Itoa(cs.Id)) } @@ -83,8 +91,8 @@ func CommitBug(files []string, productId int, noNeedConfirm bool) { } } -func coloredStatus(status commConsts.ResultStatus) string { - temp := strings.ToLower(status.String()) +func coloredStatus(status string) string { + temp := strings.ToLower(status) switch temp { case "pass": @@ -95,7 +103,7 @@ func coloredStatus(status commConsts.ResultStatus) string { return color.YellowString(i118Utils.Sprintf(temp)) } - return status.String() + return status } func reportBug(resultDir string, caseId string, productId int) error { diff --git a/internal/pkg/domain/testing.go b/internal/pkg/domain/testing.go index 176b506a..1e70edf4 100644 --- a/internal/pkg/domain/testing.go +++ b/internal/pkg/domain/testing.go @@ -112,8 +112,9 @@ type ZtfStep struct { } type ZtfBug struct { - Title string `json:"title"` - Type string `json:"type"` + Title string `json:"title"` + Type string `json:"type"` + TestType commConsts.TestType `json:"testType"` StepIds string `json:"ids"` // for to diff --git a/internal/pkg/helper/zentao/bug.go b/internal/pkg/helper/zentao/bug.go index ac379ee7..8c7e7f35 100644 --- a/internal/pkg/helper/zentao/bug.go +++ b/internal/pkg/helper/zentao/bug.go @@ -10,9 +10,11 @@ import ( commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts" commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain" analysisHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/analysis" + serverDomain "github.com/easysoft/zentaoatf/internal/server/modules/v1/domain" httpUtils "github.com/easysoft/zentaoatf/pkg/lib/http" i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118" logUtils "github.com/easysoft/zentaoatf/pkg/lib/log" + stdinUtils "github.com/easysoft/zentaoatf/pkg/lib/stdin" "github.com/fatih/color" "github.com/jinzhu/copier" uuid "github.com/satori/go.uuid" @@ -26,6 +28,9 @@ func CommitBug(ztfBug commDomain.ZtfBug, config commDomain.WorkspaceConf) (err e Login(config) + if ztfBug.TestType == commConsts.TestUnit { + return CommitUnitBug(&ztfBug, config) + } ztfBug.Steps = strings.Replace(ztfBug.Steps, " ", " ", -1) ztfBug.Steps = strings.Replace(ztfBug.Steps, "\n", "
", -1) @@ -45,6 +50,44 @@ func CommitBug(ztfBug commDomain.ZtfBug, config commDomain.WorkspaceConf) (err e return } +func CommitUnitBug(ztfBug *commDomain.ZtfBug, config commDomain.WorkspaceConf) (err error) { + if ztfBug.Product == 0 { + logUtils.Info(color.RedString(i118Utils.Sprintf("ignore_bug_without_product"))) + return + } + + if ztfBug.Title == "" { + ztfBug.Title = stdinUtils.GetInput("\\w+", "", + i118Utils.Sprintf("pls_enter")+" "+i118Utils.Sprintf("bug_title")) + } + + Login(config) + + submitBugs := make([]*commDomain.ZentaoBug, 0) + ztfBug.Steps = strings.Replace(ztfBug.Steps, " ", " ", -1) + ztfBug.Steps = strings.Replace(ztfBug.Steps, "\n", "
", -1) + ztfBug.Title = strings.Trim(ztfBug.Title, "-") + + bug := commDomain.ZentaoBug{} + copier.Copy(&bug, ztfBug) + submitBugs = append(submitBugs, &bug) + generateCaseId(submitBugs, config) + for _, bug := range submitBugs { + uri := fmt.Sprintf("/products/%d/bugs", bug.Product) + url := GenApiUrl(uri, nil, config.Url) + + _, err = httpUtils.Post(url, bug) + if err != nil { + err = ZentaoRequestErr(url, i118Utils.Sprintf("fail_to_report_bug", err.Error())) + return + } + + logUtils.Info(color.GreenString(i118Utils.Sprintf("success_to_report_bug", bug.Case))) + } + + return +} + func PrepareBug(workspacePath, seq string, caseIdStr string, productId int) (bug commDomain.ZtfBug) { caseId, err := strconv.Atoi(caseIdStr) @@ -69,21 +112,23 @@ func PrepareBug(workspacePath, seq string, caseIdStr string, productId int) (bug if step.Status == commConsts.FAIL { stepIds += step.Id + "_" } + stepIds = strings.Trim(stepIds, "_") stepsContent := GenBugStepText(step) steps = append(steps, stepsContent) stepsArray = append(stepsArray, map[string]interface{}{ - "name": step.Name, + "title": step.Name, "status": step.Status, "steps": stepsContent, }) } bug = commDomain.ZtfBug{ - Title: cs.Title, - Case: cs.Id, - Product: productId, - Steps: strings.Join(steps, "\n"), - StepIds: stepIds, + Title: cs.Title, + TestType: "func", + Case: cs.Id, + Product: productId, + Steps: strings.Join(steps, "\n"), + StepIds: stepIds, Uid: uuid.NewV4().String(), Type: "codeerror", Severity: 3, Pri: 3, OpenedBuild: []string{"trunk"}, @@ -96,6 +141,54 @@ func PrepareBug(workspacePath, seq string, caseIdStr string, productId int) (bug return } + if report.TestType == commConsts.TestFunc { + return + } + bugs := make([]map[string]interface{}, 0) + stepIds := "" + hasCaseId := true + if len(report.UnitResult) == report.UnitResult[len(report.UnitResult)-1].Id { + hasCaseId = false + } + + for _, cs := range report.UnitResult { + if (caseId > 0 && cs.Id != caseId) || cs.Status == "pass" { + continue + } + stepIds += strconv.Itoa(cs.Id) + "_" + steps := "" + if cs.Failure != nil { + steps = cs.Failure.Desc + } + caseId = cs.Id + if !hasCaseId { + caseId = 0 + } + bugMap := map[string]interface{}{ + "title": cs.Title, + "status": cs.Status, + "steps": steps, + "caseId": caseId, + "id": cs.Id, + } + bugs = append(bugs, bugMap) + break + } + + bugsJSon, _ := json.Marshal(bugs) + + bug = commDomain.ZtfBug{ + Title: report.Name, + TestType: "unit", + Case: caseId, + Product: productId, + Steps: string(bugsJSon), + StepIds: stepIds, + + Uid: uuid.NewV4().String(), + Type: "codeerror", Severity: 3, Pri: 3, OpenedBuild: []string{"trunk"}, + CaseVersion: "0", OldTaskID: "0", + } return } @@ -197,3 +290,25 @@ func LoadBugs(Product int, config commDomain.WorkspaceConf) (bugs []commDomain.Z } return } + +func generateCaseId(bugs []*commDomain.ZentaoBug, config commDomain.WorkspaceConf) (caseId int) { + if len(bugs) == 0 { + return + } + //查询所有case,标题相同则返回 + casesResp, _ := ListCaseByProduct(config.Url, bugs[0].Product) + for _, bug := range bugs { + for _, cs := range casesResp.Cases { + if cs.Title == bug.Title { + bug.Case = cs.Id + break + } + } + if bug.Case == 0 { + caseInfo, _ := CreateCase(bug.Product, bug.Title, nil, serverDomain.TestScript{}, config) + bug.Case = caseInfo.Id + } + } + + return +} diff --git a/internal/pkg/helper/zentao/case.go b/internal/pkg/helper/zentao/case.go index c20980aa..2bb9e27d 100644 --- a/internal/pkg/helper/zentao/case.go +++ b/internal/pkg/helper/zentao/case.go @@ -75,6 +75,54 @@ func CommitCase(caseId int, title string, steps []commDomain.ZentaoCaseStep, scr return } +func CreateCase(productId int, title string, steps []commDomain.ZentaoCaseStep, script serverDomain.TestScript, + config commDomain.WorkspaceConf) (cs commDomain.ZtfCase, err error) { + + err = Login(config) + if err != nil { + return + } + + uri := fmt.Sprintf("/products/%d/testcases", productId) + url := GenApiUrl(uri, nil, config.Url) + + requestObj := map[string]interface{}{ + "type": "feature", + "title": title, + "steps": steps, + "script": script.Code, + "lang": script.Lang, + "pri": 3, + } + if steps == nil { + requestObj["steps"] = []commDomain.ZentaoCaseStep{} + } + + jsonData, err := json.Marshal(requestObj) + if err != nil { + err = ZentaoRequestErr(url, commConsts.ResponseParseErr.Message) + return + } + + if commConsts.Verbose { + logUtils.Infof(string(jsonData)) + } + + bytes, err := httpUtils.Post(url, requestObj) + if err != nil { + err = ZentaoRequestErr(url, err.Error()) + return + } + err = json.Unmarshal(bytes, &cs) + if err != nil { + err = ZentaoRequestErr(url, commConsts.ResponseParseErr.Message) + return + } + logUtils.Infof(i118Utils.Sprintf("success_to_create_case", title) + "\n") + + return +} + func GetCaseById(baseUrl string, caseId int) (cs commDomain.ZtfCase, err error) { uri := fmt.Sprintf("/testcases/%d", caseId) url := GenApiUrl(uri, nil, baseUrl) @@ -450,7 +498,9 @@ func GetCasesByTaskInDir(productId int, taskId int, workspacePath, scriptDir str func ListCaseByProduct(baseUrl string, productId int) (casesResp commDomain.ZtfRespTestCases, err error) { uri := fmt.Sprintf("/products/%d/testcases", productId) - url := GenApiUrl(uri, nil, baseUrl) + url := GenApiUrl(uri, map[string]interface{}{ + "limit": 10000, + }, baseUrl) dataStr, err := httpUtils.Get(url) if err != nil { diff --git a/res/server/en/messages.json b/res/server/en/messages.json index cebbdef7..16dee469 100644 --- a/res/server/en/messages.json +++ b/res/server/en/messages.json @@ -666,6 +666,10 @@ { "id": "must_provide_allure_report_dir", "translation": "Please provide report dir if using Allure Report Framework." + }, + { + "id": "bug_title", + "translation": "BUG Title" } ] } diff --git a/res/server/zh/messages.json b/res/server/zh/messages.json index 1a3eb7fc..f85ea72e 100644 --- a/res/server/zh/messages.json +++ b/res/server/zh/messages.json @@ -653,6 +653,10 @@ { "id": "must_provide_allure_report_dir", "translation": "使用Allure报表框架时,需要指定报告所在的目录。" + }, + { + "id": "bug_title", + "translation": "缺陷标题" } ] } diff --git a/ui/src/components/Table.vue b/ui/src/components/Table.vue index 72a10d4c..b63dfa9e 100644 --- a/ui/src/components/Table.vue +++ b/ui/src/components/Table.vue @@ -587,7 +587,7 @@ export default defineComponent({ } } }); - setting.isCheckAll = isChecked.length >= localRows.value.length; + setting.isCheckAll = isChecked.length >= rowCheckbox.value.length; // 回傳畫面上選上的資料 (Return the selected data on the screen) emit("return-checked-rows", isChecked); }; diff --git a/ui/src/locales/en-US.ts b/ui/src/locales/en-US.ts index 97122089..02e80678 100644 --- a/ui/src/locales/en-US.ts +++ b/ui/src/locales/en-US.ts @@ -10,6 +10,7 @@ export default { 'fullScreen': 'Toggle Fullscreen', 'help': 'Help', 'loading': 'Loading...', + 'syncing': 'Syncing...', 'index': 'Index', 'no': 'NO.', diff --git a/ui/src/locales/zh-CN.ts b/ui/src/locales/zh-CN.ts index 0ea537fb..c03c1ebe 100644 --- a/ui/src/locales/zh-CN.ts +++ b/ui/src/locales/zh-CN.ts @@ -11,6 +11,7 @@ export default { 'fullScreen': '切换全屏', 'help': '帮助', 'loading': '正在加载……', + 'syncing': '同步中…', 'index': '索引', 'no': '编号', diff --git a/ui/src/views/result/FormBug.vue b/ui/src/views/result/FormBug.vue index 3b56bc94..c402d30e 100644 --- a/ui/src/views/result/FormBug.vue +++ b/ui/src/views/result/FormBug.vue @@ -68,7 +68,7 @@
- +