Files
automana/asanarules/rules.go

249 lines
5.0 KiB
Go
Raw Normal View History

2021-09-11 19:26:51 +00:00
package asanarules
import "fmt"
import "time"
2021-09-12 20:08:13 +00:00
import "cloud.google.com/go/civil"
import "github.com/firestuff/asana-rules/asanaclient"
type queryMutator func(*asanaclient.WorkspaceClient, *asanaclient.SearchQuery) error
type taskActor func(*asanaclient.WorkspaceClient, *asanaclient.Task) error
type workspaceClientGetter func(*asanaclient.Client) (*asanaclient.WorkspaceClient, error)
2021-09-11 19:26:51 +00:00
type periodic struct {
duration time.Duration
done chan bool
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
workspaceClientGetter workspaceClientGetter
queryMutators []queryMutator
taskActors []taskActor
2021-09-11 19:26:51 +00:00
}
var periodics = []*periodic{}
2021-09-12 21:49:54 +00:00
func EverySeconds(seconds int) *periodic {
2021-09-11 19:26:51 +00:00
ret := &periodic{
2021-09-12 21:49:54 +00:00
duration: time.Duration(seconds) * time.Second,
2021-09-11 19:26:51 +00:00
done: make(chan bool),
}
periodics = append(periodics, ret)
return ret
}
func Loop() {
2021-09-12 20:08:13 +00:00
client := asanaclient.NewClientFromEnv()
2021-09-11 19:26:51 +00:00
for _, periodic := range periodics {
2021-09-12 20:08:13 +00:00
periodic.start(client)
2021-09-11 19:26:51 +00:00
}
for _, periodic := range periodics {
periodic.wait()
}
}
2021-09-12 21:49:54 +00:00
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
2021-09-12 20:08:13 +00:00
func (p *periodic) InMyTasksSections(names ...string) *periodic {
2021-09-12 21:49:54 +00:00
p.queryMutators = append(p.queryMutators, func(wc *asanaclient.WorkspaceClient, q *asanaclient.SearchQuery) error {
utl, err := wc.GetMyUserTaskList()
2021-09-12 20:08:13 +00:00
if err != nil {
2021-09-12 21:49:54 +00:00
return err
2021-09-12 20:08:13 +00:00
}
2021-09-12 21:49:54 +00:00
secsByName, err := wc.GetSectionsByName(utl)
2021-09-12 20:08:13 +00:00
if err != nil {
2021-09-12 21:49:54 +00:00
return err
2021-09-12 20:08:13 +00:00
}
2021-09-12 21:49:54 +00:00
for _, name := range names {
sec, found := secsByName[name]
if !found {
return fmt.Errorf("Section '%s' not found", name)
}
q.SectionsAny = append(q.SectionsAny, sec)
2021-09-12 20:08:13 +00:00
}
2021-09-12 21:49:54 +00:00
return nil
})
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
return p
}
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
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")
}
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
d := civil.DateOf(time.Now())
d = d.AddDays(days)
q.DueOn = &d
return nil
})
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
return p
2021-09-12 20:08:13 +00:00
}
2021-09-12 21:49:54 +00:00
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
})
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
return p
2021-09-12 20:08:13 +00:00
}
2021-09-12 21:49:54 +00:00
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
})
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
return p
2021-09-12 20:08:13 +00:00
}
func (p *periodic) OnlyIncomplete() *periodic {
2021-09-12 21:49:54 +00:00
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
})
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
return p
2021-09-11 19:26:51 +00:00
}
2021-09-12 20:08:13 +00:00
func (p *periodic) OnlyComplete() *periodic {
2021-09-12 21:49:54 +00:00
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
})
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)
})
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
return p
2021-09-12 20:08:13 +00:00
}
func (p *periodic) PrintTasks() *periodic {
2021-09-12 21:49:54 +00:00
p.taskActors = append(p.taskActors, func(wc *asanaclient.WorkspaceClient, t *asanaclient.Task) error {
fmt.Printf("%s\n", t)
return nil
})
2021-09-12 20:08:13 +00:00
2021-09-12 21:49:54 +00:00
return p
2021-09-12 20:08:13 +00:00
}
func (p *periodic) start(client *asanaclient.Client) {
2021-09-12 21:49:54 +00:00
err := p.validate()
if err != nil {
panic(err)
}
2021-09-12 20:08:13 +00:00
go p.loop(client)
}
func (p *periodic) validate() error {
2021-09-12 21:49:54 +00:00
return nil
2021-09-11 19:26:51 +00:00
}
func (p *periodic) wait() {
<-p.done
}
2021-09-12 20:08:13 +00:00
func (p *periodic) loop(client *asanaclient.Client) {
2021-09-11 19:26:51 +00:00
ticker := time.NewTicker(p.duration)
for {
<-ticker.C
2021-09-12 21:49:54 +00:00
err := p.exec(client)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
// continue
}
2021-09-11 19:26:51 +00:00
}
close(p.done)
}
2021-09-12 20:08:13 +00:00
func (p *periodic) exec(c *asanaclient.Client) error {
2021-09-12 21:49:54 +00:00
wc, err := p.workspaceClientGetter(c)
if err != nil {
return err
}
q := &asanaclient.SearchQuery{}
for _, mut := range p.queryMutators {
err = mut(wc, q)
if err != nil {
return err
}
}
tasks, err := wc.Search(q)
if err != nil {
return err
}
for _, task := range tasks {
for _, act := range p.taskActors {
err = act(wc, task)
if err != nil {
return err
}
}
}
return nil
2021-09-11 19:26:51 +00:00
}