Skip to content

Commit 040780d

Browse files
committed
Use BTI private token for comparison jobs
1 parent 8a365b6 commit 040780d

1 file changed

Lines changed: 58 additions & 8 deletions

File tree

.gitlab/scripts/benchmark-compare.sh

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ readonly TYPE_DIR="${REPORTS_DIR}/${BENCHMARK_TYPE}"
1313
readonly CANDIDATE_RAW_DIR="${TYPE_DIR}/candidate-raw"
1414
readonly BASELINE_RAW_DIR="${TYPE_DIR}/baseline-raw"
1515
readonly BASELINE_INFO_FILE="${TYPE_DIR}/baseline-info.env"
16+
readonly BTI_TOKEN_URL="https://bti-ci-api.us1.ddbuild.io/internal/ci/gitlab/token?owner=DataDog&repository=apm-reliability/dd-trace-java"
1617

1718
mkdir -p "${TYPE_DIR}" "${CANDIDATE_RAW_DIR}" "${BASELINE_RAW_DIR}"
1819

@@ -41,6 +42,36 @@ export FAIL_ON_REGRESSION_THRESHOLD=20.0
4142

4243
readonly JOBS_API_URL="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}"
4344

45+
get_private_token() {
46+
local auth_header bti_response http_status response_body private_token
47+
48+
if ! auth_header="$(authanywhere --audience sdm 2>&1)"; then
49+
echo "Failed to get authanywhere token: ${auth_header}" >&2
50+
return 1
51+
fi
52+
53+
bti_response="$(
54+
curl -w "\nHTTP_STATUS:%{http_code}" --silent --show-error \
55+
--header "${auth_header}" \
56+
"${BTI_TOKEN_URL}" 2>&1
57+
)"
58+
http_status="$(echo "${bti_response}" | rg "HTTP_STATUS:" | sed 's/HTTP_STATUS://')"
59+
response_body="$(echo "${bti_response}" | sed '/HTTP_STATUS:/d')"
60+
61+
if [[ "${http_status}" != "200" ]]; then
62+
echo "BTI token request failed with status ${http_status}." >&2
63+
return 1
64+
fi
65+
66+
private_token="$(echo "${response_body}" | rg -o '"token":"[^"]*"' | sed 's/"token":"\([^"]*\)"/\1/')"
67+
if [[ -z "${private_token}" ]]; then
68+
echo "Failed to parse private token from BTI response." >&2
69+
return 1
70+
fi
71+
72+
echo "${private_token}"
73+
}
74+
4475
job_pattern_for_type() {
4576
case "${BENCHMARK_TYPE}" in
4677
startup)
@@ -82,19 +113,36 @@ PY
82113

83114
list_matching_jobs() {
84115
local pipeline_id="$1"
85-
local pattern
116+
local pattern private_token response http_status response_body
86117
pattern="$(job_pattern_for_type)"
87118

88-
curl --silent --show-error --fail \
89-
--header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
90-
"${JOBS_API_URL}/pipelines/${pipeline_id}/jobs?scope[]=success&per_page=100" \
91-
| python3 -c '
119+
private_token="$(get_private_token)" || return 1
120+
response="$(
121+
curl -w "\nHTTP_STATUS:%{http_code}" --silent --show-error \
122+
--header "PRIVATE-TOKEN: ${private_token}" \
123+
"${JOBS_API_URL}/pipelines/${pipeline_id}/jobs?scope[]=success&per_page=100" 2>&1
124+
)"
125+
http_status="$(echo "${response}" | rg "HTTP_STATUS:" | sed 's/HTTP_STATUS://')"
126+
response_body="$(echo "${response}" | sed '/HTTP_STATUS:/d')"
127+
128+
if [[ "${http_status}" != "200" ]]; then
129+
echo "ERROR: Failed to list jobs for pipeline ${pipeline_id} (HTTP ${http_status})." >&2
130+
echo "URL: ${JOBS_API_URL}/pipelines/${pipeline_id}/jobs?scope[]=success&per_page=100" >&2
131+
echo "First 300 chars of response body: ${response_body:0:300}" >&2
132+
return 1
133+
fi
134+
135+
echo "${response_body}" | python3 -c '
92136
import json
93137
import re
94138
import sys
95139
96140
pattern = re.compile(sys.argv[1])
97-
jobs = json.load(sys.stdin)
141+
try:
142+
jobs = json.load(sys.stdin)
143+
except Exception as e:
144+
print(f"ERROR: failed to parse jobs JSON: {e}", file=sys.stderr)
145+
sys.exit(1)
98146
for job in jobs:
99147
if not pattern.match(job["name"]):
100148
continue
@@ -109,13 +157,14 @@ download_job_artifacts() {
109157
local output_dir="$2"
110158
local archive_path="${output_dir}/artifacts.zip"
111159
local artifacts_url="${JOBS_API_URL}/jobs/${job_id}/artifacts"
112-
local http_code
160+
local http_code private_token
113161

114162
mkdir -p "${output_dir}"
115163

164+
private_token="$(get_private_token)" || return 1
116165
http_code="$(
117166
curl --silent --show-error --location \
118-
--header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
167+
--header "PRIVATE-TOKEN: ${private_token}" \
119168
--output "${archive_path}" \
120169
--write-out "%{http_code}" \
121170
"${artifacts_url}"
@@ -146,6 +195,7 @@ download_job_artifacts() {
146195
if [[ -n "${CI_PROJECT_URL:-}" ]]; then
147196
echo "See job details: ${CI_PROJECT_URL}/-/jobs/${job_id}" >&2
148197
fi
198+
echo "Artifacts URL: ${artifacts_url}" >&2
149199
return 1
150200
fi
151201

0 commit comments

Comments
 (0)