From da3d29fa54534016b069de432b62e815f41b2625 Mon Sep 17 00:00:00 2001 From: ysicing Date: Wed, 9 Oct 2024 10:54:53 +0800 Subject: [PATCH] + [feat] add modules api --- README.md | 2 ++ example/modules/main.go | 45 ++++++++++++++++++++++++ hack/docker-compose.yml | 2 +- zentao/modules.go | 77 +++++++++++++++++++++++++++++++++++++++++ zentao/types.go | 11 ++++++ zentao/zentao.go | 2 ++ 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 example/modules/main.go create mode 100644 zentao/modules.go diff --git a/README.md b/README.md index 3f262ef..4df8b35 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,8 @@ Zentao API client enabling Go programs to interact with Zentao in a simple and u - [x] 修改反馈 - [x] 关闭反馈 - [x] 指派反馈 +- [x] 模块 + - [x] 获取模块列表 ### 支持PathINFO/Get内置页面接口 diff --git a/example/modules/main.go b/example/modules/main.go new file mode 100644 index 0000000..5c720a9 --- /dev/null +++ b/example/modules/main.go @@ -0,0 +1,45 @@ +// +// Copyright 2024, easysoft +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package main + +import ( + "github.com/easysoft/go-zentao/v20/zentao" + + "github.com/davecgh/go-spew/spew" +) + +func main() { + zt, err := zentao.NewBasicAuthClient( + "admin", + "jaege1ugh4ooYip7", + zentao.WithBaseURL("http://127.0.0.1"), + zentao.WithDevMode(), + zentao.WithDumpAll(), + zentao.WithoutProxy(), + ) + if err != nil { + panic(err) + } + p2, _, err := zt.Modules.List(zentao.ModulesListOptions{ + Type: zentao.ModuleTask, + ID: 3, + }) + if err != nil { + panic(err) + } + spew.Dump(p2) +} diff --git a/hack/docker-compose.yml b/hack/docker-compose.yml index 5f99e44..a900524 100644 --- a/hack/docker-compose.yml +++ b/hack/docker-compose.yml @@ -33,7 +33,7 @@ services: # zentao service zentao: - image: easysoft/zentao:20.7 + image: easysoft/zentao:20.7.1 container_name: zentao pull_policy: always restart: always diff --git a/zentao/modules.go b/zentao/modules.go new file mode 100644 index 0000000..49bf0f1 --- /dev/null +++ b/zentao/modules.go @@ -0,0 +1,77 @@ +// +// Copyright 2024, zentao +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package zentao + +import ( + "fmt" + "net/url" + + "github.com/imroc/req/v3" +) + +type ModulesService struct { + client *Client +} + +type ModulesListOptions struct { + Type ModuleType `json:"type"` + ID int `json:"id"` +} + +func (o ModulesListOptions) toURLValues() (string, error) { + v := url.Values{} + if len(o.Type) == 0 { + return "", fmt.Errorf("Type is required") + } + if o.ID == 0 { + return "", fmt.Errorf("ID is required") + } + v.Set("type", fmt.Sprintf("%v", o.Type)) + v.Set("id", fmt.Sprintf("%v", o.ID)) + return v.Encode(), nil +} + +type ListModules struct { + Modules []Module `json:"modules"` +} + +type Module struct { + ID int `json:"id,omitempty"` // 子模块ID + Name string `json:"name"` + Root int `json:"root"` + Type ModuleType `json:"type"` + Actions bool `json:"actions,omitempty"` // 父模块字段 + Grade int `json:"grade,omitempty"` // 子模块字段 + Parent int `json:"parent,omitempty"` // 子模块字段 + URL string `json:"url,omitempty"` // 子模块字段 + Path string `json:"path,omitempty"` // 子模块字段 + Children []Module `json:"children,omitempty"` +} + +func (s *ModulesService) List(op ModulesListOptions) (*ListModules, *req.Response, error) { + var et ListModules + opValues, err := op.toURLValues() + if err != nil { + return nil, nil, err + } + resp, err := s.client.client.R(). + SetHeader("Token", s.client.token). + SetQueryString(opValues). + SetSuccessResult(&et). + Get(s.client.RequestURL("/modules")) + return &et, resp, err +} diff --git a/zentao/types.go b/zentao/types.go index dc24711..47b428d 100644 --- a/zentao/types.go +++ b/zentao/types.go @@ -152,6 +152,17 @@ var ( BugExternal BugCloseReason = "external" // 外部原因 ) +type ModuleType string // 模块类型 + +var ( + ModuleStory ModuleType = "story" // 需求 + ModuleBug ModuleType = "bug" // Bug + ModuleTask ModuleType = "task" // 任务 + ModuleCase ModuleType = "case" // 用例 + ModuleFeedback ModuleType = "feedback" // 反馈 + ModuleProduct ModuleType = "product" // 产品 +) + // CustomResp 通用Resp type CustomResp struct { Message string `json:"message,omitempty"` diff --git a/zentao/zentao.go b/zentao/zentao.go index d40a183..a56a893 100644 --- a/zentao/zentao.go +++ b/zentao/zentao.go @@ -64,6 +64,7 @@ type Client struct { TestCases *TestCasesService TestTasks *TestTasksService FeedBacks *FeedBacksService + Modules *ModulesService } func NewClient(token string, options ...ClientOptionFunc) (*Client, error) { @@ -121,6 +122,7 @@ func newClient(options ...ClientOptionFunc) (*Client, error) { c.TestCases = &TestCasesService{client: c} c.TestTasks = &TestTasksService{client: c} c.FeedBacks = &FeedBacksService{client: c} + c.Modules = &ModulesService{client: c} return c, nil }