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.

127 lines
2.5 KiB

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/util"
  9. )
  10. const (
  11. URI = "/api/1.0/tasks/%s/stories"
  12. )
  13. type Data struct {
  14. Request Request `json:"data"`
  15. }
  16. type Request struct {
  17. HtmlText string `json:"html_text,omitempty"`
  18. Text string `json:"text,omitempty"`
  19. StickerName string `json:"sticker_name,omitempty"`
  20. IsPinned bool `json:"is_pinned,omitempty"`
  21. taskID string
  22. ticketID string
  23. paToken string
  24. }
  25. func (r *Request) GetTaskID() string {
  26. return r.taskID
  27. }
  28. func (r *Request) SetTaskID(id string) {
  29. r.taskID = id
  30. }
  31. func (r *Request) GetTicketID() string {
  32. return r.ticketID
  33. }
  34. func (r *Request) SetTicketID(id string) {
  35. r.ticketID = id
  36. }
  37. func (r *Request) GetPAToken() string {
  38. return r.paToken
  39. }
  40. func (r *Request) SetPAToken(token string) {
  41. r.paToken = token
  42. }
  43. type Response struct {
  44. ID string `json:"gid"`
  45. ResourceType string `json:"resource_type"`
  46. Type string `json:"type"`
  47. Text string `json:"text"`
  48. // IsPinned Conditional
  49. // Whether the story should be pinned on the resource.
  50. IsPinned bool `json:"is_pinned"`
  51. StickerName string `json:"sticker_name,omitempty"`
  52. }
  53. func (r Request) validate() error {
  54. if "" == r.taskID {
  55. log.Errorf("task id is empty")
  56. return errors.New("task id is empty")
  57. }
  58. return nil
  59. }
  60. // Create comments on task
  61. func (r Request) Create() (resp *Response, err error) {
  62. var (
  63. reqData Data
  64. respData struct {
  65. Response Response `json:"data"`
  66. }
  67. )
  68. if err = r.validate(); nil != err {
  69. return
  70. }
  71. resp = new(Response)
  72. headers := make(map[string]string)
  73. headers["Authorization"] = r.paToken
  74. headers["Content-Type"] = util.ContentType
  75. reqData.Request = r
  76. buf, err := json.Marshal(&reqData)
  77. if nil != err {
  78. log.Errorf("failed to generate comment request: %s", err.Error())
  79. return
  80. }
  81. log.Infof("request: %s", string(buf))
  82. client := util.NewHttpClient(util.AsanaHost, fmt.Sprintf(URI, r.taskID), util.HttpPostMethod, buf)
  83. client.Headers = headers
  84. err = client.Request()
  85. if nil != err {
  86. log.Errorf("failed to create new task comment: %s", err.Error())
  87. return
  88. }
  89. log.Infof("response status: %d", client.HTTPStatus)
  90. if http.StatusCreated != client.HTTPStatus {
  91. log.Errorf("unexpected response")
  92. err = errors.New("unexpected response")
  93. return
  94. }
  95. err = json.Unmarshal(client.Body, &respData)
  96. if nil != err {
  97. log.Errorf("illegal comment result: %s", err.Error())
  98. return
  99. }
  100. resp = &respData.Response
  101. log.Infof("[%s]new comment ID:%s", r.taskID, resp.ID)
  102. return
  103. }