From 4c8ccc08ac5ce424c0436e0d0b63c1e694709575 Mon Sep 17 00:00:00 2001 From: John Haddon Date: Fri, 2 Aug 2024 16:44:36 +0100 Subject: [PATCH] Version check : Add new GitHub Action to check target PR branches To avoid the common problem of accidentally targeting `main` instead of one of the maintenance branches. --- .github/workflows/versionCheck.yml | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 .github/workflows/versionCheck.yml diff --git a/.github/workflows/versionCheck.yml b/.github/workflows/versionCheck.yml new file mode 100644 index 0000000000..a6fd62be24 --- /dev/null +++ b/.github/workflows/versionCheck.yml @@ -0,0 +1,60 @@ +name: Version check +on: [ pull_request ] +jobs: + check: + name: Version check + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v4 + with: + # So that we get the branch for `GITHUB_HEAD_REF`. + fetch-depth: 0 + - name: Check version + run: | + + import os + import re + import subprocess + import sys + + # Parses version number from the SConstruct file. + def version() : + + versions = {} + versionRe = re.compile( r"^(gaffer.*Version.*) = (\S+)" ) + with open( "SConstruct" ) as sconstruct : + for line in sconstruct.readlines() : + versionMatch = versionRe.match( line ) + if versionMatch : + versions[versionMatch.group( 1 )] = versionMatch.group( 2 ).strip( "'\"" ) + + return [ + int( versions["gafferMilestoneVersion"] ), + int( versions["gafferMajorVersion"] ), + ] + + # Check that versions match between source and target branches, to avoid + # common mistake of targeting a PR to `main` rather than a maintenance branch. + # + # > Note : We compare the source version to the merged version rather than the + # > version from the target branch itself, to allow a PR to change version number + # > if necessary. That will just be subject to the usual human review process. + + mergeVersion = version() + subprocess.check_call( [ "git", "checkout", "refs/remote/origin/" + os.environ["GITHUB_HEAD_REF"] ] ) + sourceVersion = version() + + if sourceVersion != mergeVersion : + + message = "Source version {} does not match target version {}. Did you choose the wrong target branch?\n".format( + ".".join( str( v ) for v in sourceVersion ), + ".".join( str( v ) for v in mergeVersion ) + ) + + sys.stderr.write( f"FAIL : {message}" ) + with open( os.environ["GITHUB_STEP_SUMMARY"], "a" ) as summary : + summary.write( f"> [!CAUTION]\n> {message}" ) + + sys.exit( 1 ) + + shell: python