summaryrefslogtreecommitdiff
path: root/cmd/generate/gerrit.go
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-05-18 23:10:05 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-05-19 01:30:41 -0600
commit8b92fbf0062e1b4d6b579f4918bc67929925676e (patch)
tree8855ae7e346b9e232a3d07be2136d88cd00b765f /cmd/generate/gerrit.go
parentc0da2a0bdaf1caad67f67bccbd982e1d4ca0fdc4 (diff)
imworkingon: Add Gerrit last-updated detection
Diffstat (limited to 'cmd/generate/gerrit.go')
-rw-r--r--cmd/generate/gerrit.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/cmd/generate/gerrit.go b/cmd/generate/gerrit.go
new file mode 100644
index 0000000..c8837fc
--- /dev/null
+++ b/cmd/generate/gerrit.go
@@ -0,0 +1,54 @@
+package main
+
+import (
+ "encoding"
+ "encoding/json"
+ "fmt"
+ "strings"
+ "time"
+)
+
+// httpGetGerritJSON is like [httpGetJSON], but
+// https://gerrit-review.googlesource.com/Documentation/rest-api.html#output
+func httpGetGerritJSON(u string, out any) error {
+ str, err := httpGet(u)
+ if err != nil {
+ return err
+ }
+ if _, body, ok := strings.Cut(str, "\n"); ok {
+ str = body
+ }
+ return json.Unmarshal([]byte(str), out)
+}
+
+const GerritTimeFormat = "2006-01-02 15:04:05.000000000"
+
+type GerritTime struct {
+ Val time.Time
+}
+
+var (
+ _ fmt.Stringer = GerritTime{}
+ _ encoding.TextMarshaler = GerritTime{}
+ _ encoding.TextUnmarshaler = (*GerritTime)(nil)
+)
+
+// String implements [fmt.Stringer].
+func (t GerritTime) String() string {
+ return t.Val.Format(GerritTimeFormat)
+}
+
+// MarshalText implements [encoding.TextMarshaler].
+func (t GerritTime) MarshalText() ([]byte, error) {
+ return []byte(t.String()), nil
+}
+
+// UnmarshalText implements [encoding.TextUnmarshaler].
+func (t *GerritTime) UnmarshalText(data []byte) error {
+ val, err := time.Parse(GerritTimeFormat, string(data))
+ if err != nil {
+ return err
+ }
+ t.Val = val
+ return nil
+}