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.

196 lines
4.6 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
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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package task
  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"
  9. "git.drinkme.beer/yinghe/asana/util"
  10. )
  11. type Data struct {
  12. Request Request `json:"data"`
  13. }
  14. type Request struct {
  15. ResourceSubtype string `json:"resource_subtype,omitempty"`
  16. Assignee string `json:"assignee,omitempty"`
  17. Name string `json:"name,omitempty"`
  18. Completed bool `json:"completed"`
  19. DueOn string `json:"due_on,omitempty"`
  20. Liked bool `json:"linked,omitempty"`
  21. // Notes refer to the content of each Ticket
  22. Notes string `json:"notes,omitempty"`
  23. HtmlNotes string `json:"html_notes,omitempty"`
  24. StartOn string `json:"start_on,omitempty"`
  25. CustomFields map[string]string `json:"custom_fields,omitempty"`
  26. Projects []string `json:"projects,omitempty"`
  27. Workspace string `json:"workspace,omitempty"`
  28. TicketID string `json:"-"`
  29. TicketType string `json:"-"`
  30. paToken string
  31. }
  32. func (r *Request) GetPAToken() string {
  33. return r.paToken
  34. }
  35. func (r *Request) SetPAToken(token string) {
  36. r.paToken = token
  37. }
  38. type Response struct {
  39. ID string `json:"gid"`
  40. Name string `json:"name"`
  41. ResourceType string `json:"resource_type"`
  42. AssigneeStatus string `json:"assignee_status"`
  43. }
  44. const (
  45. URICreateTask = "/api/1.0/tasks"
  46. URIUpdateTask = "/api/1.0/tasks/%s"
  47. ResourceSubtype = "default_task"
  48. )
  49. func (r Request) Update(taskID string) error {
  50. var (
  51. uri = fmt.Sprintf(URIUpdateTask, taskID)
  52. err error
  53. )
  54. c, err := r.call(uri, util.HttpPutMethod)
  55. if nil != err || nil == c {
  56. return err
  57. }
  58. if http.StatusOK != c.HTTPStatus {
  59. log.Infof("response: %s", string(c.Body))
  60. log.Errorf("unexpected response")
  61. err = errors.New("unexpected response")
  62. return err
  63. }
  64. return nil
  65. }
  66. func (r Request) Create() (resp *Response, err error) {
  67. var (
  68. respData struct {
  69. Response Response `json:"data"`
  70. Errors module.Errors `json:"errors"`
  71. }
  72. )
  73. client, err := r.call(URICreateTask, util.HttpPostMethod)
  74. log.Infof("response status: %d", client.HTTPStatus)
  75. if http.StatusCreated != client.HTTPStatus {
  76. log.Infof("response: %s", string(client.Body))
  77. log.Errorf("unexpected response")
  78. err = errors.New("unexpected response")
  79. return
  80. }
  81. err = json.Unmarshal(client.Body, &respData)
  82. if nil != err {
  83. log.Infof("response: %s", string(client.Body))
  84. log.Errorf("illegal task result: %s", err.Error())
  85. return
  86. }
  87. resp = &respData.Response
  88. log.Infof("[%s]new task ID:%s", r.TicketID, resp.ID)
  89. return
  90. }
  91. //func (r Request) Create() (resp *Response, err error) {
  92. // var (
  93. // reqData Data
  94. // respData struct {
  95. // Response Response `json:"data"`
  96. // }
  97. // )
  98. //
  99. // resp = new(Response)
  100. //
  101. // headers := make(map[string]string)
  102. // headers["Authorization"] = r.paToken
  103. // headers["Content-Type"] = util.ContentType
  104. //
  105. // if "" == r.ResourceSubtype {
  106. // r.ResourceSubtype = ResourceSubtype
  107. // }
  108. //
  109. // reqData.Request = r
  110. //
  111. // buf, err := json.Marshal(&reqData)
  112. // if nil != err {
  113. // log.Errorf("failed to generate task request: %s", err.Error())
  114. // return
  115. // }
  116. //
  117. // log.Infof("request: %s", string(buf))
  118. //
  119. // client := util.NewHttpClient(util.AsanaHost, URI, util.HttpPostMethod, buf)
  120. // client.Headers = headers
  121. //
  122. // err = client.Request()
  123. // if nil != err {
  124. // log.Errorf("failed to create new Asana task: %s", err.Error())
  125. // return
  126. // }
  127. // log.Infof("response status: %d", client.HTTPStatus)
  128. // if http.StatusCreated != client.HTTPStatus {
  129. // log.Errorf("unexpected response")
  130. // err = errors.New("unexpected response")
  131. // return
  132. // }
  133. //
  134. // err = json.Unmarshal(client.Body, &respData)
  135. // if nil != err {
  136. // log.Errorf("illegal task result: %s", err.Error())
  137. // return
  138. // }
  139. // resp = &respData.Response
  140. // log.Infof("[%s]new task ID:%s", r.TicketID, resp.ID)
  141. // return
  142. //}
  143. func (r Request) call(uri, httpMethod string) (*util.Client, error) {
  144. var (
  145. err error
  146. reqData Data
  147. )
  148. headers := make(map[string]string)
  149. headers["Authorization"] = r.paToken
  150. headers["Content-Type"] = util.ContentType
  151. if "" == r.ResourceSubtype {
  152. r.ResourceSubtype = ResourceSubtype
  153. }
  154. reqData.Request = r
  155. buf, err := json.Marshal(&reqData)
  156. if nil != err {
  157. log.Errorf("failed to generate task request: %s", err.Error())
  158. return nil, err
  159. }
  160. log.Infof("request: %s", string(buf))
  161. client := util.NewHttpClient(util.AsanaHost, uri, httpMethod, buf)
  162. client.Headers = headers
  163. err = client.Request()
  164. if nil != err {
  165. log.Errorf("failed to create new Asana task: %s", err.Error())
  166. return nil, err
  167. }
  168. return client, nil
  169. }