From 70dee5c145fa773f36e4777f7af2f990db3302e2 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 31 Jul 2026 15:57:53 +1000 Subject: [PATCH] Demo data branch backport (#12521) * Auto-detect demo dataset branch in CI * Fix github actions * Fix typo --- .github/workflows/frontend.yaml | 8 ++---- .github/workflows/import_export.yaml | 8 ++---- tasks.py | 41 +++++++++++++++++++--------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/.github/workflows/frontend.yaml b/.github/workflows/frontend.yaml index 0a1c5fbb4e..82a261c5dd 100644 --- a/.github/workflows/frontend.yaml +++ b/.github/workflows/frontend.yaml @@ -32,10 +32,6 @@ env: INVENTREE_BACKUP_DIR: /home/runner/work/InvenTree/test_inventree_backup INVENTREE_SITE_URL: http://localhost:8000 - # Use the same-named branch of the demo dataset as the target branch of this build, - # so that stable branches are not broken by changes to the demo dataset on main - DEMO_DATASET_BRANCH: ${{ github.event_name == 'pull_request' && github.base_ref || github.ref_name }} - permissions: contents: read @@ -139,7 +135,7 @@ jobs: pip-dependency: psycopg2 - name: Set up test data run: | - invoke dev.setup-test -iv --branch "$DEMO_DATASET_BRANCH" + invoke dev.setup-test -iv invoke int.rebuild-thumbnails - name: Install dependencies run: invoke int.frontend-compile --extract @@ -221,7 +217,7 @@ jobs: pip-dependency: psycopg2 - name: Set up test data run: | - invoke dev.setup-test -iv --branch "$DEMO_DATASET_BRANCH" + invoke dev.setup-test -iv invoke int.rebuild-thumbnails - name: Install dependencies run: invoke int.frontend-compile --extract diff --git a/.github/workflows/import_export.yaml b/.github/workflows/import_export.yaml index 315ee19992..8b030fef52 100644 --- a/.github/workflows/import_export.yaml +++ b/.github/workflows/import_export.yaml @@ -40,10 +40,6 @@ env: INVENTREE_DB_HOST: "127.0.0.1" INVENTREE_DB_PORT: 5432 - # Use the same-named branch of the demo dataset as the target branch of this build, - # so that stable branches are not broken by changes to the demo dataset on main - DEMO_DATASET_BRANCH: ${{ github.event_name == 'pull_request' && github.base_ref || github.ref_name }} - jobs: paths-filter: @@ -91,10 +87,10 @@ jobs: pip-dependency: psycopg update: true static: false - - name: Setup Postgres Database + - name: Setup Test Data run: | invoke migrate - invoke dev.setup-test -i --branch "$DEMO_DATASET_BRANCH" + invoke dev.setup-test -i - name: Create Plugin Data run: | pip install -U inventree-dummy-app-plugin==0.1.0 diff --git a/tasks.py b/tasks.py index 16bdb4b234..8031da9da3 100644 --- a/tasks.py +++ b/tasks.py @@ -1837,12 +1837,33 @@ def test( manage(c, cmd, pty=pty) +def _detect_ci_branch() -> Optional[str]: + """Attempt to auto-detect the target branch when running in GitHub CI. + + - For pull_request events, GITHUB_BASE_REF holds the PR's target branch (e.g. "master" or "1.4.x") + - For push events, GITHUB_REF_NAME holds the branch being pushed to + + Returns None if not running in GitHub Actions, or no branch could be determined. + """ + if os.environ.get('GITHUB_ACTIONS') != 'true': + return None + + base_ref = os.environ.get('GITHUB_BASE_REF') + if base_ref: + return base_ref + + if os.environ.get('GITHUB_REF_TYPE') == 'branch': + return os.environ.get('GITHUB_REF_NAME') + + return None + + @task( help={ 'dev': 'Set up development environment at the end', 'validate_files': 'Validate media files are correctly copied', 'use_ssh': 'Use SSH protocol for cloning the demo dataset (requires SSH key)', - 'branch': 'Specify branch of demo-dataset to clone (default = main)', + 'branch': 'Specify branch of demo-dataset to clone (default = auto-detected target branch in CI, else main)', 'verbose': 'Print verbose output from management commands', } ) @@ -1854,7 +1875,7 @@ def setup_test( use_ssh=False, verbose=False, path='inventree-demo-dataset', - branch='main', + branch: Optional[str] = None, ): """Setup a testing environment.""" from src.backend.InvenTree.InvenTree.config import ( # type: ignore[import] @@ -1876,23 +1897,17 @@ def setup_test( # Use SSH protocol for cloning the demo dataset URL = 'git@github.com:inventree/demo-dataset.git' + if not branch: + branch = _detect_ci_branch() + if branch: + info(f"Auto-detected target branch from CI: '{branch}'") + # InvenTree's trunk branch is named "master", but the demo-dataset # repository uses "main" as its trunk branch - map this automatically # so CI can pass through the detected target branch unmodified. if not branch or branch == 'master': branch = 'main' - if branch != 'main': - # Stable release branches (e.g. "1.4.x") may not yet exist in the - # demo-dataset repository (e.g. it has not been backported yet). - # Fall back to "main" in that case, rather than failing the clone. - result = c.run(f'git ls-remote --heads {URL} {branch}', hide=True, warn=True) - if not result.ok or not result.stdout.strip(): - warning( - f"Demo dataset has no branch named '{branch}' - falling back to 'main'" - ) - branch = 'main' - # Get test data info(f"Cloning demo dataset (branch '{branch}') ...") run(c, f'git clone {URL} {template_dir} -b {branch} -v --depth=1')