bug.go 2.8 KB
Newer Older
1 2 3 4 5
package zentaoService

import (
	"encoding/json"
	"fmt"
6
	"github.com/easysoft/zentaoatf/src/model"
7 8 9 10
	"github.com/easysoft/zentaoatf/src/service/client"
	testingService "github.com/easysoft/zentaoatf/src/service/testing"
	configUtils "github.com/easysoft/zentaoatf/src/utils/config"
	printUtils "github.com/easysoft/zentaoatf/src/utils/print"
11
	"github.com/easysoft/zentaoatf/src/utils/vari"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
12
	uuid "github.com/satori/go.uuid"
13
	"strconv"
14
	"strings"
15 16
)

17
func GenBug() (model.Bug, string, string) {
18
	conf := configUtils.ReadCurrConfig()
19 20
	productId := conf.ProductId
	projectId := conf.ProjectId
21

22
	report := testingService.GetTestTestReportForSubmit(vari.CurrScriptFile, vari.CurrResultDate)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
23
	for _, cs := range report.Cases {
24 25 26 27
		if cs.Id != vari.CurrCaseId {
			continue
		}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
28
		title := cs.Title
29 30 31 32 33 34 35 36 37 38 39
		module := "0"
		typ := "install"
		openedBuild := map[string]interface{}{"0": "trunk"}
		severity := "1"
		priority := "1"

		product := productId
		project := projectId
		caseId := cs.Id
		Result := cs.ZentaoResultId
		taskId := cs.TaskId
40

41 42 43
		uid := uuid.NewV4().String()
		caseVersion := ""
		oldTaskID := "0"
44

45
		idInTask := strconv.Itoa(cs.IdInTask)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
46

47 48
		stepIds := ""
		steps := make([]string, 0)
49
		for _, step := range cs.Steps {
50
			if !step.Status {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
51
				stepIds += strconv.Itoa(step.Id) + "_"
52 53
			}

54 55
			stepsContent := testingService.GetStepContent(step)
			steps = append(steps, stepsContent+"<br/>")
56 57
		}

58 59 60 61 62
		bug := model.Bug{Title: title,
			Module: module, Type: typ, OpenedBuild: openedBuild, Severity: severity, Pri: priority,
			Product: strconv.Itoa(product), Project: strconv.Itoa(project), Case: strconv.Itoa(caseId),
			Result: strconv.Itoa(Result), Testtask: strconv.Itoa(taskId), Steps: strings.Join(steps, "<br/>"),
			Uid: uid, CaseVersion: caseVersion, OldTaskID: oldTaskID,
63
		}
64
		return bug, idInTask, stepIds
65

66
	}
67

68 69
	return model.Bug{}, "", ""
}
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
func SubmitBug(bug model.Bug, idInTask string, stepIds string) {
	conf := configUtils.ReadCurrConfig()
	Login(conf.Url, conf.Account, conf.Password)

	productId := bug.Product
	projectId := bug.Project

	// bug-create-1-0-caseID=1,version=3,resultID=93,runID=0,stepIdList=9_12_
	// bug-create-1-0-caseID=1,version=3,resultID=84,runID=6,stepIdList=9_12_,testtask=2,projectID=1,buildID=1
	params := fmt.Sprintf("caseID=%s,version=0,resultID=%s,runID=%s,stepIdList=%s",
		bug.Case, bug.Result, idInTask, stepIds)

	if bug.Testtask != "" {
		temp := fmt.Sprintf("testtask=%s,projectID=%s,buildID=1", bug.Testtask, projectId)
		params += temp
86
	}
87

88 89 90 91 92 93 94 95 96 97 98 99
	uri := fmt.Sprintf("bug-create-%s-0-%s.json", productId, params)
	printUtils.PrintToCmd(uri)

	reqStr, _ := json.Marshal(bug)
	printUtils.PrintToCmd(string(reqStr))

	url := conf.Url + uri
	_, ok := client.PostObject(url, bug)
	if ok {
		printUtils.PrintToCmd(
			fmt.Sprintf("success to submit a bug for case %s-%s", bug.Case, idInTask))
	}
100
}