-
Notifications
You must be signed in to change notification settings - Fork 0
/
asana.go
127 lines (108 loc) · 2.56 KB
/
asana.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package asana
import (
"cloud.google.com/go/bigquery"
"cloud.google.com/go/civil"
"fmt"
"github.com/okdewit/go-utils/catch"
"github.com/okdewit/go-utils/httputils"
"time"
)
type Events struct { Events []Event }
type Event struct {
HasTimeStamps
User User
Action string
Parent Resource `json:"parent"`
Task Task `json:"resource"`
}
type Resource struct {
HasTimeStamps
Gid string
Name string
ResourceType string `json:"resource_type"`
ResourceSubtype string `json:"resource_subtype"`
CreatedBy User `json:"created_by"`
}
type Tasks []Task
type Task struct {
Resource
Assignee User
AssigneeStatus string `json:"assignee_status"`
Stories Stories
Completed bool
CompletedAt string `json:"completed_at"`
}
type Stories []Story
type Story struct {
Resource
Text string
}
type HasTimeStamps struct {
CreatedAt string `json:"created_at"`
}
type User struct {
Gid string
Name string
ResourceType string `json:"resource_type"`
}
func GetTasks(project string) []Task {
uri := Geturl("tasks", "")
httputils.AddParams(uri, map[string]string{
"project": project,
"opt_fields": "created_at",
"limit": "100",
})
var tasks []Task
for {
data := Call("GET", uri, nil)
response := TaskListResponse{}.Transform(data)
tasks = append(tasks, response.Data...)
if response.Next.Uri == "" { break }
uri = response.GetNextPageUrl()
}
return tasks
}
func (task *Task) Enrich() Task {
fmt.Printf("enriching task %v\n", task.Gid)
data := Call("GET", Geturl("tasks", task.Gid), nil)
return TaskResponse{}.Transform(data)
}
func (stories Stories) Filter(storyTypes map[string]bool) (filteredStories Stories) {
for _, story := range stories {
func(story Story) {
if storyTypes[story.ResourceSubtype] {
filteredStories = append(filteredStories, story)
}
}(story)
}
return filteredStories
}
func (timestampable *HasTimeStamps) GetCreatedAt() (t time.Time) {
t, err := time.Parse(time.RFC3339, timestampable.CreatedAt)
catch.Check(err)
return
}
func (task *Task) GetCompletedAt() (t time.Time) {
t, err := time.Parse(time.RFC3339, task.CompletedAt)
catch.Check(err)
return
}
func (task *Task) LastEvent(max time.Time) (lastEvent time.Time) {
lastEvent = time.Now()
if task.Completed {
lastEvent = task.GetCompletedAt()
}
if max.Before(lastEvent) {
lastEvent = max
}
return
}
func (resource *Resource) Load(vs []bigquery.Value, s bigquery.Schema) error{
for _, v := range vs {
switch value := v.(type) {
case string: resource.Gid = value
case civil.DateTime: resource.CreatedAt = value.In(time.UTC).Format(time.RFC3339)
}
}
return nil
}