repos / git-pr

a self-hosted git collaboration server
git clone https://github.com/picosh/git-pr.git

commit
4cce4d5
parent
45174ab
author
Eric Bower
date
2024-06-17 14:01:03 -0400 EDT
refactor(pr-detail): combine pages
4 files changed,  +33, -139
M web.go
D tmpl/pr-detail-patches.html
+0, -36
 1@@ -1,36 +0,0 @@
 2-{{template "base" .}}
 3-
 4-{{define "title"}}{{.Pr.Title}} - pr patches{{end}}
 5-
 6-{{define "meta"}}
 7-<link rel="alternate" type="application/atom+xml"
 8-      title="RSS feed for git collaboration server"
 9-      href="/prs/{{.Pr.ID}}/rss" />
10-{{end}}
11-
12-{{define "body"}}
13-{{template "pr-header" .}}
14-
15-<main>
16-  <div class="group">
17-    {{range .Patches}}
18-    <div class="box">
19-      <div class="group-h">
20-        <h2 class="text-lg m-0 p-0">
21-          {{if .Review}}<code>REVIEW</code>{{end}}
22-          <span>{{.Title}}</span>
23-        </h2>
24-      </div>
25-      <div>{{.DiffStr}}</div>
26-    </div>
27-    {{end}}
28-  </div>
29-  <hr />
30-</main>
31-
32-<hr />
33-
34-<footer>
35-  <a href="/prs/{{.Pr.ID}}/rss">rss</a>
36-</footer>
37-{{end}}
M tmpl/pr-detail.html
+18, -8
 1@@ -11,24 +11,18 @@
 2 {{define "body"}}
 3 {{template "pr-header" .}}
 4 
 5-<main>
 6+<main class="group">
 7   <div class="group">
 8     {{range .Patches}}
 9     <div class="box">
10       <h2 class="text-lg m-0 p-0">
11         {{if .Review}}<code>REVIEW</code>{{end}}
12-        <span>{{.Title}}</span>
13+        <a href="{{.Url}}">{{.Title}}</a>
14       </h2>
15       <div class="group-h text-sm">
16         <code>{{.AuthorName}} &lt;{{.AuthorEmail}}&gt;</code>
17         <date>{{.AuthorDate}}</date>
18       </div>
19-
20-      {{if .Body}}
21-      <div class="my">
22-        <pre>{{.Body}}</pre>
23-      </div>
24-      {{end}}
25     </div>
26     {{else}}
27     <div class="box">
28@@ -36,6 +30,22 @@
29     </div>
30     {{end}}
31   </div>
32+
33+  <hr class="w-full" />
34+
35+  <div class="group">
36+    {{range .Patches}}
37+    <div class="box">
38+      <div class="group-h">
39+        <h2 class="text-lg m-0 p-0">
40+          {{if .Review}}<code>REVIEW</code>{{end}}
41+          <a href="{{.Url}}">{{.Title}}</a>
42+        </h2>
43+      </div>
44+      <div>{{.DiffStr}}</div>
45+    </div>
46+    {{end}}
47+  </div>
48 </main>
49 
50 <hr />
M tmpl/pr-header.html
+2, -12
 1@@ -11,17 +11,7 @@
 2     <code>{{.Pr.Pubkey}}</code>
 3   </div>
 4 
 5-  <div class="mt">
 6-  {{if eq .Page "pr"}}
 7-    <strong>summary</strong> &middot; <a href="{{.PatchesUrl}}">patches</a>
 8-  {{else}}
 9-    <a href="{{.SummaryUrl}}">summary</a> &middot; <strong>patches</strong>
10-  {{end}}
11-  </div>
12-
13-	<div>
14-    <span>Submit change to patch: </span>
15-    <code>git format-patch -1 HEAD --stdout | ssh pr.pico.sh pr add {{.Pr.ID}}</code>
16-  </div>
17+    <pre># add changes to patch request
18+git format-patch main --stdout | ssh pr.pico.sh pr add {{.Pr.ID}}</pre>
19 </header>
20 {{end}}
M web.go
+13, -83
  1@@ -143,7 +143,7 @@ type RepoDetailData struct {
  2 	ClosedPrs   []PrListData
  3 }
  4 
  5-func repoHandler(w http.ResponseWriter, r *http.Request) {
  6+func repoDetailHandler(w http.ResponseWriter, r *http.Request) {
  7 	repoID := r.PathValue("id")
  8 
  9 	web, err := getWebCtx(r)
 10@@ -214,19 +214,18 @@ type PrData struct {
 11 
 12 type PatchData struct {
 13 	*Patch
 14+	Url     template.URL
 15 	DiffStr template.HTML
 16 }
 17 
 18 type PrHeaderData struct {
 19-	Page       string
 20-	Repo       LinkData
 21-	Pr         PrData
 22-	PatchesUrl template.URL
 23-	SummaryUrl template.URL
 24-	Patches    []PatchData
 25+	Page    string
 26+	Repo    LinkData
 27+	Pr      PrData
 28+	Patches []PatchData
 29 }
 30 
 31-func prHandler(w http.ResponseWriter, r *http.Request) {
 32+func prDetailHandler(w http.ResponseWriter, r *http.Request) {
 33 	id := r.PathValue("id")
 34 	prID, err := strconv.Atoi(id)
 35 	if err != nil {
 36@@ -261,73 +260,6 @@ func prHandler(w http.ResponseWriter, r *http.Request) {
 37 		return
 38 	}
 39 
 40-	patchesData := []PatchData{}
 41-	for _, patch := range patches {
 42-		patchesData = append(patchesData, PatchData{
 43-			Patch:   patch,
 44-			DiffStr: "",
 45-		})
 46-	}
 47-
 48-	w.Header().Set("content-type", "text/html")
 49-	tmpl := getTemplate("pr-detail.html")
 50-	err = tmpl.Execute(w, PrHeaderData{
 51-		Page: "pr",
 52-		Repo: LinkData{
 53-			Url:  template.URL("/repos/" + repo.ID),
 54-			Text: repo.ID,
 55-		},
 56-		SummaryUrl: template.URL(fmt.Sprintf("/prs/%d", pr.ID)),
 57-		PatchesUrl: template.URL(fmt.Sprintf("/prs/%d/patches", pr.ID)),
 58-		Patches:    patchesData,
 59-		Pr: PrData{
 60-			ID:     pr.ID,
 61-			Title:  pr.Name,
 62-			Pubkey: pr.Pubkey,
 63-			Date:   pr.CreatedAt.Format(time.RFC3339),
 64-			Status: pr.Status,
 65-		},
 66-	})
 67-	if err != nil {
 68-		fmt.Println(err)
 69-	}
 70-}
 71-
 72-func prPatchesHandler(w http.ResponseWriter, r *http.Request) {
 73-	id := r.PathValue("id")
 74-	prID, err := strconv.Atoi(id)
 75-	if err != nil {
 76-		w.WriteHeader(http.StatusUnprocessableEntity)
 77-		return
 78-	}
 79-
 80-	web, err := getWebCtx(r)
 81-	if err != nil {
 82-		w.WriteHeader(http.StatusInternalServerError)
 83-		return
 84-	}
 85-
 86-	pr, err := web.Pr.GetPatchRequestByID(int64(prID))
 87-	if err != nil {
 88-		web.Pr.Backend.Logger.Error("cannot get prs", "err", err)
 89-		w.WriteHeader(http.StatusInternalServerError)
 90-		return
 91-	}
 92-
 93-	repo, err := web.Pr.GetRepoByID(pr.RepoID)
 94-	if err != nil {
 95-		web.Logger.Error("cannot get repo", "err", err)
 96-		w.WriteHeader(http.StatusInternalServerError)
 97-		return
 98-	}
 99-
100-	patches, err := web.Pr.GetPatchesByPrID(int64(prID))
101-	if err != nil {
102-		web.Pr.Backend.Logger.Error("cannot get patches", "err", err)
103-		w.WriteHeader(http.StatusInternalServerError)
104-		return
105-	}
106-
107 	patchesData := []PatchData{}
108 	for _, patch := range patches {
109 		diffStr, err := parseText(web.Formatter, web.Theme, patch.RawText)
110@@ -338,21 +270,20 @@ func prPatchesHandler(w http.ResponseWriter, r *http.Request) {
111 
112 		patchesData = append(patchesData, PatchData{
113 			Patch:   patch,
114+			Url:     template.URL(fmt.Sprintf("#patch-%d", patch.ID)),
115 			DiffStr: template.HTML(diffStr),
116 		})
117 	}
118 
119 	w.Header().Set("content-type", "text/html")
120-	tmpl := getTemplate("pr-detail-patches.html")
121+	tmpl := getTemplate("pr-detail.html")
122 	err = tmpl.Execute(w, PrHeaderData{
123-		Page: "patches",
124+		Page: "pr",
125 		Repo: LinkData{
126 			Url:  template.URL("/repos/" + repo.ID),
127 			Text: repo.ID,
128 		},
129-		SummaryUrl: template.URL(fmt.Sprintf("/prs/%d", pr.ID)),
130-		PatchesUrl: template.URL(fmt.Sprintf("/prs/%d/patches", pr.ID)),
131-		Patches:    patchesData,
132+		Patches: patchesData,
133 		Pr: PrData{
134 			ID:     pr.ID,
135 			Title:  pr.Name,
136@@ -517,10 +448,9 @@ func StartWebServer() {
137 
138 	// ensure legacy router is disabled
139 	// GODEBUG=httpmuxgo121=0
140-	http.HandleFunc("GET /prs/{id}/patches", ctxMdw(ctx, prPatchesHandler))
141-	http.HandleFunc("GET /prs/{id}", ctxMdw(ctx, prHandler))
142+	http.HandleFunc("GET /prs/{id}", ctxMdw(ctx, prDetailHandler))
143 	http.HandleFunc("GET /prs/{id}/rss", ctxMdw(ctx, rssHandler))
144-	http.HandleFunc("GET /repos/{id}", ctxMdw(ctx, repoHandler))
145+	http.HandleFunc("GET /repos/{id}", ctxMdw(ctx, repoDetailHandler))
146 	http.HandleFunc("GET /repos/{repoid}/rss", ctxMdw(ctx, rssHandler))
147 	http.HandleFunc("GET /", ctxMdw(ctx, repoListHandler))
148 	http.HandleFunc("GET /syntax.css", ctxMdw(ctx, chromaStyleHandler))