Skip to content

Commit

Permalink
Add version check when needs review label is added
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverguenther committed Sep 9, 2024
1 parent b9b2e08 commit 861bdc2
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/version-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Check work package version

on:
pull_request:
types: [labeled]

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') && github.event.pull_request.draft == 'true'
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: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: ${{ github.event.pull_request.number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: `Version mismatch detected:\n
- Work package URL: ${steps.pr-details.outputs.wp_url}\n
- Work package version: ${steps.pr-details.outputs.wp_version}\n
- Core version: ${steps.pr-details.outputs.core_version}\n`
})
- name: Output success
if: success() && steps.version-check.outputs.version_mismatch != 'true'
run: echo "Version check passed."
81 changes: 81 additions & 0 deletions script/ci/version_check.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 861bdc2

Please sign in to comment.