1+ #! /bin/sh
2+
3+ if [[ -z " $1 " ]] || [[ -z " $2 " ]] || [[ -z " $3 " ]]
4+ then
5+ echo " [CLONE-DEPS] This scripts $0 requires 3 paramaters: branch_name:$1 , workspace:$2 (optional: PR_number:$3 )"
6+ exit 0
7+ fi
8+
9+ PR_BRANCH_NAME=$1
10+ WORKSPACE=$2
11+ PR_NUMBER=$3
12+
13+ OPENCTI_DIR=" ${WORKSPACE} /opencti"
14+
15+ clone_for_pr_build () {
16+ cd ${WORKSPACE}
17+ export GH_TOKEN=" ${GITHUB_TOKEN} "
18+
19+ gh auth login --hostname github.com --with-token ${GH_TOKEN}
20+ gh auth status
21+ gh repo set-default https://github.com/OpenCTI-Platform/client-python
22+
23+ # Check current PR to see if label "multi-repository" is set
24+ IS_MULTI_REPO=$( gh pr view ${PR_NUMBER} --json labels | grep -c " multi-repository" )
25+ if [[ ${IS_MULTI_REPO} -eq 1 ]]
26+ then
27+
28+ OPENCTI_BRANCH=${PR_BRANCH_NAME}
29+ echo " [CLONE-DEPS] Multi repository PR, looking for opencti related branch"
30+ if [[ $( echo ${PR_BRANCH_NAME} | cut -d " /" -f 1) == " opencti" ]]
31+ then
32+ # remove opencti prefix when present
33+ OPENCTI_BRANCH=$( echo ${PR_BRANCH_NAME} | cut -d " /" -f2-)
34+ fi
35+ echo " [CLONE-DEPS] OPENCTI_BRANCH is ${OPENCTI_BRANCH} "
36+ gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR}
37+ cd ${OPENCTI_DIR}
38+
39+ # search for the first opencti PR that matches OPENCTI_BRANCH
40+ gh repo set-default https://github.com/OpenCTI-Platform/opencti
41+ gh pr list --label " multi-repository" > multi-repo-prs.txt
42+
43+ cat multi-repo-prs.txt
44+
45+ OPENCTI_PR_NUMBER=$( cat multi-repo-prs.txt | grep " ${OPENCTI_BRANCH} " | head -n 1 | sed ' s/#//g' | awk ' {print $1}' )
46+ echo " OPENCTI_PR_NUMBER=${OPENCTI_PR_NUMBER} "
47+
48+ if [[ " ${OPENCTI_PR_NUMBER} " != " " ]]
49+ then
50+ echo " [CLONE-DEPS] Found a PR in opencti with number ${OPENCTI_PR_NUMBER} , using it."
51+ gh pr checkout ${OPENCTI_PR_NUMBER}
52+ else
53+ echo " [CLONE-DEPS] No PR found in opencti side, keeping opencti:master"
54+ # Repository already clone on master branch
55+ fi
56+
57+ else
58+ echo " [CLONE-DEPS] NOT multi repo, cloning opencti:master"
59+ gh repo clone https://github.com/OpenCTI-Platform/opencti ${OPENCTI_DIR}
60+ fi
61+ }
62+
63+ clone_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+ git clone -b $OPENCTI_BRANCH https://github.com/OpenCTI-Platform/opencti.git
67+ }
68+
69+ echo " [CLONE-DEPS] START; with PR_BRANCH_NAME=${PR_BRANCH_NAME} , PR_NUMBER=${PR_NUMBER} , OPENCTI_DIR=${OPENCTI_DIR} ."
70+ if [[ -z ${PR_NUMBER} ]] || [[ ${PR_NUMBER} == " " ]]
71+ then
72+ # No PR number from Drone = "Push build". And it's only for repository branch (not fork)
73+ # Only check branches from OpenCTI-Platform org
74+ echo " [CLONE-DEPS] No PR number from Drone = " Push build" ; it's only for repository branch (not fork)."
75+ clone_for_push_build
76+ else
77+ # PR build is trigger from Pull Request coming both from branch and forks.
78+ # We need to have this clone accross repository that works for forks (community PR)
79+ echo " [CLONE-DEPS] Got PR number ${PR_NUMBER} from Drone = " PR build" ; Pull Request coming both from branch and forks."
80+ clone_for_pr_build
81+ fi
82+
83+ cd ${OPENCTI_DIR}
84+ echo " [CLONE-DEPS] END; Using opencti on branch:$( git branch --show-current) "
85+
86+ cd ${WORKSPACE}
0 commit comments