From f63a777c07094af31c8841a3f50af8beca0aa369 Mon Sep 17 00:00:00 2001
From: Alexander Foremny <aforemny@posteo.de>
Date: Wed, 13 Mar 2024 05:35:58 +0100
Subject: chore: add review commit template

---
 app/Main.hs   |  6 +++---
 app/Review.hs | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 38 insertions(+), 4 deletions(-)

(limited to 'app')

diff --git a/app/Main.hs b/app/Main.hs
index a2fef0b..5a21787 100644
--- a/app/Main.hs
+++ b/app/Main.hs
@@ -343,7 +343,7 @@ import Options.Applicative ((<**>))
 import Options.Applicative qualified as O
 import Patch qualified as A
 import Process (proc, sh_, textInput)
-import Render (renderAsText, (<<<))
+import Render ((<<<))
 import Render qualified as P
 import Review qualified as R
 import Settings (Settings (..), readSettings)
@@ -592,8 +592,8 @@ main = do
         `catch` \(_ :: E.ProcessException) ->
           error "working directory not clean, aborting.."
       plan <- R.formulatePlan perCommit baseBranch featureBranch
-      patch <- A.Patch . concat <$> mapM R.reviewStep (NE.toList plan.steps)
-      T.writeFile "review.patch" (renderAsText patch)
+      R.commitReview plan . A.Patch . concat
+        =<< mapM R.reviewStep (NE.toList plan.steps)
     -- REVIEW Why is withReviewing in the Status module and not the Review
     -- module?
     --
diff --git a/app/Review.hs b/app/Review.hs
index 37690d9..3b7afbe 100644
--- a/app/Review.hs
+++ b/app/Review.hs
@@ -3,10 +3,11 @@ module Review
     PlanStep (..),
     formulatePlan,
     reviewStep,
+    commitReview,
   )
 where
 
-import Control.Monad (ap, forM, forM_)
+import Control.Monad (ap, forM, forM_, when)
 import Data.Binary qualified as B
 import Data.ByteString.Lazy qualified as LB
 import Data.Function ((&))
@@ -28,6 +29,8 @@ import Text.Diff.Parse.Types qualified as D
 
 data Plan = Plan
   { baseBranch :: BranchName,
+    featureBranch :: BranchName,
+    commit :: Git.CommitHash,
     perCommit :: Bool,
     steps :: NE.NonEmpty PlanStep
   }
@@ -71,6 +74,7 @@ formulatePlan perCommit baseBranch featureBranch = do
                 )
                 fileDeltas
             ),
+        commit = featureCommit,
         ..
       }
 
@@ -177,3 +181,33 @@ withTempSourceFiles (Git.Commit hash) fileDeltas action = do
 if' :: Bool -> a -> a -> a
 if' True a _ = a
 if' False _ b = b
+
+commitReview :: Plan -> A.Patch -> IO ()
+commitReview plan patch = do
+  withSystemTempDirectory "anissue" $ \tmp -> do
+    when (not (null patch.fileDeltas)) do
+      T.writeFile (tmp </> "review.patch") (renderAsText patch)
+      sh_ (proc "patch -p0 <%/review.patch" tmp)
+    T.writeFile (tmp </> "commit_editmsg") (commit_editmsg plan)
+    sh_ (proc "git add %" (map (.fileDeltaDestFile) patch.fileDeltas))
+    sh_ (proc "git commit --allow-empty --template %/commit_editmsg" tmp)
+
+commit_editmsg :: Plan -> T.Text
+commit_editmsg plan = do
+  T.unlines
+    [ "",
+      "# Please enter the commit message for your review. Lines starting",
+      "# with '#' will be ignored, and an empty message aborts the commit.",
+      "#",
+      "# To approve the changes, format your commit message like this:",
+      "#",
+      "# review: approve " <> plan.featureBranch,
+      "#",
+      "# Reviewed branch " <> plan.featureBranch <> " at commit " <> Git.toTextUnsafe plan.commit <> ".",
+      "#",
+      "# To requst changes, format your commit message like this:",
+      "#",
+      "# review: request-changes " <> plan.featureBranch,
+      "#",
+      "# Reviewed branch " <> plan.featureBranch <> " at commit " <> Git.toTextUnsafe plan.commit <> "."
+    ]
-- 
cgit v1.2.3