Actually moving tasks
This commit is contained in:
@@ -20,7 +20,9 @@ type WorkspaceClient struct {
|
|||||||
type SearchQuery struct {
|
type SearchQuery struct {
|
||||||
SectionsAny []*Section
|
SectionsAny []*Section
|
||||||
Completed *bool
|
Completed *bool
|
||||||
DueOn *string
|
DueOn *civil.Date
|
||||||
|
DueBefore *civil.Date
|
||||||
|
DueAfter *civil.Date
|
||||||
}
|
}
|
||||||
|
|
||||||
type Project struct {
|
type Project struct {
|
||||||
@@ -135,6 +137,34 @@ func (wc *WorkspaceClient) GetSections(project *Project) ([]*Section, error) {
|
|||||||
return resp.Data, nil
|
return resp.Data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (wc *WorkspaceClient) GetSectionsByName(project *Project) (map[string]*Section, error) {
|
||||||
|
secs, err := wc.GetSections(project)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
secsByName := map[string]*Section{}
|
||||||
|
for _, sec := range secs {
|
||||||
|
secsByName[sec.Name] = sec
|
||||||
|
}
|
||||||
|
|
||||||
|
return secsByName, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc *WorkspaceClient) GetSectionByName(project *Project, name string) (*Section, error) {
|
||||||
|
secsByName, err := wc.GetSectionsByName(project)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sec, found := secsByName[name]
|
||||||
|
if !found {
|
||||||
|
return nil, fmt.Errorf("Section '%s' not found", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sec, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (wc *WorkspaceClient) GetTasksFromSection(section *Section) ([]*Task, error) {
|
func (wc *WorkspaceClient) GetTasksFromSection(section *Section) ([]*Task, error) {
|
||||||
path := fmt.Sprintf("sections/%s/tasks", section.GID)
|
path := fmt.Sprintf("sections/%s/tasks", section.GID)
|
||||||
resp := &tasksResponse{}
|
resp := &tasksResponse{}
|
||||||
@@ -157,6 +187,15 @@ func (wc *WorkspaceClient) GetUserTaskList(user *User) (*Project, error) {
|
|||||||
return resp.Data, nil
|
return resp.Data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (wc *WorkspaceClient) GetMyUserTaskList() (*Project, error) {
|
||||||
|
me, err := wc.GetMe()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return wc.GetUserTaskList(me)
|
||||||
|
}
|
||||||
|
|
||||||
func (wc *WorkspaceClient) Search(q *SearchQuery) ([]*Task, error) {
|
func (wc *WorkspaceClient) Search(q *SearchQuery) ([]*Task, error) {
|
||||||
path := fmt.Sprintf("workspaces/%s/tasks/search", wc.workspace.GID)
|
path := fmt.Sprintf("workspaces/%s/tasks/search", wc.workspace.GID)
|
||||||
|
|
||||||
@@ -177,7 +216,15 @@ func (wc *WorkspaceClient) Search(q *SearchQuery) ([]*Task, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if q.DueOn != nil {
|
if q.DueOn != nil {
|
||||||
values.Add("due_on", *q.DueOn)
|
values.Add("due_on", q.DueOn.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if q.DueBefore != nil {
|
||||||
|
values.Add("due_on.before", q.DueBefore.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if q.DueAfter != nil {
|
||||||
|
values.Add("due_on.after", q.DueAfter.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := &tasksResponse{}
|
resp := &tasksResponse{}
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ type periodic struct {
|
|||||||
|
|
||||||
var periodics = []*periodic{}
|
var periodics = []*periodic{}
|
||||||
|
|
||||||
func Every(d time.Duration) *periodic {
|
func EverySeconds(seconds int) *periodic {
|
||||||
ret := &periodic{
|
ret := &periodic{
|
||||||
duration: d,
|
duration: time.Duration(seconds) * time.Second,
|
||||||
done: make(chan bool),
|
done: make(chan bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,28 +44,31 @@ func Loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *periodic) InWorkspace(name string) *periodic {
|
||||||
|
if p.workspaceClientGetter != nil {
|
||||||
|
panic("Multiple calls to InWorkspace()")
|
||||||
|
}
|
||||||
|
|
||||||
|
p.workspaceClientGetter = func(c *asanaclient.Client) (*asanaclient.WorkspaceClient, error) {
|
||||||
|
return c.InWorkspace(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query mutators
|
||||||
func (p *periodic) InMyTasksSections(names ...string) *periodic {
|
func (p *periodic) InMyTasksSections(names ...string) *periodic {
|
||||||
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
||||||
me, err := wc.GetMe()
|
utl, err := wc.GetMyUserTaskList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
utl, err := wc.GetUserTaskList(me)
|
secsByName, err := wc.GetSectionsByName(utl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
secs, err := wc.GetSections(utl)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
secsByName := map[string]*asanaclient.Section{}
|
|
||||||
for _, sec := range secs {
|
|
||||||
secsByName[sec.Name] = sec
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
sec, found := secsByName[name]
|
sec, found := secsByName[name]
|
||||||
if !found {
|
if !found {
|
||||||
@@ -83,26 +86,55 @@ func (p *periodic) InMyTasksSections(names ...string) *periodic {
|
|||||||
|
|
||||||
func (p *periodic) DueInDays(days int) *periodic {
|
func (p *periodic) DueInDays(days int) *periodic {
|
||||||
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
||||||
|
if q.DueOn != nil {
|
||||||
|
return fmt.Errorf("Multiple clauses set DueOn")
|
||||||
|
}
|
||||||
|
|
||||||
d := civil.DateOf(time.Now())
|
d := civil.DateOf(time.Now())
|
||||||
d = d.AddDays(days)
|
d = d.AddDays(days)
|
||||||
dueOn := d.String()
|
q.DueOn = &d
|
||||||
q.DueOn = &dueOn
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *periodic) InWorkspace(name string) *periodic {
|
func (p *periodic) DueInAtLeastDays(days int) *periodic {
|
||||||
p.workspaceClientGetter = func(c *asanaclient.Client) (*asanaclient.WorkspaceClient, error) {
|
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
||||||
return c.InWorkspace(name)
|
if q.DueAfter != nil {
|
||||||
|
return fmt.Errorf("Multiple clauses set DueAfter")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
d := civil.DateOf(time.Now())
|
||||||
|
d = d.AddDays(days)
|
||||||
|
q.DueAfter = &d
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *periodic) DueInAtMostDays(days int) *periodic {
|
||||||
|
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
||||||
|
if q.DueBefore != nil {
|
||||||
|
return fmt.Errorf("Multiple clauses set DueBefore")
|
||||||
|
}
|
||||||
|
|
||||||
|
d := civil.DateOf(time.Now())
|
||||||
|
d = d.AddDays(days)
|
||||||
|
q.DueBefore = &d
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *periodic) OnlyIncomplete() *periodic {
|
func (p *periodic) OnlyIncomplete() *periodic {
|
||||||
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
||||||
|
if q.Completed != nil {
|
||||||
|
return fmt.Errorf("Multiple clauses set Completed")
|
||||||
|
}
|
||||||
|
|
||||||
q.Completed = asanaclient.FALSE
|
q.Completed = asanaclient.FALSE
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@@ -112,6 +144,10 @@ func (p *periodic) OnlyIncomplete() *periodic {
|
|||||||
|
|
||||||
func (p *periodic) OnlyComplete() *periodic {
|
func (p *periodic) OnlyComplete() *periodic {
|
||||||
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
|
||||||
|
if q.Completed != nil {
|
||||||
|
return fmt.Errorf("Multiple clauses set Completed")
|
||||||
|
}
|
||||||
|
|
||||||
q.Completed = asanaclient.TRUE
|
q.Completed = asanaclient.TRUE
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@@ -119,6 +155,25 @@ func (p *periodic) OnlyComplete() *periodic {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Task actors
|
||||||
|
func (p *periodic) MoveToMyTasksSection(name string) *periodic {
|
||||||
|
p.taskActors = append(p.taskActors, func(wc *asanaclient.WorkspaceClient, t *asanaclient.Task) error {
|
||||||
|
utl, err := wc.GetMyUserTaskList()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sec, err := wc.GetSectionByName(utl, name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return wc.AddTaskToSection(t, sec)
|
||||||
|
})
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
func (p *periodic) PrintTasks() *periodic {
|
func (p *periodic) PrintTasks() *periodic {
|
||||||
p.taskActors = append(p.taskActors, func(wc *asanaclient.WorkspaceClient, t *asanaclient.Task) error {
|
p.taskActors = append(p.taskActors, func(wc *asanaclient.WorkspaceClient, t *asanaclient.Task) error {
|
||||||
fmt.Printf("%s\n", t)
|
fmt.Printf("%s\n", t)
|
||||||
@@ -152,7 +207,7 @@ func (p *periodic) loop(client *asanaclient.Client) {
|
|||||||
<-ticker.C
|
<-ticker.C
|
||||||
err := p.exec(client)
|
err := p.exec(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s\n", err)
|
fmt.Printf("ERROR: %s\n", err)
|
||||||
// continue
|
// continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
main.go
16
main.go
@@ -1,16 +1,24 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
import . "github.com/firestuff/asana-rules/asanarules"
|
import . "github.com/firestuff/asana-rules/asanarules"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
Every(5 * time.Second).
|
EverySeconds(30).
|
||||||
InWorkspace("flamingcow.io").
|
InWorkspace("flamingcow.io").
|
||||||
InMyTasksSections("Recently Assigned").
|
InMyTasksSections("Recently Assigned").
|
||||||
OnlyIncomplete().
|
OnlyIncomplete().
|
||||||
DueInDays(0).
|
DueInDays(0).
|
||||||
PrintTasks()
|
PrintTasks().
|
||||||
|
MoveToMyTasksSection("Today")
|
||||||
|
|
||||||
|
EverySeconds(30).
|
||||||
|
InWorkspace("flamingcow.io").
|
||||||
|
InMyTasksSections("Recently Assigned").
|
||||||
|
OnlyIncomplete().
|
||||||
|
DueInAtLeastDays(1).
|
||||||
|
DueInAtMostDays(7).
|
||||||
|
PrintTasks().
|
||||||
|
MoveToMyTasksSection("Upcoming")
|
||||||
|
|
||||||
Loop()
|
Loop()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user