tenseleyflow/shithub / 5c975ca

Browse files

Fix editor template rendering

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
5c975ca6bcfb31f6515a24685aaa94fdb6622bdf
Parents
54bd204
Tree
ed45d04

2 changed files

StatusFile+-
M internal/web/handlers/repo/editor.go 23 5
A internal/web/handlers/repo/editor_test.go 63 0
internal/web/handlers/repo/editor.gomodified
@@ -21,8 +21,10 @@ import (
2121
 type codeEditorData struct {
2222
 	Title       string
2323
 	CSRFToken   string
24
+	Viewer      middleware.CurrentUser
2425
 	Owner       string
2526
 	Repo        any
27
+	Org         any
2628
 	Ref         string
2729
 	RefDisplay  string
2830
 	BaseOID     string
@@ -42,7 +44,12 @@ type codeEditorData struct {
4244
 	Notice      string
4345
 	IsMarkdown  bool
4446
 
45
-	RepoCounts   any
47
+	OGTitle           string
48
+	OGDescription     string
49
+	OGImage           string
50
+	GlobalSearchQuery string
51
+	RepoActions       repoActionView
52
+	RepoCounts        repoSubnavData
4653
 	CanSettings       bool
4754
 	ActiveSubnav      string
4855
 }
@@ -298,6 +305,7 @@ func (h *Handlers) codeMarkdownPreview(w http.ResponseWriter, r *http.Request) {
298305
 }
299306
 
300307
 func (h *Handlers) editorData(r *http.Request, cc *codeContext, mode, pathValue, content string) codeEditorData {
308
+	viewer := middleware.CurrentUserFromContext(r.Context())
301309
 	head, headFound, _ := repogit.CommitAt(r.Context(), cc.gitDir, cc.ref)
302310
 	baseOID := ""
303311
 	if headFound {
@@ -346,6 +354,7 @@ func (h *Handlers) editorData(r *http.Request, cc *codeContext, mode, pathValue,
346354
 	return codeEditorData{
347355
 		Title:        titleVerb + " · " + cc.row.Name,
348356
 		CSRFToken:    middleware.CSRFTokenForRequest(r),
357
+		Viewer:       viewer,
349358
 		Owner:        cc.owner,
350359
 		Repo:         cc.row,
351360
 		Ref:          cc.ref,
@@ -363,17 +372,26 @@ func (h *Handlers) editorData(r *http.Request, cc *codeContext, mode, pathValue,
363372
 		Message:      message,
364373
 		Primary:      primary,
365374
 		IsMarkdown:   hasExt(strings.ToLower(pathValue), []string{".md", ".markdown"}),
375
+		RepoActions:  h.repoActions(r, cc.row.ID),
366376
 		RepoCounts:   h.subnavCounts(r.Context(), cc.row.ID, cc.row.ForkCount),
367
-		CanSettings:  h.canViewSettings(middleware.CurrentUserFromContext(r.Context())),
377
+		CanSettings:  h.canViewSettings(viewer),
368378
 		ActiveSubnav: "code",
369379
 	}
370380
 }
371381
 
372382
 func (h *Handlers) renderEditor(w http.ResponseWriter, r *http.Request, data codeEditorData, status int) {
383
+	w.Header().Set("Content-Type", "text/html; charset=utf-8")
373384
 	if status != http.StatusOK {
374385
 		w.WriteHeader(status)
375386
 	}
376
-	h.d.Render.RenderPage(w, r, "repo/editor", data)
387
+	if err := h.d.Render.RenderPage(w, r, "repo/editor", data); err != nil {
388
+		if h.d.Logger != nil {
389
+			h.d.Logger.ErrorContext(r.Context(), "code: render editor", "error", err, "mode", data.Mode, "owner", data.Owner, "path", data.Path)
390
+		}
391
+		if status == http.StatusOK {
392
+			h.d.Render.HTTPError(w, r, http.StatusInternalServerError, "")
393
+		}
394
+	}
377395
 }
378396
 
379397
 func (h *Handlers) renderWebEditError(w http.ResponseWriter, r *http.Request, data codeEditorData, err error) {
internal/web/handlers/repo/editor_test.goadded
@@ -0,0 +1,63 @@
1
+// SPDX-License-Identifier: AGPL-3.0-or-later
2
+
3
+package repo
4
+
5
+import (
6
+	"net/http/httptest"
7
+	"os"
8
+	"strings"
9
+	"testing"
10
+
11
+	"github.com/tenseleyFlow/shithub/internal/web/middleware"
12
+	"github.com/tenseleyFlow/shithub/internal/web/render"
13
+)
14
+
15
+func TestCodeEditorDataRendersAgainstRealTemplates(t *testing.T) {
16
+	t.Parallel()
17
+
18
+	tmplFS := os.DirFS("../../templates")
19
+	renderer, err := render.New(tmplFS, render.Options{})
20
+	if err != nil {
21
+		t.Fatalf("render.New on real templates: %v", err)
22
+	}
23
+
24
+	req := httptest.NewRequest("GET", "/octo/demo/edit/trunk/README.md", nil)
25
+	rw := httptest.NewRecorder()
26
+	data := codeEditorData{
27
+		Title:       "Edit · demo",
28
+		CSRFToken:   "test-token",
29
+		Viewer:      middleware.CurrentUser{ID: 1, Username: "octo"},
30
+		Owner:       "octo",
31
+		Repo:        editorTemplateRepo{Name: "demo", Visibility: "public"},
32
+		Ref:         "trunk",
33
+		RefDisplay:  "trunk",
34
+		Path:        "README.md",
35
+		PathValue:   "README.md",
36
+		Mode:        "edit",
37
+		FormAction:  "/octo/demo/edit/trunk/README.md",
38
+		CancelURL:   "/octo/demo/blob/trunk/README.md",
39
+		PreviewURL:  "/octo/demo/markdown-preview",
40
+		Content:     "# Demo\n",
41
+		Message:     "Update README.md",
42
+		Primary:     "Commit changes",
43
+		RepoActions: repoActionView{IsLoggedIn: true, ReturnTo: "/octo/demo/edit/trunk/README.md"},
44
+		RepoCounts:  repoSubnavData{},
45
+		CanSettings: true,
46
+	}
47
+
48
+	if err := renderer.RenderPage(rw, req, "repo/editor", data); err != nil {
49
+		t.Fatalf("RenderPage(repo/editor): %v", err)
50
+	}
51
+	body := rw.Body.String()
52
+	if !strings.Contains(body, `data-code-editor`) {
53
+		t.Fatalf("rendered editor body missing editor root: %s", body)
54
+	}
55
+}
56
+
57
+type editorTemplateRepo struct {
58
+	Name         string
59
+	Visibility   string
60
+	WatcherCount int64
61
+	ForkCount    int64
62
+	StarCount    int64
63
+}