Move tasks between sections
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
package asana
|
package asana
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
import "io/ioutil"
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
import "io/ioutil"
|
||||||
import "net/http"
|
import "net/http"
|
||||||
import "net/url"
|
import "net/url"
|
||||||
import "os"
|
import "os"
|
||||||
@@ -56,6 +57,18 @@ type Workspace struct {
|
|||||||
Name string `json:"name"`
|
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 {
|
type projectResponse struct {
|
||||||
Data *Project `json:"data"`
|
Data *Project `json:"data"`
|
||||||
}
|
}
|
||||||
@@ -96,6 +109,24 @@ func NewClientFromEnv() *Client {
|
|||||||
return NewClient(os.Getenv("ASANA_TOKEN"))
|
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) {
|
func (c *Client) GetMe() (*User, error) {
|
||||||
resp := &userResponse{}
|
resp := &userResponse{}
|
||||||
err := c.get("users/me", nil, resp)
|
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
|
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 {
|
func (p *Project) String() string {
|
||||||
return fmt.Sprintf("%s (%s)", p.GID, p.Name)
|
return fmt.Sprintf("%s (%s)", p.GID, p.Name)
|
||||||
}
|
}
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -37,6 +37,10 @@ func main() {
|
|||||||
for _, sec := range secs {
|
for _, sec := range secs {
|
||||||
fmt.Printf("\t%s\n", sec)
|
fmt.Printf("\t%s\n", sec)
|
||||||
|
|
||||||
|
if sec.Name != "Recently Assigned" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
q := &asana.SearchQuery{
|
q := &asana.SearchQuery{
|
||||||
SectionsAny: []*asana.Section{sec},
|
SectionsAny: []*asana.Section{sec},
|
||||||
Completed: asana.FALSE,
|
Completed: asana.FALSE,
|
||||||
@@ -49,6 +53,9 @@ func main() {
|
|||||||
|
|
||||||
for _, task := range tasks {
|
for _, task := range tasks {
|
||||||
fmt.Printf("\t\t%s\n", task)
|
fmt.Printf("\t\t%s\n", task)
|
||||||
|
a.AddTaskToSection(task, &asana.Section{
|
||||||
|
GID: "1200372179004456",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user