-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbackend.go
566 lines (502 loc) · 12.5 KB
/
backend.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package main
import (
"bytes"
"crypto/sha256"
"database/sql"
"encoding/hex"
"fmt"
"github.com/garyburd/redigo/redis"
_ "github.com/go-sql-driver/mysql"
"github.com/vaughan0/go-ini"
"strconv"
"time"
)
var conn_str string
/* To read config file values. */
func load_config(filepath string) {
file, _ := ini.LoadFile(filepath)
db_user, _ := file.Get("bugspad", "user")
db_pass, _ := file.Get("bugspad", "password")
db_host, _ := file.Get("bugspad", "host")
db_name, _ := file.Get("bugspad", "database")
conn_str = fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?parseTime=true", db_user, db_pass, db_host, db_name)
}
/* Gets the HEX values of the string password*/
func get_hex(password string) string {
hash := sha256.New()
hash.Write([]byte(password))
md := hash.Sum(nil)
mdstr := hex.EncodeToString(md)
return mdstr
}
/* To find out if an user already exists or not. */
func find_user(email string) (bool, string) {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
fmt.Print(err)
return true, "Redis error."
}
defer conn.Close()
ret, err := conn.Do("HGET", "users", email)
if ret != nil {
return true, "User exists."
}
return false, "No such user."
}
/*
Adds a new user to the system. user_type can be 0,1,2. First it updates the
redis server and then saves the details to the MySQL db.
*/
func add_user(name string, email string, user_type string, password string) string {
answer, res := find_user(email)
if answer == true {
return res
}
mdstr := get_hex(password)
c := make(chan int)
go update_redis(email, mdstr, user_type, c)
fmt.Println(mdstr)
add_user_mysql(name, email, user_type, mdstr)
_ = <-c
return "User added."
}
/*
Adds a new user into the database.
*/
func add_user_mysql(name string, email string, user_type string, password string) {
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return
}
defer db.Close()
db.Exec("INSERT INTO users (name, email, type, password) VALUES (?, ?, ?, ?)", name, email, user_type, password)
}
/*
Inserts a new product into the database.
*/
func insert_product(name string, desc string) (id string, err error) {
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return "", err
}
defer db.Close()
ret, err := db.Exec("INSERT INTO products (name, description) VALUES (?, ?)", name, desc)
if err != nil {
return "", err
}
rid, err := ret.LastInsertId()
return strconv.FormatInt(rid, 10), err
}
/*
Inserts a new component in the database for a given product_id.
*/
func insert_component(name string, desc string, product_id int, owner int) (id string, err error) {
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return "", err
}
defer db.Close()
ret, err := db.Exec("INSERT INTO components (name, description, product_id, owner) VALUES (?, ?, ?, ?)", name, desc, product_id, owner)
if err != nil {
fmt.Println(err.Error())
return "No such product.", err
}
rid, err := ret.LastInsertId()
return strconv.FormatInt(rid, 10), err
}
/*
Authenticate a given username (email) and password against the redis
db. The details are stored in a hashmap "users" in the redis db.
*/
func authenticate_redis(email string, password string) bool {
mdstr := get_hex(password)
ret := redis_hget("users", email)
if string(ret) == mdstr {
return true
} else {
return false
}
}
/* Finds all components for a given product id*/
func get_components_by_id(product_id string) map[string][3]string {
m := make(map[string][3]string)
db, err := sql.Open("mysql", conn_str)
defer db.Close()
rows, err := db.Query("SELECT id, name, description from components where product_id=?", product_id)
if err != nil {
return m
}
defer rows.Close()
var name, description, c_id string
for rows.Next() {
err = rows.Scan(&c_id, &name, &description)
//fmt.Println(c_id, name, description)
m[name] = [3]string{c_id, name, description}
}
return m
}
/* Files a new bug based on input*/
func new_bug(data map[string]interface{}) (id string, err error) {
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return "", err
}
defer db.Close()
var status, summary string
var buffer, buffer2 bytes.Buffer
vals := make([]interface{}, 0)
buffer.WriteString("INSERT INTO bugs (")
val, ok := data["status"]
if ok {
buffer.WriteString("status")
buffer2.WriteString("?")
vals = append(vals, val)
status = val.(string)
} else {
buffer.WriteString("status")
buffer2.WriteString("'new'")
status = "new"
}
val, ok = data["version"]
if ok {
buffer.WriteString(", version")
buffer2.WriteString(",?")
vals = append(vals, val)
}
val, ok = data["severity"]
if ok {
buffer.WriteString(", severity")
buffer2.WriteString(",?")
vals = append(vals, val)
}
val, ok = data["hardware"]
if ok {
buffer.WriteString(", hardware")
buffer2.WriteString(",?")
vals = append(vals, val)
}
val, ok = data["priority"]
if ok {
buffer.WriteString(", priority")
buffer2.WriteString(",?")
vals = append(vals, val)
}
val, ok = data["whiteboard"]
if ok {
buffer.WriteString(", whiteboard")
buffer2.WriteString(",?")
vals = append(vals, val)
}
val, ok = data["subcomponent_id"]
if ok {
buffer.WriteString(", subcomponent_id")
buffer2.WriteString(",?")
vals = append(vals, val)
}
val, ok = data["reporter"]
if ok {
buffer.WriteString(", reporter")
buffer2.WriteString(",?")
vals = append(vals, val)
} else {
return "Missing input: reporter", nil
}
val, ok = data["summary"]
if ok {
buffer.WriteString(", summary")
buffer2.WriteString(",?")
vals = append(vals, val)
summary = val.(string)
} else {
return "Missing input: summary", nil
}
val, ok = data["description"]
if ok {
buffer.WriteString(", description")
buffer2.WriteString(",?")
vals = append(vals, val)
} else {
return "Missing input: description", nil
}
val, ok = data["component_id"]
if ok {
buffer.WriteString(", component_id")
buffer2.WriteString(",?")
vals = append(vals, val)
} else {
return "Missing input: component_id", nil
}
buffer.WriteString(", reported) VALUES (")
buffer2.WriteString(",NOW()")
buffer.WriteString(buffer2.String())
buffer.WriteString(")")
ret, err := db.Exec(buffer.String(), vals...)
if err != nil {
fmt.Println(err.Error())
return "Error in entering a new bug", err
}
rid, err := ret.LastInsertId()
bug_id := strconv.FormatInt(rid, 10)
// Now update redis cache for status
update_redis_bug_status(bug_id, status)
set_redis_bug(rid, status, summary)
add_latest_created(bug_id)
return bug_id, err
}
/*
Updates a given bug based on input
*/
func update_bug(data map[string]interface{}) {
var buffer bytes.Buffer
vals := make([]interface{}, 0)
buffer.WriteString("UPDATE bugs SET ")
bug_id := data["bug_id"].(float64)
val1, ok1 := data["status"]
if ok1 {
buffer.WriteString("status=?")
vals = append(vals, val1)
// TODO
// In case of status change we have to update the redis index for the bug
} else {
bug := get_redis_bug(strconv.FormatInt(int64(bug_id), 10))
buffer.WriteString("status='")
buffer.WriteString(bug["status"].(string))
buffer.WriteString("'")
}
val, ok := data["version"]
if ok {
buffer.WriteString(", version=?")
vals = append(vals, val)
}
val, ok = data["severity"]
if ok {
buffer.WriteString(", severity=?")
vals = append(vals, val)
}
val, ok = data["hardware"]
if ok {
buffer.WriteString(", hardware=?")
vals = append(vals, val)
}
val, ok = data["priority"]
if ok {
buffer.WriteString(", priority=?")
vals = append(vals, val)
}
val, ok = data["reporter"]
if ok {
buffer.WriteString(", reporter=?")
vals = append(vals, val)
}
val, ok = data["qa"]
if ok {
buffer.WriteString(", qa=?")
vals = append(vals, val)
}
val, ok = data["docs"]
if ok {
buffer.WriteString(", docs=?")
vals = append(vals, val)
}
val, ok = data["whiteboard"]
if ok {
buffer.WriteString(", whiteboard=?")
vals = append(vals, val)
}
val, ok = data["subcomponent_id"]
if ok {
buffer.WriteString(", subcomponent_id=?")
vals = append(vals, val)
}
val, ok = data["fixedinver"]
if ok {
buffer.WriteString(", fixedinver=?")
vals = append(vals, val)
}
val, ok = data["component_id"]
if ok {
buffer.WriteString(", component_id=?")
vals = append(vals, val)
}
buffer.WriteString(" WHERE id=?")
fmt.Println(buffer.String())
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Println(err)
return
}
defer db.Close()
vals = append(vals, data["bug_id"])
_, err = db.Exec(buffer.String(), vals...)
if err != nil {
fmt.Println(err)
return
}
if ok1 {
bug := get_redis_bug(strconv.FormatInt(int64(bug_id), 10))
old_status := bug["status"].(string)
delete_redis_bug_status(strconv.FormatInt(int64(bug_id), 10), old_status)
update_redis_bug_status(strconv.FormatInt(int64(bug_id), 10), val1.(string))
}
add_latest_updated(strconv.FormatInt(int64(bug_id), 10))
}
/*
Get a bug details.
*/
func get_bug(bug_id string) Bug {
fmt.Println(bug_id)
m := make(Bug)
db, err := sql.Open("mysql", conn_str)
defer db.Close()
row := db.QueryRow("SELECT status, description, version, severity, hardware, priority, whiteboard, reported, component_id, subcomponent_id, reporter from bugs where id=?", bug_id)
var status, description, version, severity, hardware, priority, whiteboard, subcomponent_id []byte
var reporter, component_id int
var reported time.Time
err = row.Scan(&status, &description, &version, &severity, &hardware, &priority, &whiteboard, &reported, &component_id, &subcomponent_id, &reporter)
if err == nil {
m["id"] = bug_id
m["status"] = string(status)
m["description"] = string(description)
m["version"] = string(version)
m["hardware"] = string(hardware)
m["priority"] = string(priority)
m["whiteboard"] = string(whiteboard)
m["reported"] = reported.String()
} else {
fmt.Println(err)
}
return m
}
/*
Adds new CC users to a bug
*/
func add_bug_cc(bug_id int64, emails interface{}) bool {
email_list := emails.([]interface{})
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return false
}
defer db.Close()
for i := range email_list {
email := email_list[i].(string)
user_id := get_user_id(email)
// If user_id is -1 means no such user
if user_id != -1 {
db.Exec("INSERT INTO cc (bug_id, who) VALUES (?,?)", bug_id, user_id)
}
}
return true
}
/*
Removes CC users from a bug
*/
func remove_bug_cc(bug_id int64, emails interface{}) bool {
email_list := emails.([]interface{})
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return false
}
defer db.Close()
for i := range email_list {
email := email_list[i].(string)
user_id := get_user_id(email)
// If user_id is -1 means no such user
if user_id != -1 {
_, err = db.Exec("DELETE FROM cc WHERE bug_id=? and who=?", bug_id, user_id)
if err != nil {
fmt.Println(err)
}
}
}
return true
}
/*
Adds a new comment to a given bug.
*/
func new_comment(reporter int, bug_id int, desc string) (id string, err error) {
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return "", err
}
defer db.Close()
ret, err := db.Exec("INSERT INTO comments (description, user, datec, bug) VALUES (?,?,NOW(),?)", desc, reporter, bug_id)
if err != nil {
fmt.Println(err.Error())
return "Error in entering a new comment", err
}
rid, err := ret.LastInsertId()
return strconv.FormatInt(rid, 10), err
}
func get_user_id(email string) int {
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return -1
}
defer db.Close()
rows, err := db.Query("SELECT id from users where email = ?", email)
if err == nil {
var id int
for rows.Next() {
err = rows.Scan(&id)
if id != 0 {
return id
}
}
}
return -1
}
func add_release(name string) {
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return
}
defer db.Close()
_, err = db.Exec("INSERT INTO releases (name) VALUES (?)", name)
if err != nil {
// handle error
fmt.Print(err)
return
}
}
func get_releases() []string {
m := make([]string, 0)
db, err := sql.Open("mysql", conn_str)
if err != nil {
// handle error
fmt.Print(err)
return m
}
defer db.Close()
rows, err := db.Query("SELECT name FROM releases")
if err != nil {
return m
}
defer rows.Close()
var name string
for rows.Next() {
err = rows.Scan(&name)
//fmt.Println(c_id, name, description)
m = append(m, name)
}
return m
}