Skip to content

Commit ef5516f

Browse files
committed
Fix reply-to-feedback authorization api
Signed-off-by: Derek Misler <derek.misler@docker.com>
1 parent 971618e commit ef5516f

File tree

1 file changed

+52
-13
lines changed

1 file changed

+52
-13
lines changed

.github/workflows/review-pr.yml

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -417,25 +417,53 @@ jobs:
417417
id: auth
418418
shell: bash
419419
env:
420+
GH_TOKEN: ${{ secrets.CAGENT_ORG_MEMBERSHIP_TOKEN }}
421+
ORG: ${{ inputs.auto-review-org }}
422+
USERNAME: ${{ github.event.comment.user.login }}
420423
# Use the event context expression — $GITHUB_EVENT_PATH is empty/minimal
421424
# in workflow_call context, so jq parsing it fails silently.
422425
AUTHOR_ASSOCIATION: ${{ github.event.comment.author_association }}
423426
run: |
424-
if [ -z "$AUTHOR_ASSOCIATION" ]; then
425-
echo "::warning::Could not read author_association from event context"
427+
if [ -z "$GH_TOKEN" ]; then
428+
echo "::warning::CAGENT_ORG_MEMBERSHIP_TOKEN not configured — falling back to author_association"
429+
case "$AUTHOR_ASSOCIATION" in
430+
OWNER|MEMBER|COLLABORATOR)
431+
echo "authorized=true" >> $GITHUB_OUTPUT
432+
echo "✅ Authorized via author_association (fallback)"
433+
;;
434+
*)
435+
echo "authorized=false" >> $GITHUB_OUTPUT
436+
echo "⏭️ Not authorized via author_association (fallback)"
437+
;;
438+
esac
439+
exit 0
440+
fi
441+
# Check org membership with explicit error handling
442+
if ! RESPONSE=$(gh api "orgs/$ORG/members/$USERNAME" --silent -i 2>/dev/null); then
443+
echo "authorized=false" >> $GITHUB_OUTPUT
444+
echo "⏭️ API call failed or @$USERNAME is not a $ORG org member — not authorized"
445+
exit 0
446+
fi
447+
# Verify response starts with HTTP status line before parsing
448+
if ! echo "$RESPONSE" | head -1 | grep -q '^HTTP/'; then
449+
echo "::warning::Unexpected API response format"
450+
echo "authorized=false" >> $GITHUB_OUTPUT
451+
exit 0
452+
fi
453+
# Extract status code from HTTP/1.1 204 No Content format
454+
STATUS=$(echo "$RESPONSE" | head -1 | grep -oP 'HTTP/[0-9.]+ \K[0-9]+')
455+
if [ -z "$STATUS" ]; then
456+
echo "::warning::Failed to extract HTTP status code"
426457
echo "authorized=false" >> $GITHUB_OUTPUT
427458
exit 0
428459
fi
429-
case "$AUTHOR_ASSOCIATION" in
430-
OWNER|MEMBER|COLLABORATOR)
431-
echo "authorized=true" >> $GITHUB_OUTPUT
432-
echo "✅ Author is $AUTHOR_ASSOCIATION — authorized to trigger reply"
433-
;;
434-
*)
435-
echo "authorized=false" >> $GITHUB_OUTPUT
436-
echo "⏭️ Author is $AUTHOR_ASSOCIATION — not authorized for reply"
437-
;;
438-
esac
460+
if [ "$STATUS" = "204" ]; then
461+
echo "authorized=true" >> $GITHUB_OUTPUT
462+
echo "✅ @$USERNAME is a $ORG org member — authorized"
463+
else
464+
echo "authorized=false" >> $GITHUB_OUTPUT
465+
echo "⏭️ @$USERNAME is not a $ORG org member — not authorized"
466+
fi
439467
440468
- name: Notify unauthorized user
441469
if: steps.check.outputs.is_agent == 'true' && steps.auth.outputs.authorized == 'false'
@@ -448,12 +476,23 @@ jobs:
448476
ROOT_COMMENT_ID: ${{ steps.check.outputs.root_comment_id }}
449477
AUTHOR: ${{ github.event.comment.user.login }}
450478
run: |
479+
# Validate ROOT_COMMENT_ID is a valid integer before using it
480+
if [ -z "$ROOT_COMMENT_ID" ]; then
481+
echo "::error::ROOT_COMMENT_ID is not set"
482+
exit 1
483+
fi
484+
485+
if ! [[ "$ROOT_COMMENT_ID" =~ ^[0-9]+$ ]]; then
486+
echo "::error::ROOT_COMMENT_ID is not a valid integer: '$ROOT_COMMENT_ID'"
487+
exit 1
488+
fi
489+
451490
jq -n \
452491
--arg body "Sorry @$AUTHOR, conversational replies are currently available to repository collaborators only. Your feedback has still been captured and will be used to improve future reviews.
453492
454493
<!-- cagent-review-reply -->" \
455494
--argjson reply_to "$ROOT_COMMENT_ID" \
456-
'{body: $body, in_reply_to_id: $reply_to}' | \
495+
'{body: $body, in_reply_to: $reply_to}' | \
457496
gh api "repos/$REPO/pulls/$PR_NUMBER/comments" --input -
458497
459498
- name: Build thread context

0 commit comments

Comments
 (0)