-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add
check-action-handlers
(#111)
* 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
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters