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
107 lines
2.4 KiB
package task
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"git.drinkme.beer/yinghe/log"
|
|
|
|
"git.drinkme.beer/yinghe/asana/util"
|
|
)
|
|
|
|
type Data struct {
|
|
Request Request `json:"data"`
|
|
}
|
|
|
|
type Request struct {
|
|
ResourceSubtype string `json:"resource_subtype"`
|
|
Assignee string `json:"assignee"`
|
|
Name string `json:"name"`
|
|
Completed bool `json:"completed"`
|
|
DueOn string `json:"due_on"`
|
|
Liked bool `json:"linked"`
|
|
// Notes refer to the content of each Ticket
|
|
Notes string `json:"notes"`
|
|
StartOn string `json:"start_on"`
|
|
CustomFields map[string]string `json:"custom_fields"`
|
|
Projects []string `json:"projects"`
|
|
Workspace string `json:"workspace"`
|
|
|
|
TicketID string `json:"-"`
|
|
TicketType string `json:"-"`
|
|
paToken string
|
|
}
|
|
|
|
func (r *Request) GetPAToken() string {
|
|
return r.paToken
|
|
}
|
|
|
|
func (r *Request) SetPAToken(token string) {
|
|
r.paToken = token
|
|
}
|
|
|
|
type Response struct {
|
|
ID string `json:"gid"`
|
|
Name string `json:"name"`
|
|
ResourceType string `json:"resource_type"`
|
|
AssigneeStatus string `json:"assignee_status"`
|
|
}
|
|
|
|
const (
|
|
URI = "/api/1.0/tasks"
|
|
ResourceSubtype = "default_task"
|
|
)
|
|
|
|
func (r Request) Create() (resp *Response, err error) {
|
|
var (
|
|
reqData Data
|
|
respData struct {
|
|
Response Response `json:"data"`
|
|
}
|
|
)
|
|
|
|
resp = new(Response)
|
|
|
|
headers := make(map[string]string)
|
|
headers["Authorization"] = r.paToken
|
|
headers["Content-Type"] = util.ContentType
|
|
|
|
if "" == r.ResourceSubtype {
|
|
r.ResourceSubtype = ResourceSubtype
|
|
}
|
|
|
|
reqData.Request = r
|
|
|
|
buf, err := json.Marshal(&reqData)
|
|
if nil != err {
|
|
log.Errorf("failed to generate task request: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
log.Infof("request: %s", string(buf))
|
|
|
|
client := util.NewHttpClient(util.AsanaHost, URI, util.HttpPostMethod, buf)
|
|
client.Headers = headers
|
|
|
|
err = client.Request()
|
|
if nil != err {
|
|
log.Errorf("failed to create new Asana task: %s", err.Error())
|
|
return
|
|
}
|
|
log.Infof("response status: %d", client.HTTPStatus)
|
|
if http.StatusCreated != client.HTTPStatus {
|
|
log.Errorf("unexpected response")
|
|
err = errors.New("unexpected response")
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(client.Body, &respData)
|
|
if nil != err {
|
|
log.Errorf("illegal task result: %s", err.Error())
|
|
return
|
|
}
|
|
resp = &respData.Response
|
|
log.Infof("[%s]new task ID:%s", r.TicketID, resp.ID)
|
|
return
|
|
}
|