Skip to content

Commit b388ca5

Browse files
committed
fix(ci): post PR comments via GitHub API + dd-octo-sts token (drop pr-commenter)
1 parent 549012a commit b388ca5

3 files changed

Lines changed: 86 additions & 52 deletions

File tree

.gitlab/benchmarks/post-pr-comment.sh

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,13 @@
66
#
77
# Required env:
88
# DDPROF_COMMIT_BRANCH – branch name used to locate the open PR
9-
# CI_JOB_TOKEN – used to clone benchmarking-platform if needed
109
# Optional env:
1110
# CI_PIPELINE_URL, DDPROF_COMMIT_SHA
1211

1312
set -euo pipefail
1413

1514
REPORTS_DIR="${1:-reports}"
16-
REPO="DataDog/java-profiler"
17-
18-
# Skip for main / unset branches (no PR to comment on)
19-
if [ -z "${DDPROF_COMMIT_BRANCH:-}" ] || \
20-
[ "${DDPROF_COMMIT_BRANCH}" = "main" ] || \
21-
[ "${DDPROF_COMMIT_BRANCH}" = "master" ]; then
22-
echo "Skipping PR comment for branch: ${DDPROF_COMMIT_BRANCH:-<unset>}"
23-
exit 0
24-
fi
25-
26-
# Acquire pr-commenter from benchmarking-platform if not already on PATH
27-
if ! command -v pr-commenter >/dev/null 2>&1; then
28-
PLATFORM_DIR=$(mktemp -d)
29-
trap "rm -rf ${PLATFORM_DIR}" EXIT
30-
git clone --depth 1 --branch dd-trace-go \
31-
"https://github.com/DataDog/benchmarking-platform" "${PLATFORM_DIR}"
32-
export PATH="${PLATFORM_DIR}/tools:${PATH}"
33-
fi
15+
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3416

3517
# Aggregate all per-cell reports into a single comment body
3618
SECTIONS=""
@@ -51,14 +33,15 @@ if [ -z "${SECTIONS}" ]; then
5133
exit 0
5234
fi
5335

54-
COMMENT_BODY="## Benchmark Results
36+
BODY_FILE=$(mktemp)
37+
trap 'rm -f "${BODY_FILE}"' EXIT
38+
cat > "${BODY_FILE}" <<EOF
39+
## Benchmark Results
5540
5641
Pipeline: ${CI_PIPELINE_URL:-} Commit: \`${DDPROF_COMMIT_SHA:-unknown}\`
5742
58-
${SECTIONS}"
43+
${SECTIONS}
44+
EOF
5945

60-
echo "${COMMENT_BODY}" | pr-commenter \
61-
--for-repo="${REPO}" \
62-
--for-pr="${DDPROF_COMMIT_BRANCH}" \
63-
--header="Benchmarks" \
64-
--on-duplicate=replace
46+
"${HERE}/../scripts/upsert-github-pr-comment.sh" \
47+
"benchmark-results" "${DDPROF_COMMIT_BRANCH:-}" "${BODY_FILE}"

.gitlab/reliability/post-pr-comment.sh

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,12 @@
66
#
77
# Required env:
88
# DDPROF_COMMIT_BRANCH – branch name used to locate the open PR
9-
# CI_JOB_TOKEN – used to clone benchmarking-platform if needed
109
# Optional env:
1110
# CI_PIPELINE_URL
1211

1312
set -euo pipefail
1413

15-
REPO="DataDog/java-profiler"
16-
17-
# Skip for main / unset branches
18-
if [ -z "${DDPROF_COMMIT_BRANCH:-}" ] || \
19-
[ "${DDPROF_COMMIT_BRANCH}" = "main" ] || \
20-
[ "${DDPROF_COMMIT_BRANCH}" = "master" ]; then
21-
echo "Skipping PR comment for branch: ${DDPROF_COMMIT_BRANCH:-<unset>}"
22-
exit 0
23-
fi
24-
25-
# Acquire pr-commenter from benchmarking-platform if not already on PATH
26-
if ! command -v pr-commenter >/dev/null 2>&1; then
27-
PLATFORM_DIR=$(mktemp -d)
28-
trap "rm -rf ${PLATFORM_DIR}" EXIT
29-
git clone --depth 1 --branch dd-trace-go \
30-
"https://github.com/DataDog/benchmarking-platform" "${PLATFORM_DIR}"
31-
export PATH="${PLATFORM_DIR}/tools:${PATH}"
32-
fi
14+
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3315

3416
# ── Collect failures from REASON_* env vars ────────────────────────────────────
3517
rel_fail=0; rel_failures=""
@@ -73,13 +55,14 @@ else
7355
overall="✅ **All reliability & chaos checks passed**"
7456
fi
7557

76-
COMMENT_BODY="## Reliability & Chaos Results
58+
BODY_FILE=$(mktemp)
59+
trap 'rm -f "${BODY_FILE}"' EXIT
60+
cat > "${BODY_FILE}" <<EOF
61+
## Reliability & Chaos Results
7762
7863
${overall} Pipeline: ${CI_PIPELINE_URL:-}
79-
${rel_failures}${chaos_failures}"
64+
${rel_failures}${chaos_failures}
65+
EOF
8066

81-
echo "${COMMENT_BODY}" | pr-commenter \
82-
--for-repo="${REPO}" \
83-
--for-pr="${DDPROF_COMMIT_BRANCH}" \
84-
--header="Reliability & Chaos" \
85-
--on-duplicate=replace
67+
"${HERE}/../scripts/upsert-github-pr-comment.sh" \
68+
"reliability-results" "${DDPROF_COMMIT_BRANCH:-}" "${BODY_FILE}"
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bash
2+
# Upsert a comment on the java-profiler GitHub PR for the current branch.
3+
#
4+
# Posts (or replaces) a single marker-tagged comment using a short-lived GitHub
5+
# token obtained via dd-octo-sts. No pr-commenter / benchmarking-platform clone
6+
# is required — only dd-octo-sts (present in dd-octo-sts-ci-base) plus curl/jq.
7+
#
8+
# Usage:
9+
# upsert-github-pr-comment.sh <comment-id> <branch> <body-file>
10+
#
11+
# comment-id : unique slug used as an HTML marker to find/replace the comment
12+
# branch : head branch name used to locate the open PR
13+
# body-file : path to a file holding the markdown comment body
14+
#
15+
# Requires in CI: dd-octo-sts CLI + DDOCTOSTS_ID_TOKEN id_token, curl, jq.
16+
# Token policy async-profiler-build.ci grants issues:write + pull_requests:read.
17+
18+
set -euo pipefail
19+
20+
COMMENT_ID="${1:?comment-id required}"
21+
BRANCH="${2:?branch required}"
22+
BODY_FILE="${3:?body-file required}"
23+
REPO="DataDog/java-profiler"
24+
API="https://api.github.com/repos/${REPO}"
25+
26+
log() { echo "[upsert-pr-comment] $*" >&2; }
27+
28+
if [ -z "${BRANCH}" ] || [ "${BRANCH}" = "main" ] || [ "${BRANCH}" = "master" ]; then
29+
log "Skipping PR comment for branch: ${BRANCH:-<unset>}"
30+
exit 0
31+
fi
32+
if [ ! -s "${BODY_FILE}" ]; then
33+
log "Empty body file (${BODY_FILE}) — nothing to post"
34+
exit 0
35+
fi
36+
37+
# 1. Obtain a GitHub token via dd-octo-sts (no stored secrets).
38+
TOKEN=$(dd-octo-sts token --scope "${REPO}" --policy async-profiler-build.ci 2>/dev/null || true)
39+
if [ -z "${TOKEN}" ]; then
40+
log "Failed to obtain GitHub token via dd-octo-sts — skipping comment"
41+
exit 0
42+
fi
43+
AUTH=(-H "Authorization: Bearer ${TOKEN}" -H "Accept: application/vnd.github+json")
44+
45+
# 2. Resolve the open PR for this branch.
46+
PR=$(curl -fsS "${AUTH[@]}" "${API}/pulls?head=DataDog:${BRANCH}&state=open&per_page=1" \
47+
| jq -r '.[0].number // empty')
48+
if [ -z "${PR}" ]; then
49+
log "No open PR found for branch ${BRANCH} — skipping comment"
50+
exit 0
51+
fi
52+
53+
# 3. Prepend a stable marker and build the JSON payload safely.
54+
MARKER="<!-- ${COMMENT_ID} -->"
55+
BODY="${MARKER}"$'\n'"$(cat "${BODY_FILE}")"
56+
PAYLOAD=$(jq -n --arg body "${BODY}" '{body: $body}')
57+
58+
# 4. Find an existing marker comment and PATCH it, otherwise POST a new one.
59+
CID=$(curl -fsS "${AUTH[@]}" "${API}/issues/${PR}/comments?per_page=100" \
60+
| jq -r --arg m "${MARKER}" '.[] | select(.body | contains($m)) | .id' | head -n1)
61+
62+
if [ -n "${CID}" ]; then
63+
curl -fsS -X PATCH "${AUTH[@]}" "${API}/issues/comments/${CID}" -d "${PAYLOAD}" >/dev/null
64+
log "Updated comment ${CID} on PR #${PR}"
65+
else
66+
curl -fsS -X POST "${AUTH[@]}" "${API}/issues/${PR}/comments" -d "${PAYLOAD}" >/dev/null
67+
log "Created comment on PR #${PR}"
68+
fi

0 commit comments

Comments
 (0)