11#! /bin/sh
22
3- if [[ -z " $1 " ]] || [[ -z " $2 " ]]
3+ if [[ -z " $1 " ]] || [[ -z " $2 " ]] || [[ -z " $3 " ]]
44then
5- echo " [CLONE-DEPS] This scripts $0 requires 2 paramaters : branch_name:$1 , workspace: $2 (optional: PR_number:$3 )"
5+ echo " [CLONE-DEPS] This scripts $0 requires 3 parameters : branch_name:$1 , pr_target_branch: $2 , workspace: $3 (optional: PR_number:$4 )"
66 exit 0
77fi
88
99PR_BRANCH_NAME=$1
10- WORKSPACE=$2
11- PR_NUMBER=$3
10+ PR_TARGET_BRANCH=$2
11+ WORKSPACE=$3
12+ PR_NUMBER=$4
1213
1314OPENCTI_DIR=" ${WORKSPACE} /opencti"
1415
@@ -32,8 +33,8 @@ clone_for_pr_build() {
3233 # remove opencti prefix when present
3334 OPENCTI_BRANCH=$( echo ${PR_BRANCH_NAME} | cut -d " /" -f2-)
3435 fi
35- echo " [CLONE-DEPS] OPENCTI_BRANCH is ${OPENCTI_BRANCH} "
36- gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR}
36+ echo " [CLONE-DEPS] OPENCTI_BRANCH is ${OPENCTI_BRANCH} , target branch is ${PR_TARGET_BRANCH} "
37+ gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR} -- --branch ${PR_TARGET_BRANCH} --depth=1
3738 cd ${OPENCTI_DIR}
3839
3940 # search for the first opencti PR that matches OPENCTI_BRANCH
@@ -50,35 +51,46 @@ clone_for_pr_build() {
5051 echo " [CLONE-DEPS] Found a PR in opencti with number ${OPENCTI_PR_NUMBER} , using it."
5152 gh pr checkout ${OPENCTI_PR_NUMBER}
5253 else
53- echo " [CLONE-DEPS] No PR found in opencti side, keeping opencti:master "
54- # Repository already clone on master branch
54+ echo " [CLONE-DEPS] No PR found in opencti side, keeping opencti:${PR_TARGET_BRANCH} "
55+ # Repository already clone on PR target branch
5556 fi
5657
5758 else
58- echo " [CLONE-DEPS] NOT multi repo, cloning opencti:master "
59- gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR}
59+ echo " [CLONE-DEPS] NOT multi repo, cloning opencti:${PR_TARGET_BRANCH} "
60+ gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR} -- --branch ${PR_TARGET_BRANCH} --depth=1
6061 fi
6162}
6263
6364clone_for_push_build () {
64- echo " [CLONE-DEPS] Build from a commit, checking if a dedicated branch is required."
65- OPENCTI_BRANCH =$( [ $( echo $PR_BRANCH_NAME | cut -d " /" -f 1) == opencti ] && echo $( echo $PR_BRANCH_NAME | cut -d " / " -f2- ) || echo ' master ' )
66- if [ " $( echo " $( git ls-remote --heads https://github.com/OpenCTI-Platform/opencti.git refs/heads/OPENCTI_BRANCH ) " ) " != ' ' ]
65+ echo " [CLONE-DEPS] Build from a commit, checking if a dedicated branch is required."
66+ BRANCH_PREFIX =$( echo $PR_BRANCH_NAME | cut -d " /" -f 1 | grep -c " opencti " )
67+ if [[ " ${BRANCH_PREFIX} " -eq " 1 " ] ]
6768 then
68- git clone -b $OPENCTI_BRANCH https://github.com/OpenCTI-Platform/opencti.git
69+ echo " [CLONE-DEPS] Dedicated OpenCTI branch found, using it"
70+ OPENCTI_BRANCH=$( echo $PR_BRANCH_NAME | cut -d " /" -f2-)
71+ git clone -b $OPENCTI_BRANCH https://github.com/OpenCTI-Platform/opencti.git
6972 else
70- git clone -b master https://github.com/OpenCTI-Platform/opencti.git
73+ echo " [CLONE-DEPS] No dedicated OpenCTI branch found, using master"
74+ git clone https://github.com/OpenCTI-Platform/opencti.git
7175 fi
72-
7376}
7477
75- echo " [CLONE-DEPS] START; with PR_BRANCH_NAME=${PR_BRANCH_NAME} , PR_NUMBER=${PR_NUMBER} , OPENCTI_DIR=${OPENCTI_DIR} ."
78+ 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} ."
7679if [[ -z ${PR_NUMBER} ]] || [[ ${PR_NUMBER} == " " ]]
7780then
7881 # No PR number from Drone = "Push build". And it's only for repository branch (not fork)
79- # Only check branches from OpenCTI-Platform org
80- echo " [CLONE-DEPS] No PR number from Drone = " Push build" ; it's only for repository branch (not fork)."
81- clone_for_push_build
82+ # Using github cli to get PR number anyway
83+ PR_NUMBER=$( gh pr view ${PR_BRANCH_NAME} --json number --jq ' .number' )
84+ PR_TARGET_BRANCH=$( gh pr view ${PR_BRANCH_NAME} --json baseRefName --jq ' .baseRefName' )
85+
86+ if [[ -z ${PR_NUMBER} ]] || [[ ${PR_NUMBER} == " " ]]
87+ then
88+ echo " [CLONE-DEPS] PR is not created on github yet, using clone without PR number and default fallback to master"
89+ clone_for_push_build
90+ else
91+ echo " [CLONE-DEPS] Got data from github cli, continue with: PR_TARGET_BRANCH=${PR_TARGET_BRANCH} , PR_NUMBER=${PR_NUMBER} ."
92+ clone_for_pr_build
93+ fi
8294else
8395 # PR build is trigger from Pull Request coming both from branch and forks.
8496 # We need to have this clone accross repository that works for forks (community PR)
0 commit comments