-
Notifications
You must be signed in to change notification settings - Fork 38
/
pages.go
3839 lines (3450 loc) 路 114 KB
/
pages.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
com "github.com/sqlitebrowser/dbhub.io/common"
gfm "github.com/sqlitebrowser/github_flavored_markdown"
)
// Renders the "About Us" page.
func aboutPage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
Auth0 com.Auth0Set
Meta com.MetaInfo
}
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
if com.Conf.Environment.Environment != "docker" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
u = sess.Values["UserName"]
} else {
u = com.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
pageData.Meta.LoggedInUser = loggedInUser
}
// Retrieve the details and status updates count for the logged in user
if loggedInUser != "" {
ur, err := com.User(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if ur.AvatarURL != "" {
pageData.Meta.AvatarURL = ur.AvatarURL + "&s=48"
}
pageData.Meta.NumStatusUpdates, err = com.UserStatusUpdates(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
pageData.Meta.Title = "What is DBHub.io?"
// Add Auth0 info to the page data
pageData.Auth0.CallbackURL = "https://" + com.Conf.Web.ServerName + "/x/callback"
pageData.Auth0.ClientID = com.Conf.Auth0.ClientID
pageData.Auth0.Domain = com.Conf.Auth0.Domain
// Render the page
t := tmpl.Lookup("aboutPage")
err := t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// Render the branches page, which lists the branches for a database.
func branchesPage(w http.ResponseWriter, r *http.Request) {
// Structure to hold page data
type brEntry struct {
Commit string `json:"commit"`
Description string `json:"description"`
MarkDownDesc string `json:"mkdowndesc"`
Name string `json:"name"`
}
var pageData struct {
Auth0 com.Auth0Set
Branches []brEntry
DB com.SQLiteDBinfo
DefaultBranch string
Meta com.MetaInfo
}
pageData.Meta.Title = "Branch list"
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
if com.Conf.Environment.Environment != "docker" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
u = sess.Values["UserName"]
} else {
u = com.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
pageData.Meta.LoggedInUser = loggedInUser
}
// Retrieve the database owner & name
// TODO: Add folder and branch name support
dbFolder := "/"
dbOwner, dbName, err := com.GetOD(1, r) // 1 = Ignore "/branches/" at the start of the URL
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Validate the supplied information
if dbOwner == "" || dbName == "" {
errorPage(w, r, http.StatusBadRequest, "Missing database owner or database name")
return
}
// Check if the requested database exists
exists, err := com.CheckDBExists(loggedInUser, dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !exists {
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Database '%s%s%s' doesn't exist", dbOwner, dbFolder,
dbName))
return
}
// Check if the user has access to the requested database (and get it's details if available)
err = com.DBDetails(&pageData.DB, loggedInUser, dbOwner, dbFolder, dbName, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Read the branch heads list from the database
branches, err := com.GetBranches(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
pageData.DefaultBranch, err = com.GetDefaultBranchName(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Retrieve correctly capitalised username for the user
usr, err := com.User(dbOwner)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
pageData.Meta.Owner = usr.Username
pageData.Meta.Database = dbName
for i, j := range branches {
// Create a branch entry
var r string
if j.Description == "" {
r = "No description"
} else {
r = string(gfm.Markdown([]byte(j.Description)))
}
k := brEntry{
Commit: j.Commit,
Description: j.Description,
MarkDownDesc: r,
Name: i,
}
pageData.Branches = append(pageData.Branches, k)
}
// Retrieve the details and status updates count for the logged in user
if loggedInUser != "" {
ur, err := com.User(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if ur.AvatarURL != "" {
pageData.Meta.AvatarURL = ur.AvatarURL + "&s=48"
}
pageData.Meta.NumStatusUpdates, err = com.UserStatusUpdates(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// Add Auth0 info to the page data
pageData.Auth0.CallbackURL = "https://" + com.Conf.Web.ServerName + "/x/callback"
pageData.Auth0.ClientID = com.Conf.Auth0.ClientID
pageData.Auth0.Domain = com.Conf.Auth0.Domain
// Render the page
t := tmpl.Lookup("branchesPage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// Render the commits page. This shows all of the commits in a given branch, in reverse order from newest to oldest.
func commitsPage(w http.ResponseWriter, r *http.Request) {
// Structure to hold page data
type HistEntry struct {
AuthorEmail string `json:"author_email"`
AuthorName string `json:"author_name"`
AuthorUserName string `json:"author_user_name"`
AvatarURL string `json:"avatar_url"`
CommitterEmail string `json:"committer_email"`
CommitterName string `json:"committer_name"`
ID string `json:"id"`
Message string `json:"message"`
Parent string `json:"parent"`
Timestamp time.Time `json:"timestamp"`
Tree com.DBTree `json:"tree"`
}
var pageData struct {
Auth0 com.Auth0Set
Branch string
Branches []string
DB com.SQLiteDBinfo
History []HistEntry
Meta com.MetaInfo
}
pageData.Meta.Title = "Commits settings"
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
if com.Conf.Environment.Environment != "docker" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
u = sess.Values["UserName"]
} else {
u = com.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
pageData.Meta.LoggedInUser = loggedInUser
}
// Retrieve the database owner & name, and branch name
// TODO: Add folder support
dbFolder := "/"
dbOwner, dbName, err := com.GetOD(1, r) // 1 = Ignore "/commits/" at the start of the URL
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
branchName, err := com.GetFormBranch(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Validate the supplied information
if dbOwner == "" || dbName == "" {
errorPage(w, r, http.StatusBadRequest, "Missing database owner or database name")
return
}
// Check if the requested database exists
exists, err := com.CheckDBExists(loggedInUser, dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !exists {
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Database '%s%s%s' doesn't exist", dbOwner, dbFolder,
dbName))
return
}
// Check if the user has access to the requested database (and get it's details if available)
err = com.DBDetails(&pageData.DB, loggedInUser, dbOwner, dbFolder, dbName, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Read the branch heads list from the database
branches, err := com.GetBranches(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// If no branch name was given, we use the default branch
if branchName == "" {
branchName, err = com.GetDefaultBranchName(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// Work out the head commit ID for the requested branch
headCom, ok := branches[branchName]
if !ok {
// Unknown branch
errorPage(w, r, http.StatusInternalServerError, fmt.Sprintf("Branch '%s' not found", branchName))
return
}
headID := headCom.Commit
if headID == "" {
// The requested branch wasn't found. Bad request?
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Walk the commit history backwards from the head commit, assembling the commit history for this branch from the
// full list
rawList, err := com.GetCommitList(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// TODO: Ugh, this is an ugly approach just to add the username to the commit data. Surely there's a better way?
// TODO Maybe store the username in the commit data structure in the database instead?
// TODO: Display licence changes too
uName, avatarURL, err := com.GetUsernameFromEmail(rawList[headID].AuthorEmail)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if avatarURL != "" {
avatarURL += "&s=30"
}
// Create the history entry
pageData.History = []HistEntry{
{
AuthorEmail: rawList[headID].AuthorEmail,
AuthorName: rawList[headID].AuthorName,
AuthorUserName: uName,
AvatarURL: avatarURL,
CommitterEmail: rawList[headID].CommitterEmail,
CommitterName: rawList[headID].CommitterName,
ID: rawList[headID].ID,
Message: string(gfm.Markdown([]byte(rawList[headID].Message))),
Parent: rawList[headID].Parent,
Timestamp: rawList[headID].Timestamp,
},
}
commitData := com.CommitEntry{Parent: rawList[headID].Parent}
for commitData.Parent != "" {
commitData, ok = rawList[commitData.Parent]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Internal error when retrieving commit data")
return
}
uName, avatarURL, err = com.GetUsernameFromEmail(commitData.AuthorEmail)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if avatarURL != "" {
avatarURL += "&s=30"
}
// Create a history entry
newEntry := HistEntry{
AuthorEmail: commitData.AuthorEmail,
AuthorName: commitData.AuthorName,
AuthorUserName: uName,
AvatarURL: avatarURL,
CommitterEmail: commitData.CommitterEmail,
CommitterName: commitData.CommitterName,
ID: commitData.ID,
Message: string(gfm.Markdown([]byte(commitData.Message))),
Parent: commitData.Parent,
Timestamp: commitData.Timestamp,
}
pageData.History = append(pageData.History, newEntry)
}
// Retrieve correctly capitalised username for the user
usr, err := com.User(dbOwner)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
pageData.Meta.Owner = usr.Username
// Retrieve the details and status updates count for the logged in user
if loggedInUser != "" {
ur, err := com.User(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if ur.AvatarURL != "" {
pageData.Meta.AvatarURL = ur.AvatarURL + "&s=48"
}
pageData.Meta.NumStatusUpdates, err = com.UserStatusUpdates(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// Fill out the metadata
pageData.Meta.Database = dbName
pageData.Branch = branchName
for i := range branches {
pageData.Branches = append(pageData.Branches, i)
}
// Add Auth0 info to the page data
pageData.Auth0.CallbackURL = "https://" + com.Conf.Web.ServerName + "/x/callback"
pageData.Auth0.ClientID = com.Conf.Auth0.ClientID
pageData.Auth0.Domain = com.Conf.Auth0.Domain
// Render the page
t := tmpl.Lookup("commitsPage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// Render the compare page, for creating new merge requests
func comparePage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
Auth0 com.Auth0Set
CommitList []com.CommitData
DB com.SQLiteDBinfo
DestDBBranches []string
DestDBDefaultBranch string
DestDBName string
DestFolder string
DestOwner string
Forks []com.ForkEntry
Meta com.MetaInfo
MyStar bool
SourceDBBranches []string
SourceDBDefaultBranch string
SourceDBName string
SourceFolder string
SourceOwner string
}
pageData.Meta.Title = "Create a Merge Request"
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
validSession := false
if com.Conf.Environment.Environment != "docker" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
u = sess.Values["UserName"]
} else {
u = com.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
pageData.Meta.LoggedInUser = loggedInUser
validSession = true
}
// Ensure we have a valid logged in user
if validSession != true {
errorPage(w, r, http.StatusUnauthorized, "You need to be logged in")
return
}
// Retrieve the database owner & name, and branch name
// TODO: Add folder support
dbFolder := "/"
dbOwner, dbName, err := com.GetOD(1, r) // 1 = Ignore "/compare/" at the start of the URL
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Retrieve list of forks for the database
pageData.Forks, err = com.ForkTree(loggedInUser, dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError,
fmt.Sprintf("Error retrieving fork list for '%s%s%s': %v\n", dbOwner, dbFolder,
dbName, err.Error()))
return
}
// Use the database which the "New Merge Request" button was pressed on as the initially selected source
pageData.SourceOwner = dbOwner
pageData.SourceFolder = dbFolder
pageData.SourceDBName = dbName
// If the source database has an (accessible) parent, use that as the default destination selected for the user.
// If it doesn't, then set the source as the destination as well and the user will have to manually choose
pageData.DestOwner, pageData.DestFolder, pageData.DestDBName, err = com.ForkParent(loggedInUser, dbOwner, dbFolder,
dbName)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
if pageData.DestOwner == "" || pageData.DestFolder == "" || pageData.DestDBName == "" {
pageData.DestOwner = dbOwner
pageData.DestFolder = dbFolder
pageData.DestDBName = dbName
}
// * Determine the source and destination database branches *
// Retrieve the branch info for the source database
srcBranchList, err := com.GetBranches(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
for name := range srcBranchList {
pageData.SourceDBBranches = append(pageData.SourceDBBranches, name)
}
pageData.SourceDBDefaultBranch, err = com.GetDefaultBranchName(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Retrieve the branch info for the destination database
destBranchList, err := com.GetBranches(pageData.DestOwner, pageData.DestFolder, pageData.DestDBName)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
for name := range destBranchList {
pageData.DestDBBranches = append(pageData.DestDBBranches, name)
}
pageData.DestDBDefaultBranch, err = com.GetDefaultBranchName(pageData.DestOwner, pageData.DestFolder,
pageData.DestDBName)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Check if the user has access to the requested database (and get it's details if available)
err = com.DBDetails(&pageData.DB, loggedInUser, dbOwner, dbFolder, dbName, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Get latest star and fork count
_, pageData.DB.Info.Stars, pageData.DB.Info.Forks, err = com.SocialStats(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Check if the database was starred by the logged in user
pageData.MyStar, err = com.CheckDBStarred(loggedInUser, dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Couldn't retrieve latest social stats")
return
}
// Retrieve correctly capitalised username for the database owner
usr, err := com.User(dbOwner)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
pageData.Meta.Owner = usr.Username
// Retrieve the details and status updates count for the logged in user
if loggedInUser != "" {
ur, err := com.User(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if ur.AvatarURL != "" {
pageData.Meta.AvatarURL = ur.AvatarURL + "&s=48"
}
pageData.Meta.NumStatusUpdates, err = com.UserStatusUpdates(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// If the initially chosen source and destinations can be directly applied, fill out the initial commit list entries
// for display to the user
ancestorID, cList, errType, err := com.GetCommonAncestorCommits(dbOwner, dbFolder, dbName,
pageData.SourceDBDefaultBranch, pageData.DestOwner, pageData.DestFolder, pageData.DestDBName,
pageData.DestDBDefaultBranch)
if err != nil && errType != http.StatusBadRequest {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if ancestorID != "" {
// Retrieve the commit ID for the destination branch
destBranch, ok := destBranchList[pageData.DestDBDefaultBranch]
if !ok {
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
destCommitID := destBranch.Commit
// Retrieve the current licence for the destination branch
commitList, err := com.GetCommitList(pageData.DestOwner, pageData.DestFolder, pageData.DestDBName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
destCommit, ok := commitList[destCommitID]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Destination commit ID not found in commit list.")
return
}
destLicenceSHA := destCommit.Tree.Entries[0].LicenceSHA
// Convert the commit entries into something we can display in a commit list
for _, j := range cList {
var c com.CommitData
c.AuthorEmail = j.AuthorEmail
c.AuthorName = j.AuthorName
c.ID = j.ID
c.Message = j.Message
c.Timestamp = j.Timestamp
c.AuthorUsername, c.AuthorAvatar, err = com.GetUsernameFromEmail(j.AuthorEmail)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if c.AuthorAvatar != "" {
c.AuthorAvatar += "&s=18"
}
// Check for licence changes
commitLicSHA := j.Tree.Entries[0].LicenceSHA
if commitLicSHA != destLicenceSHA {
lName, _, err := com.GetLicenceInfoFromSha256(dbOwner, commitLicSHA)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
c.LicenceChange = fmt.Sprintf("This commit includes a licence change to '%s'", lName)
}
pageData.CommitList = append(pageData.CommitList, c)
}
}
// Retrieve the "forked from" information
frkOwn, frkFol, frkDB, frkDel, err := com.ForkedFrom(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Database query failure")
return
}
pageData.Meta.ForkOwner = frkOwn
pageData.Meta.ForkFolder = frkFol
pageData.Meta.ForkDatabase = frkDB
pageData.Meta.ForkDeleted = frkDel
// Fill out the metadata
pageData.Meta.Database = dbName
// Add Auth0 info to the page data
pageData.Auth0.CallbackURL = "https://" + com.Conf.Web.ServerName + "/x/callback"
pageData.Auth0.ClientID = com.Conf.Auth0.ClientID
pageData.Auth0.Domain = com.Conf.Auth0.Domain
// Render the page
t := tmpl.Lookup("comparePage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// Displays a web page asking the user to confirm deleting their database.
func confirmDeletePage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
Auth0 com.Auth0Set
Meta com.MetaInfo
}
pageData.Meta.Title = "Confirm database deletion"
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
validSession := false
if com.Conf.Environment.Environment != "docker" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
u = sess.Values["UserName"]
} else {
u = com.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
pageData.Meta.LoggedInUser = loggedInUser
validSession = true
}
// Ensure we have a valid logged in user
if validSession != true {
errorPage(w, r, http.StatusUnauthorized, "You need to be logged in")
return
}
// Retrieve the owner and database name
dbOwner, dbName, err := com.GetOD(1, r) // "1" means skip the first URL word
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for owner or database value")
return
}
// TODO: Add folder support
dbFolder := "/"
// Check if the requested database exists
exists, err := com.CheckDBExists(loggedInUser, dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !exists {
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Database '%s%s%s' doesn't exist", dbOwner, dbFolder,
dbName))
return
}
// Make sure the database owner matches the logged in user
if strings.ToLower(loggedInUser) != strings.ToLower(dbOwner) {
errorPage(w, r, http.StatusUnauthorized, "You can't change databases you don't own")
return
}
// Retrieve correctly capitalised username for the database owner
usr, err := com.User(dbOwner)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
pageData.Meta.Owner = usr.Username
// Retrieve the details and status updates count for the logged in user
ur, err := com.User(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if ur.AvatarURL != "" {
pageData.Meta.AvatarURL = ur.AvatarURL + "&s=48"
}
pageData.Meta.NumStatusUpdates, err = com.UserStatusUpdates(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Fill out metadata for the page to be rendered
pageData.Meta.Database = dbName
// Add Auth0 info to the page data
pageData.Auth0.CallbackURL = "https://" + com.Conf.Web.ServerName + "/x/callback"
pageData.Auth0.ClientID = com.Conf.Auth0.ClientID
pageData.Auth0.Domain = com.Conf.Auth0.Domain
// Render the page
t := tmpl.Lookup("confirmDeletePage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// Render the contributors page, which lists the contributors to a database.
func contributorsPage(w http.ResponseWriter, r *http.Request) {
// Structures to hold page data
type AuthorEntry struct {
AuthorEmail string `json:"author_email"`
AuthorName string `json:"author_name"`
AuthorUserName string `json:"author_user_name"`
AvatarURL string `json:"avatar_url"`
NumCommits int `json:"num_commits"`
}
var pageData struct {
Auth0 com.Auth0Set
Contributors map[string]AuthorEntry
DB com.SQLiteDBinfo
Meta com.MetaInfo
}
pageData.Meta.Title = "Branch list"
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
if com.Conf.Environment.Environment != "docker" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
u = sess.Values["UserName"]
} else {
u = com.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
pageData.Meta.LoggedInUser = loggedInUser
}
// Retrieve the database owner & name
// TODO: Add folder and branch support
dbFolder := "/"
dbOwner, dbName, err := com.GetOD(1, r) // 1 = Ignore "/branches/" at the start of the URL
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Validate the supplied information
if dbOwner == "" || dbName == "" {
errorPage(w, r, http.StatusBadRequest, "Missing database owner or database name")
return
}
// Check if the requested database exists
exists, err := com.CheckDBExists(loggedInUser, dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !exists {
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Database '%s%s%s' doesn't exist", dbOwner, dbFolder,
dbName))
return
}
// Check if the user has access to the requested database (and get it's details if available)
err = com.DBDetails(&pageData.DB, loggedInUser, dbOwner, dbFolder, dbName, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Read the commit list from the database
commitList, err := com.GetCommitList(dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Retrieve correctly capitalised username for the user
usr, err := com.User(dbOwner)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
pageData.Meta.Owner = usr.Username
// Fill out the metadata
pageData.Meta.Database = dbName
pageData.Contributors = make(map[string]AuthorEntry)
for _, j := range commitList {
// Look up the author's username
// TODO: There are likely a bunch of ways to optimise this, from keeping the user name entries in a map to
// TODO directly storing the username in the jsonb commit data. Storing the user name entry in the jsonb is
// TODO probably the way to go, as it would save lookups in a lot of places
u, avatarURL, err := com.GetUsernameFromEmail(j.AuthorEmail)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if avatarURL != "" {
avatarURL += "&s=30"
}
// This ok check is just a way to decide whether to increment the NumCommits counter
if _, ok := pageData.Contributors[j.AuthorName]; !ok {
// This is the first time in the loop we're adding the author to the Contributors list
pageData.Contributors[j.AuthorName] = AuthorEntry{
AuthorEmail: j.AuthorEmail,
AuthorName: j.AuthorName,
AuthorUserName: u,
AvatarURL: avatarURL,
NumCommits: 1,
}
} else {
// The author is already in the contributors list, so we increment their NumCommits counter
n := pageData.Contributors[j.AuthorName].NumCommits + 1
pageData.Contributors[j.AuthorName] = AuthorEntry{
AuthorEmail: j.AuthorEmail,
AuthorName: j.AuthorName,
AuthorUserName: u,
AvatarURL: avatarURL,
NumCommits: n,
}
}
}
// Retrieve the details and status updates count for the logged in user
if loggedInUser != "" {
ur, err := com.User(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if ur.AvatarURL != "" {
pageData.Meta.AvatarURL = ur.AvatarURL + "&s=48"
}
pageData.Meta.NumStatusUpdates, err = com.UserStatusUpdates(loggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// Add Auth0 info to the page data
pageData.Auth0.CallbackURL = "https://" + com.Conf.Web.ServerName + "/x/callback"
pageData.Auth0.ClientID = com.Conf.Auth0.ClientID
pageData.Auth0.Domain = com.Conf.Auth0.Domain
// Render the page
t := tmpl.Lookup("contributorsPage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// Displays a web page asking for the new branch name.
func createBranchPage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
Auth0 com.Auth0Set
Meta com.MetaInfo
Commit string
}
pageData.Meta.Title = "Create new branch"
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
validSession := false
if com.Conf.Environment.Environment != "docker" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
u = sess.Values["UserName"]
} else {
u = com.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
pageData.Meta.LoggedInUser = loggedInUser
validSession = true
}
// Ensure we have a valid logged in user
if validSession != true {
errorPage(w, r, http.StatusUnauthorized, "You need to be logged in")
return
}
// Retrieve the owner, database, and commit ID
dbOwner, dbName, commit, err := com.GetODC(1, r) // "1" means skip the first URL word
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for commit value")
return
}
// TODO: Add folder support
dbFolder := "/"
// Check if the requested database exists
exists, err := com.CheckDBExists(loggedInUser, dbOwner, dbFolder, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !exists {
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Database '%s%s%s' doesn't exist", dbOwner, dbFolder,
dbName))
return
}