Move tasks between sections

This commit is contained in:
Ian Gulliver
2021-09-11 03:53:50 +00:00
parent 1bae0bf42e
commit c9269718ec
2 changed files with 74 additions and 1 deletions

View File

@@ -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)
}

View File

@@ -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",
})
}
}
}