Skip to content

Commit ea0e306

Browse files
protobuf: automate derived file generation
* Use the bufbuild/buf tool to: * Lint the protobuf schema. * Check for breaking changes. * Configure the generation of derived Python files. * Adds a GitHub action which automates the checking and generation of derived files and commits the results back on pull request branches.
1 parent a967c00 commit ea0e306

File tree

16 files changed

+238
-126
lines changed

16 files changed

+238
-126
lines changed

.codacy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
exclude_paths:
22
- 'etc/**'
33
- 'tests/**'
4-
- 'cylc/flow/**_pb2.py'
4+
- 'cylc/flow/network/protobuf/**_pb2.py'

.codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ coverage:
2828
# files to ignore
2929
ignore:
3030
- "tests/**"
31-
- "ws_messages_pb2.py"
31+
- "cylc/flow/network/protobuf/cylc/v5/schema_pb2.py"
3232
- "cylc/flow/scripts/report_timings.py"
3333

3434
flag_management:

.coveragerc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ disable_warnings =
1414
module-not-measured
1515
omit =
1616
tests/*
17-
*/cylc/flow/*_pb2.py
17+
*/cylc/flow/network/protobuf/cylc/v5/*_pb2.py
1818
cylc/flow/etc/*
1919
cylc/flow/scripts/report_timings.py
2020
parallel = True
@@ -43,7 +43,7 @@ fail_under=0
4343
ignore_errors = False
4444
omit =
4545
tests/*
46-
*/cylc/flow/*_pb2.py
46+
*/cylc/flow/network/protobuf/cylc/v5/*_pb2.py
4747
cylc/flow/etc/*
4848
precision = 2
4949
show_missing = False

.github/workflows/protobuf.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: protobuf
2+
# CI tests to run against the protobuf schema on change:
3+
4+
on:
5+
# run for any PRs raising changes to the protobuf files or setup
6+
pull_request:
7+
paths:
8+
- 'cylc/flow/network/protobuf/**'
9+
10+
jobs:
11+
protobuf:
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 10
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
with:
19+
# we need all of the commits on the PR branch in order to be able to add new ones
20+
fetch-depth: 100
21+
22+
- name: Configure git
23+
uses: cylc/release-actions/configure-git@v1
24+
25+
- name: Install Protobuf
26+
uses: mamba-org/setup-micromamba@v1
27+
with:
28+
# install protobuf into a mamba env (note use shell = "bash -el {0}"
29+
# to access this envionment)
30+
environment-name: protobuf
31+
create-args: protobuf
32+
init-shell: bash
33+
34+
- name: Install bufbuild/buf
35+
run: |
36+
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" # activate homebrew
37+
38+
# NOTE: bufbuild does exist on conda-forge but hasn't been updated for a while
39+
brew install bufbuild/buf/buf
40+
41+
- name: Lint
42+
run: |
43+
# lint .proto files
44+
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" # activate homebrew
45+
cd cylc/flow/network/protobuf
46+
buf lint
47+
48+
- name: Compatibility
49+
shell: bash -el {0}
50+
run: |
51+
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" # activate homebrew
52+
cd cylc/flow/network/protobuf
53+
# NOTE: there is currently no process for committing a breaking change.
54+
# If a breaking change is needed:
55+
# - Increment the Cylc API version number.
56+
# - Increment the protobuf schema version number to match.
57+
# - Increment the API number filter in cylc-uiserver.
58+
# - Bypass this step of the workflow.
59+
buf breaking \
60+
--against 'https://github.com/cylc/cylc-flow.git#tag=${{ github.base_ref }},subdir=cylc/flow/network/protobuf'
61+
62+
- name: Build
63+
shell: bash -el {0}
64+
run: |
65+
# generate .py and .pyi files from the .proto files
66+
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" # activate homebrew
67+
micromamba activate protobuf
68+
cd cylc/flow/network/protobuf
69+
buf generate
70+
71+
- name: Commit & Push
72+
run: |
73+
if [[ -z $(git diff --stat) ]]; then
74+
echo '::error:: No functional changes made to the protobuf schema'
75+
exit 0
76+
else
77+
echo '::info:: pushing update commit'
78+
git add -u
79+
git commit -m 'protobuf: updating generated files'
80+
git remote add pr https://github.com/${{ github.event.pull_request.head.repo.owner.login }}/cylc-flow
81+
git push pr HEAD:${{ github.head_ref }}
82+
exit 0
83+
fi

cylc/flow/data_messages_pb2.py

Lines changed: 0 additions & 100 deletions
This file was deleted.

cylc/flow/data_store_mgr.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373

7474
from cylc.flow import __version__ as CYLC_VERSION, LOG
7575
from cylc.flow.cycling.loader import get_point
76-
from cylc.flow.data_messages_pb2 import (
76+
from cylc.flow.network.protobuf.cylc.v5.schema_pb2 import (
7777
PbEdge, PbEntireWorkflow, PbFamily, PbFamilyProxy, PbJob, PbTask,
7878
PbTaskProxy, PbWorkflow, PbRuntime, AllDeltas, EDeltas, FDeltas,
7979
FPDeltas, JDeltas, TDeltas, TPDeltas, WDeltas)
@@ -382,7 +382,7 @@ def create_delta_store(delta=None, workflow_id=None):
382382
"""Create a mini data-store out of the all deltas message.
383383
384384
Args:
385-
delta (cylc.flow.data_messages_pb2.AllDeltas):
385+
delta (cylc.flow.v5.schema_pb2.AllDeltas):
386386
The message of accumulated deltas for publish/push.
387387
workflow_id (str):
388388
The workflow ID.
@@ -430,18 +430,18 @@ class DataStoreMgr:
430430
Local store of config.get_first_parent_ancestors()
431431
.data (dict):
432432
.edges (dict):
433-
cylc.flow.data_messages_pb2.PbEdge by internal ID.
433+
cylc.flow.v5.schema_pb2.PbEdge by internal ID.
434434
.families (dict):
435-
cylc.flow.data_messages_pb2.PbFamily by name (internal ID).
435+
cylc.flow.v5.schema_pb2.PbFamily by name (internal ID).
436436
.family_proxies (dict):
437-
cylc.flow.data_messages_pb2.PbFamilyProxy by internal ID.
437+
cylc.flow.v5.schema_pb2.PbFamilyProxy by internal ID.
438438
.jobs (dict):
439-
cylc.flow.data_messages_pb2.PbJob by internal ID.
439+
cylc.flow.v5.schema_pb2.PbJob by internal ID.
440440
.tasks (dict):
441-
cylc.flow.data_messages_pb2.PbTask by name (internal ID).
441+
cylc.flow.v5.schema_pb2.PbTask by name (internal ID).
442442
.task_proxies (dict):
443-
cylc.flow.data_messages_pb2.PbTaskProxy by internal ID.
444-
.workflow (cylc.flow.data_messages_pb2.PbWorkflow)
443+
cylc.flow.v5.schema_pb2.PbTaskProxy by internal ID.
444+
.workflow (cylc.flow.v5.schema_pb2.PbWorkflow)
445445
Message containing the global information of the workflow.
446446
.descendants (dict):
447447
Local store of config.get_first_parent_descendants()
@@ -2688,7 +2688,7 @@ def get_entire_workflow(self):
26882688
"""Gather data elements into single Protobuf message.
26892689
26902690
Returns:
2691-
cylc.flow.data_messages_pb2.PbEntireWorkflow
2691+
cylc.flow.v5.schema_pb2.PbEntireWorkflow
26922692
26932693
"""
26942694

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# file containing protobuf configuration
2+
3+
version: v2
4+
5+
plugins:
6+
- protoc_builtin: python
7+
out: .
8+
9+
- protoc_builtin: pyi
10+
out: .
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# file containing protobuf configuration
2+
3+
version: v2
4+
5+
lint:
6+
use:
7+
- DEFAULT
8+
9+
breaking:
10+
use:
11+
- FILE

cylc/flow/data_messages.proto renamed to cylc/flow/network/protobuf/cylc/v5/schema.proto

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
syntax = "proto3";
22

3+
package cylc.v5;
4+
35
/* THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
46
Copyright (C) NIWA & British Crown (Met Office) & Contributors.
57
@@ -16,6 +18,7 @@ syntax = "proto3";
1618
You should have received a copy of the GNU General Public License
1719
along with this program. If not, see <http://www.gnu.org/licenses/>.*/
1820

21+
1922
/* Protobuf message definitions
2023
*
2124
* The original intention of these messages is for use as data elements sent
@@ -24,15 +27,21 @@ syntax = "proto3";
2427
* This file is not needed at runtime. It is used to generate python protobuf
2528
* message modules.
2629
*
27-
* Command:
28-
* $ protoc -I=./ --python_out=./ --pyi_out=./ data_messages.proto
30+
* Hand edit this file to make changes to the schema, use "bufbuild" to lint
31+
* changes and regenerate Python files e.g:
2932
*
30-
* Pre-compiled protoc binary may be download from:
31-
* https://github.com/protocolbuffers/protobuf/releases
33+
* # cd into the directory which contains the "buf.yaml" file, this is the
34+
* # project's root directory as far as protobuf is concerned
35+
* $ cd cylc/flow/network/protobuf
3236
*
33-
* If merge/rebase conflicts arise, then regenerate the module.
34-
* (DO NOT manually resolve conflicts)
37+
* # install bufbuild
38+
* $ npm install @bufbuild/buf
3539
*
40+
* # lint protobuf schema source files
41+
* node_modules/@bufbuild/buf/bin/buf lint
42+
*
43+
* # generate Python files
44+
* node_modules/@bufbuild/buf/bin/buf generate
3645
*
3746
* WARNING: Avoid re-indexing existing fields!
3847
* - Field numbers do not need to be continuous/sequential (gaps are fine).
@@ -43,14 +52,14 @@ syntax = "proto3";
4352
*
4453
* https://developers.google.com/protocol-buffers/docs/proto3#assigning_field_numbers
4554
*
46-
*
4755
* */
4856

4957

5058
// Query type messages
5159
message PbMeta {
5260
optional string title = 1;
5361
optional string description = 2;
62+
// buf:lint:ignore FIELD_LOWER_SNAKE_CASE
5463
optional string URL = 3;
5564
optional string user_defined = 4;
5665
}

cylc/flow/network/protobuf/cylc/v5/schema_pb2.py

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)