From c9269718ec03c03ad40f8228e1d3db68cb4f7ad4 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 11 Sep 2021 03:53:50 +0000 Subject: [PATCH] Move tasks between sections --- asana/client.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++- main.go | 7 +++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/asana/client.go b/asana/client.go index 93b1354..e69caa7 100644 --- a/asana/client.go +++ b/asana/client.go @@ -1,8 +1,9 @@ package asana +import "bytes" import "encoding/json" -import "io/ioutil" import "fmt" +import "io/ioutil" import "net/http" import "net/url" import "os" @@ -56,6 +57,18 @@ type Workspace struct { Name string `json:"name"` } +type addTaskDetails struct { + Task string `json:"task"` +} + +type addTaskRequest struct { + Data *addTaskDetails `json:"data"` +} + +type emptyResponse struct { + Data interface{} `json:"data"` +} + type projectResponse struct { Data *Project `json:"data"` } @@ -96,6 +109,24 @@ func NewClientFromEnv() *Client { return NewClient(os.Getenv("ASANA_TOKEN")) } +func (c *Client) AddTaskToSection(task *Task, section *Section) error { + req := &addTaskRequest{ + Data: &addTaskDetails{ + Task: task.GID, + }, + } + + resp := &emptyResponse{} + + path := fmt.Sprintf("sections/%s/addTask", section.GID) + err := c.post(path, req, resp) + if err != nil { + return err + } + + return nil +} + func (c *Client) GetMe() (*User, error) { resp := &userResponse{} err := c.get("users/me", nil, resp) @@ -243,6 +274,41 @@ func (c *Client) get(path string, values *url.Values, out interface{}) error { return nil } +func (c *Client) post(path string, body interface{}, out interface{}) error { + url := fmt.Sprintf("%s%s", baseURL, path) + + enc, err := json.Marshal(body) + if err != nil { + return err + } + + req, err := http.NewRequest("POST", url, bytes.NewReader(enc)) + if err != nil { + return err + } + + resp, err := c.client.Do(req) + if err != nil { + return err + } + + if resp.StatusCode != 200 { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + return fmt.Errorf("%s: %s", resp.Status, string(body)) + } + + dec := json.NewDecoder(resp.Body) + err = dec.Decode(out) + if err != nil { + return err + } + + return nil +} + func (p *Project) String() string { return fmt.Sprintf("%s (%s)", p.GID, p.Name) } diff --git a/main.go b/main.go index 68f83fe..83067b6 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,10 @@ func main() { for _, sec := range secs { fmt.Printf("\t%s\n", sec) + if sec.Name != "Recently Assigned" { + continue + } + q := &asana.SearchQuery{ SectionsAny: []*asana.Section{sec}, Completed: asana.FALSE, @@ -49,6 +53,9 @@ func main() { for _, task := range tasks { fmt.Printf("\t\t%s\n", task) + a.AddTaskToSection(task, &asana.Section{ + GID: "1200372179004456", + }) } } }