Fix editor template rendering
Authored by
mfwolffe <wolffemf@dukes.jmu.edu>
- SHA
5c975ca6bcfb31f6515a24685aaa94fdb6622bdf- Parents
-
54bd204 - Tree
ed45d04
5c975ca
5c975ca6bcfb31f6515a24685aaa94fdb6622bdf54bd204
ed45d04| Status | File | + | - |
|---|---|---|---|
| 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 ( | ||
| 21 | 21 | type codeEditorData struct { |
| 22 | 22 | Title string |
| 23 | 23 | CSRFToken string |
| 24 | + Viewer middleware.CurrentUser | |
| 24 | 25 | Owner string |
| 25 | 26 | Repo any |
| 27 | + Org any | |
| 26 | 28 | Ref string |
| 27 | 29 | RefDisplay string |
| 28 | 30 | BaseOID string |
@@ -42,7 +44,12 @@ type codeEditorData struct { | ||
| 42 | 44 | Notice string |
| 43 | 45 | IsMarkdown bool |
| 44 | 46 | |
| 45 | - RepoCounts any | |
| 47 | + OGTitle string | |
| 48 | + OGDescription string | |
| 49 | + OGImage string | |
| 50 | + GlobalSearchQuery string | |
| 51 | + RepoActions repoActionView | |
| 52 | + RepoCounts repoSubnavData | |
| 46 | 53 | CanSettings bool |
| 47 | 54 | ActiveSubnav string |
| 48 | 55 | } |
@@ -298,6 +305,7 @@ func (h *Handlers) codeMarkdownPreview(w http.ResponseWriter, r *http.Request) { | ||
| 298 | 305 | } |
| 299 | 306 | |
| 300 | 307 | func (h *Handlers) editorData(r *http.Request, cc *codeContext, mode, pathValue, content string) codeEditorData { |
| 308 | + viewer := middleware.CurrentUserFromContext(r.Context()) | |
| 301 | 309 | head, headFound, _ := repogit.CommitAt(r.Context(), cc.gitDir, cc.ref) |
| 302 | 310 | baseOID := "" |
| 303 | 311 | if headFound { |
@@ -346,6 +354,7 @@ func (h *Handlers) editorData(r *http.Request, cc *codeContext, mode, pathValue, | ||
| 346 | 354 | return codeEditorData{ |
| 347 | 355 | Title: titleVerb + " · " + cc.row.Name, |
| 348 | 356 | CSRFToken: middleware.CSRFTokenForRequest(r), |
| 357 | + Viewer: viewer, | |
| 349 | 358 | Owner: cc.owner, |
| 350 | 359 | Repo: cc.row, |
| 351 | 360 | Ref: cc.ref, |
@@ -363,17 +372,26 @@ func (h *Handlers) editorData(r *http.Request, cc *codeContext, mode, pathValue, | ||
| 363 | 372 | Message: message, |
| 364 | 373 | Primary: primary, |
| 365 | 374 | IsMarkdown: hasExt(strings.ToLower(pathValue), []string{".md", ".markdown"}), |
| 375 | + RepoActions: h.repoActions(r, cc.row.ID), | |
| 366 | 376 | RepoCounts: h.subnavCounts(r.Context(), cc.row.ID, cc.row.ForkCount), |
| 367 | - CanSettings: h.canViewSettings(middleware.CurrentUserFromContext(r.Context())), | |
| 377 | + CanSettings: h.canViewSettings(viewer), | |
| 368 | 378 | ActiveSubnav: "code", |
| 369 | 379 | } |
| 370 | 380 | } |
| 371 | 381 | |
| 372 | 382 | 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") | |
| 373 | 384 | if status != http.StatusOK { |
| 374 | 385 | w.WriteHeader(status) |
| 375 | 386 | } |
| 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 | + } | |
| 377 | 395 | } |
| 378 | 396 | |
| 379 | 397 | 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 | +} | |