Skip to content

Commit efd6387

Browse files
committed
PS-10100 [8.0]: Improve the way the base branch info is fetched in circleCI check
https://perconadev.atlassian.net/browse/PS-10100 The base branch information is crucial for performing clang-tidy checks because the clang-tidy checks are configured run on only the changes made in the source code files within a prticular pull request(even with multiple commits) and the base branch is necessary to do the comparison with the source branch to fetch the changes made in a PR. So, Github API is used to dynamically fetch the pull request data which is in turn used to filter out the base branch info. It has also been made sure to handle cases where the circleCI checks are run in the context of the fork(if enabled). Curl is being used to fetch the JSON data from the API and all the possible errors are handled and in such cases the base branch will be set to 8.0 as a fallback.
1 parent 400664d commit efd6387

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

.circleci/config.yml

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616
command: |
1717
set -o xtrace
1818
UBUNTU_CODE_NAME="noble"
19+
BASE_OWNER="percona"
20+
BASE_REPO="percona-server"
1921
BASE_BRANCH="8.0"
2022
COMPILER_VERSION="19"
2123
BOOST_VERSION="1_77_0"
@@ -32,7 +34,63 @@ jobs:
3234
cd ~/project
3335
cmake -B /home/circleci/debug-build -DCMAKE_BUILD_TYPE=Debug -DWITH_BOOST=${BOOST_DIR} -DWITH_SSL=system -DWITH_AUTHENTICATION_LDAP=ON -DWITH_ROCKSDB=ON -DCMAKE_C_COMPILER=clang-${COMPILER_VERSION} -DCMAKE_CXX_COMPILER=clang++-${COMPILER_VERSION} -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DCMAKE_EXE_LINKER_FLAGS="-stdlib=libc++" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DWITH_SYSTEM_LIBS=ON -DWITH_FIDO=bundled -DWITH_ZSTD=bundled -DWITH_LZ4=bundled -DWITH_PROTOBUF=bundled ~/project
3436
35-
git remote add target "https://github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
37+
# Method to fetch JSON from a Github API URL with error handling
38+
fetch_json() {
39+
local url="$1" tmp http_code curl_exit body
40+
tmp=$(mktemp) || return 1
41+
# write body to tmp, capture HTTP code on stdout
42+
http_code=$(curl -sS --location --connect-timeout 10 --max-time 30 -w "%{http_code}" -o "$tmp" "$url" 2>/dev/null) || true
43+
curl_exit=$?
44+
body="$(cat "$tmp")"
45+
rm -f "$tmp"
46+
47+
if [ $curl_exit -ne 0 ]; then
48+
echo "curl exit $curl_exit for $url" >&2
49+
return 2
50+
fi
51+
if [ -z "$http_code" ] || [ "$http_code" -ge 400 ]; then
52+
echo "HTTP $http_code from $url" >&2
53+
return 3
54+
fi
55+
if [ -z "${body:-}" ]; then
56+
echo "empty response from $url" >&2
57+
return 4
58+
fi
59+
if ! printf '%s' "$body" | jq -e . >/dev/null 2>&1; then
60+
echo "response from $url is not valid JSON" >&2
61+
return 5
62+
fi
63+
64+
printf '%s' "$body"
65+
}
66+
67+
PR_DATA=""
68+
if [[ "$CIRCLE_BRANCH" =~ ^pull/[0-9]+$ ]]; then
69+
# If the circleCI check is run in the context of the official percona repo.
70+
PR_API_URL="https://api.github.com/repos/${BASE_OWNER}/${BASE_REPO}/pulls/${CIRCLE_PR_NUMBER}"
71+
PR_DATA="$(fetch_json "$PR_API_URL")" || PR_DATA=""
72+
else
73+
# GitHub API URL to get open PRs matching this fork + branch
74+
API_URL="https://api.github.com/repos/${BASE_OWNER}/${BASE_REPO}/pulls?head=${CIRCLE_PROJECT_USERNAME}:${CIRCLE_BRANCH}"
75+
echo "Fetching PRs from: $API_URL" >&2
76+
77+
# Fetch PRs
78+
PRS_JSON="$(fetch_json "$API_URL")" || PRS_JSON="[]"
79+
80+
# Filter PR whose head.sha matches CIRCLE_SHA1
81+
PR_DATA="$(printf '%s' "$PRS_JSON" | jq --arg sha "$CIRCLE_SHA1" '.[] | select(.head.sha == $sha)')"
82+
fi
83+
84+
if [[ -z "$PR_DATA" ]]; then
85+
echo "Failed to find PR data for commit $CIRCLE_SHA1. $BASE_BRANCH will be used as the base branch."
86+
else
87+
BASE_BRANCH=$(echo "$PR_DATA" | jq -r '.base.ref')
88+
PR_NUMBER=$(echo "$PR_DATA" | jq -r '.number')
89+
echo "Found PR: $PR_NUMBER with base branch: $BASE_BRANCH"
90+
fi
91+
92+
echo "$BASE_BRANCH"
93+
git remote add target "https://github.com/${BASE_OWNER}/${BASE_REPO}"
3694
git fetch --no-tags --no-recurse-submodules target $BASE_BRANCH
3795
3896
echo "Checking clang-format results"

0 commit comments

Comments
 (0)