提交 39893ce4 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

change to use new rest interface

上级 a9c41cae
......@@ -35,7 +35,8 @@ var (
ProductId string
ZenTaoVersion string
SessionVar string
Token = "Token"
SessionVar = "zentaosid"
SessionId string
RequestFix string
......
......@@ -38,11 +38,11 @@ func CommitBug(ztfBug commDomain.ZtfBug, projectPath string) (err error) {
params = fmt.Sprintf("productID=%s&branch=0&$extras=%s", ztfBug.Product, extras)
}
//params = ""
url := config.Url + GenApiUri("bug", "create", params)
url := config.Url + GenApiUriOld("bug", "create", params)
bug := commDomain.ZentaoBug{}
copier.Copy(&bug, ztfBug)
_, err = httpUtils.Post(url, bug, true)
_, err = httpUtils.PostWithFormat(url, bug, true)
msg := ""
......@@ -137,7 +137,7 @@ func GetBugFiledOptions(req commDomain.FuncResult, projectPath string) (
params = fmt.Sprintf("productID=%d", req.ProductId)
}
url := config.Url + GenApiUri("bug", "ajaxGetBugFieldOptions", params)
url := config.Url + GenApiUriOld("bug", "ajaxGetBugFieldOptions", params)
bytes, err := httpUtils.Get(url)
if err == nil {
jsonData := &simplejson.Json{}
......
......@@ -121,7 +121,7 @@ func ListCaseByProduct(baseUrl string, productId int) []commDomain.ZtfCase {
params = fmt.Sprintf("productID=%d&branch=&browseType=byModule&param=0&orderBy=id_desc&recTotal=0&recPerPage=10000", productId)
}
url := baseUrl + GenApiUri("testcase", "browse", params)
url := baseUrl + GenApiUriOld("testcase", "browse", params)
dataStr, err := httpUtils.Get(url)
if err == nil {
......@@ -156,7 +156,7 @@ func ListCaseByModule(baseUrl string, productId, moduleId int) []commDomain.ZtfC
productId, moduleId)
}
url := baseUrl + GenApiUri("testcase", "browse", params)
url := baseUrl + GenApiUriOld("testcase", "browse", params)
bytes, err := httpUtils.Get(url)
if err == nil {
......@@ -190,7 +190,7 @@ func ListCaseBySuite(baseUrl string, productId, suiteId int) []commDomain.ZtfCas
params = fmt.Sprintf("suiteID=%d&orderBy=id_asc&recTotal=0&recPerPage=10000", suiteId)
}
url := baseUrl + GenApiUri("testsuite", "view", params)
url := baseUrl + GenApiUriOld("testsuite", "view", params)
bytes, err := httpUtils.Get(url)
if err == nil {
......@@ -225,7 +225,7 @@ func ListCaseByTask(baseUrl string, productId, taskId int) []commDomain.ZtfCase
params = fmt.Sprintf("taskID=%d&browseType=all&param=0&orderBy=id_asc&recTotal=0&recPerPage=10000", taskId)
}
url := baseUrl + GenApiUri("testtask", "cases", params)
url := baseUrl + GenApiUriOld("testtask", "cases", params)
bytes, err := httpUtils.Get(url)
if err == nil {
......@@ -265,28 +265,18 @@ func genCaseSteps(csWithSteps commDomain.ZtfCase) (ret []commDomain.ZtfStep) {
return
}
func GetCaseById(baseUrl string, caseId string) commDomain.ZtfCase {
// $caseID, $version = 0, $from = 'testcase', $taskID = 0
func GetCaseById(baseUrl string, caseId string) (cs commDomain.ZtfCase) {
uri := fmt.Sprintf("/testcases/%s", caseId)
url := GenApiUrl(uri, nil, baseUrl)
params := ""
if commConsts.RequestType == commConsts.PathInfo {
params = fmt.Sprintf("%s-0-testcase-0", caseId)
} else {
params = fmt.Sprintf("caseID=%s&version=0&$from=testcase&taskID=0", caseId)
}
url := baseUrl + GenApiUri("testcase", "view", params)
bytes, err := httpUtils.Get(url)
if err == nil {
var csw commDomain.ZtfCaseWrapper
json.Unmarshal(bytes, &csw)
cs := csw.Case
return cs
if err != nil {
return
}
return commDomain.ZtfCase{}
json.Unmarshal(bytes, &cs)
return
}
func CommitCase(caseId int, title string,
......@@ -306,7 +296,7 @@ func CommitCase(caseId int, title string,
params = fmt.Sprintf("caseID=%d&comment=0", caseId)
}
url := config.Url + GenApiUri("testcase", "edit", params)
url := config.Url + GenApiUriOld("testcase", "edit", params)
requestObj := map[string]interface{}{"title": title,
"steps": commonUtils.LinkedMapToMap(stepMap),
......@@ -329,7 +319,7 @@ func CommitCase(caseId int, title string,
}
if yes {
_, err = httpUtils.Post(url, requestObj, true)
_, err = httpUtils.PostWithFormat(url, requestObj, true)
if err == nil {
logUtils.Infof(i118Utils.Sprintf("success_to_commit_case", caseId) + "\n")
}
......
......@@ -7,10 +7,35 @@ import (
commConsts "github.com/aaronchen2k/deeptest/internal/comm/consts"
i118Utils "github.com/aaronchen2k/deeptest/internal/pkg/lib/i118"
serverDomain "github.com/aaronchen2k/deeptest/internal/server/modules/v1/domain"
"path"
"strings"
)
func GenApiUri(module string, methd string, param string) string {
const (
ApiPath = "api.php/v1/"
)
func GenApiUrl(pth string, params map[string]interface{}, baseUrl string) (url string) {
uri := path.Join(ApiPath, pth)
index := 0
for key, val := range params {
if index == 0 {
uri += "?"
} else {
uri += "&"
}
uri += fmt.Sprintf("%v=%v", key, val)
index++
}
url = baseUrl + uri
return
}
func GenApiUriOld(module string, methd string, param string) string {
var uri string
if commConsts.RequestType == commConsts.PathInfo {
......
......@@ -30,8 +30,8 @@ func CommitResult(report commDomain.ZtfReport, productId, taskId string, project
config := configUtils.LoadByProjectPath(projectPath)
Login(config)
url := config.Url + GenApiUri("ci", "commitResult", "")
ret, err := httpUtils.Post(url, report, false)
url := config.Url + GenApiUriOld("ci", "commitResult", "")
ret, err := httpUtils.PostWithFormat(url, report, false)
msg := ""
if err == nil {
......
package zentaoHelper
import (
"encoding/json"
"errors"
"fmt"
commConsts "github.com/aaronchen2k/deeptest/internal/comm/consts"
......@@ -16,42 +17,6 @@ import (
"strings"
)
func Login(config commDomain.ProjectConf) (err error) {
err = GetConfig(config.Url)
if err != nil {
logUtils.Infof(i118Utils.Sprintf("fail_to_login"))
return
}
uri := ""
if commConsts.RequestType == commConsts.PathInfo {
uri = "user-login.json"
} else {
uri = "index.php?m=user&f=login&t=json"
}
url := config.Url + uri
params := make(map[string]string)
params["account"] = config.Username
params["password"] = config.Password
var bodyBytes []byte
bodyBytes, err = httpUtils.PostStr(url, params)
if err != nil || (err == nil && strings.Index(string(bodyBytes), "title") > 0) { // use PostObject to login again for new system
_, err = httpUtils.Post(url, params, true)
}
if err == nil {
if commConsts.Verbose {
logUtils.Info(i118Utils.Sprintf("success_to_login"))
}
} else {
logUtils.Errorf(i118Utils.Sprintf("fail_to_login"))
}
return
}
func GetConfig(baseUrl string) (err error) {
//if commConsts.RequestType != "" {
// return true
......@@ -87,6 +52,39 @@ func GetConfig(baseUrl string) (err error) {
return
}
func Login(config commDomain.ProjectConf) (err error) {
err = GetConfig(config.Url)
if err != nil {
logUtils.Infof(i118Utils.Sprintf("fail_to_login"))
return
}
url := GenApiUrl("tokens", nil, config.Url)
params := map[string]string{
"account": config.Username,
"password": config.Password,
}
bodyBytes, err := httpUtils.Post(url, params)
if err != nil {
logUtils.Errorf(i118Utils.Sprintf("fail_to_login"))
}
if commConsts.Verbose {
logUtils.Info(i118Utils.Sprintf("success_to_login") + string(bodyBytes))
}
jsn, _ := simplejson.NewJson(bodyBytes)
mp, _ := jsn.Map()
val, ok := mp["token"]
if ok {
commConsts.SessionId = val.(string)
}
return
}
func ListLang() (langs []serverDomain.ZentaoLang, err error) {
for key, _ := range langUtils.LangMap {
langs = append(langs, serverDomain.ZentaoLang{Code: key, Name: key})
......@@ -107,62 +105,29 @@ func ListProduct(projectPath string) (products []serverDomain.ZentaoProduct, err
return
}
// $productID = 0, $branch = 0, $browseType = '', $param = 0, $storyType = 'story',
// $orderBy = '', $recTotal = 0, $recPerPage = 20, $pageID = 1, $projectID = 0)
params := ""
if commConsts.RequestType == commConsts.PathInfo {
params = fmt.Sprintf("-----id_asc-0-10000-1-0")
} else {
params = fmt.Sprintf("orderBy=id_asc&recTotal=0&recPerPage=10000")
}
url := config.Url + GenApiUri("product", "browse", params)
url := GenApiUrl("products", nil, config.Url)
bytes, err := httpUtils.Get(url)
if err != nil {
return
}
jsn, _ := simplejson.NewJson(bytes)
productMap, err := jsn.Get("products").Map()
for key, val := range productMap {
id, _ := strconv.Atoi(key)
products = append(products, serverDomain.ZentaoProduct{Id: id, Name: val.(string)})
}
return
}
func ListModuleByProduct(productId int, projectPath string) (modules []serverDomain.ZentaoModule, err error) {
config := configUtils.LoadByProjectPath(projectPath)
err = Login(config)
jsn, err := simplejson.NewJson(bytes)
if err != nil {
return
}
// tree-browse-1-story.html#app=product
params := ""
if commConsts.RequestType == commConsts.PathInfo {
params = fmt.Sprintf("%d-story", productId)
} else {
params = fmt.Sprintf("rootID=%d&viewType=story", productId)
}
url := config.Url + GenApiUri("tree", "browse", params)
url += "#app=product"
bytes, err := httpUtils.Get(url)
items, err := jsn.Get("products").Array()
if err != nil {
return
}
jsn, _ := simplejson.NewJson(bytes)
arr, _ := jsn.Get("tree").Array()
for _, item := range arr {
mp := item.(map[string]interface{})
mp["level"] = 0
GenModuleData(mp, &modules)
for _, item := range items {
productMap, _ := item.(map[string]interface{})
id, _ := productMap["id"].(json.Number).Int64()
name, _ := productMap["name"].(string)
products = append(products, serverDomain.ZentaoProduct{Id: int(id), Name: name})
}
return
......@@ -183,7 +148,7 @@ func ListModuleForCase(productId int, projectPath string) (modules []serverDomai
params = fmt.Sprintf("rootID=%d&viewType=case&from=qa", productId)
}
url := config.Url + GenApiUri("tree", "browse", params)
url := config.Url + GenApiUriOld("tree", "browse", params)
url += "#app=product"
bytes, err := httpUtils.Get(url)
......@@ -237,7 +202,7 @@ func ListSuiteByProduct(productId int, projectPath string) (suites []serverDomai
params = fmt.Sprintf("productID=%d&orderBy=id_asc&recTotal=0&recPerPage=10000", productId)
}
url := config.Url + GenApiUri("testsuite", "browse", params)
url := config.Url + GenApiUriOld("testsuite", "browse", params)
bytes, err := httpUtils.Get(url)
if err != nil {
......@@ -261,36 +226,42 @@ func ListSuiteByProduct(productId int, projectPath string) (suites []serverDomai
func ListTaskByProduct(productId int, projectPath string) (tasks []serverDomain.ZentaoTask, err error) {
config := configUtils.LoadByProjectPath(projectPath)
if config.Url == "" {
err = errors.New(i118Utils.Sprintf("pls_config_project"))
return
}
err = Login(config)
if err != nil {
return
}
// $productID = 0, $branch = '', $type = 'local,totalStatus', $orderBy = 'id_asc', $recTotal = 0, $recPerPage = 20, $pageID = 1, $beginTime = 0, $endTime = 0)
params := ""
if commConsts.RequestType == commConsts.PathInfo {
params = fmt.Sprintf("%d--local,totalStatus-id_asc-0-10000-1", productId)
} else {
params = fmt.Sprintf("productID=%d&type=local,totalStatus&orderBy=id_asc&recTotal=0&recPerPage=10000", productId)
params := map[string]interface{}{
"product": productId,
"limit": 10000,
}
url := config.Url + GenApiUri("testtask", "browse", params)
url := GenApiUrl("testtasks", params, config.Url)
bytes, err := httpUtils.Get(url)
if err != nil {
return
}
jsn, err := simplejson.NewJson(bytes)
if err != nil {
return
}
items, err := jsn.Get("testtasks").Array()
if err != nil {
return
}
jsn, _ := simplejson.NewJson(bytes)
mp, _ := jsn.Get("tasks").Map()
for _, item := range mp {
mp := item.(map[string]interface{})
for _, item := range items {
taskMap, _ := item.(map[string]interface{})
idStr := mp["id"].(string)
id, _ := strconv.Atoi(idStr)
name := mp["name"].(string)
id, _ := taskMap["id"].(json.Number).Int64()
name, _ := taskMap["name"].(string)
tasks = append(tasks, serverDomain.ZentaoTask{Id: id, Name: name})
tasks = append(tasks, serverDomain.ZentaoTask{Id: int(id), Name: name})
}
return
......
......@@ -18,9 +18,6 @@ import (
)
func Get(url string) (ret []byte, err error) {
if strings.Index(url, "mode=getconfig") < 0 {
url = AddToken(url)
}
if commConsts.Verbose {
logUtils.Info(url)
}
......@@ -33,6 +30,10 @@ func Get(url string) (ret []byte, err error) {
return
}
if strings.Index(url, "user-login") < 0 && strings.Index(url, "mode=getconfig") < 0 {
req.Header.Add(commConsts.Token, commConsts.SessionId)
}
resp, err := client.Do(req)
if err != nil {
logUtils.Infof(color.RedString("get request failed, error: %s.", err.Error()))
......@@ -82,7 +83,61 @@ func Get(url string) (ret []byte, err error) {
return
}
func Post(url string, data interface{}, useFormFormat bool) (ret []byte, err error) {
func Post(url string, data interface{}) (ret []byte, err error) {
if commConsts.Verbose {
logUtils.Infof(i118Utils.Sprintf("server_address") + url)
}
client := &http.Client{}
dataBytes, err := json.Marshal(data)
if err != nil {
logUtils.Infof(color.RedString("marshal request failed, error: %s.", err.Error()))
return
}
dataStr := string(dataBytes)
if commConsts.Verbose {
logUtils.Infof(i118Utils.Sprintf("request_content"))
logUtils.Infof(dataStr)
}
req, err := http.NewRequest("POST", url, strings.NewReader(dataStr))
if err != nil {
logUtils.Infof(color.RedString("post request failed, error: %s.", err.Error()))
return
}
if strings.Index(url, "/tokens") < 0 {
req.Header.Add(commConsts.Token, commConsts.SessionId)
}
//req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
logUtils.Infof(color.RedString("post request failed, error: %s.", err.Error()))
return
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
logUtils.Infof(color.RedString("read response failed, error: %s.", err.Error()))
return
}
if commConsts.Verbose {
logUtils.Infof(i118Utils.Sprintf("request_response"))
logUtils.Infof(logUtils.ConvertUnicode(bodyBytes))
}
defer resp.Body.Close()
ret, err = GetRespErr(bodyBytes, url)
return
}
func PostWithFormat(url string, data interface{}, useFormFormat bool) (ret []byte, err error) {
url = AddToken(url)
if commConsts.Verbose {
......@@ -233,7 +288,7 @@ func AddToken(url string) (ret string) {
paramPir := commConsts.SessionVar + "=" + commConsts.SessionId
if commConsts.RequestType == commConsts.PathInfo {
if commConsts.RequestType == commConsts.PathInfo && strings.Index(address, "?") < 0 {
address = address + "?" + paramPir
} else {
address = address + "&" + paramPir
......
{
"status": "success",
"data": "",
"md5": "892b6c49eee86c8a4273c6707e1a2f04"
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册