Actually moving tasks

This commit is contained in:
Ian Gulliver
2021-09-12 21:49:54 +00:00
parent 305b1a0f98
commit a2c25af435
4 changed files with 234 additions and 124 deletions

View File

@@ -20,7 +20,9 @@ type WorkspaceClient struct {
type SearchQuery struct {
SectionsAny []*Section
Completed *bool
DueOn *string
DueOn *civil.Date
DueBefore *civil.Date
DueAfter *civil.Date
}
type Project struct {
@@ -135,6 +137,34 @@ func (wc *WorkspaceClient) GetSections(project *Project) ([]*Section, error) {
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) {
path := fmt.Sprintf("sections/%s/tasks", section.GID)
resp := &tasksResponse{}
@@ -157,6 +187,15 @@ func (wc *WorkspaceClient) GetUserTaskList(user *User) (*Project, error) {
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) {
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 {
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{}

View File

@@ -21,9 +21,9 @@ type periodic struct {
var periodics = []*periodic{}
func Every(d time.Duration) *periodic {
func EverySeconds(seconds int) *periodic {
ret := &periodic{
duration: d,
duration: time.Duration(seconds) * time.Second,
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 {
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
me, err := wc.GetMe()
utl, err := wc.GetMyUserTaskList()
if err != nil {
return err
}
utl, err := wc.GetUserTaskList(me)
secsByName, err := wc.GetSectionsByName(utl)
if err != nil {
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 {
sec, found := secsByName[name]
if !found {
@@ -83,26 +86,55 @@ func (p *periodic) InMyTasksSections(names ...string) *periodic {
func (p *periodic) DueInDays(days int) *periodic {
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 = d.AddDays(days)
dueOn := d.String()
q.DueOn = &dueOn
q.DueOn = &d
return nil
})
return p
}
func (p *periodic) InWorkspace(name string) *periodic {
p.workspaceClientGetter = func(c *asanaclient.Client) (*asanaclient.WorkspaceClient, error) {
return c.InWorkspace(name)
func (p *periodic) DueInAtLeastDays(days int) *periodic {
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
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
}
func (p *periodic) OnlyIncomplete() *periodic {
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
return nil
})
@@ -112,6 +144,10 @@ func (p *periodic) OnlyIncomplete() *periodic {
func (p *periodic) OnlyComplete() *periodic {
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
return nil
})
@@ -119,6 +155,25 @@ func (p *periodic) OnlyComplete() *periodic {
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 {
p.taskActors = append(p.taskActors, func(wc *asanaclient.WorkspaceClient, t *asanaclient.Task) error {
fmt.Printf("%s\n", t)
@@ -152,7 +207,7 @@ func (p *periodic) loop(client *asanaclient.Client) {
<-ticker.C
err := p.exec(client)
if err != nil {
fmt.Printf("%s\n", err)
fmt.Printf("ERROR: %s\n", err)
// continue
}
}

16
main.go
View File

@@ -1,16 +1,24 @@
package main
import "time"
import . "github.com/firestuff/asana-rules/asanarules"
func main() {
Every(5 * time.Second).
EverySeconds(30).
InWorkspace("flamingcow.io").
InMyTasksSections("Recently Assigned").
OnlyIncomplete().
DueInDays(0).
PrintTasks()
PrintTasks().
MoveToMyTasksSection("Today")
EverySeconds(30).
InWorkspace("flamingcow.io").
InMyTasksSections("Recently Assigned").
OnlyIncomplete().
DueInAtLeastDays(1).
DueInAtMostDays(7).
PrintTasks().
MoveToMyTasksSection("Upcoming")
Loop()
}