diff --git a/go.mod b/go.mod index abf0097..49b511a 100644 --- a/go.mod +++ b/go.mod @@ -2,43 +2,41 @@ module github.com/cdevents/gerrit-translator go 1.22.0 -replace github.com/cdevents/webhook-adapter => github.com/Nordix/webhook-cdevents-adapter v0.0.0-20240314095508-2cc2c286506d - require ( github.com/cdevents/sdk-go v0.3.2 - github.com/cdevents/webhook-adapter v0.0.0-00010101000000-000000000000 - github.com/hashicorp/go-plugin v1.6.0 + github.com/cdevents/webhook-adapter v0.0.0-20240319203345-6d6c6c54f065 + github.com/hashicorp/go-plugin v1.6.1 + go.uber.org/zap v1.27.0 ) require ( github.com/cloudevents/sdk-go/v2 v2.15.2 // indirect - github.com/fatih/color v1.16.0 // indirect + github.com/fatih/color v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.18.0 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/hashicorp/go-hclog v1.6.2 // indirect + github.com/hashicorp/go-hclog v0.14.1 // indirect github.com/hashicorp/yamux v0.1.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/leodido/go-urn v1.4.0 // indirect - github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mitchellh/go-testing-interface v1.14.1 // indirect + github.com/mattn/go-colorable v0.1.4 // indirect + github.com/mattn/go-isatty v0.0.10 // indirect + github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/oklog/run v1.1.0 // indirect + github.com/oklog/run v1.0.0 // indirect github.com/package-url/packageurl-go v0.1.2 // indirect github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 // indirect go.uber.org/multierr v1.11.0 // indirect - go.uber.org/zap v1.27.0 // indirect golang.org/x/crypto v0.19.0 // indirect golang.org/x/mod v0.15.0 // indirect golang.org/x/net v0.21.0 // indirect golang.org/x/sys v0.17.0 // indirect golang.org/x/text v0.14.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect google.golang.org/grpc v1.62.1 // indirect google.golang.org/protobuf v1.33.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 9b610be..1b91ee0 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,16 @@ -github.com/Nordix/webhook-cdevents-adapter v0.0.0-20240314095508-2cc2c286506d h1:Q9qrqKuFI5LkHHdDgc72CPqGHrjsXh2OB/Bnz8ZV9AU= -github.com/Nordix/webhook-cdevents-adapter v0.0.0-20240314095508-2cc2c286506d/go.mod h1:0Rhvovzk4lUTR65yAmxWJH2xmxl52TomCpA4HEn+0bU= github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/cdevents/sdk-go v0.3.2 h1:E9spqXsOCqSDJgKff7IS2pFFx479fC9ocrfBwCZyXJM= github.com/cdevents/sdk-go v0.3.2/go.mod h1:8bMgOeAIlKTnjGcafMRCKLatA1mEywd0LM6XqJ06eP8= +github.com/cdevents/webhook-adapter v0.0.0-20240319203345-6d6c6c54f065 h1:rQzuzOrMrdh6jnkOjL6V5Q37Gj62YvramIOwDPZf8XI= +github.com/cdevents/webhook-adapter v0.0.0-20240319203345-6d6c6c54f065/go.mod h1:0Rhvovzk4lUTR65yAmxWJH2xmxl52TomCpA4HEn+0bU= github.com/cloudevents/sdk-go/v2 v2.15.2 h1:54+I5xQEnI73RBhWHxbI1XJcqOFOVJN85vb41+8mHUc= github.com/cloudevents/sdk-go/v2 v2.15.2/go.mod h1:lL7kSWAE/V8VI4Wh0jbL2v/jvqsm6tjmaQBSvxcv4uE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= -github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= -github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= @@ -31,10 +30,10 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/hashicorp/go-hclog v1.6.2 h1:NOtoftovWkDheyUM/8JW3QMiXyxJK3uHRK7wV04nD2I= -github.com/hashicorp/go-hclog v1.6.2/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= -github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A= -github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI= +github.com/hashicorp/go-hclog v0.14.1 h1:nQcJDQwIAGnmoUWp8ubocEX40cCml/17YkF6csQLReU= +github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= +github.com/hashicorp/go-plugin v1.6.1 h1:P7MR2UP6gNKGPp+y7EZw2kOiq4IR9WiqLvp0XOsVdwI= +github.com/hashicorp/go-plugin v1.6.1/go.mod h1:XPHFku2tFo3o3QKFgSYo+cghcUhw1NA1hZyMK0PWAw0= github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c= @@ -45,17 +44,13 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= -github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= -github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= -github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU= -github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8= +github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10= +github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= +github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 h1:7GoSOOW2jpsfkntVKaS2rAr1TJqfcxotyaUcuxoZSzg= +github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -63,8 +58,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA= -github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU= +github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= +github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/package-url/packageurl-go v0.1.2 h1:0H2DQt6DHd/NeRlVwW4EZ4oEI6Bn40XlNPRqegcxuo4= github.com/package-url/packageurl-go v0.1.2/go.mod h1:uQd4a7Rh3ZsVg5j0lNyAfyxIeGde9yrlhjF78GzeW0c= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -72,8 +67,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4= github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= 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/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -90,13 +85,8 @@ golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= @@ -104,8 +94,8 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs= golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c h1:NUsgEN92SQQqzfA+YtqYNqYmB3DMMYLlIwUZAQFVFbo= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 h1:AjyfHzEPEFp/NpvfN5g+KDla3EMojjhRVZc1i7cj+oM= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s= google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk= google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= diff --git a/pkg/gerrit/constants.go b/pkg/gerrit/constants.go new file mode 100644 index 0000000..a7a52b7 --- /dev/null +++ b/pkg/gerrit/constants.go @@ -0,0 +1,15 @@ +package gerrit + +const ( + ProjectCreatedEventType = "project-created" + RefUpdatedEventType = "ref-updated" + ProjectUpdatedEventType = "project-head-updated" + PatchsetCreatedEventType = "patchset-created" + CommentAddedEventType = "comment-added" + ChangeMergedEventType = "change-merged" + ChangeAbandonedEventType = "change-abandoned" + + ZeroedSha = "0000000000000000000000000000000000000000" + + PatchSetCreatedNum = 1 +) diff --git a/pkg/gerrit/create_cdevents.go b/pkg/gerrit/create_cdevents.go index c057b64..218c721 100644 --- a/pkg/gerrit/create_cdevents.go +++ b/pkg/gerrit/create_cdevents.go @@ -100,3 +100,103 @@ func (refUpdated *RefUpdated) BranchDeletedCDEvent() (string, error) { } return cdEventStr, nil } + +func (patchsetCreated *PatchsetCreated) ChangeCreatedCDEvent() (string, error) { + Log().Info("Creating CDEvent ChangeCreatedEvent") + cdEvent, err := sdk.NewChangeCreatedEvent() + if err != nil { + Log().Error("Error creating CDEvent ChangeCreatedEvent %s\n", err) + return "", err + } + cdEvent.SetSource(patchsetCreated.RepoURL) + cdEvent.SetSubjectId(patchsetCreated.Change.Branch) + cdEvent.SetSubjectSource(patchsetCreated.Change.Url) + cdEvent.SetSubjectRepository(&sdk.Reference{Id: patchsetCreated.Project.Name}) + cdEventStr, err := sdk.AsJsonString(cdEvent) + if err != nil { + Log().Error("Error creating ChangeCreated CDEvent as Json string %s\n", err) + return "", err + } + + return cdEventStr, nil +} + +func (patchsetCreated *PatchsetCreated) ChangeUpdatedCDEvent() (string, error) { + Log().Info("Creating CDEvent ChangeUpdatedEvent") + cdEvent, err := sdk.NewChangeUpdatedEvent() + if err != nil { + Log().Error("Error creating CDEvent ChangeUpdatedEvent %s\n", err) + return "", err + } + cdEvent.SetSource(patchsetCreated.RepoURL) + cdEvent.SetSubjectId(patchsetCreated.Change.Branch) + cdEvent.SetSubjectSource(patchsetCreated.Change.Url) + cdEvent.SetSubjectRepository(&sdk.Reference{Id: patchsetCreated.Project.Name}) + cdEventStr, err := sdk.AsJsonString(cdEvent) + if err != nil { + Log().Error("Error creating ChangeUpdated CDEvent as Json string %s\n", err) + return "", err + } + + return cdEventStr, nil +} + +func (commentAdded *CommentAdded) ChangeReviewedCDEvent() (string, error) { + Log().Info("Creating CDEvent ChangeReviewedEvent") + cdEvent, err := sdk.NewChangeReviewedEvent() + if err != nil { + Log().Error("Error creating CDEvent ChangeReviewedEvent %s\n", err) + return "", err + } + cdEvent.SetSource(commentAdded.RepoURL) + cdEvent.SetSubjectId(commentAdded.Change.Branch) + cdEvent.SetSubjectSource(commentAdded.Change.Url) + cdEvent.SetSubjectRepository(&sdk.Reference{Id: commentAdded.Project.Name}) + cdEventStr, err := sdk.AsJsonString(cdEvent) + if err != nil { + Log().Error("Error creating ChangeReviewed CDEvent as Json string %s\n", err) + return "", err + } + + return cdEventStr, nil +} + +func (changeMerged *ChangeMerged) ChangeMergedCDEvent() (string, error) { + Log().Info("Creating CDEvent ChangeMergedEvent") + cdEvent, err := sdk.NewChangeMergedEvent() + if err != nil { + Log().Error("Error creating CDEvent ChangeMergedEvent %s\n", err) + return "", err + } + cdEvent.SetSource(changeMerged.RepoURL) + cdEvent.SetSubjectId(changeMerged.Change.Branch) + cdEvent.SetSubjectSource(changeMerged.Change.Url) + cdEvent.SetSubjectRepository(&sdk.Reference{Id: changeMerged.Project.Name}) + cdEventStr, err := sdk.AsJsonString(cdEvent) + if err != nil { + Log().Error("Error creating ChangeMerged CDEvent as Json string %s\n", err) + return "", err + } + + return cdEventStr, nil +} + +func (changeAbandoned *ChangeAbandoned) ChangeAbandonedCDEvent() (string, error) { + Log().Info("Creating CDEvent ChangeAbandonedEvent") + cdEvent, err := sdk.NewChangeAbandonedEvent() + if err != nil { + Log().Error("Error creating CDEvent ChangeAbandonedEvent %s\n", err) + return "", err + } + cdEvent.SetSource(changeAbandoned.RepoURL) + cdEvent.SetSubjectId(changeAbandoned.Change.Branch) + cdEvent.SetSubjectSource(changeAbandoned.Change.Url) + cdEvent.SetSubjectRepository(&sdk.Reference{Id: changeAbandoned.Project.Name}) + cdEventStr, err := sdk.AsJsonString(cdEvent) + if err != nil { + Log().Error("Error creating ChangeAbandoned CDEvent as Json string %s\n", err) + return "", err + } + + return cdEventStr, nil +} diff --git a/pkg/gerrit/event.go b/pkg/gerrit/event.go index e9ebf5a..8f99d70 100644 --- a/pkg/gerrit/event.go +++ b/pkg/gerrit/event.go @@ -34,7 +34,7 @@ func NewGerritEvent(event string, repoURL string) (pEvent *GerritEvent) { } func HandleTranslateGerritEvent(event string, header http.Header) (string, error) { - Log().Info("Handle translation into CDEvent from Gerrit event %s\n", event) + Log().Debug("Handle translation into CDEvent from Gerrit event %s\n", event) repoURL := "" if header.Get("X-Origin-Url") != "" { repoURL = header.Get("X-Origin-Url") @@ -45,7 +45,7 @@ func HandleTranslateGerritEvent(event string, header http.Header) (string, error Log().Error("Error translating Gerrit event into CDEvent %s\n", err) return "", err } - Log().Info("Gerrit Event translated into CDEvent %s\n", cdEvent) + Log().Debug("Gerrit Event translated into CDEvent %s\n", cdEvent) return cdEvent, nil } @@ -61,21 +61,41 @@ func (pEvent *GerritEvent) TranslateIntoCDEvent() (string, error) { Log().Info("handling translating to CDEvent from Gerrit Event type: %s\n", eventType) switch eventType { - case "project-created": + case ProjectCreatedEventType: cdEvent, err = pEvent.HandleProjectCreatedEvent() if err != nil { return "", err } - case "ref-updated": + case RefUpdatedEventType: cdEvent, err = pEvent.HandleRefUpdatedEvent() if err != nil { return "", err } - case "project-head-updated": + case ProjectUpdatedEventType: cdEvent, err = pEvent.HandleProjectHeadUpdatedEvent() if err != nil { return "", err } + case PatchsetCreatedEventType: + cdEvent, err = pEvent.HandlePatchsetCreatedEvent() + if err != nil { + return "", err + } + case CommentAddedEventType: + cdEvent, err = pEvent.HandleCommentAddedEvent() + if err != nil { + return "", err + } + case ChangeMergedEventType: + cdEvent, err = pEvent.HandleChangeMergedEvent() + if err != nil { + return "", err + } + case ChangeAbandonedEventType: + cdEvent, err = pEvent.HandleChangeAbandonedEvent() + if err != nil { + return "", err + } default: Log().Info("Not handling CDEvent translation for Gerrit event type: %s\n", eventMap["type"]) return "", fmt.Errorf("gerrit event type %s, not supported for translation", eventType) diff --git a/pkg/gerrit/event_test.go b/pkg/gerrit/event_test.go new file mode 100644 index 0000000..95b49dd --- /dev/null +++ b/pkg/gerrit/event_test.go @@ -0,0 +1,40 @@ +package gerrit + +import ( + "net/http" + "os" + "testing" +) + +func TestHandleTranslateProjectCreatedEvent(t *testing.T) { + event, err := os.ReadFile("testdata/project-created.json") + if err != nil { + t.Fatalf("Failed to read project-created.json file: %v", err) + } + headers := http.Header{} + headers.Set("X-Origin-Url", "http://gerrit.est.tech") + + cdEvent, err := HandleTranslateGerritEvent(string(event), headers) + if err != nil { + t.Errorf("Expected RepositoryCreated CDEvent to be successful.") + return + } + Log().Info("Handle project-created gerrit event into dev.cdevents.repository.created is successful ", cdEvent) +} + +func TestHandleTranslateProjectHeadUpdatedEvent(t *testing.T) { + + event, err := os.ReadFile("testdata/project-head-updated.json") + if err != nil { + t.Fatalf("Failed to read project-head-updated.json file: %v", err) + } + headers := http.Header{} + headers.Set("X-Origin-Url", "http://gerrit.est.tech") + + cdEvent, err := HandleTranslateGerritEvent(string(event), headers) + if err != nil { + t.Errorf("Expected RepositoryModified CDEvent to be successful.") + return + } + Log().Info("Handle project-head-updated gerrit event into dev.cdevents.repository.modified is successful ", cdEvent) +} diff --git a/pkg/gerrit/event_types.go b/pkg/gerrit/event_types.go index 0056952..76b9180 100644 --- a/pkg/gerrit/event_types.go +++ b/pkg/gerrit/event_types.go @@ -17,6 +17,8 @@ SPDX-License-Identifier: Apache-2.0 package gerrit +// event fields and structures + type CommonFields struct { Type string `json:"type"` EventCreatedOn float64 `json:"eventCreatedOn"` @@ -34,22 +36,103 @@ type RefUpdate struct { RefName string `json:"refName"` Project string `json:"project"` } +type PatchSet struct { + Number int `json:"number"` + Revision string `json:"revision"` + Parents []string `json:"parents"` + Ref string `json:"ref"` + Uploader Submitter `json:"uploader"` + CreatedOn float64 `json:"createdOn"` + Author Submitter `json:"author"` + Kind string `json:"kind"` + SizeInsertions int `json:"sizeInsertions"` + SizeDeletions int `json:"sizeDeletions"` +} + +type Change struct { + Project string `json:"project"` + Branch string `json:"branch"` + Id string `json:"id"` + Number int `json:"number"` + Subject string `json:"subject"` + Owner Submitter `json:"owner"` + Url string `json:"url"` + CommitMessage string `json:"commitMessage"` + CreatedOn float64 `json:"createdOn"` + Status string `json:"status"` +} + +type Project struct { + Name string `json:"name"` +} +type ChangeKey struct { + Key string `json:"key"` +} +type CommonChangeFields struct { + PatchSet PatchSet `json:"patchSet"` + Change Change `json:"change"` + Project Project `json:"project"` + RefName string `json:"refName"` + ChangeKey ChangeKey `json:"changeKey"` +} +type Approval struct { + Type string `json:"type"` + Description string `json:"description"` + Value string `json:"value"` +} // Gerrit event types +// ProjectCreated project-created gerrit event structure type ProjectCreated struct { ProjectName string `json:"projectName"` HeadName string `json:"headName"` CommonFields } + +// ProjectHeadUpdated project-head-updated gerrit event structure type ProjectHeadUpdated struct { ProjectName string `json:"projectName"` OldHead string `json:"oldHead"` NewHead string `json:"newHead"` CommonFields } + +// RefUpdated ref-updated gerrit event structure type RefUpdated struct { Submitter Submitter `json:"submitter"` RefUpdate RefUpdate `json:"refUpdate"` CommonFields } + +// PatchsetCreated patchset-created gerrit event structure +type PatchsetCreated struct { + Uploader Submitter `json:"submitter"` + CommonChangeFields + CommonFields +} + +// CommentAdded comment-added gerrit event structure +type CommentAdded struct { + Author Submitter `json:"author"` + Approvals []Approval `json:"approvals"` + Comment string `json:"comment"` + CommonChangeFields + CommonFields +} + +// ChangeMerged change-merged gerrit event structure +type ChangeMerged struct { + Submitter Submitter `json:"submitter"` + NewRev string `json:"newRev"` + CommonChangeFields + CommonFields +} + +// ChangeAbandoned change-abandoned gerrit event structure +type ChangeAbandoned struct { + Abandoner Submitter `json:"abandoner"` + Reason string `json:"reason"` + CommonChangeFields + CommonFields +} diff --git a/pkg/gerrit/testdata/project-created.json b/pkg/gerrit/testdata/project-created.json new file mode 100644 index 0000000..769fdf0 --- /dev/null +++ b/pkg/gerrit/testdata/project-created.json @@ -0,0 +1,6 @@ +{ + "projectName": "TestRepo3", + "headName": "refs/heads/master", + "type": "project-created", + "eventCreatedOn": 1700131789 +} \ No newline at end of file diff --git a/pkg/gerrit/testdata/project-head-updated.json b/pkg/gerrit/testdata/project-head-updated.json new file mode 100644 index 0000000..b5e5129 --- /dev/null +++ b/pkg/gerrit/testdata/project-head-updated.json @@ -0,0 +1,7 @@ +{ + "projectName": "TestRepo3", + "oldHead": "refs/heads/master", + "newHead": "refs/heads/main", + "type": "project-head-updated", + "eventCreatedOn": 1700148011 +} \ No newline at end of file diff --git a/pkg/gerrit/translate_events.go b/pkg/gerrit/translate_events.go index b0f73d9..b217aed 100644 --- a/pkg/gerrit/translate_events.go +++ b/pkg/gerrit/translate_events.go @@ -19,6 +19,8 @@ package gerrit import ( "encoding/json" + "errors" + "strings" ) func (pEvent *GerritEvent) HandleProjectCreatedEvent() (string, error) { @@ -28,7 +30,7 @@ func (pEvent *GerritEvent) HandleProjectCreatedEvent() (string, error) { Log().Error("Error occurred while Unmarshal GerritEvent into ProjectCreated struct", err) return "", err } - Log().Info("ProjectCreated GerritEvent received : ", projectCreated.ProjectName, projectCreated.HeadName, projectCreated.CommonFields.Type) + Log().Debug("ProjectCreated GerritEvent received : ", projectCreated.ProjectName, projectCreated.HeadName, projectCreated.CommonFields.Type) projectCreated.RepoURL = pEvent.RepoURL cdEvent, err := projectCreated.RepositoryCreatedCDEvent() if err != nil { @@ -45,7 +47,7 @@ func (pEvent *GerritEvent) HandleProjectHeadUpdatedEvent() (string, error) { Log().Error("Error occurred while Unmarshal GerritEvent into ProjectHeadUpdated struct", err) return "", err } - Log().Info("ProjectHeadUpdated GerritEvent received for project : ", projectHeadUpdated.ProjectName) + Log().Debug("ProjectHeadUpdated GerritEvent received for project : ", projectHeadUpdated.ProjectName) projectHeadUpdated.RepoURL = pEvent.RepoURL cdEvent, err := projectHeadUpdated.RepositoryModifiedCDEvent() if err != nil { @@ -63,21 +65,104 @@ func (pEvent *GerritEvent) HandleRefUpdatedEvent() (string, error) { Log().Error("Error occurred while Unmarshal GerritEvent into RefUpdated struct", err) return "", err } - Log().Info("RefUpdated GerritEvent received : ", refUpdated.RefUpdate.RefName, refUpdated.Submitter.Name, refUpdated.CommonFields.Type) + Log().Debug("RefUpdated GerritEvent received : ", refUpdated.RefUpdate.RefName, refUpdated.Submitter.Name, refUpdated.CommonFields.Type) refUpdated.RepoURL = pEvent.RepoURL - if refUpdated.RefUpdate.OldRev == "0000000000000000000000000000000000000000" { + if strings.Contains(refUpdated.RefUpdate.RefName, "refs/changes") { + Log().Debug("Ignoring handling ref-updated gerrit event as this is followed by patchset/change events: ", refUpdated) + return "", errors.New("ignoring translating ref-updated gerrit event") + } else if refUpdated.RefUpdate.OldRev == ZeroedSha { cdEvent, err = refUpdated.BranchCreatedCDEvent() if err != nil { return "", err } Log().Info("Translated ref-updated gerrit event into dev.cdevents.branch.created CDEvent: ", cdEvent) - } else if refUpdated.RefUpdate.NewRev == "0000000000000000000000000000000000000000" { + } else if refUpdated.RefUpdate.NewRev == ZeroedSha { cdEvent, err = refUpdated.BranchDeletedCDEvent() if err != nil { return "", err } Log().Info("Translated ref-updated gerrit event into dev.cdevents.branch.deleted CDEvent: ", cdEvent) + } else { + Log().Debug("Ignoring handling ref-updated gerrit event for refName : ", refUpdated.RefUpdate.RefName) + return "", errors.New("ignoring translating ref-updated gerrit event") } return cdEvent, nil } + +func (pEvent *GerritEvent) HandlePatchsetCreatedEvent() (string, error) { + cdEvent := "" + var patchsetCreated PatchsetCreated + err := json.Unmarshal([]byte(pEvent.Event), &patchsetCreated) + if err != nil { + Log().Error("Error occurred while Unmarshal GerritEvent into PatchsetCreated struct", err) + return "", err + } + Log().Debug("PatchsetCreated GerritEvent received for project : ", patchsetCreated.Project.Name) + patchsetCreated.RepoURL = pEvent.RepoURL + if patchsetCreated.PatchSet.Number == PatchSetCreatedNum { + cdEvent, err = patchsetCreated.ChangeCreatedCDEvent() + if err != nil { + return "", err + } + Log().Info("Translated patchset-created gerrit event into dev.cdevents.change.created CDEvent: ", cdEvent) + } else { + cdEvent, err = patchsetCreated.ChangeUpdatedCDEvent() + if err != nil { + return "", err + } + Log().Info("Translated patchset-created gerrit event into dev.cdevents.change.updated CDEvent: ", cdEvent) + } + + return cdEvent, nil +} + +func (pEvent *GerritEvent) HandleCommentAddedEvent() (string, error) { + var commentAdded CommentAdded + err := json.Unmarshal([]byte(pEvent.Event), &commentAdded) + if err != nil { + Log().Error("Error occurred while Unmarshal GerritEvent into CommentAdded struct", err) + return "", err + } + Log().Debug("CommentAdded GerritEvent received for project : ", commentAdded.Project.Name) + commentAdded.RepoURL = pEvent.RepoURL + cdEvent, err := commentAdded.ChangeReviewedCDEvent() + if err != nil { + return "", err + } + Log().Info("Translated comment-added gerrit event into dev.cdevents.change.reviewed CDEvent: ", cdEvent) + return cdEvent, nil +} +func (pEvent *GerritEvent) HandleChangeMergedEvent() (string, error) { + var changeMerged ChangeMerged + err := json.Unmarshal([]byte(pEvent.Event), &changeMerged) + if err != nil { + Log().Error("Error occurred while Unmarshal GerritEvent into ChangeMerged struct", err) + return "", err + } + Log().Debug("ChangeMerged GerritEvent received for project : ", changeMerged.Project.Name) + changeMerged.RepoURL = pEvent.RepoURL + cdEvent, err := changeMerged.ChangeMergedCDEvent() + if err != nil { + return "", err + } + Log().Info("Translated change-merged gerrit event into dev.cdevents.change.merged CDEvent: ", cdEvent) + return cdEvent, nil +} + +func (pEvent *GerritEvent) HandleChangeAbandonedEvent() (string, error) { + var changeAbandoned ChangeAbandoned + err := json.Unmarshal([]byte(pEvent.Event), &changeAbandoned) + if err != nil { + Log().Error("Error occurred while Unmarshal GerritEvent into ChangeAbandoned struct", err) + return "", err + } + Log().Debug("ChangeAbandoned GerritEvent received for project : ", changeAbandoned.Project.Name) + changeAbandoned.RepoURL = pEvent.RepoURL + cdEvent, err := changeAbandoned.ChangeAbandonedCDEvent() + if err != nil { + return "", err + } + Log().Info("Translated change-abandoned gerrit event into dev.cdevents.change.abandoned CDEvent: ", cdEvent) + return cdEvent, nil +}