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.

143 lines
3.2 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
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. c.Print()
  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. client.Print()
  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. client.Print()
  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) call(uri, httpMethod string) (*util.Client, error) {
  92. var (
  93. err error
  94. reqData Data
  95. client = new(util.Client)
  96. )
  97. headers := make(map[string]string)
  98. headers["Authorization"] = r.paToken
  99. headers["Content-Type"] = util.ContentType
  100. if "" == r.ResourceSubtype {
  101. r.ResourceSubtype = ResourceSubtype
  102. }
  103. reqData.Request = r
  104. buf, err := json.Marshal(&reqData)
  105. if nil != err {
  106. log.Errorf("failed to generate task request: %s", err.Error())
  107. return client, err
  108. }
  109. log.Infof("request: %s", string(buf))
  110. client = util.NewHttpClient(util.AsanaHost, uri, httpMethod, buf)
  111. client.Headers = headers
  112. err = client.Request()
  113. if nil != err {
  114. log.Errorf("failed to create new Asana task: %s", err.Error())
  115. return client, err
  116. }
  117. return client, nil
  118. }