From c5edc362a96cdc8c68ceb409f582f1224cac1b82 Mon Sep 17 00:00:00 2001 From: Alessandro Domanico Date: Wed, 30 Oct 2024 17:47:09 +0100 Subject: [PATCH] workflow: use PR fork and branch (#2078) * Align with API * Improve logging * Fix CHECK_BRANCH_URL --- .github/workflows/maven.yml | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 91e0b29c70..1c510b6f32 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -27,23 +27,27 @@ jobs: - name: Determine PR source branch and fork repository id: vars run: | - # Get branch name from either pull_request or push context - if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then - # Get branch and fork repo for PR events - echo "BRANCH_NAME=${GITHUB_HEAD_REF}" >> $GITHUB_ENV - echo "FORK_REPO=${GITHUB_HEAD_REPOSITORY:-informatici/openhospital-core}" >> $GITHUB_ENV - elif [[ "${GITHUB_EVENT_NAME}" == "push" ]]; then - # Get branch name for push events - echo "BRANCH_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV - - # Default FORK_REPO to main repository, with option to fallback if the fork exists - OWNER=${GITHUB_ACTOR} - if curl -s -o /dev/null -w "%{http_code}" "https://api.github.com/repos/${OWNER}/openhospital-core/branches/${GITHUB_REF##*/}" | grep -q "200"; then - echo "FORK_REPO=${OWNER}/openhospital-core" >> $GITHUB_ENV - else - echo "FORK_REPO=informatici/openhospital-core" >> $GITHUB_ENV - fi + # Set default FORK_REPO and BRANCH_NAME values. + BRANCH_NAME="${GITHUB_HEAD_REF:-${GITHUB_REF##*/}}" + FORK_REPO="${GITHUB_HEAD_REPOSITORY:-${GITHUB_ACTOR}/openhospital-core}" + CHECK_BRANCH_URL="https://github.com/${GITHUB_ACTOR}/openhospital-core/tree/$BRANCH_NAME" + echo "Checking branch existence with: curl -s -o /dev/null -w \"%{http_code}\" $CHECK_BRANCH_URL" + + # Determine FORK_REPO with fallback logic + if [[ -n "${GITHUB_HEAD_REPOSITORY}" ]]; then + echo "Using ${GITHUB_HEAD_REPOSITORY}." + FORK_REPO=${GITHUB_HEAD_REPOSITORY} + elif curl -s -o /dev/null -w "%{http_code}" $CHECK_BRANCH_URL | grep -q "200"; then + echo "Using ${GITHUB_ACTOR}/openhospital-core" + FORK_REPO=${GITHUB_ACTOR}/openhospital-core + else + echo "Using informatici/openhospital-core." + FORK_REPO="informatici/openhospital-core" fi + + # Export FORK_REPO and BRANCH_NAME to GITHUB_ENV for the next step + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV + echo "FORK_REPO=$FORK_REPO" >> $GITHUB_ENV - name: Log variables run: |