- commit
- c2df660
- parent
- adca693
- author
- jolheiser
- date
- 2025-08-21 22:55:30 -0400 EDT
strongly type event data Signed-off-by: jolheiser <git@jolheiser.com>
3 files changed,
+45,
-4
+38,
-1
1@@ -2,6 +2,9 @@ package git
2
3 import (
4 "database/sql"
5+ "database/sql/driver"
6+ "encoding/json"
7+ "fmt"
8 "time"
9
10 "github.com/bluekeyes/go-gitdiff/gitdiff"
11@@ -97,6 +100,40 @@ type EventLog struct {
12 PatchRequestID sql.NullInt64 `db:"patch_request_id"`
13 PatchsetID sql.NullInt64 `db:"patchset_id"`
14 Event string `db:"event"`
15- Data string `db:"data"`
16 CreatedAt time.Time `db:"created_at"`
17+ Data EventData `db:"data"`
18+}
19+
20+type EventData struct {
21+ Name string `json:"name,omitempty"`
22+ Status Status `json:"status,omitempty"`
23+}
24+
25+func (e EventData) String() string {
26+ b, _ := json.Marshal(e)
27+ bs := string(b)
28+ if bs == "{}" {
29+ return ""
30+ }
31+ return bs
32+}
33+
34+func (e *EventData) Scan(value any) error {
35+ if value == nil {
36+ return nil
37+ }
38+ var bytes []byte
39+ switch v := value.(type) {
40+ case []byte:
41+ bytes = v
42+ case string:
43+ bytes = []byte(v)
44+ default:
45+ return fmt.Errorf("cannot scan %T into EventData", value)
46+ }
47+ return json.Unmarshal(bytes, e)
48+}
49+
50+func (e EventData) Value() (driver.Value, error) {
51+ return json.Marshal(e)
52 }
M
pr.go
+6,
-2
1@@ -322,7 +322,9 @@ func (cmd PrCmd) UpdatePatchRequestStatus(prID int64, userID int64, status Statu
2 RepoID: sql.NullInt64{Int64: pr.RepoID, Valid: true},
3 PatchRequestID: sql.NullInt64{Int64: prID, Valid: true},
4 Event: "pr_status_changed",
5- Data: fmt.Sprintf(`{"status":"%s"}`, status),
6+ Data: EventData{
7+ Status: status,
8+ },
9 })
10 if err != nil {
11 return err
12@@ -364,7 +366,9 @@ func (cmd PrCmd) UpdatePatchRequestName(prID int64, userID int64, name string) e
13 RepoID: sql.NullInt64{Int64: pr.RepoID, Valid: true},
14 PatchRequestID: sql.NullInt64{Int64: prID, Valid: true},
15 Event: "pr_name_changed",
16- Data: fmt.Sprintf(`{"name":"%s"}`, name),
17+ Data: EventData{
18+ Name: name,
19+ },
20 })
21 if err != nil {
22 return err
+1,
-1
1@@ -42,7 +42,7 @@
2 {{end}}
3 </span>
4 <span>on <date>{{.Date}}</date></span>
5- {{if .Data}}<code>{{.Data}}</code>{{end}}
6+ {{if .Data.String}}<code>{{.Data}}</code>{{end}}
7 </div>
8 {{end}}
9 </div>