Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check_protos workflow to validate that all .proto files in the repo are valid #2

Merged
merged 15 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/check_protos/_check_protos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
"""Generates gRPC Python stubs from proto files."""

import pathlib
import sys
import grpc_tools.protoc
import pkg_resources

PROTO_ROOT_PATH = pathlib.Path(__file__).parent.parent.parent
PROTO_PATH = PROTO_ROOT_PATH / "ni" / "measurementlink"
PROTO_FILES = list(PROTO_PATH.rglob("*.proto"))
GRPC_DEVICE_PROTO_PATH = PROTO_ROOT_PATH / "ni" / "grpcdevice" / "v1"

def main():
# Generate python files from .proto files with protoc.
arguments = [
"protoc",
f"--proto_path={str(PROTO_ROOT_PATH)}",
f"--proto_path={str(GRPC_DEVICE_PROTO_PATH)}",
f"--proto_path={pkg_resources.resource_filename('grpc_tools', '_proto')}",
f"--python_out={str(PROTO_PATH)}",
f"--grpc_python_out={str(PROTO_PATH)}",
]

arguments += [str(path.relative_to(PROTO_ROOT_PATH)).replace("\\", "/") for path in PROTO_FILES]
print("::group::Arguments for protoc")
for argument in arguments:
print(argument)
print("::endgroup::")

result = grpc_tools.protoc.main(arguments)
if result != 0:
sys.exit(result)

if __name__ == "__main__":
main()
208 changes: 208 additions & 0 deletions .github/check_protos/poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .github/check_protos/poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[virtualenvs]
in-project = true
19 changes: 19 additions & 0 deletions .github/check_protos/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.poetry]
name = "check_protos"
version = "0.1"
description = "Check .proto files in the repo"
authors = ["NI <[email protected]>"]
readme = "README.md" # apply the repo readme to the package as well
repository = "https://github.com/ni/ni-apis/"
license = "MIT"

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.group.dev.dependencies]
grpcio-tools = ">=1.59.0"
mypy-protobuf = ">=3.4"

[build-system]
requires = ["poetry-core>=1.2.0"]
build-backend = "poetry.core.masonry.api"
31 changes: 31 additions & 0 deletions .github/workflows/check_protos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Check .proto files

on:
workflow_call:
workflow_dispatch:

env:
POETRY_VERSION: 1.2.2
PYTHON_VERSION: 3.9

jobs:
check_protos:
name: Check .proto files
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Set up Poetry
uses: Gr1N/setup-poetry@v8
with:
poetry-version: ${{ env.POETRY_VERSION }}
- name: Install the dependencies for checking .proto files
run: poetry install -v
working-directory: ./.github/check_protos
- name: Check .proto files
run: poetry run python _check_protos.py
working-directory: ./.github/check_protos
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: CI

on:
workflow_call:
workflow_dispatch:

jobs:
check_protos:
name: Check .proto files
uses: ./.github/workflows/check_protos.yml
13 changes: 13 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: PR

on:
pull_request:
branches:
- main
workflow_call:
workflow_dispatch:

jobs:
check_protos:
name: Check .proto files
uses: ./.github/workflows/check_protos.yml
13 changes: 13 additions & 0 deletions ni/grpcdevice/v1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# NI gRPC Device Server APIs

These are APIs for [NI gRPC Device Server](https://github.com/ni/grpc-device/).

These APIs predate the NI gRPC Protocol Buffer Style Guide, so they do not
follow our current conventions and style for `.proto` files. For example, the
files are not organized by package name, and their package names are in a flat
namespace (`nidevice_grpc`, `nidcpower_grpc`, etc.) as opposed to a hierarchy
under the `ni` package.

When you run `protoc` on these APIs or other APIs that depend on them, add
`ni/grpcdevice/v1` to the include path in order to satisfy import directives
such as `import "session.proto"`.
18 changes: 18 additions & 0 deletions ni/grpcdevice/v1/session.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.ni.grpc.device";
option java_outer_classname = "NiDevice";
option csharp_namespace = "NationalInstruments.Grpc.Device";

package nidevice_grpc;

enum SessionInitializationBehavior {
SESSION_INITIALIZATION_BEHAVIOR_UNSPECIFIED = 0;
SESSION_INITIALIZATION_BEHAVIOR_INITIALIZE_NEW = 1;
SESSION_INITIALIZATION_BEHAVIOR_ATTACH_TO_EXISTING = 2;
}

message Session {
string name = 1;
}