repos / git-pr

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

commit
29e9d14
parent
f090087
author
Eric Bower
date
2024-07-17 10:26:54 -0400 EDT
refactor: convert AuthorDate to time.Time then format with cfg

Unfortunately, `patch.AuthorDate` is stored as `TEXT` in sqlite and not
a `DATETIME` so we have to do extra work to convert it on-the-fly.

We can create a separate migration to convert it to a datetime.
7 files changed,  +45, -10
M cli.go
M web.go
M cli.go
+4, -2
 1@@ -380,6 +380,7 @@ Here's how it works:
 2 								if patch.Review {
 3 									reviewTxt = "[review]"
 4 								}
 5+								timestamp := AuthorDateToTime(patch.AuthorDate, be.Logger).Format(be.Cfg.TimeFormat)
 6 								wish.Printf(
 7 									sesh,
 8 									"%s %s %s\n%s <%s>\n%s\n\n---\n%s\n%s\n\n\n",
 9@@ -388,7 +389,7 @@ Here's how it works:
10 									truncateSha(patch.CommitSha),
11 									patch.AuthorName,
12 									patch.AuthorEmail,
13-									patch.AuthorDate,
14+									timestamp,
15 									patch.BodyAppendix,
16 									patch.Body,
17 								)
18@@ -451,6 +452,7 @@ Here's how it works:
19 								if patch.Review {
20 									reviewTxt = "[review]"
21 								}
22+								timestamp := AuthorDateToTime(patch.AuthorDate, be.Logger).Format(be.Cfg.TimeFormat)
23 								fmt.Fprintf(
24 									w,
25 									"%d\t%s\t%s\t%s\t%s <%s>\t%s\n",
26@@ -460,7 +462,7 @@ Here's how it works:
27 									truncateSha(patch.CommitSha),
28 									patch.AuthorName,
29 									patch.AuthorEmail,
30-									patch.AuthorDate,
31+									timestamp,
32 								)
33 							}
34 							w.Flush()
M cmd/ssh/main.go
+1, -1
1@@ -9,7 +9,7 @@ import (
2 )
3 
4 func main() {
5-	fpath := flag.String("config", "git-pr.toml", "configuration toml file")
6+	fpath := flag.String("config", "example.toml", "configuration toml file")
7 	flag.Parse()
8 	opts := &slog.HandlerOptions{
9 		AddSource: true,
M cmd/web/main.go
+1, -1
1@@ -9,7 +9,7 @@ import (
2 )
3 
4 func main() {
5-	fpath := flag.String("config", "git-pr.toml", "configuration toml file")
6+	fpath := flag.String("config", "example.toml", "configuration toml file")
7 	flag.Parse()
8 	opts := &slog.HandlerOptions{
9 		AddSource: true,
A example.toml
+14, -0
 1@@ -0,0 +1,14 @@
 2+# url is used for help commands, exclude protocol
 3+url = "localhost"
 4+# where we store the sqlite db, this toml file, git repos, and ssh host keys
 5+data_dir = "./data"
 6+# this gives users the ability to submit reviews and other admin permissions
 7+admins = []
 8+# set datetime format for our clients
 9+time_format = "2006-01-02"
10+
11+# add as many repos as you want
12+[[repo]]
13+id = "test"
14+clone_addr = "https://github.com/picosh/test.git"
15+desc = "Test repo"
M tmpl/pr-detail.html
+1, -1
1@@ -53,7 +53,7 @@
2 
3         <div class="group-h text-sm">
4           <code class="pill{{if $val.Review}}-alert{{end}}">{{$val.AuthorName}} &lt;{{$val.AuthorEmail}}&gt;</code>
5-          <date>{{$val.AuthorDate}}</date>
6+          <date>{{$val.FormattedAuthorDate}}</date>
7         </div>
8       </div>
9 
M util.go
+16, -0
 1@@ -6,10 +6,12 @@ import (
 2 	"encoding/hex"
 3 	"fmt"
 4 	"io"
 5+	"log/slog"
 6 	"math/rand"
 7 	"regexp"
 8 	"strconv"
 9 	"strings"
10+	"time"
11 
12 	"github.com/bluekeyes/go-gitdiff/gitdiff"
13 	"github.com/charmbracelet/ssh"
14@@ -213,6 +215,20 @@ func calcContentSha(diffFiles []*gitdiff.File, header *gitdiff.PatchHeader) stri
15 	return shaStr
16 }
17 
18+func AuthorDateToTime(date string, logger *slog.Logger) time.Time {
19+	// TODO: convert sql column to DATETIME
20+	ds, err := time.Parse("2006-01-02T15:04:05Z", date)
21+	if err != nil {
22+		logger.Error(
23+			"cannot parse author date for patch",
24+			"datetime", date,
25+			"err", err,
26+		)
27+		return time.Now()
28+	}
29+	return ds
30+}
31+
32 /* func gitServiceCommands(sesh ssh.Session, be *Backend, cmd, repoName string) error {
33 	name := utils.SanitizeRepo(repoName)
34 	// git bare repositories should end in ".git"
M web.go
+8, -5
 1@@ -277,8 +277,9 @@ type PrData struct {
 2 
 3 type PatchData struct {
 4 	*Patch
 5-	Url     template.URL
 6-	DiffStr template.HTML
 7+	Url                 template.URL
 8+	DiffStr             template.HTML
 9+	FormattedAuthorDate string
10 }
11 
12 type EventLogData struct {
13@@ -341,10 +342,12 @@ func prDetailHandler(w http.ResponseWriter, r *http.Request) {
14 			return
15 		}
16 
17+		timestamp := AuthorDateToTime(patch.AuthorDate, web.Logger).Format(web.Backend.Cfg.TimeFormat)
18 		patchesData = append(patchesData, PatchData{
19-			Patch:   patch,
20-			Url:     template.URL(fmt.Sprintf("patch-%d", patch.ID)),
21-			DiffStr: template.HTML(diffStr),
22+			Patch:               patch,
23+			Url:                 template.URL(fmt.Sprintf("patch-%d", patch.ID)),
24+			DiffStr:             template.HTML(diffStr),
25+			FormattedAuthorDate: timestamp,
26 		})
27 	}
28