Skip to content

Commit d1a65b4

Browse files
github-actions[bot]amikofalvyclaude
committed
fix: add temp file cleanup trap and paginate comment search
- Add EXIT trap to clean up mktemp file - Paginate through all PR comments when searching for the existing marker comment, fixing duplicate-comment risk on PRs with 100+ comments Co-authored-by: Andrew Mikofalvy <amikofalvy@users.noreply.github.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 77c9096 commit d1a65b4

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

.github/scripts/preview/comment-preview-urls.sh

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ COMMENT_MARKER="<!-- preview-environments:stable-urls -->"
1717
COMMENTS_ENDPOINT="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments"
1818
API_HEALTH_URL="${API_URL%/}/health"
1919
COMMENT_BODY_FILE="$(mktemp)"
20+
trap 'rm -f "${COMMENT_BODY_FILE}"' EXIT
2021

2122
cat > "${COMMENT_BODY_FILE}" <<EOF
2223
${COMMENT_MARKER}
@@ -47,19 +48,34 @@ if [ -n "${UI_DEPLOYMENT_URL:-}" ] || [ -n "${API_DEPLOYMENT_URL:-}" ]; then
4748
} >> "${COMMENT_BODY_FILE}"
4849
fi
4950

50-
COMMENTS_JSON="$(
51-
curl --connect-timeout 10 --max-time 30 -fsS \
52-
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
53-
-H "Accept: application/vnd.github+json" \
54-
"${COMMENTS_ENDPOINT}?per_page=100"
55-
)"
51+
EXISTING_COMMENT_ID=""
52+
PAGE=1
53+
while :; do
54+
PAGE_JSON="$(
55+
curl --connect-timeout 10 --max-time 30 -fsS \
56+
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
57+
-H "Accept: application/vnd.github+json" \
58+
"${COMMENTS_ENDPOINT}?per_page=100&page=${PAGE}"
59+
)"
60+
61+
PAGE_MATCH="$(
62+
jq -r \
63+
--arg marker "${COMMENT_MARKER}" \
64+
'[.[] | select(.user.login == "github-actions[bot]" and (.body | contains($marker)))] | last | .id // empty' \
65+
<<< "${PAGE_JSON}"
66+
)"
67+
68+
if [ -n "${PAGE_MATCH}" ]; then
69+
EXISTING_COMMENT_ID="${PAGE_MATCH}"
70+
fi
71+
72+
PAGE_COUNT="$(jq 'length' <<< "${PAGE_JSON}")"
73+
if [ "${PAGE_COUNT}" -lt 100 ]; then
74+
break
75+
fi
5676

57-
EXISTING_COMMENT_ID="$(
58-
jq -r \
59-
--arg marker "${COMMENT_MARKER}" \
60-
'[.[] | select(.user.login == "github-actions[bot]" and (.body | contains($marker)))] | last | .id // empty' \
61-
<<< "${COMMENTS_JSON}"
62-
)"
77+
PAGE=$((PAGE + 1))
78+
done
6379

6480
COMMENT_PAYLOAD="$(jq -Rs '{body: .}' < "${COMMENT_BODY_FILE}")"
6581

0 commit comments

Comments
 (0)