Asana Integeration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package story
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "git.drinkme.beer/yinghe/log"
  8. "git.drinkme.beer/yinghe/asana/module/users"
  9. "git.drinkme.beer/yinghe/asana/util"
  10. )
  11. const (
  12. URI = "/api/1.0/tasks/%s/stories"
  13. )
  14. type Data struct {
  15. Request Request `json:"data"`
  16. }
  17. type Request struct {
  18. HtmlText string `json:"html_text,omitempty"`
  19. Text string `json:"text,omitempty"`
  20. StickerName string `json:"sticker_name,omitempty"`
  21. IsPinned bool `json:"is_pinned,omitempty"`
  22. taskID string
  23. ticketID string
  24. paToken string
  25. }
  26. func (r *Request) GetTaskID() string {
  27. return r.taskID
  28. }
  29. func (r *Request) SetTaskID(id string) {
  30. r.taskID = id
  31. }
  32. func (r *Request) GetTicketID() string {
  33. return r.ticketID
  34. }
  35. func (r *Request) SetTicketID(id string) {
  36. r.ticketID = id
  37. }
  38. func (r *Request) GetPAToken() string {
  39. return r.paToken
  40. }
  41. func (r *Request) SetPAToken(token string) {
  42. r.paToken = token
  43. }
  44. type Response struct {
  45. ID string `json:"gid"`
  46. ResourceType string `json:"resource_type"`
  47. ResourceSubtype string `json:"resource_subtype"`
  48. Type string `json:"type"`
  49. Text string `json:"text"`
  50. // IsPinned Conditional
  51. // Whether the story should be pinned on the resource.
  52. IsPinned bool `json:"is_pinned"`
  53. StickerName string `json:"sticker_name,omitempty"`
  54. CreatedAt string `json:"created_at"`
  55. CreatedBy users.User `json:"created_by"`
  56. }
  57. func (r *Response) IsComplete() bool {
  58. return "marked_complete" == r.ResourceSubtype
  59. }
  60. func (r Request) validate() error {
  61. if "" == r.taskID {
  62. log.Errorf("task id is empty")
  63. return errors.New("task id is empty")
  64. }
  65. return nil
  66. }
  67. // Create comments on task
  68. func (r Request) Create() (resp *Response, err error) {
  69. var (
  70. reqData Data
  71. respData struct {
  72. Response Response `json:"data"`
  73. }
  74. )
  75. if err = r.validate(); nil != err {
  76. return
  77. }
  78. resp = new(Response)
  79. headers := make(map[string]string)
  80. headers["Authorization"] = r.paToken
  81. headers["Content-Type"] = util.ContentType
  82. reqData.Request = r
  83. buf, err := json.Marshal(&reqData)
  84. if nil != err {
  85. log.Errorf("failed to generate comment request: %s", err.Error())
  86. return
  87. }
  88. log.Infof("request: %s", string(buf))
  89. client := util.NewHttpClient(util.AsanaHost, fmt.Sprintf(URI, r.taskID), util.HttpPostMethod, buf)
  90. client.Headers = headers
  91. err = client.Request()
  92. if nil != err {
  93. log.Errorf("failed to create new task comment: %s", err.Error())
  94. return
  95. }
  96. log.Infof("response status: %d", client.HTTPStatus)
  97. if http.StatusCreated != client.HTTPStatus {
  98. log.Errorf("unexpected response")
  99. err = errors.New("unexpected response")
  100. return
  101. }
  102. err = json.Unmarshal(client.Body, &respData)
  103. if nil != err {
  104. log.Errorf("illegal comment result: %s", err.Error())
  105. return
  106. }
  107. resp = &respData.Response
  108. log.Infof("[%s]new comment ID:%s", r.taskID, resp.ID)
  109. return
  110. }
  111. func (r Request) Get() ([]Response, error) {
  112. var (
  113. resp struct {
  114. Data []Response `json:"data"`
  115. }
  116. )
  117. headers := make(map[string]string)
  118. headers["Authorization"] = r.paToken
  119. headers["Content-Type"] = util.ContentType
  120. client := util.NewHttpClient(util.AsanaHost, fmt.Sprintf(URI, r.taskID), util.HttpGetMethod, nil)
  121. client.Headers = headers
  122. err := client.Request()
  123. if nil != err || http.StatusOK != client.HTTPStatus {
  124. client.Print()
  125. log.Errorf("failed")
  126. }
  127. err = json.Unmarshal(client.Body, &resp)
  128. if nil != err {
  129. log.Errorf("invalid response")
  130. return nil, fmt.Errorf("invalid response")
  131. }
  132. return resp.Data, nil
  133. }