Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 77a10cc

Browse files
authored
[client] Enable multi repository CI for forks (#664)
1 parent afc2912 commit 77a10cc

File tree

2 files changed

+95
-7
lines changed

2 files changed

+95
-7
lines changed

.drone.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ services:
9292
RABBITMQ__HOSTNAME: rabbitmq
9393
EXPIRATION_SCHEDULER__ENABLED: false
9494
SUBSCRIPTION_SCHEDULER__ENABLED: false
95+
GITHUB_TOKEN:
96+
from_secret: github_token
9597
commands:
96-
- apk add build-base git libffi-dev cargo
97-
- BRANCH=$(echo $DRONE_SOURCE_BRANCH)
98-
- OPENCTI_BRANCH=$([ $(echo $BRANCH | cut -d "/" -f 1) == opencti ] && echo $(echo $BRANCH | cut -d "/" -f2-) || echo 'master' )
99-
- git clone -b $OPENCTI_BRANCH https://github.com/OpenCTI-Platform/opencti.git /tmp/opencti
100-
- cd /tmp/opencti/opencti-platform/opencti-graphql
101-
- OPENCTI_COMMIT=$(git rev-parse --short HEAD)
102-
- echo [INFO] using opencti@$OPENCTI_BRANCH:$OPENCTI_COMMIT
98+
- apk add build-base git libffi-dev cargo github-cli
99+
- chmod 777 scripts/*
100+
- echo "DRONE_SOURCE_BRANCH:${DRONE_SOURCE_BRANCH}, DRONE_PULL_REQUEST:${DRONE_PULL_REQUEST}"
101+
- ./scripts/clone-opencti.sh "${DRONE_SOURCE_BRANCH}" "$(pwd)" "${DRONE_PULL_REQUEST}"
102+
- ls -lart
103+
- cd opencti/opencti-platform/opencti-graphql
103104
- yarn install
104105
- yarn install:python
105106
- NODE_OPTIONS=--max_old_space_size=8192 yarn start
107+

scripts/clone-opencti.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)