1818
1919readonly PROJECT_API_URL=" ${CI_API_V4_URL} /projects/${CI_PROJECT_ID} "
2020
21+ log_debug () {
22+ echo " [baseline-debug] $* " >&2
23+ }
24+
25+ response_snippet () {
26+ local response=" $1 "
27+ echo " ${response} " | tr ' \n' ' ' | sed ' s/[[:space:]]\+/ /g' | cut -c1-220
28+ }
29+
2130get_pipeline_id_for_commit () {
2231 local commit_sha=" $1 "
23-
24- # Query GitLab API to get pipeline ID that matches the commit SHA
2532 local api_url=" ${PROJECT_API_URL} /repository/commits/${commit_sha} "
2633 local response pipeline_id
34+
35+ log_debug " query commit endpoint: ${api_url} "
2736 response=" $( curl --request GET --silent --show-error --header " JOB-TOKEN: ${CI_JOB_TOKEN} " " ${api_url} " || true) "
2837 pipeline_id=" $( echo " ${response} " | grep -o ' "last_pipeline"[^}]*"id":[0-9]*' | grep -o ' [0-9]*$' | head -1 || true) "
2938 if [[ -n " ${pipeline_id} " && " ${pipeline_id} " != " null" ]]; then
39+ log_debug " found pipeline_id=${pipeline_id} for commit_sha=${commit_sha} "
3040 echo " ${pipeline_id} "
3141 return 0
3242 fi
3343
34- echo " "
44+ log_debug " no last_pipeline.id for commit_sha= ${commit_sha} ; response=' $( response_snippet " ${response} " ) ' "
3545 return 1
3646}
3747
38- get_branch_head_sha () {
39- local branch=" $1 "
40-
41- # Query GitLab API to get branch head SHA
42- local api_url=" ${PROJECT_API_URL} /repository/branches/${branch} "
43- local response branch_sha
44- response=" $( curl --request GET --silent --show-error --header " JOB-TOKEN: ${CI_JOB_TOKEN} " " ${api_url} " || true) "
45- branch_sha=" $( echo " ${response} " | grep -o ' "commit"[^}]*"id":"[a-f0-9]\{40\}"' | sed -E ' s/.*"id":"([a-f0-9]{40})".*/\1/' | head -1 || true) "
46-
47- echo " ${branch_sha} "
48- }
49-
5048get_latest_pipeline_id_for_branch () {
5149 local branch=" $1 "
52- local api_url=" ${PROJECT_API_URL} /pipelines?ref=${branch} &status=success& order_by=updated_at &sort=desc&per_page=1"
50+ local api_url=" ${PROJECT_API_URL} /pipelines?ref=${branch} &order_by=id &sort=desc&per_page=1"
5351 local response pipeline_id
5452
53+ log_debug " query pipelines endpoint: ${api_url} "
5554 response=" $( curl --request GET --silent --show-error --header " JOB-TOKEN: ${CI_JOB_TOKEN} " " ${api_url} " || true) "
5655 pipeline_id=" $( echo " ${response} " | grep -o ' "id":[0-9]*' | head -1 | grep -o ' [0-9]*' || true) "
56+ if [[ -n " ${pipeline_id} " ]]; then
57+ log_debug " found latest pipeline_id=${pipeline_id} for branch=${branch} "
58+ else
59+ log_debug " no pipeline found for branch=${branch} ; response='$( response_snippet " ${response} " ) '"
60+ fi
5761 echo " ${pipeline_id} "
5862}
5963
@@ -73,21 +77,24 @@ BASELINE_PIPELINE_ID=""
7377BASELINE_SOURCE=" merge_base"
7478FALLBACK_TO_MASTER=" false"
7579
80+ log_debug " resolved merge_base_sha='${MERGE_BASE_SHA:- <empty>} ' target_branch='${TARGET_BRANCH} '"
81+
7682if [[ -n " ${MERGE_BASE_SHA} " ]]; then
7783 BASELINE_PIPELINE_ID=" $( get_pipeline_id_for_commit " ${MERGE_BASE_SHA} " || true) "
7884fi
7985
8086if [[ -z " ${BASELINE_PIPELINE_ID} " ]]; then
8187 FALLBACK_TO_MASTER=" true"
8288 BASELINE_SOURCE=" ${TARGET_BRANCH} "
89+ log_debug " merge-base pipeline not found; falling back to branch='${TARGET_BRANCH} '"
8390
8491 BASELINE_SHA=" $( git rev-parse " origin/${TARGET_BRANCH} " 2> /dev/null || true) "
85- if [[ -z " ${BASELINE_SHA} " ]]; then
86- BASELINE_SHA=" $( get_branch_head_sha " ${TARGET_BRANCH} " || true) "
92+ log_debug " resolved branch sha from local git: '${BASELINE_SHA:- <empty>} '"
93+ if [[ -n " ${BASELINE_SHA} " ]]; then
94+ BASELINE_PIPELINE_ID=" $( get_pipeline_id_for_commit " ${BASELINE_SHA} " || true) "
8795 fi
88- BASELINE_PIPELINE_ID=" $( get_pipeline_id_for_commit " ${BASELINE_SHA} " || true) "
8996
90- # use latest successful branch pipeline
97+ # Final fallback: use latest branch pipeline
9198 if [[ -z " ${BASELINE_PIPELINE_ID} " ]]; then
9299 BASELINE_PIPELINE_ID=" $( get_latest_pipeline_id_for_branch " ${TARGET_BRANCH} " || true) "
93100 fi
0 commit comments