|
| 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