From 6a3bb951aac50641d3587ec1e2ab3415ce5a325e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Thu, 31 Oct 2024 14:52:58 +0100 Subject: [PATCH] ci: check if a versioned .msg file is changed, a new version is added as well --- .github/workflows/ros_translation_node.yml | 9 ++- Tools/ci/check_msg_versioning.sh | 67 ++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100755 Tools/ci/check_msg_versioning.sh diff --git a/.github/workflows/ros_translation_node.yml b/.github/workflows/ros_translation_node.yml index 877233349fb2..3c50a61880ea 100644 --- a/.github/workflows/ros_translation_node.yml +++ b/.github/workflows/ros_translation_node.yml @@ -21,7 +21,14 @@ jobs: with: required-ros-distributions: humble - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Check .msg file versioning + if: github.event_name == 'pull_request' + run: | + ./Tools/ci/check_msg_versioning.sh ${{ github.event.pull_request.base.sha }} ${{github.event.pull_request.head.sha}} - name: Build and test run: | diff --git a/Tools/ci/check_msg_versioning.sh b/Tools/ci/check_msg_versioning.sh new file mode 100755 index 000000000000..ee1432b46af2 --- /dev/null +++ b/Tools/ci/check_msg_versioning.sh @@ -0,0 +1,67 @@ +#! /bin/bash +# Copy a git diff between two commits if msg versioning is added + +DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +PX4_SRC_DIR="$DIR/.." + +BASE_COMMIT="$1" +HEAD_COMMIT="$2" +if [ -z "${BASE_COMMIT}" ] || [ -z "${HEAD_COMMIT}" ] +then + echo "Usage: $0 " + exit 1 +fi + +failed=0 + +# Iterate git diff files between BASE_COMMIT and HEAD_COMMIT +modified_files="$(git --no-pager diff --no-color --name-only --diff-filter=M "${BASE_COMMIT}"..."${HEAD_COMMIT}")" +all_files="$( git --no-pager diff --no-color --name-only "${BASE_COMMIT}"..."${HEAD_COMMIT}")" +for file in ${modified_files} +do + if [[ "$file" == msg/versioned/*.msg ]]; then + echo "Modified msg file: ${file}" + # A modified versioned .msg file requires: + # - Incrementing the version + # - An old .msg version exists + # - A translation header exists and is included + diff=$(git --no-pager diff --no-color --diff-filter=M "${BASE_COMMIT}"..."${HEAD_COMMIT}" -- "${file}") + old_version=$(echo "${diff}" | sed -n 's/^-uint32 MESSAGE_VERSION = \([0-9]*\).*/\1/p') + new_version=$(echo "${diff}" | sed -n 's/^+uint32 MESSAGE_VERSION = \([0-9]*\).*/\1/p') + + # Check that the version is incremented + if [ -z "${new_version}" ] || [ -z "${old_version}" ]; then + echo "ERROR: Missing version update for ${file}" + failed=1 + else + if [ $((new_version-old_version)) -ne 1 ]; then + echo "ERROR: Version not incremented by +1 for ${file}" + failed=1 + fi + fi + + # Check that an old version exists + filename=$(basename -- "$file") + filename="${filename%.*}" + old_version_file="px4_msgs_old/msg/${filename}V${old_version}.msg" + if [[ "${all_files}" != *"${old_version_file}"* ]]; then + echo "ERROR: Old message version does not exist for ${file} (missing ${old_version_file})" + failed=1 + fi + + # Check that a translation header got added by checking for a modification to all_translations.h + # If it is changed, we assume a new header got added too, so we don't explicitly check for that + if [[ "${modified_files}" != *"translations/all_translations.h"* ]]; then + echo "ERROR: missing modification to translations/all_translations.h" + failed=1 + fi + fi +done + +if [ ${failed} -ne 0 ]; then + echo "" + echo "ERROR: missing message versioning due to changed .msg file(s) (see above)" + echo "Check the documentation under msg/translation_node/README.md for how to add a new version" + exit 1 +fi