-
Notifications
You must be signed in to change notification settings - Fork 10
/
pipe.go
264 lines (242 loc) · 5.55 KB
/
pipe.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/toggl/bugsnag"
"strings"
)
type Pipe struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
AccountID int `json:"account_id,omitempty"`
AccountName string `json:"account_name,omitempty"`
Automatic bool `json:"automatic,omitempty"`
AutomaticOption bool `json:"automatic_option"`
Configured bool `json:"configured"`
Premium bool `json:"premium"`
PipeStatus *PipeStatus `json:"pipe_status,omitempty"`
authorization *Authorization
workspaceID int
serviceID string
pipeID string
key string
payload []byte
}
const (
selectPipesSQL = `SELECT workspace_id, key, data
FROM pipes WHERE workspace_id = $1
`
selectAutomaticPipesSQL = `SELECT workspace_id, key, data
FROM pipes WHERE
data->>'automatic' = 'true'
`
singlePipesSQL = `SELECT workspace_id, key, data
FROM pipes WHERE workspace_id = $1
AND key = $2 LIMIT 1
`
deletePipeSQL = `DELETE FROM pipes
WHERE workspace_id = $1
AND key = $2
`
insertPipesSQL = `
WITH existing_pipe AS (
UPDATE pipes SET data = $3
WHERE workspace_id = $1 AND key = $2
RETURNING key
),
inserted_pipe AS (
INSERT INTO pipes(workspace_id, key, data)
SELECT $1, $2, $3
WHERE NOT EXISTS (SELECT 1 FROM existing_pipe)
RETURNING key
)
SELECT * FROM inserted_pipe
UNION
SELECT * FROM existing_pipe
`
)
func NewPipe(workspaceID int, serviceID, pipeID string) *Pipe {
return &Pipe{
ID: pipeID,
Name: strings.Title(pipeID),
key: pipesKey(serviceID, pipeID),
serviceID: serviceID,
workspaceID: workspaceID,
}
}
func pipesKey(serviceID, pipeID string) string {
return fmt.Sprintf("%s:%s", serviceID, pipeID)
}
func (p *Pipe) save() error {
p.Configured = true
b, err := json.Marshal(p)
if err != nil {
return err
}
_, err = db.Exec(insertPipesSQL, p.workspaceID, p.key, b)
if err != nil {
return err
}
return nil
}
func (p *Pipe) validate() string {
var message string
if p.AccountID == 0 {
message = "Pipe account_id must be present"
}
return message
}
func (p *Pipe) validatePayload(payload []byte) string {
if p.ID == "users" && len(payload) == 0 {
return "Missing request payload"
}
p.payload = payload
return ""
}
func (p *Pipe) load(rows *sql.Rows) error {
var wid int
var b []byte
var key string
if err := rows.Scan(&wid, &key, &b); err != nil {
return err
}
err := json.Unmarshal(b, p)
if err != nil {
return err
}
p.key = key
p.workspaceID = wid
p.serviceID = strings.Split(key, ":")[0]
return nil
}
func (p *Pipe) NewStatus() error {
p.PipeStatus = NewPipeStatus(p.workspaceID, p.serviceID, p.ID)
return p.PipeStatus.save()
}
func (p *Pipe) Service() Service {
service := getService(p.serviceID, p.workspaceID)
service.setAccount(p.AccountID)
loadAuth(service)
return service
}
func (p *Pipe) loadAuth() error {
service := getService(p.serviceID, p.workspaceID)
auth, err := loadAuth(service)
if err != nil {
return err
}
p.authorization = auth
return nil
}
func (p *Pipe) run() {
var err error
defer func() { p.endSync(true, err) }()
if err = p.NewStatus(); err != nil {
return
}
if err = p.loadAuth(); err != nil {
return
}
if err = p.fetchObjects(false); err != nil {
return
}
if err = p.postObjects(false); err != nil {
return
}
}
func (p *Pipe) fetchObjects(saveStatus bool) (err error) {
switch p.ID {
case "users":
err = fetchUsers(p)
case "projects":
err = fetchProjects(p)
case "todolists":
err = fetchTodoLists(p)
case "todos":
err = fetchTasks(p)
default:
panic(fmt.Sprintf("fetchObjects: Unrecognized pipeID - %s", p.ID))
}
return p.endSync(saveStatus, err)
}
func (p *Pipe) postObjects(saveStatus bool) (err error) {
switch p.ID {
case "users":
err = postUsers(p)
case "projects":
err = postProjects(p)
case "todolists":
err = postTodoLists(p)
case "todos":
err = postTasks(p)
default:
panic(fmt.Sprintf("postObjects: Unrecognized pipeID - %s", p.ID))
}
return p.endSync(saveStatus, err)
}
func (p *Pipe) endSync(saveStatus bool, err error) error {
if err != nil {
p.PipeStatus.addError(err)
}
if saveStatus {
if err := p.PipeStatus.save(); err != nil {
bugsnag.Notify(err)
return err
}
}
return err
}
func (p *Pipe) destroy(workspaceID int) error {
_, err := db.Exec(deletePipeSQL, workspaceID, p.key)
return err
}
func loadPipe(workspaceID int, serviceID, pipeID string) (*Pipe, error) {
key := pipesKey(serviceID, pipeID)
rows, err := db.Query(singlePipesSQL, workspaceID, key)
if err != nil {
return nil, err
}
defer rows.Close()
if !rows.Next() {
return nil, rows.Err()
}
var pipe Pipe
if err := pipe.load(rows); err != nil {
return nil, err
}
return &pipe, nil
}
func loadPipes(workspaceID int) (map[string]*Pipe, error) {
pipes := make(map[string]*Pipe)
rows, err := db.Query(selectPipesSQL, workspaceID)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var pipe Pipe
if err := pipe.load(rows); err != nil {
return nil, err
}
pipes[pipe.key] = &pipe
}
return pipes, nil
}
func loadAutomaticPipes() ([]*Pipe, error) {
var pipes []*Pipe
rows, err := db.Query(selectAutomaticPipesSQL)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var pipe Pipe
if err := pipe.load(rows); err != nil {
return nil, err
}
pipes = append(pipes, &pipe)
}
return pipes, nil
}