From 78cf47df192318caf3d01a3ac57382e6dece9240 Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Tue, 10 Oct 2023 16:55:30 +0200 Subject: [PATCH] ci: Use referenced core PR to install Indico --- .github/utils/get_core_repo.py | 99 ++++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 45 +++------------- 2 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 .github/utils/get_core_repo.py diff --git a/.github/utils/get_core_repo.py b/.github/utils/get_core_repo.py new file mode 100644 index 0000000..0c97007 --- /dev/null +++ b/.github/utils/get_core_repo.py @@ -0,0 +1,99 @@ +# This file is part of the Indico plugins. +# Copyright (C) 2002 - 2023 CERN +# +# The Indico plugins are free software; you can redistribute +# them and/or modify them under the terms of the MIT License; +# see the LICENSE file for more details. + +import itertools +import json +import os +import re +import subprocess +import sys + + +CORE = sys.argv[1] +SCOPE = sys.argv[2] +CORE_USER, CORE_REPO = CORE.split('/') + +def lookup_via_referenced_prs(): + this_pr_body = os.environ['PR_BODY'] + this_base_ref = os.environ['PR_BASE_REF'] + + referenced_prs = set() + for match in itertools.chain( + re.finditer(r'(?:(? 1: + print('Found multiple PRs, not guessing which one to use') + # Fail the CI run - if this ever happens, we may be able to come up with suitable logic to pick one + sys.exit(1) + elif not candidates: + print('Found no suitable PRs') + return None + return candidates.popitem()[1] + + +def main(): + gh_event = os.environ['GITHUB_EVENT_NAME'] + check_common_branch = True + if gh_event == 'push': + full_repo = CORE + branch = os.environ['GITHUB_REF'].removeprefix('refs/heads/') + print(f'Using current branch: {branch}') + elif gh_event == 'pull_request': + if res := lookup_via_referenced_prs(): + full_repo, branch = res + check_common_branch = False + print(f'Using branch from referenced core PR: {full_repo}:{branch}') + else: + full_repo = CORE + branch = os.environ['GITHUB_BASE_REF'].removeprefix('refs/heads/') + print(f'Using branch from PR target: {branch}') + else: + print(f'Unsupported event: {gh_event}') + return 1 + + if check_common_branch and branch != 'master' and not branch.endswith('.x'): + print(f'Uncommon branch {branch}; defaulting to master') + branch = 'master' + + with open(os.environ['GITHUB_ENV'], 'a') as f: + f.write(f'{SCOPE}_REPO={full_repo}\n') + f.write(f'{SCOPE}_BRANCH={branch}\n') + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6400dcd..fbf6783 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,29 +23,18 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Get upstream branch name - run: | - if [[ $GITHUB_EVENT_NAME == push ]]; then - upstream_branch="${GITHUB_REF#refs/heads/}" - elif [[ $GITHUB_EVENT_NAME == pull_request ]]; then - upstream_branch="${GITHUB_BASE_REF#refs/heads/}" - else - echo "unsupported event: $GITHUB_EVENT_NAME" - exit 1 - fi - if [[ $upstream_branch != master && $upstream_branch != *.x ]]; then - echo "assuming there is no branch named ${upstream_branch} in indico; defaulting to master" - upstream_branch=master - else - echo "using indico upstream branch ${upstream_branch}" - fi - echo "INDICO_BRANCH=${upstream_branch}" >> "$GITHUB_ENV" - - name: Setup Python uses: actions/setup-python@v4 with: python-version: ${{ env.PYTHON_VERSION }} + - name: Pick Indico core repo + env: + GH_TOKEN: ${{ github.token }} + PR_BODY: ${{ github.event_name == 'pull_request' && github.event.pull_request.body }} + PR_BASE_REF: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.ref }} + run: python .github/utils/get_core_repo.py indico/indico INDICO + - name: Install system dependencies run: sudo apt-get install postgresql-client libpq-dev @@ -86,7 +75,7 @@ jobs: - name: Install Indico run: | pip install Babel - pip install git+https://github.com/indico/indico.git@${INDICO_BRANCH}#egg=indico[dev] + pip install "indico[dev] @ git+https://github.com/${INDICO_REPO}.git@${INDICO_BRANCH}" - name: Install node dependencies if: steps.cache-npm.outputs.cache-hit != 'true' @@ -113,24 +102,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Get upstream branch name - run: | - if [[ $GITHUB_EVENT_NAME == push ]]; then - upstream_branch="${GITHUB_REF#refs/heads/}" - elif [[ $GITHUB_EVENT_NAME == pull_request ]]; then - upstream_branch="${GITHUB_BASE_REF#refs/heads/}" - else - echo "unsupported event: $GITHUB_EVENT_NAME" - exit 1 - fi - if [[ $upstream_branch != master && $upstream_branch != *.x ]]; then - echo "assuming there is no branch named ${upstream_branch} in indico; defaulting to master" - upstream_branch=master - else - echo "using indico upstream branch ${upstream_branch}" - fi - echo "INDICO_BRANCH=${upstream_branch}" >> "$GITHUB_ENV" - - name: Setup Python uses: actions/setup-python@v4 with: