-
Notifications
You must be signed in to change notification settings - Fork 40
/
pages.go
2456 lines (2185 loc) · 75 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"
"sort"
"strconv"
"strings"
"time"
com "github.com/sqlitebrowser/dbhub.io/common"
"github.com/sqlitebrowser/dbhub.io/common/config"
"github.com/sqlitebrowser/dbhub.io/common/database"
gfm "github.com/sqlitebrowser/github_flavored_markdown"
)
// Renders the "About Us" page.
func aboutPage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
PageMeta PageMetaInfo
}
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
pageData.PageMeta.Title = "What is DBHub.io?"
// 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
var pageData struct {
Branches map[string]database.BranchEntry
DB database.SQLiteDBinfo
PageMeta PageMetaInfo
}
pageData.PageMeta.Title = "Branch list"
pageData.PageMeta.PageSection = "db_data"
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
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 = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Read the branch heads list from the database
pageData.Branches, err = database.GetBranches(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// 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 database.DBTree `json:"tree"`
}
var pageData struct {
Branches map[string]database.BranchEntry
DB database.SQLiteDBinfo
History []HistEntry
PageMeta PageMetaInfo
}
pageData.PageMeta.Title = "Commits"
pageData.PageMeta.PageSection = "db_data"
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Retrieve the branch name
branchName, err := com.GetFormBranch(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Check if the user has access to the requested database (and get its details if available)
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Read the branch heads list from the database
pageData.Branches, err = database.GetBranches(dbName.Owner, dbName.Database)
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 = pageData.DB.Info.DefaultBranch
}
// Work out the head commit ID for the requested branch
headCom, ok := pageData.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 := database.GetCommitList(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// To create the commit history we need to follow both, the parent commit id and the other parents commit ids,
// to include merged commits. When following a merged branch we do however end up with the regular commits at
// some point. In this example the first line is what you get by following the parent ids. We also want to
// include the second line which we get by following c8's other parent's id.
// c1 -> c2 -> c3 -> c4 -> c5 -> c8
// \-> c6 -> c7 /
// However, we don't want c1 and c2 to be included twice. This is why we assemble a list of all regular parent
// commit ids first as a look-up table for knowing when to stop traversing the other branches of the tree.
regularBranchCommitIds := map[string]bool{}
commitData := database.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
}
regularBranchCommitIds[commitData.ID] = true
}
// This function recursively follows all branches of the tree
var traverseTree func(string, bool) (err error)
traverseTree = func(id string, stopAtRegularBranch bool) (err error) {
for id != "" {
// If we want to stop at the regular branch check if this commit id is a known regular branch
// commit id. If so return here
if stopAtRegularBranch {
_, ok = regularBranchCommitIds[id]
if ok {
return
}
// Add this commit id to the list of known commit ids to stop at. Just to be sure
// to avoid double commits in messes up commit histories.
// TODO Maybe remove this when we are able to display an actual tree structure.
regularBranchCommitIds[id] = true
}
// 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
commit, ok := rawList[id]
if !ok {
return fmt.Errorf("Internal error when retrieving commit data")
}
uName, avatarURL, err := database.GetUsernameFromEmail(commit.AuthorEmail)
if err != nil {
return err
}
if avatarURL != "" {
avatarURL += "&s=30"
}
// Create a history entry
newEntry := HistEntry{
AuthorEmail: commit.AuthorEmail,
AuthorName: commit.AuthorName,
AuthorUserName: uName,
AvatarURL: avatarURL,
CommitterEmail: commit.CommitterEmail,
CommitterName: commit.CommitterName,
ID: commit.ID,
Message: string(gfm.Markdown([]byte(commit.Message))),
Parent: commit.Parent,
Timestamp: commit.Timestamp,
}
pageData.History = append(pageData.History, newEntry)
// Follow the other parents if there are any
for _, v := range commit.OtherParents {
traverseTree(v, true)
}
id = commit.Parent
}
return
}
// Create the history list
err = traverseTree(headID, false)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// 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 {
CommitList []CommitData
DB database.SQLiteDBinfo
DestDBBranches []string
DestDBDefaultBranch string
DestDBName string
DestOwner string
Forks []database.ForkEntry
PageMeta PageMetaInfo
SourceDBBranches []string
SourceDBDefaultBranch string
SourceDBName string
SourceOwner string
}
pageData.PageMeta.Title = "Create a Merge Request"
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Require login
errCode, err = requireLogin(pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
// Check if the user has access to the requested database (and get it's details if available)
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Retrieve list of forks for the database
pageData.Forks, err = database.ForkTree(pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError,
fmt.Sprintf("Error retrieving fork list for '%s/%s': %v\n", dbName.Owner, dbName.Database, err.Error()))
return
}
// Use the database which the "New Merge Request" button was pressed on as the initially selected source
pageData.SourceOwner = dbName.Owner
pageData.SourceDBName = dbName.Database
// 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.DestDBName, err = database.ForkParent(pageData.PageMeta.LoggedInUser, dbName.Owner,
dbName.Database)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
if pageData.DestOwner == "" || pageData.DestDBName == "" {
pageData.DestOwner = dbName.Owner
pageData.DestDBName = dbName.Database
}
// * Determine the source and destination database branches *
// Retrieve the branch info for the source database
srcBranchList, err := database.GetBranches(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
for name := range srcBranchList {
pageData.SourceDBBranches = append(pageData.SourceDBBranches, name)
}
pageData.SourceDBDefaultBranch = pageData.DB.Info.DefaultBranch
// Retrieve the branch info for the destination database
destBranchList, err := database.GetBranches(pageData.DestOwner, 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 = database.GetDefaultBranchName(pageData.DestOwner,
pageData.DestDBName)
if err != nil {
errorPage(w, r, http.StatusBadRequest, 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(dbName.Owner, dbName.Database,
pageData.SourceDBDefaultBranch, pageData.DestOwner, 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 := database.GetCommitList(pageData.DestOwner, 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 CommitData
c.AuthorEmail = j.AuthorEmail
c.AuthorName = j.AuthorName
c.ID = j.ID
c.Parent = j.Parent
c.Message = j.Message
c.Timestamp = j.Timestamp
c.AuthorUsername, c.AuthorAvatar, err = database.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 := database.GetLicenceInfoFromSha256(dbName.Owner, 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)
}
}
// Render the page
pageData.PageMeta.PageSection = "db_merge"
t := tmpl.Lookup("comparePage")
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 {
Contributors map[string]AuthorEntry
DB database.SQLiteDBinfo
PageMeta PageMetaInfo
}
pageData.PageMeta.Title = "Contributors"
pageData.PageMeta.PageSection = "db_data"
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
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 = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Read the commit list from the database
commitList, err := database.GetCommitList(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Fill out the metadata
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 := database.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,
}
}
}
// 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 {
DB database.SQLiteDBinfo
PageMeta PageMetaInfo
Commit string
}
pageData.PageMeta.Title = "Create new branch"
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Require login
errCode, err = requireLogin(pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
// Retrieve the commit ID
pageData.Commit, err = com.GetFormCommit(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Make sure the logged in user has the permissions to proceed
allowed, err := database.CheckDBPermissions(pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, true)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if allowed == false {
errorPage(w, r, http.StatusUnauthorized, "You are not authorised to change this database")
return
}
// Check if the user has access to the requested database (and get it's details if available)
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Render the page
t := tmpl.Lookup("createBranchPage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// Displays a web page to input information needed for creating a new discussion.
func createDiscussionPage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
DB database.SQLiteDBinfo
PageMeta PageMetaInfo
}
pageData.PageMeta.Title = "Create new discussion"
pageData.PageMeta.PageSection = "db_disc"
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Require login
errCode, err = requireLogin(pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
// Check if the user has access to the requested database (and get it's details if available)
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Render the page
t := tmpl.Lookup("createDiscussionPage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// Displays a web page asking for the new tag details.
func createTagPage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
DB database.SQLiteDBinfo
PageMeta PageMetaInfo
Commit string
}
pageData.PageMeta.Title = "Create new tag"
// Retrieve the commit ID
commit, err := com.GetFormCommit(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for commit value")
return
}
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Require login
errCode, err = requireLogin(pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
// Check if the user has access to the requested database (and get it's details if available)
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Make sure the logged in user has the permissions to proceed
allowed, err := database.CheckDBPermissions(pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, true)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if allowed == false {
errorPage(w, r, http.StatusUnauthorized, "You are not authorised to change this database")
return
}
// Fill out metadata for the page to be rendered
pageData.Commit = commit
// Render the page
t := tmpl.Lookup("createTagPage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
func databasePage(w http.ResponseWriter, r *http.Request, dbOwner string, dbName string) {
var pageData struct {
DB database.SQLiteDBinfo
PageMeta PageMetaInfo
DB4S config.DB4SConfig
WriteEnabled bool
}
pageData.PageMeta.PageSection = "db_data"
pageData.DB4S = config.Conf.DB4S
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
// Check if the database exists and the user has access to view it
exists, err := database.CheckDBPermissions(pageData.PageMeta.LoggedInUser, dbOwner, dbName, false)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !exists {
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Database '%s/%s' doesn't exist", dbOwner, dbName))
return
}
// Figure out the correct commit ID from the provided tag, branch, release name or commit id
// For live databases these do not exist yet, so this step is skipped.
var commitID string
branchHeads := make(map[string]database.BranchEntry)
if !pageData.DB.Info.IsLive {
// Check if a specific database commit ID was given
commitID, err = com.GetFormCommit(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Invalid database commit ID")
return
}
// Check if a branch name was requested
branchName, err := com.GetFormBranch(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for branch name")
return
}
// Check if a named tag was requested
tagName, err := com.GetFormTag(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for tag name")
return
}
// Check if a specific release was requested
releaseName := r.FormValue("release")
if releaseName != "" {
err = com.ValidateBranchName(releaseName)
if err != nil {
errorPage(w, r, http.StatusBadRequest, "Validation failed for release name")
return
}
}
// If a specific commit was requested, make sure it exists in the database commit history
if commitID != "" {
commitList, err := database.GetCommitList(dbOwner, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if _, ok := commitList[commitID]; !ok {
// The requested commit isn't one in the database commit history so error out
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Unknown commit for database '%s/%s'", dbOwner,
dbName))
return
}
}
// If a specific release was requested, and no commit ID was given, retrieve the commit ID matching the release
if commitID == "" && releaseName != "" {
releases, err := database.GetReleases(dbOwner, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Couldn't retrieve releases for database")
return
}
rls, ok := releases[releaseName]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Unknown release requested for this database")
return
}
commitID = rls.Commit
}
// Load the branch info for the database
branchHeads, err = database.GetBranches(dbOwner, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Couldn't retrieve branch information for database")
return
}
// If a specific branch was requested and no commit ID was given, use the latest commit for the branch
if commitID == "" && branchName != "" {
c, ok := branchHeads[branchName]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Unknown branch requested for this database")
return
}
commitID = c.Commit
}
// If a specific tag was requested, and no commit ID was given, retrieve the commit ID matching the tag
// TODO: If we need to reduce database calls, we can probably make a function merging this, GetBranches(), and
// TODO GetCommitList() above. Potentially also the DBDetails() call below too.
if commitID == "" && tagName != "" {
tags, err := database.GetTags(dbOwner, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, "Couldn't retrieve tags for database")
return
}
tg, ok := tags[tagName]
if !ok {
errorPage(w, r, http.StatusInternalServerError, "Unknown tag requested for this database")
return
}
commitID = tg.Commit
}
// If we still haven't determined the required commit ID, use the head commit of the default branch
if commitID == "" {
commitID, err = database.DefaultCommit(dbOwner, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
pageData.DB.Info.Branch = branchName
}
// Retrieve the database details
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbOwner, dbName, commitID)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Check if the current user is allowed to write to the database
pageData.WriteEnabled, err = database.CheckDBPermissions(pageData.PageMeta.LoggedInUser, dbOwner, dbName, true)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// For non-live databases, add branch, table and view information by querying it directly, otherwise we get the details from our job queue backend
if !pageData.DB.Info.IsLive {
// Retrieve default branch name details
if pageData.DB.Info.Branch == "" {
pageData.DB.Info.Branch = pageData.DB.Info.DefaultBranch
}
for i := range branchHeads {
pageData.DB.Info.BranchList = append(pageData.DB.Info.BranchList, i)
}
pageData.DB.Info.Commits = branchHeads[pageData.DB.Info.Branch].CommitCount
// Query the database
sdb, err := com.OpenSQLiteDatabaseDefensive(w, r, dbOwner, dbName, commitID, pageData.PageMeta.LoggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
defer sdb.Close()
pageData.DB.Info.Tables, err = com.TablesAndViews(sdb, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
} else {
pageData.DB.Info.Tables, err = com.LiveTablesAndViews(pageData.DB.Info.LiveNode, pageData.PageMeta.LoggedInUser, dbOwner, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
pageData.DB.Info.DBEntry.Size, err = com.LiveSize(pageData.DB.Info.LiveNode, pageData.PageMeta.LoggedInUser, dbOwner, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// Fill out various metadata fields
pageData.PageMeta.Title = fmt.Sprintf("%s / %s", dbOwner, dbName)
// Determine the number of rows to display
if pageData.PageMeta.LoggedInUser != "" {
pageData.DB.MaxRows = database.PrefUserMaxRows(pageData.PageMeta.LoggedInUser)
} else {
// Not logged in, so use the default number of rows
pageData.DB.MaxRows = database.DefaultNumDisplayRows
}
// Render the full description as markdown
pageData.DB.Info.FullDesc = string(gfm.Markdown([]byte(pageData.DB.Info.FullDesc)))
// Increment the view counter for the database (excluding people viewing their own databases)
if strings.ToLower(pageData.PageMeta.LoggedInUser) != strings.ToLower(dbOwner) {
err = com.IncrementViewCount(dbOwner, dbName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
}
// Render the page
t := tmpl.Lookup("databasePage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
func diffPage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
DB database.SQLiteDBinfo
Diffs com.Diffs
ColumnNamesBefore map[string][]string
ColumnNamesAfter map[string][]string
PageMeta PageMetaInfo
}
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Get the commit ids
commitA := r.FormValue("commit_a")
commitB := r.FormValue("commit_b")
// Validate the supplied information
if commitA == "" || commitB == "" {
errorPage(w, r, http.StatusBadRequest, "Missing commit ids")
return
}
// Check if the user has access to the requested database (and get it's details if available)
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, commitA)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, commitB)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Retrieve the diffs for these commits
pageData.Diffs, err = com.Diff(dbName.Owner, dbName.Database, commitA, dbName.Owner, dbName.Database, commitB, pageData.PageMeta.LoggedInUser, com.NoMerge, true)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Retrieve the column information for each table with data changes
sdbBefore, err := com.OpenSQLiteDatabaseDefensive(w, r, dbName.Owner, dbName.Database, commitA, pageData.PageMeta.LoggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
defer sdbBefore.Close()
sdbAfter, err := com.OpenSQLiteDatabaseDefensive(w, r, dbName.Owner, dbName.Database, commitB, pageData.PageMeta.LoggedInUser)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
defer sdbAfter.Close()
pageData.ColumnNamesBefore = make(map[string][]string)
pageData.ColumnNamesAfter = make(map[string][]string)
for _, diff := range pageData.Diffs.Diff {
if diff.ObjectType == "table" && len(diff.Data) > 0 {
pks, _, other, err := com.GetPrimaryKeyAndOtherColumns(sdbBefore, "main", diff.ObjectName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
pageData.ColumnNamesBefore[diff.ObjectName] = append(pks, other...)
pks, _, other, err = com.GetPrimaryKeyAndOtherColumns(sdbAfter, "main", diff.ObjectName)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
pageData.ColumnNamesAfter[diff.ObjectName] = append(pks, other...)
}