diff --git a/.github/workflows/version-check.yml b/.github/workflows/version-check.yml new file mode 100644 index 000000000000..fe185801878b --- /dev/null +++ b/.github/workflows/version-check.yml @@ -0,0 +1,43 @@ +name: Check work package version + +on: + pull_request: + types: [labeled, synchronize] + +permissions: + contents: read # to fetch code (actions/checkout) + pull-requests: write # to comment on the PR + +jobs: + check-pr: + if: contains(github.event.label.name, 'needs review') + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + + - name: Verify linked version matches core version + id: pr-details + run: ./script/version_check.sh ${{ github.event.pull_request.body }} + + - name: Add comment if versions differ + if: steps.version-check.outputs.version_mismatch == 'true' + uses: marocchino/sticky-pull-request-comment@v2 + with: + message: | + The provided work package version does not match the core version: + + - Work package URL: ${{ steps.pr-details.outputs.wp_url }} + - Work package version: ${{steps.pr-details.outputs.wp_version}} + - Core version: ${{steps.pr-details.outputs.core_version}} + + Please make sure that: + + - The work package version OR your pull request target branch is correct + - name: Output success + if: success() && steps.version-check.outputs.version_mismatch != 'true' + run: echo "Version check passed." diff --git a/script/ci/version_check.sh b/script/ci/version_check.sh new file mode 100755 index 000000000000..02be53038f2f --- /dev/null +++ b/script/ci/version_check.sh @@ -0,0 +1,81 @@ +#!/bin/bash +#-- copyright +# OpenProject is a project management system. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +#++ + +set -e + +# script/ci/version_check + +PR_BODY="$1" + +# Extract URL from PR description +WP_URL=$(echo "${PR_BODY}" | grep -oE 'https://community.openproject.org/(wp|work_packages|projects/[^/]+/work_packages)/[0-9]+') + +if [ -z "$WP_URL" ]; then + echo "::warning::PR description does not contain a valid URL to an OpenProject ticket." + exit 0 +fi + +# Extract the work package ID +WORK_PACKAGE_ID=$(echo "$WP_URL" | grep -oE '[0-9]+$') +echo "Work Package ID: $WORK_PACKAGE_ID" + +# Perform API request to fetch version +API_URL="https://community.openproject.org/api/v3/work_packages/${WORK_PACKAGE_ID}" +RESPONSE=$(curl -s -w "%{http_code}" -o response.json "$API_URL") +HTTP_STATUS=$(tail -n1 <<< "$RESPONSE") + +if [ "$HTTP_STATUS" -ne 200 ]; then + echo "API request failed with status code $HTTP_STATUS. Exiting." + cat response.json + exit 0 +fi + +VERSION_FROM_API=$(jq -r '._links.version.title // empty' response.json) +if [ -z "$VERSION_FROM_API" ]; then + echo "::warning::Failed to extract version from API response." + exit 0 +fi + +echo "Version from API: $VERSION_FROM_API" + +# Extract version from the Ruby file using 'rake version' +VERSION_FROM_FILE=$(ruby -e 'require_relative "./lib/open_project/version"; puts OpenProject::VERSION.to_s') + +echo "Version from file: $VERSION_FROM_FILE" + +# Compare the versions +if [[ "$VERSION_FROM_API" != "$VERSION_FROM_FILE" ]]; then + echo "Version mismatch detected." + echo "::set-output name=version_mismatch::true" + echo "::set-output name=wp_version::${VERSION_FROM_API}" + echo "::set-output name=wp_url::${WP_URL}" + echo "::set-output name=core_version::${VERSION_FROM_FILE}" +else + echo "Version from the work package ${WORK_PACKAGE_ID} matches the version in the version file this PR targets." +fi