From 47d204773cb040e96b63096bf9b92f8756d839c4 Mon Sep 17 00:00:00 2001 From: "A. Jard" Date: Mon, 28 Oct 2024 13:55:39 +0100 Subject: [PATCH] [CI] Update scripts to use PR target branch instead of hardcoded master (#750) --- .drone.yml | 2 +- scripts/clone-opencti.sh | 52 ++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/.drone.yml b/.drone.yml index 7e152dd8..812bfac2 100644 --- a/.drone.yml +++ b/.drone.yml @@ -98,7 +98,7 @@ services: - apk add build-base git libffi-dev cargo github-cli - chmod 777 scripts/* - echo "DRONE_SOURCE_BRANCH:${DRONE_SOURCE_BRANCH}, DRONE_PULL_REQUEST:${DRONE_PULL_REQUEST}" - - ./scripts/clone-opencti.sh "${DRONE_SOURCE_BRANCH}" "$(pwd)" "${DRONE_PULL_REQUEST}" + - ./scripts/clone-opencti.sh "${DRONE_SOURCE_BRANCH}" "${DRONE_TARGET_BRANCH}" "$(pwd)" "${DRONE_PULL_REQUEST}" - ls -lart - cd opencti/opencti-platform/opencti-graphql - yarn install diff --git a/scripts/clone-opencti.sh b/scripts/clone-opencti.sh index db2b8028..5493d895 100644 --- a/scripts/clone-opencti.sh +++ b/scripts/clone-opencti.sh @@ -1,14 +1,15 @@ #!/bin/sh -if [[ -z "$1" ]] || [[ -z "$2" ]] +if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] then - echo "[CLONE-DEPS] This scripts $0 requires 2 paramaters: branch_name:$1, workspace:$2 (optional: PR_number:$3)" + echo "[CLONE-DEPS] This scripts $0 requires 3 parameters: branch_name:$1, pr_target_branch: $2, workspace:$3 (optional: PR_number:$4)" exit 0 fi PR_BRANCH_NAME=$1 -WORKSPACE=$2 -PR_NUMBER=$3 +PR_TARGET_BRANCH=$2 +WORKSPACE=$3 +PR_NUMBER=$4 OPENCTI_DIR="${WORKSPACE}/opencti" @@ -32,8 +33,8 @@ clone_for_pr_build() { #remove opencti prefix when present OPENCTI_BRANCH=$(echo ${PR_BRANCH_NAME} | cut -d "/" -f2-) fi - echo "[CLONE-DEPS] OPENCTI_BRANCH is ${OPENCTI_BRANCH}" - gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR} + echo "[CLONE-DEPS] OPENCTI_BRANCH is ${OPENCTI_BRANCH}, target branch is ${PR_TARGET_BRANCH}" + gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR} -- --branch ${PR_TARGET_BRANCH} --depth=1 cd ${OPENCTI_DIR} # search for the first opencti PR that matches OPENCTI_BRANCH @@ -50,35 +51,46 @@ clone_for_pr_build() { echo "[CLONE-DEPS] Found a PR in opencti with number ${OPENCTI_PR_NUMBER}, using it." gh pr checkout ${OPENCTI_PR_NUMBER} else - echo "[CLONE-DEPS] No PR found in opencti side, keeping opencti:master" - # Repository already clone on master branch + echo "[CLONE-DEPS] No PR found in opencti side, keeping opencti:${PR_TARGET_BRANCH}" + # Repository already clone on PR target branch fi else - echo "[CLONE-DEPS] NOT multi repo, cloning opencti:master" - gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR} + echo "[CLONE-DEPS] NOT multi repo, cloning opencti:${PR_TARGET_BRANCH}" + gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR} -- --branch ${PR_TARGET_BRANCH} --depth=1 fi } clone_for_push_build() { - echo "[CLONE-DEPS] Build from a commit, checking if a dedicated branch is required." - OPENCTI_BRANCH=$([ $(echo $PR_BRANCH_NAME | cut -d "/" -f 1) == opencti ] && echo $(echo $PR_BRANCH_NAME | cut -d "/" -f2-) || echo 'master' ) - if [ "$(echo "$(git ls-remote --heads https://github.com/OpenCTI-Platform/opencti.git refs/heads/OPENCTI_BRANCH)")" != '' ] + echo "[CLONE-DEPS] Build from a commit, checking if a dedicated branch is required." + BRANCH_PREFIX=$(echo $PR_BRANCH_NAME | cut -d "/" -f 1 | grep -c "opencti") + if [[ "${BRANCH_PREFIX}" -eq "1" ]] then - git clone -b $OPENCTI_BRANCH https://github.com/OpenCTI-Platform/opencti.git + echo "[CLONE-DEPS] Dedicated OpenCTI branch found, using it" + OPENCTI_BRANCH=$(echo $PR_BRANCH_NAME | cut -d "/" -f2-) + git clone -b $OPENCTI_BRANCH https://github.com/OpenCTI-Platform/opencti.git else - git clone -b master https://github.com/OpenCTI-Platform/opencti.git + echo "[CLONE-DEPS] No dedicated OpenCTI branch found, using master" + git clone https://github.com/OpenCTI-Platform/opencti.git fi - } -echo "[CLONE-DEPS] START; with PR_BRANCH_NAME=${PR_BRANCH_NAME}, PR_NUMBER=${PR_NUMBER}, OPENCTI_DIR=${OPENCTI_DIR}." +echo "[CLONE-DEPS] START; with PR_BRANCH_NAME=${PR_BRANCH_NAME},PR_TARGET_BRANCH=${PR_TARGET_BRANCH}, PR_NUMBER=${PR_NUMBER}, OPENCTI_DIR=${OPENCTI_DIR}." if [[ -z ${PR_NUMBER} ]] || [[ ${PR_NUMBER} == "" ]] then # No PR number from Drone = "Push build". And it's only for repository branch (not fork) - # Only check branches from OpenCTI-Platform org - echo "[CLONE-DEPS] No PR number from Drone = "Push build"; it's only for repository branch (not fork)." - clone_for_push_build + # Using github cli to get PR number anyway + PR_NUMBER=$(gh pr view ${PR_BRANCH_NAME} --json number --jq '.number') + PR_TARGET_BRANCH=$(gh pr view ${PR_BRANCH_NAME} --json baseRefName --jq '.baseRefName') + + if [[ -z ${PR_NUMBER} ]] || [[ ${PR_NUMBER} == "" ]] + then + echo "[CLONE-DEPS] PR is not created on github yet, using clone without PR number and default fallback to master" + clone_for_push_build + else + echo "[CLONE-DEPS] Got data from github cli, continue with: PR_TARGET_BRANCH=${PR_TARGET_BRANCH}, PR_NUMBER=${PR_NUMBER}." + clone_for_pr_build + fi else # PR build is trigger from Pull Request coming both from branch and forks. # We need to have this clone accross repository that works for forks (community PR)