From bbdfa50c91910f1a09ba268dcda2208e99aa1abb Mon Sep 17 00:00:00 2001
From: Sarah Jamie Lewis <sarah@openprivacy.ca>
Date: Sat, 15 Feb 2025 14:16:13 -0800
Subject: [PATCH 1/1] Check if a Patch has been applied. Nicer Client Side
 Validation

---
 repo/patch.go                       |  8 ++++++++
 repo/requests.go                    | 18 +++++++++++++++---
 templates/request.list.tpl.html     |  3 +++
 templates/request.new.tpl.html      |  2 ++
 templates/request.newpatch.tpl.html |  2 ++
 5 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/repo/patch.go b/repo/patch.go
index 55e991d..95c6d42 100644
--- a/repo/patch.go
+++ b/repo/patch.go
@@ -24,6 +24,14 @@ func (ph *PatchHandler) CheckApply(patchfile string) ([]byte, error) {
 	return output, err
 }
 
+func (ph *PatchHandler) CheckApplied(patchfile string) ([]byte, error) {
+	patchFileAbs, _ := filepath.Abs(patchfile)
+	cmd := exec.Command("git", "apply", "--reverse", "--check", patchFileAbs)
+	cmd.Dir = ph.RepoPath
+	output, err := cmd.CombinedOutput()
+	return output, err
+}
+
 func (ph *PatchHandler) Apply(patchfile string) error {
 	patchFileAbs, _ := filepath.Abs(patchfile)
 	cmd := exec.Command("git", "am", "--keep-cr", patchFileAbs)
diff --git a/repo/requests.go b/repo/requests.go
index c9edb99..54adad2 100644
--- a/repo/requests.go
+++ b/repo/requests.go
@@ -69,10 +69,18 @@ func (rm *RequestManager) ApplyPatch(w http.ResponseWriter, r *http.Request) {
 		patchHash := r.PathValue("patch")
 		err := rm.patchHandler.Apply(rm.RequestIssueElement(idString, patchHash))
 		if err == nil {
-			rm.patchHandler.Upstream()
-			rm.patchHandler.Rebuild()
+			err := rm.patchHandler.Upstream()
+			if err != nil {
+				fmt.Printf("could not upstream: %v\n", err)
+			}
+			err = rm.patchHandler.Rebuild()
+			if err != nil {
+				fmt.Printf("could not rebuild: %v\n", err)
+			}
 			http.Redirect(w, r, r.Header.Get("Referer"), 302)
 			return
+		} else {
+			fmt.Printf("could not apply patch: %v\n", err)
 		}
 	}
 	http.Redirect(w, r, "/404", 404)
@@ -236,7 +244,11 @@ func (rm *RequestManager) LoadIssues(logpath string, loadfn func(hash string) (*
 								issue.PatchInfo = patchSummary
 
 								if _, err := rm.patchHandler.CheckApply(rm.RequestIssueElement(le.Hash, issue.PatchRef)); err != nil {
-									warningCallback(WARN, "This Patch Does Not Apply To The Current Repository")
+									if _, err := rm.patchHandler.CheckApplied(rm.RequestIssueElement(le.Hash, issue.PatchRef)); err != nil {
+										warningCallback(WARN, "This Patch Does Not Apply To The Current Repository")
+									} else {
+										warningCallback(INFO, "This Patch Has Been Applied")
+									}
 								} else {
 									warningCallback(INFO, "This Patch Can Be Applied To The Current Repository")
 									issue.PatchInfo.CanBeApplied = true
diff --git a/templates/request.list.tpl.html b/templates/request.list.tpl.html
index 68567c0..e709ce0 100644
--- a/templates/request.list.tpl.html
+++ b/templates/request.list.tpl.html
@@ -21,6 +21,8 @@
 </tr>
 {{$p := .}}
 {{range .Requests}}
+
+        {{if .Approved}}
     <tr><td><a href="./{{.ID}}">{{.Summary}}</a></td><td>{{.Created.Format "Jan 02, 2006 15:04" }}</td>
     
         {{if eq $p.User.IsMaintainer true}}
@@ -39,6 +41,7 @@
 
         </td>
         {{end}}
+        {{end}}
     </tr>
 {{end}}
 </table>
diff --git a/templates/request.new.tpl.html b/templates/request.new.tpl.html
index 19b2682..dff04ad 100644
--- a/templates/request.new.tpl.html
+++ b/templates/request.new.tpl.html
@@ -17,6 +17,7 @@
       <input
         name="summary"
         placeholder="A one line summary of the issue/request"
+        minlength="10"
       />
     </label>
     <label>
@@ -24,6 +25,7 @@
       <textarea
         name="description"
         placeholder="Please outline the issue/change request in full."
+        minlength="100"
       ></textarea>
     </label>
   </fieldset>
diff --git a/templates/request.newpatch.tpl.html b/templates/request.newpatch.tpl.html
index d765fb1..d1dfa31 100644
--- a/templates/request.newpatch.tpl.html
+++ b/templates/request.newpatch.tpl.html
@@ -18,6 +18,7 @@
       <input
         name="summary"
         placeholder="A one line summary of the issue/request"
+          minlength="10"
       />
     </label>
         <label>
@@ -25,6 +26,7 @@
       <input
         type="file"
         name="patchfile"
+        required
       />
     </label>
 
-- 
2.43.0

