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.

107 lines
2.4 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
  1. package task
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "git.drinkme.beer/yinghe/log"
  7. "git.drinkme.beer/yinghe/asana/util"
  8. )
  9. type Data struct {
  10. Request Request `json:"data"`
  11. }
  12. type Request struct {
  13. ResourceSubtype string `json:"resource_subtype,omitempty"`
  14. Assignee string `json:"assignee"`
  15. Name string `json:"name"`
  16. Completed bool `json:"completed,omitempty"`
  17. DueOn string `json:"due_on,omitempty"`
  18. Liked bool `json:"linked,omitempty"`
  19. // Notes refer to the content of each Ticket
  20. Notes string `json:"notes"`
  21. StartOn string `json:"start_on,omitempty"`
  22. CustomFields map[string]string `json:"custom_fields"`
  23. Projects []string `json:"projects"`
  24. Workspace string `json:"workspace"`
  25. TicketID string `json:"-"`
  26. TicketType string `json:"-"`
  27. paToken string
  28. }
  29. func (r *Request) GetPAToken() string {
  30. return r.paToken
  31. }
  32. func (r *Request) SetPAToken(token string) {
  33. r.paToken = token
  34. }
  35. type Response struct {
  36. ID string `json:"gid"`
  37. Name string `json:"name"`
  38. ResourceType string `json:"resource_type"`
  39. AssigneeStatus string `json:"assignee_status"`
  40. }
  41. const (
  42. URI = "/api/1.0/tasks"
  43. ResourceSubtype = "default_task"
  44. )
  45. func (r Request) Create() (resp *Response, err error) {
  46. var (
  47. reqData Data
  48. respData struct {
  49. Response Response `json:"data"`
  50. }
  51. )
  52. resp = new(Response)
  53. headers := make(map[string]string)
  54. headers["Authorization"] = r.paToken
  55. headers["Content-Type"] = util.ContentType
  56. if "" == r.ResourceSubtype {
  57. r.ResourceSubtype = ResourceSubtype
  58. }
  59. reqData.Request = r
  60. buf, err := json.Marshal(&reqData)
  61. if nil != err {
  62. log.Errorf("failed to generate task request: %s", err.Error())
  63. return
  64. }
  65. log.Infof("request: %s", string(buf))
  66. client := util.NewHttpClient(util.AsanaHost, URI, util.HttpPostMethod, buf)
  67. client.Headers = headers
  68. err = client.Request()
  69. if nil != err {
  70. log.Errorf("failed to create new Asana task: %s", err.Error())
  71. return
  72. }
  73. log.Infof("response status: %d", client.HTTPStatus)
  74. if http.StatusCreated != client.HTTPStatus {
  75. log.Errorf("unexpected response")
  76. err = errors.New("unexpected response")
  77. return
  78. }
  79. err = json.Unmarshal(client.Body, &respData)
  80. if nil != err {
  81. log.Errorf("illegal task result: %s", err.Error())
  82. return
  83. }
  84. resp = &respData.Response
  85. log.Infof("[%s]new task ID:%s", r.TicketID, resp.ID)
  86. return
  87. }