Skip to content

Commit

Permalink
[CI] Update scripts to use PR target branch instead of hardcoded mast…
Browse files Browse the repository at this point in the history
…er (#750)
  • Loading branch information
aHenryJard authored Oct 28, 2024
1 parent b539fff commit 47d2047
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 32 additions & 20 deletions scripts/clone-opencti.sh
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 47d2047

Please sign in to comment.