1package web
2
3import (
4 "fmt"
5 "html/template"
6 "net/http"
7
8 "github.com/charmbracelet/log/v2"
9 "github.com/charmbracelet/soft-serve/pkg/backend"
10 "github.com/charmbracelet/soft-serve/pkg/config"
11 "github.com/charmbracelet/soft-serve/pkg/proto"
12 "github.com/charmbracelet/soft-serve/pkg/ui/common"
13)
14
15// OverviewData contains data for rendering repository overview page.
16type OverviewData struct {
17 RepoBaseData
18 IsEmpty bool
19 SSHURL string
20 HTTPURL string
21 ReadmeHTML template.HTML
22}
23
24// repoOverview handles repository overview page.
25func repoOverview(w http.ResponseWriter, r *http.Request) {
26 ctx := r.Context()
27 logger := log.FromContext(ctx)
28 repo := proto.RepositoryFromContext(ctx)
29 cfg := config.FromContext(ctx)
30
31 gr, err := openRepository(repo)
32 if err != nil {
33 logger.Debug("failed to open repository", "repo", repo.Name(), "err", err)
34 renderInternalServerError(w, r)
35 return
36 }
37
38 defaultBranch := getDefaultBranch(gr)
39 isEmpty := defaultBranch == ""
40
41 sshURL := common.RepoURL(cfg.SSH.PublicURL, repo.Name())
42 httpURL := fmt.Sprintf("%s/%s.git", cfg.HTTP.PublicURL, repo.Name())
43
44 var readmeHTML template.HTML
45 if !isEmpty {
46 head, _ := gr.HEAD()
47 readmeContent, readmePath, err := backend.Readme(repo, head)
48 if err == nil && readmeContent != "" {
49 ctx := &ReadmeContext{
50 RepoName: repo.Name(),
51 CommitHash: head.Reference.ID,
52 ReadmePath: readmePath,
53 }
54 var mdErr error
55 readmeHTML, mdErr = renderMarkdown([]byte(readmeContent), ctx)
56 if mdErr != nil {
57 logger.Debug("failed to render readme markdown", "repo", repo.Name(), "path", readmePath, "err", mdErr)
58 }
59 }
60 }
61
62 repoDisplayName := repo.ProjectName()
63 if repoDisplayName == "" {
64 repoDisplayName = repo.Name()
65 }
66
67 data := OverviewData{
68 RepoBaseData: RepoBaseData{
69 BaseData: BaseData{
70 ServerName: cfg.Name,
71 ActiveTab: "overview",
72 Title: repoDisplayName,
73 Description: repo.Description(),
74 },
75 Repo: repo,
76 DefaultBranch: defaultBranch,
77 },
78 IsEmpty: isEmpty,
79 SSHURL: sshURL,
80 HTTPURL: httpURL,
81 ReadmeHTML: readmeHTML,
82 }
83
84 renderHTML(w, "overview.html", data)
85}