fix(ops/gerrit-webhook-to-irccat): fix json parsing
patchset and change numbers are numbers in the JSON, trying to deserialize them into a string fails: Jul 28 20:34:03 gerrit01 gerrit-webhook-to-irccat[2430441]: time=2025-07-28T20:34:03.675Z level=WARN msg="failed to parse body" error="json: cannot unmarshal number into Go struct field PatchSet.patchSet.number of type string" Update our library dependency to a version containing the fixed types. Also add tests for the three messages we care about. Change-Id: Iac44e6ad01a8590b4cafa4d301c9f45000f335d0 Reviewed-on: https://cl.snix.dev/c/snix/+/30624 Tested-by: besadii Reviewed-by: Ryan Lahfa <ryan@lahfa.xyz> Reviewed-by: Jade Lovelace <jade@lix.systems> Autosubmit: Florian Klink <flokli@flokli.de> Reviewed-by: Florian Klink <flokli@flokli.de>
This commit is contained in:
parent
a1aa231b3f
commit
723ef9146b
5 changed files with 54 additions and 5 deletions
|
|
@ -10,5 +10,5 @@ pkgs.buildGoModule {
|
|||
./go.sum
|
||||
];
|
||||
};
|
||||
vendorHash = "sha256-Xq0p6EEPFS23H+RMkzQw6767d8WujAz7doR6E/YKrgY=";
|
||||
vendorHash = "sha256-i7EpH9/2FGZwhn4VHP32aL13M07fd+cbUhzddYi5XfE=";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,11 +10,15 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.10.0 // indirect
|
||||
go.opentelemetry.io/otel v1.29.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.29.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
// Use the different data types sent in streams
|
||||
// https://github.com/andygrunwald/go-gerrit/pull/189
|
||||
replace github.com/andygrunwald/go-gerrit v1.0.0 => github.com/flokli/go-gerrit v0.0.0-20250523210852-9e357d872ff3
|
||||
replace github.com/andygrunwald/go-gerrit => github.com/flokli/go-gerrit v0.0.0-20250728210913-fadc61de9b89
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8
|
|||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/flokli/go-gerrit v0.0.0-20250523210852-9e357d872ff3 h1:bkCL+U77VI69eCzZjBszrtl4ytCJcWZdkZeKfiNlz3o=
|
||||
github.com/flokli/go-gerrit v0.0.0-20250523210852-9e357d872ff3/go.mod h1:SeP12EkHZxEVjuJ2HZET304NBtHGG2X6w2Gzd0QXAZw=
|
||||
github.com/flokli/go-gerrit v0.0.0-20250728210913-fadc61de9b89 h1:vuwMHbg/N5rwilamDH5CiUtIKzTXMECDeUPFUvryjsw=
|
||||
github.com/flokli/go-gerrit v0.0.0-20250728210913-fadc61de9b89/go.mod h1:SeP12EkHZxEVjuJ2HZET304NBtHGG2X6w2Gzd0QXAZw=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
|
|
@ -17,6 +17,8 @@ github.com/samber/slog-http v1.6.0 h1:+rD5QtOWGTcFT7jq8Yf0EgGy87krv0pcgh9jtWkrqj
|
|||
github.com/samber/slog-http v1.6.0/go.mod h1:PAcQQrYFo5KM7Qbk50gNNwKEAMGCyfsw6GN5dI0iv9g=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw=
|
||||
go.opentelemetry.io/otel v1.29.0/go.mod h1:N/WtXPs1CNCUEx+Agz5uouwCba+i+bJGFicT8SR4NP8=
|
||||
go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4=
|
||||
|
|
@ -24,5 +26,6 @@ go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+M
|
|||
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
|
||||
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ import (
|
|||
|
||||
var logger *slog.Logger
|
||||
var tmplStr = `{{- if eq .Type "patchset-created" -}}
|
||||
{{- if (and (eq .PatchSet.Number "1") (eq .Change.Wip false) ) -}}
|
||||
{{- if (and (eq .PatchSet.Number 1) (eq .Change.Wip false) ) -}}
|
||||
#snix CL/{{.Change.Number}} proposed by {{.Change.Owner.Username}} - {{.Change.Subject}} - {{.Change.URL}}
|
||||
{{- end -}}
|
||||
{{- else if eq .Type "change-merged" -}}
|
||||
|
|
|
|||
42
ops/gerrit-webhook-to-irccat/main_test.go
Normal file
42
ops/gerrit-webhook-to-irccat/main_test.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
gerritStreams "github.com/andygrunwald/go-gerrit/streams"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var jsonMsgs = []struct {
|
||||
in string
|
||||
out string
|
||||
}{
|
||||
{`{"uploader":{"name":"Florian Klink","email":"flokli@flokli.de","username":"flokli"},"patchSet":{"number":1,"revision":"e135bd7a83994d008b587e166186a95e200dd4b7","parents":["be68d899017ff0d79aa7bc9167381dba131342e0"],"ref":"refs/changes/23/30623/1","uploader":{"name":"Florian Klink","email":"flokli@flokli.de","username":"flokli"},"createdOn":1753736174,"author":{"name":"Florian Klink","email":"flokli@flokli.de","username":"flokli"},"kind":"REWORK","sizeInsertions":17,"sizeDeletions":0},"change":{"project":"snix","branch":"canon","id":"If9e2ca897e52ae9aa4da033b52dee6f17e66f636","number":30623,"subject":"feat(ops/machines/*01): install kitty terminfo","owner":{"name":"Florian Klink","email":"flokli@flokli.de","username":"flokli"},"url":"https://cl.snix.dev/c/snix/+/30623","commitMessage":"feat(ops/machines/*01): install kitty terminfo\n\nChange-Id: If9e2ca897e52ae9aa4da033b52dee6f17e66f636\n","createdOn":1753736174,"status":"NEW"},"project":"snix","refName":"refs/heads/canon","changeKey":{"id":"If9e2ca897e52ae9aa4da033b52dee6f17e66f636"},"type":"patchset-created","eventCreatedOn":1753736174}`,
|
||||
`#snix CL/30623 proposed by flokli - feat(ops/machines/*01): install kitty terminfo - https://cl.snix.dev/c/snix/+/30623`},
|
||||
{"{\"submitter\":{\"name\":\"Jade Lovelace\",\"email\":\"jade@lix.systems\",\"username\":\"jade\"},\"newRev\":\"be68d899017ff0d79aa7bc9167381dba131342e0\",\"patchSet\":{\"number\":2,\"revision\":\"be68d899017ff0d79aa7bc9167381dba131342e0\",\"parents\":[\"afec95e269e2d59e10fb205eb793d7261b638dc2\"],\"ref\":\"refs/changes/05/30605/2\",\"uploader\":{\"name\":\"Jade Lovelace\",\"email\":\"jade@lix.systems\",\"username\":\"jade\"},\"createdOn\":1753734843,\"author\":{\"name\":\"Jade Lovelace\",\"email\":\"jade@lix.systems\",\"username\":\"jade\"},\"kind\":\"NO_CODE_CHANGE\",\"sizeInsertions\":26,\"sizeDeletions\":1},\"change\":{\"project\":\"snix\",\"branch\":\"canon\",\"id\":\"I65b14839a62c4e779136c1c34750d15cedaaddc8\",\"number\":30605,\"subject\":\"fix(ops/gerrit): send mail with the triggering user\\u0027s name on it\",\"owner\":{\"name\":\"Jade Lovelace\",\"email\":\"jade@lix.systems\",\"username\":\"jade\"},\"url\":\"https://cl.snix.dev/c/snix/+/30605\",\"commitMessage\":\"fix(ops/gerrit): send mail with the triggering user\\u0027s name on it\\n\\nWe found this bug in Lix\\u0027s config and noticed Snix had the same bug; see\\nhttps://git.lix.systems/the-distro/infra/commit/4b9e84fa0a7390b05bfaf82739173875dfe16b66\\nand https://git.lix.systems/the-distro/infra/commit/b47965fe8f472e146d21995db1e852a22f5fa673.\\n\\nChange-Id: I65b14839a62c4e779136c1c34750d15cedaaddc8\\nReviewed-on: https://cl.snix.dev/c/snix/+/30605\\nTested-by: besadii\\nReviewed-by: Florian Klink \\u003cflokli@flokli.de\\u003e\\n\",\"createdOn\":1753663580,\"status\":\"MERGED\"},\"project\":{\"name\":\"snix\"},\"refName\":\"refs/heads/canon\",\"changeKey\":{\"key\":\"I65b14839a62c4e779136c1c34750d15cedaaddc8\"},\"type\":\"change-merged\",\"eventCreatedOn\":1753734843}",
|
||||
"#snix CL/30605 applied by jade - fix(ops/gerrit): send mail with the triggering user's name on it - https://cl.snix.dev/c/snix/+/30605"},
|
||||
{"{\"submitter\":{\"name\":\"clbot\",\"username\":\"clbot\"},\"newRev\":\"afec95e269e2d59e10fb205eb793d7261b638dc2\",\"patchSet\":{\"number\":2,\"revision\":\"099f14194bcee28133af01a6e324b2fe4ecccd7c\",\"parents\":[\"9942014989bcc56b1a302ef6eb2b9f2e2aecccb8\"],\"ref\":\"refs/changes/99/30599/2\",\"uploader\":{\"name\":\"clbot\",\"username\":\"clbot\"},\"createdOn\":1751906446,\"author\":{\"name\":\"Florian Klink\",\"email\":\"flokli@flokli.de\",\"username\":\"flokli\"},\"kind\":\"NO_CODE_CHANGE\",\"sizeInsertions\":20,\"sizeDeletions\":0},\"change\":{\"project\":\"snix\",\"branch\":\"canon\",\"id\":\"I449101cfda7c8a728d3e2f3be71dee00be780ec3\",\"number\":30599,\"subject\":\"doc(README): point to website once more\",\"owner\":{\"name\":\"Florian Klink\",\"email\":\"flokli@flokli.de\",\"username\":\"flokli\"},\"url\":\"https://cl.snix.dev/c/snix/+/30599\",\"commitMessage\":\"doc(README): point to website once more\\n\\nThere\\u0027s no point in replicating redundant information in many different\\nplaces. Add some more pointers and encouragement to read the docs where\\nthey live.\\n\\nChange-Id: I449101cfda7c8a728d3e2f3be71dee00be780ec3\\nReviewed-on: https://cl.snix.dev/c/snix/+/30599\\nTested-by: besadii\\nReviewed-by: Vova Kryachko \\u003cv.kryachko@gmail.com\\u003e\\nAutosubmit: Florian Klink \\u003cflokli@flokli.de\\u003e\\n\",\"createdOn\":1751899755,\"status\":\"MERGED\"},\"project\":{\"name\":\"snix\"},\"refName\":\"refs/heads/canon\",\"changeKey\":{\"key\":\"I449101cfda7c8a728d3e2f3be71dee00be780ec3\"},\"type\":\"change-merged\",\"eventCreatedOn\":1751906446}",
|
||||
"#snix CL/30599 by flokli autosubmitted - doc(README): point to website once more - https://cl.snix.dev/c/snix/+/30599"},
|
||||
}
|
||||
|
||||
func TestParseChangeProposed(t *testing.T) {
|
||||
for _, tt := range jsonMsgs {
|
||||
var event gerritStreams.Event
|
||||
err := json.Unmarshal([]byte(tt.in), &event)
|
||||
require.NoError(t, err, "json unmarshal")
|
||||
|
||||
var msg bytes.Buffer
|
||||
err = tmpl.Execute(&msg, event)
|
||||
require.NoError(t, err, "tmpl execute")
|
||||
|
||||
msgStr := strings.TrimSpace(msg.String())
|
||||
|
||||
assert.Equal(t, tt.out, msgStr)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue