repos / git-pr

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

commit
0abd9a9
parent
131005f
author
Eric Bower
date
2024-10-21 16:03:23 -0400 EDT
feat: ability to filter pr table data by status, title, and user
1 files changed,  +31, -3
M web.go
M web.go
+31, -3
 1@@ -11,10 +11,12 @@ import (
 2 	"log/slog"
 3 	"mime"
 4 	"net/http"
 5+	"net/url"
 6 	"os"
 7 	"path/filepath"
 8 	"slices"
 9 	"strconv"
10+	"strings"
11 	"time"
12 
13 	"github.com/alecthomas/chroma/v2"
14@@ -99,8 +101,13 @@ type PrTableData struct {
15 	MetaData
16 }
17 
18-func getPrTableData(web *WebCtx, prs []*PatchRequest) ([]*PrListData, error) {
19+func getPrTableData(web *WebCtx, prs []*PatchRequest, query url.Values) ([]*PrListData, error) {
20 	prdata := []*PrListData{}
21+	status := strings.ToLower(query.Get("status"))
22+	username := strings.ToLower(query.Get("user"))
23+	title := strings.ToLower(query.Get("title"))
24+	hasFilter := status != "" || username != "" || title != ""
25+
26 	for _, curpr := range prs {
27 		user, err := web.Pr.GetUserByID(curpr.UserID)
28 		if err != nil {
29@@ -112,6 +119,27 @@ func getPrTableData(web *WebCtx, prs []*PatchRequest) ([]*PrListData, error) {
30 			web.Logger.Error("cannot get pubkey from user public key", "err", err)
31 			continue
32 		}
33+
34+		if hasFilter {
35+			if status != "" {
36+				if status != curpr.Status {
37+					continue
38+				}
39+			}
40+
41+			if username != "" {
42+				if username != user.Name {
43+					continue
44+				}
45+			}
46+
47+			if title != "" {
48+				if !strings.Contains(strings.ToLower(curpr.Name), title) {
49+					continue
50+				}
51+			}
52+		}
53+
54 		isAdmin := web.Backend.IsAdmin(pk)
55 		prls := &PrListData{
56 			RepoID: curpr.RepoID,
57@@ -152,7 +180,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
58 		return
59 	}
60 
61-	prdata, err := getPrTableData(web, prs)
62+	prdata, err := getPrTableData(web, prs, r.URL.Query())
63 	if err != nil {
64 		web.Logger.Error("could not get pr table data", "err", err)
65 		w.WriteHeader(http.StatusInternalServerError)
66@@ -226,7 +254,7 @@ func repoDetailHandler(w http.ResponseWriter, r *http.Request) {
67 		return
68 	}
69 
70-	prdata, err := getPrTableData(web, prs)
71+	prdata, err := getPrTableData(web, prs, r.URL.Query())
72 	if err != nil {
73 		web.Logger.Error("cannot get pr table data", "err", err)
74 		w.WriteHeader(http.StatusInternalServerError)