Skip to content

Fixes an issue when a bot triggers a PR review#60

Merged
derekmisler merged 1 commit intodocker:mainfrom
derekmisler:fix-fun-authorization-issue-when-a-user-comments
Feb 27, 2026
Merged

Fixes an issue when a bot triggers a PR review#60
derekmisler merged 1 commit intodocker:mainfrom
derekmisler:fix-fun-authorization-issue-when-a-user-comments

Conversation

@derekmisler
Copy link
Contributor

@derekmisler derekmisler commented Feb 27, 2026

Summary

When a bot posts a /review comment on a PR, it was inadvertently triggering the manual-review workflow, causing an infinite loop or unintended review runs. This PR fixes that by filtering out bot-authored comments, switching from bare PR numbers to full PR URLs (which work correctly in detached HEAD checkouts), and guarding against artifact upload failures when no verbose log file was produced.

Changes

  • .github/workflows/review-pr.yml: Added github.event.comment.user.type != 'Bot' guard to the manual-review job's if condition, preventing bot comments from triggering the /review workflow.
  • review-pr/action.yml: Replaced bare $PR_NUMBER references with full $PR_URL (https://github.com/${REPO}/pull/${PR_NUMBER}) in all gh pr view and gh pr diff calls, fixing 403/resolution failures in detached HEAD (forked PR) checkouts. Also switches the diff pre-fetch to use github.token (which has contents:read) instead of the App token.
  • action.yml: Added a steps.run-agent.outputs.verbose-log-file != '' check to the "Upload verbose agent log" step so it only runs when a log file was actually produced, avoiding spurious upload failures.
  • review-pr/agents/pr-review.yaml: Updated agent instructions to reflect that the prompt now contains a full PR URL instead of a bare number, and to prefer pre-fetched pr_metadata.json/changed_files.txt before falling back to gh pr view.

How to Test

  • Post a /review comment as a bot account on an open PR and confirm the manual-review workflow does not trigger.
  • Post a /review comment as a human user on a forked PR and confirm the workflow runs successfully, pre-fetches the diff using the full URL, and posts a review without a 403 error.
  • Trigger a workflow run where no verbose log is produced and confirm the "Upload verbose agent log" step is skipped cleanly without an error.

@derekmisler
Copy link
Contributor Author

/describe

@derekmisler derekmisler marked this pull request as ready for review February 27, 2026 15:01
@derekmisler derekmisler requested a review from a team as a code owner February 27, 2026 15:01
@docker-agent
Copy link
Contributor

docker-agent bot commented Feb 27, 2026

✅ PR description has been generated and updated!

@derekmisler derekmisler self-assigned this Feb 27, 2026
Copy link
Contributor

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Approved - No issues found.

This PR successfully addresses the bot authorization issue by:

  • Adding a bot filter to prevent bot-triggered reviews (github.event.comment.user.type != 'Bot')
  • Improving token usage by separating read permissions (github.token) from review posting permissions (app token)
  • Fixing detached HEAD issues by using full PR URLs instead of just PR numbers
  • Adding proper conditional checks for artifact uploads

The changes follow GitHub Actions best practices and include clear documentation updates. All potential concerns were verified and found to be properly handled.

@derekmisler derekmisler force-pushed the fix-fun-authorization-issue-when-a-user-comments branch from c1c46d8 to 82cec73 Compare February 27, 2026 15:55
@derekmisler
Copy link
Contributor Author

/review

Copy link
Contributor

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

✅ This PR effectively addresses the bot-triggered infinite review loop issue with well-structured changes. However, I've identified two notable concerns regarding the token handling and fallback mechanism implementation that should be addressed.

Key Changes Reviewed

  • ✅ Bot filtering in workflow conditions is correct
  • ⚠️ Token override approach needs refinement
  • ⚠️ Fallback mechanism messaging could be more accurate
  • ✅ Artifact upload guard is correctly implemented
  • ✅ Documentation updates are clear and helpful

# Uses github.token (has contents:read) instead of the App token (used for posting reviews).
# Uses the full PR URL instead of just the number — gh can't resolve a bare number
# from a detached HEAD (refs/pull/N/head).
if GH_TOKEN="$READ_TOKEN" gh pr diff "$PR_URL" > pr.diff 2>/dev/null; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Token override may not work reliably due to inline environment variable shadowing

The inline GH_TOKEN="$READ_TOKEN" override, while technically functional in bash, creates potential issues:

  1. The outer env block (lines 144-146) already sets GH_TOKEN to the App token, creating confusion about which token is active
  2. Inline environment variable overrides are fragile and non-standard in GitHub Actions
  3. If the command fails or doesn't properly use the overridden token, there's a risk of token leakage

Recommendation: Use a subshell or separate run block with explicit token scoping:

- name: Pre-fetch diff with read token
  env:
    GH_TOKEN: ${{ github.token }}
  run: |
    if gh pr diff "$PR_URL" > pr.diff 2>/dev/null; then
      echo "✅ Pre-fetched PR diff"
    fi

This makes the token scope explicit and prevents confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VAR=value command is standard POSIX shell — it temporarily overrides the env var for exactly that one process, then reverts. This is well-defined bash behavior, not a GitHub Actions quirk. Both tokens are already in the environment (GH_TOKEN via the env: block, READ_TOKEN alongside it), so there's no "token leakage" risk. A separate step would split the logically-related "get PR information" work for no real benefit.

echo "✅ Pre-fetched PR diff ($(wc -l < pr.diff | tr -d ' ') lines)"
else
echo "::warning::Failed to pre-fetch PR diff — agent will fetch it directly"
echo "::warning::Failed to pre-fetch PR diff — agent will use git diff as fallback"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallback mechanism mentioned but not guaranteed to work

The warning message states "agent will use git diff as fallback", but this fallback is not validated by the orchestration layer:

  1. The code removes pr.diff on failure but doesn't verify that the git-based fallback will succeed
  2. If git diff $(git merge-base origin/main HEAD)...HEAD also fails (e.g., detached HEAD without proper refs, shallow clone issues), there's no error recovery
  3. The warning message implies a guaranteed recovery mechanism, but it's actually best-effort

Recommendation: Either:

  • Soften the message: "agent will attempt git diff as fallback"
  • Or add validation: Test that the git fallback works before proceeding
else
  if git diff $(git merge-base origin/main HEAD 2>/dev/null || echo "main")...HEAD > /dev/null 2>&1; then
    echo "::warning::Failed to pre-fetch PR diff — agent will use git diff as fallback"
  else
    echo "::error::Cannot fetch PR diff and git fallback unavailable"
    exit 1
  fi
  rm -f pr.diff
fi
``` <!-- cagent-review -->

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion to test the git fallback and exit 1 on failure is harmful — we don't want to block the entire review because a pre-fetch failed. The agent has its own fallback chain and should be given a chance to run.

Signed-off-by: Derek Misler <derek.misler@docker.com>
@derekmisler derekmisler force-pushed the fix-fun-authorization-issue-when-a-user-comments branch from 82cec73 to 74b3844 Compare February 27, 2026 16:03
if: |
github.event.issue.pull_request &&
contains(github.event.comment.body, '/review') &&
github.event.comment.user.type != 'Bot'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I better understand what might have happened:
I didn't have the issue until this morning on https://github.com/docker/pinata/pull/39588, after I hit /help to get some pinata PR commands.
(and I did not hit /review ).
as the output of /help the pinata bot displayed all commands, and that triggered the condition contains(github.event.comment.body, '/review').
So filtering out bot-authored comments should guard from this indeed.
(could check if there's a better way testing for ^/review at line start, but otherwise this is fine)

@derekmisler derekmisler merged commit beb6a45 into docker:main Feb 27, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants