Skip to content

Add PR branch checkout when pull request context is available#1324

Merged
pelikhan merged 12 commits intomainfrom
copilot/configure-pull-request-branch
Oct 7, 2025
Merged

Add PR branch checkout when pull request context is available#1324
pelikhan merged 12 commits intomainfrom
copilot/configure-pull-request-branch

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 7, 2025

Add checkout step for pull_request events using JavaScript

This PR adds a new step after git configuration that checks out the PR branch when PR context is available, now implemented using JavaScript and actions/github-script@v8.

Latest Changes

Fixed Tests with Coverage

Updated test assertions to match the current Claude Code version 2.0.9:

  • Updated TestClaudeEngine to expect version 2.0.9 instead of 2.0.1
  • Updated TestCodexAIConfiguration to expect version 2.0.9 instead of 2.0.1

Test Results:

  • ✅ All unit tests pass (100% success rate)
  • ✅ Overall test coverage: 81.7% of statements
  • ✅ PR checkout function coverage: 100% (generatePRReadyForReviewCheckout)
  • ✅ All linting passes with no warnings
  • ✅ Code formatting is correct

Code Cleanup

Removed unused code to fix linter warnings:

  • Removed generatePRBranchCheckout function from pr.go (replaced by generatePRReadyForReviewCheckout)
  • Removed checkoutPRScript variable from sh.go (replaced by checkoutPRBranchScript which uses JavaScript)

The old shell-based implementation has been completely replaced by the new JavaScript-based implementation using actions/github-script@v8.

Changes

Migrated to exec.exec from @actions/exec

Replaced execSync from Node's child_process with exec.exec from @actions/exec, which is already available in the actions/github-script environment:

JavaScript Implementation:

await exec.exec("git", ["fetch", "origin", branchName]);
await exec.exec("git", ["checkout", branchName]);

Key Benefits

  • Uses @actions/exec: Leverages the built-in exec module from actions/github-script, which is designed for GitHub Actions
  • Better integration: Uses the same execution framework as other GitHub Actions
  • Safer argument handling: Passes arguments as an array instead of string interpolation, avoiding shell injection risks
  • Better error reporting: exec.exec provides better error messages and integrates with GitHub Actions logging
  • Async/await: Uses proper async/await pattern instead of synchronous execution
  • Clean codebase: Removed unused legacy shell-based implementation
  • Complete test coverage: 100% coverage for PR checkout functionality

Implementation

  1. New JavaScript file (pkg/workflow/js/checkout_pr_branch.cjs) - Implements the checkout logic in JavaScript with proper error handling using exec.exec from @actions/exec
  2. Updated js.go - Added embed directive for the new JavaScript file
  3. Updated compiler - Always generates the checkout step after git configuration (when contents permission exists) using actions/github-script@v8
  4. New function (generatePRReadyForReviewCheckout in pr.go) - Generates the checkout step with simple github.event.pull_request condition
  5. Comprehensive tests - Validates the feature works correctly for all scenarios, updated to check for JavaScript patterns
  6. Code cleanup - Removed unused generatePRBranchCheckout function and checkoutPRScript variable
  7. Fixed tests - Updated version assertions to match current Claude Code version

Verification

✅ All PR checkout tests pass (100%)
✅ All unit tests pass (100% success rate)
✅ Test coverage: 81.7% overall, 100% for PR checkout function
✅ Linting passes with no warnings
✅ Code formatting is correct
✅ Handles pull_request events (all action types)
✅ Handles issue_comment events on PRs
✅ Handles command triggers on PRs
✅ Step is skipped for non-PR events
✅ All workflows recompiled successfully

Original prompt

In the agentic job, add a step after setting the git user to configure the correct branch when the event is a pull request and the type is read_for_review.

The PR branch is stored in the variable github.event.pull_request.head.ref. Add a script that fetches the branch and checks it out. Remember that the default behavior of actions/checkout is detached.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 7, 2025 18:29
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Add branch configuration step for PR review Add PR branch checkout for pull_request ready_for_review events Oct 7, 2025
Copilot AI requested a review from pelikhan October 7, 2025 18:43
@pelikhan
Copy link
Copy Markdown
Collaborator

pelikhan commented Oct 7, 2025

@copilot this applies to all pull_request event types, not just ready_for_review

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 7, 2025

@copilot this applies to all pull_request event types, not just ready_for_review

Fixed in commit. The checkout step now applies to all pull_request event types, not just ready_for_review. The runtime condition is simplified to check only github.event_name == 'pull_request' without the action check.

Copilot AI changed the title Add PR branch checkout for pull_request ready_for_review events Add PR branch checkout for all pull_request events Oct 7, 2025
Comment thread pkg/workflow/pr.go
…condition

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title Add PR branch checkout for all pull_request events Add PR branch checkout when pull request context is available Oct 7, 2025
Copilot AI requested a review from pelikhan October 7, 2025 20:56
if: |
github.event.pull_request
run: |
set -e
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot use javascript and actions/github-script instead of shell.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Migrated to JavaScript with actions/github-script@v8 in commit. The checkout step now uses JavaScript instead of shell script, consistent with other safe-outputs jobs.

…hell

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI requested a review from pelikhan October 7, 2025 21:18
Comment thread pkg/workflow/pr.go
RenderConditionAsIf(yaml, condition, " ")

// Use actions/github-script instead of shell script
yaml.WriteString(" uses: actions/github-script@v8\n")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot add if expression that checks that github.event.pull_request is present

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The if expression checking github.event.pull_request is already present on lines 101-102 of pr.go. It's rendered in the YAML as:

if: |
  github.event.pull_request

This ensures the step only runs when PR context is available.

Comment thread pkg/workflow/js/checkout_pr_branch.cjs Outdated
* This script handles both pull_request events and comment events on PRs
*/

const { execSync } = require("child_process");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot use exec.getExecOut from actions/core (already ambient in actions/github-script)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to use exec.exec from @actions/exec in commit. The checkout now uses await exec.exec("git", ["fetch", "origin", branchName]) instead of execSync, which is safer and better integrated with GitHub Actions.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI requested a review from pelikhan October 7, 2025 22:22
@pelikhan pelikhan marked this pull request as ready for review October 7, 2025 22:22
@pelikhan
Copy link
Copy Markdown
Collaborator

pelikhan commented Oct 7, 2025

@copilot format, lint, fix tests

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
…variable

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 7, 2025

@copilot format, lint, fix tests

Completed in commit. Removed unused generatePRBranchCheckout function and checkoutPRScript variable. All linting passes, formatting is correct, and all PR-related tests pass.

@pelikhan
Copy link
Copy Markdown
Collaborator

pelikhan commented Oct 7, 2025

@copilot fix tests with coverage

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Oct 7, 2025

@copilot fix tests with coverage

Fixed in commit ba317a9. All tests now pass with 81.7% overall coverage and 100% coverage for the PR checkout function (generatePRReadyForReviewCheckout). Updated test assertions to use Claude Code version 2.0.9.

@pelikhan pelikhan merged commit 9982620 into main Oct 7, 2025
18 checks passed
@pelikhan pelikhan deleted the copilot/configure-pull-request-branch branch October 7, 2025 22:48
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