Skip to content

Commit

Permalink
ci: add check-action-handlers (#111)
Browse files Browse the repository at this point in the history
* feat: add `check-action-handers` to ensure all handlers are in place

* ci(wardend): add `check-action-handlers` to the lint stage
  • Loading branch information
Pitasi committed Mar 21, 2024
1 parent 4003bb1 commit 1e02be6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/wardend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
with:
version: v1.56
args: --timeout=10m ${{ env.modules }}
- name: check-action-handlers
run: go run ./cmd/check-action-handlers

unit-test:
runs-on: ubuntu-latest
Expand Down
96 changes: 96 additions & 0 deletions cmd/check-action-handlers/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"fmt"
"os"
"path"
"strings"

"github.com/yoheimuta/go-protoparser/v4"
"github.com/yoheimuta/go-protoparser/v4/parser"
)

var ProtoFiles = []Target{
{
ProtoFile: "proto/warden/warden/v1beta2/tx.proto",
ModuleFile: "warden/x/warden/keeper/msg_server.go",
},
}

type Target struct {
ProtoFile string
ModuleFile string
}

func main() {
var isMissing bool
for _, target := range ProtoFiles {
f := target.ProtoFile
msgTypes, err := findMessageTypes(f)
if err != nil {
panic(err)
}

goSrcBytes, err := os.ReadFile(target.ModuleFile)
if err != nil {
panic(err)
}
goSrc := string(goSrcBytes)

for _, msgType := range msgTypes {
if !strings.Contains(goSrc, msgType) {
isMissing = true
fmt.Printf("Not found: %s in %s\n", msgType, target.ModuleFile)
}
}
}

if isMissing {
panic("You have proto RPCs that return an intent.MsgActionCreated but not registered in the server code.")
}
}

func findMessageTypes(file string) ([]string, error) {
var res []string
reader, err := os.Open(file)
if err != nil {
return nil, err
}
defer reader.Close()

got, err := protoparser.Parse(reader)
if err != nil {
return nil, err
}

for _, item := range got.ProtoBody {
switch item := item.(type) {
case *parser.Service:
{
for _, serviceItem := range item.ServiceBody {
switch serviceItem := serviceItem.(type) {
case *parser.RPC:
if isReturningAction(serviceItem) {
reqType := serviceItem.RPCRequest.MessageType
res = append(res, buildTypeURL(file, reqType))
}
}
}
}
}
}

return res, nil
}

func isReturningAction(rpc *parser.RPC) bool {
return rpc.RPCResponse.MessageType == "intent.MsgActionCreated"
}

func buildTypeURL(file string, messageType string) string {
p := path.Dir(file)
p = strings.TrimPrefix(p, "proto/")
p = strings.ReplaceAll(p, "/", ".")

return fmt.Sprintf("/%s.%s", p, messageType)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ require (
github.com/tidwall/btree v1.7.0 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
github.com/yoheimuta/go-protoparser/v4 v4.9.0 // indirect
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,8 @@ github.com/vbatts/tar-split v0.11.5 h1:3bHCTIheBm1qFTcgh9oPu+nNBtX+XJIupG/vacinC
github.com/vbatts/tar-split v0.11.5/go.mod h1:yZbwRsSeGjusneWgA781EKej9HF8vme8okylkAeNKLk=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yoheimuta/go-protoparser/v4 v4.9.0 h1:zHRXzRjkOamwMkPu7bpiCtOpxHkM9c8zxQOvW99eWlo=
github.com/yoheimuta/go-protoparser/v4 v4.9.0/go.mod h1:AHNNnSWnb0UoL4QgHPiOAg2BniQceFscPI5X/BZNHl8=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down

0 comments on commit 1e02be6

Please sign in to comment.