123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- package dao
- import (
- "context"
- "encoding/json"
- "fmt"
- "net/http"
- "go-common/app/admin/ep/marthe/model"
- "go-common/library/cache/memcache"
- "go-common/library/ecode"
- "go-common/library/log"
- )
- const (
- _userName = "bilibilinik"
- _password = "0989D4F0-AF9D-949F-C950-E22A3F891NIK"
- _successCode = 1
- _releaseURL = "https://api.tapd.cn/releases?workspace_id=%s&id=%s"
- _bugPreURL = "https://api.tapd.cn/bugs?workspace_id=%s&id=%s"
- _createBugURL = "https://api.tapd.cn/bugs"
- _workspaceUserURL = "https://api.tapd.cn/workspaces/users?workspace_id=%s&fields=user"
- )
- //Bug Bug
- func (d *Dao) Bug(bugURL string) (res *model.BugResponse, err error) {
- var req *http.Request
- res = &model.BugResponse{}
- if req, err = d.newTapdRequest(http.MethodGet, bugURL, nil); err != nil {
- log.Error("Bug url(%s) res(%v) err(%v)", bugURL, res, err)
- return nil, ecode.MartheTapdErr
- }
- if err = d.httpClient.Do(context.TODO(), req, res); err != nil {
- log.Error("WorkspaceUser url(%s) res(%v) err(%v)", bugURL, res, err)
- err = ecode.MartheTapdResDataErr
- return
- }
- if res.Status != _successCode {
- log.Error("WorkspaceUser url(%s) res(%v) err(%v)", bugURL, res, err)
- err = ecode.MartheTapdResDataErr
- }
- return
- }
- //BugPre Bug Pre
- func (d *Dao) BugPre(workSpaceID, bugID string) (bug *model.Bug, err error) {
- var (
- req *http.Request
- res = &model.BugSingleResponse{}
- )
- url := fmt.Sprintf(_bugPreURL, workSpaceID, bugID)
- if req, err = d.newTapdRequest(http.MethodGet, url, nil); err != nil {
- log.Error("BugPre url(%s) res(%v) err(%v)", url, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if err = d.httpClient.Do(context.TODO(), req, res); err != nil || res.Data == nil {
- log.Error("BugPre url(%s) res(%v) err(%v)", url, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if res.Status != _successCode || res.Data == nil {
- log.Error("BugPre url(%s) res(%v) err(%v)", url, res, err)
- err = ecode.MartheTapdResDataErr
- return
- }
- bug = res.Data.Bug
- return
- }
- //Release Release
- func (d *Dao) Release(workSpaceID, releaseID string) (release *model.Release, err error) {
- var (
- req *http.Request
- res = &model.ReleaseResponse{}
- )
- releaseURL := fmt.Sprintf(_releaseURL, workSpaceID, releaseID)
- if req, err = d.newTapdRequest(http.MethodGet, releaseURL, nil); err != nil {
- log.Error("Release url(%s) res(%v) err(%v)", _releaseURL, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if err = d.httpClient.Do(context.TODO(), req, res); err != nil || res.Data == nil {
- log.Error("Release url(%s) res(%v) err(%v)", _releaseURL, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if res.Status != _successCode || res.Data == nil {
- log.Error("Release url(%s) res(%v) err(%v)", _releaseURL, res, err)
- err = ecode.MartheTapdResDataErr
- return
- }
- release = res.Data.Release
- return
- }
- // ReleaseName ReleaseName
- func (d *Dao) ReleaseName(workspaceID, releaseID string) (releaseName string, err error) {
- var (
- item *memcache.Item
- conn = d.mc.Get(context.Background())
- release *model.Release
- )
- defer conn.Close()
- if item, err = conn.Get(workspaceID + releaseID); err == nil {
- if err = json.Unmarshal(item.Value, &releaseName); err != nil {
- log.Error("Json unmarshal err(%v)", err)
- }
- return
- }
- if release, err = d.Release(workspaceID, releaseID); err != nil {
- return
- }
- releaseName = release.Name
- item = &memcache.Item{Key: workspaceID + releaseID, Object: release.Name, Flags: memcache.FlagJSON, Expiration: d.expire}
- d.cacheSave(context.Background(), item)
- return
- }
- //newTapdRequest new tapd request
- func (d *Dao) newTapdRequest(method, url string, v interface{}) (req *http.Request, err error) {
- req, err = d.newRequest(method, url, v)
- req.SetBasicAuth(_userName, _password)
- return
- }
- // CreateBug Create Bug.
- func (d *Dao) CreateBug(bug *model.Bug) (bugID string, err error) {
- var (
- req *http.Request
- res = &model.BugSingleResponse{}
- )
- if req, err = d.newTapdRequest(http.MethodPost, _createBugURL, bug); err != nil {
- log.Error("CreateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if err = d.httpClient.Do(context.TODO(), req, res); err != nil {
- log.Error("CreateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if res.Status != _successCode {
- log.Error("CreateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
- err = ecode.MartheTapdResDataErr
- }
- bugID = res.Data.Bug.ID
- return
- }
- // UpdateBug Update Bug.
- func (d *Dao) UpdateBug(bug *model.UpdateBug) (err error) {
- var (
- req *http.Request
- res = &model.BugSingleResponse{}
- )
- if req, err = d.newTapdRequest(http.MethodPost, _createBugURL, bug); err != nil {
- log.Error("UpdateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if err = d.httpClient.Do(context.TODO(), req, res); err != nil {
- log.Error("UpdateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if res.Status != _successCode {
- log.Error("UpdateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
- err = ecode.MartheTapdResDataErr
- }
- return
- }
- // WorkspaceUser Workspace User
- func (d *Dao) WorkspaceUser(workspaceID string) (usernames []string, err error) {
- var (
- req *http.Request
- res = &model.WorkspaceUserResponse{}
- )
- workspaceUserURL := fmt.Sprintf(_workspaceUserURL, workspaceID)
- if req, err = d.newTapdRequest(http.MethodGet, workspaceUserURL, nil); err != nil {
- log.Error("WorkspaceUser url(%s) res(%v) err(%v)", workspaceUserURL, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if err = d.httpClient.Do(context.TODO(), req, res); err != nil {
- log.Error("WorkspaceUser url(%s) res(%v) err(%v)", workspaceUserURL, res, err)
- err = ecode.MartheTapdErr
- return
- }
- if res.Status != _successCode || res.Data == nil {
- log.Error("WorkspaceUser url(%s) res(%v) err(%v)", workspaceUserURL, res, err)
- err = ecode.MartheTapdResDataErr
- return
- }
- for _, workspaceUser := range res.Data {
- usernames = append(usernames, workspaceUser.UserWrapper.User)
- }
- return
- }
|