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 workflow for detecting breaking protos #7

Merged
merged 8 commits into from
Jun 13, 2024
Merged
Changes from 7 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
98 changes: 98 additions & 0 deletions .github/workflows/proto_breaking.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Identify Breaking Proto Changes

on:
workflow_call:

jobs:
proto_breaking:
name: Identify Breaking Proto Changes
runs-on: ubuntu-latest
env:
BAZEL: bazelisk-linux-amd64
BAZELISK_VERSION: v1.19.0

steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install bazelisk
run: |
curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/${BAZELISK_VERSION}/${BAZEL}"
chmod +x "${BAZEL}"
sudo mv "${BAZEL}" /usr/local/bin/bazel
- name: Mount bazel cache
uses: actions/cache@v4
with:
# See https://docs.bazel.build/versions/master/output_directories.html
path: "~/.cache/bazel"
# Create a new cache entry whenever Bazel files change.
# See https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows
key: bazel-${{ runner.os }}-build-${{ hashFiles('**/*.bzl', '**/*.bazel') }}
restore-keys: bazel-${{ runner.os }}-build-
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: 3.11
- name: Compare HEAD and BASE for breaking protobuf changes
run: |
python3 -m venv .venv
source .venv/bin/activate
pip install git+https://github.com/googleapis/[email protected]
HEAD="${{ github.event.pull_request.head.sha }}"
BASE="$(git merge-base origin/main "${HEAD}")"
TMPDIR="$(mktemp -d -t compare-XXXX)"
TMPDIRBASE="${TMPDIR}/base"
TMPDIRHEAD="${TMPDIR}/head"

extract_protodesc() {
local TMPTARGETDIR
local TARGETS
local GENF
local TARGETDIR
local PROTONAME
TMPTARGETDIR="${1}"
TARGETS="$(bazel query 'attr("generator_function", "^proto_library$", "//...")' 2>/dev/null)"
GENF="$(bazel info bazel-genfiles)"
for TARGET in ${TARGETS}; do
TARGETDIR="$(echo "${TARGET}" | cut -d ":" -f1 | sed 's#^//##')"
PROTONAME="$(echo "${TARGET}" | cut -d ":" -f2-)"
mkdir -p "${TMPTARGETDIR}/${TARGETDIR}"
bazel build "${TARGET}"
cp "${GENF}/${TARGETDIR}/${PROTONAME}-descriptor-set.proto.bin" \
"${TMPTARGETDIR}/${TARGETDIR}/${PROTONAME}-descriptor-set.proto.bin"
done
}

bazel clean
extract_protodesc "${TMPDIRHEAD}"
git checkout "${BASE}"
bazel clean
BASETARGETS="$(bazel query 'attr("generator_function", "^proto_library$", "//...")' 2>/dev/null)"
extract_protodesc "${TMPDIRBASE}"

ERROR=false
for TARGET in ${BASETARGETS}; do
TARGETDIR="$(echo "${TARGET}" | cut -d ":" -f1 | sed 's#^//##')"
PROTONAME="$(echo "${TARGET}" | cut -d ":" -f2-)"
BASEPROTODESC="${TMPDIRBASE}/${TARGETDIR}/${PROTONAME}-descriptor-set.proto.bin"
HEADPROTODESC="${TMPDIRHEAD}/${TARGETDIR}/${PROTONAME}-descriptor-set.proto.bin"
if [ -f "${BASEPROTODESC}" ] && [ -f "${HEADPROTODESC}" ]; then
echo "Comparing build target ${TARGET} at HEAD and BASE..."
OUTPUT=$(proto-breaking-change-detector --original_descriptor_set_file_path="${BASEPROTODESC}" \
bstoll marked this conversation as resolved.
Show resolved Hide resolved
--update_descriptor_set_file_path="${HEADPROTODESC}" --human_readable_message 2>&1)
if [ "${OUTPUT}" == "" ]; then
echo "No breaking changes."
else
echo -e "${OUTPUT}"
ERROR=true
fi
fi
if [ -f "${BASEPROTODESC}" ] && ! [ -f "${HEADPROTODESC}" ]; then
ERROR=true
echo "${BASEPROTODESC} exists and ${HEADPROTODESC} does not: removing a proto library is breaking."
fi
done
if [ "${ERROR}" == "true" ]; then
exit 1
fi
Loading