Skip to content

Conversation

@thoreinstein
Copy link
Owner

No description provided.

The previous message "Invalid JIRA CLI command" was misleading since
the error occurs during client initialization, not command parsing.
The new message "Could not initialize JIRA client" more accurately
describes the failure point.
When merging PRs from a worktree, `gh pr merge --delete-branch` fails
with "fatal: 'main' is already used by worktree" because gh tries to
checkout main locally before deleting the branch.

Change the approach: skip the --delete-branch flag on gh pr merge and
instead delete the remote branch via the GitHub API directly. This is
worktree-safe since it only touches the remote ref.

- Add --delete-branch/-d flag to pr merge command
- Support config fallback via github.delete_branch_on_merge
- Call DeleteBranch() API after successful merge
- Gracefully handle already-deleted branches (repo auto-delete)
Config structs in tests were only partially initialized, leaving the
AI field at zero value. This could cause tests to pass/fail for wrong
reasons if default behavior changes.

Explicitly set AI.Enabled=false in all three test functions to make
test dependencies clear and improve test reliability.

Fixes: rig-wpq
Copilot AI review requested due to automatic review settings January 15, 2026 19:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issues related to branch deletion after merging PRs, updates error messages, and adds missing test configuration fields. The main fix addresses a worktree conflict that occurs when using gh pr merge --delete-branch by handling branch deletion separately via the API.

Changes:

  • Modified merge workflow to handle branch deletion separately via API to avoid worktree conflicts
  • Added --delete-branch flag to pr merge command with proper precedence logic (flag > config)
  • Updated error message for JIRA client initialization to be more accurate
  • Added missing AI config field to test configurations

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pkg/workflow/steps.go Implements API-based branch deletion after merge to avoid worktree conflicts
pkg/github/cli_client.go Removes --delete-branch flag from MergePR method with explanatory comment
cmd/pr_merge.go Adds --delete-branch flag and implements precedence logic (flag > config > default)
pkg/workflow/router_test.go Adds missing AI config field to test configurations
cmd/work.go Updates error message for JIRA client initialization

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +324 to +329
// Check if it's a "not found" error (branch already deleted by GitHub)
errStr := err.Error()
if !strings.Contains(errStr, "Reference does not exist") && !strings.Contains(errStr, "404") {
e.logger.Warn("failed to delete remote branch", "branch", wf.Context.BranchName, "error", err)
}
// Non-fatal either way: merge succeeded
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

Error detection by string matching is fragile. Consider checking the error type or using a more structured approach. If the GitHub API returns specific error types or codes, use those instead of substring matching on the error message.

Suggested change
// Check if it's a "not found" error (branch already deleted by GitHub)
errStr := err.Error()
if !strings.Contains(errStr, "Reference does not exist") && !strings.Contains(errStr, "404") {
e.logger.Warn("failed to delete remote branch", "branch", wf.Context.BranchName, "error", err)
}
// Non-fatal either way: merge succeeded
// Best-effort: log any failure, but do not treat it as fatal since merge succeeded.
e.logger.Warn("failed to delete remote branch", "branch", wf.Context.BranchName, "error", err)

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings January 15, 2026 19:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// Build workflow options
// Determine if we should delete branch (flag takes precedence over config)
var deleteBranch *bool
if cmd.Flags().Changed("delete-branch") {
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

The variable cmd is not defined in the scope of the runPRMerge function. This will cause a compilation error. Use prMergeCmd.Flags().Changed(\"delete-branch\") instead to reference the global command variable.

Suggested change
if cmd.Flags().Changed("delete-branch") {
if prMergeCmd.Flags().Changed("delete-branch") {

Copilot uses AI. Check for mistakes.
if err := e.github.DeleteBranch(ctx, wf.Context.BranchName); err != nil {
// Check if it's a "not found" error (branch already deleted by GitHub)
errStr := err.Error()
if !strings.Contains(errStr, "Reference does not exist") && !strings.Contains(errStr, "404") {
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

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

The error checking relies on string matching which is fragile and may not catch all cases. Consider checking for specific error types or wrapping errors with typed error handling for more robust detection of 'not found' errors.

Copilot uses AI. Check for mistakes.
The runPRMerge function referenced cmd.Flags().Changed() but cmd
was not in scope. Pass the cobra.Command as a parameter so the
function can check whether --delete-branch was explicitly set.

This fixes the build error:
  cmd/pr_merge.go:152:6: undefined: cmd
The preflight step checks PR approval, CI status, and Jira state, but
the error guidance incorrectly suggested fixing "uncommitted changes"
and "required tools" - completely irrelevant to actual failure modes.

Replace static generic guidance with dynamic help that parses the
failure reason and provides specific remediation steps:
- PR not approved → get approval or use --skip-approval
- CI failing → check GitHub CI status
- PR not open → verify PR state
- Jira not in review → update ticket or use --no-jira
- Fetch failed → check PR number and connectivity

Closes: rig-061
Copilot AI review requested due to automatic review settings January 15, 2026 20:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Two code paths could cause IsReady() to return false with empty
FailureReason, resulting in useless error messages:

1. When Jira client is unavailable (nil or !IsAvailable()) but
   --skip-jira wasn't set - JiraSkipped stayed false, causing
   the check to fail silently.

2. When Jira ticket fetch failed with an error - only a warning
   was logged but JiraSkipped stayed false.

Fix both by setting JiraSkipped=true with appropriate warnings
when Jira cannot be checked, treating these as soft skips rather
than hard failures. Jira issues shouldn't block PR merges.

Closes: rig-061
The switch statement lacked a default case for TicketSourceUnknown,
causing JiraSkipped to remain false when the ticket couldn't be
identified as Jira or Beads. This led to IsReady() returning false
with an empty FailureReason.

Add default case that sets JiraSkipped=true with a warning.

Part of: rig-061
The beads detection code looked for .beads/beads.jsonl but the actual
beads CLI uses .beads/issues.jsonl as the issue database. This caused
rig pr merge to fail to recognize beads projects, falling through to
Jira checks which then failed with empty FailureReason.

Update BeadsFileName constant and all related tests to use the correct
filename.

Closes: rig-061
@thoreinstein thoreinstein merged commit 7e54767 into main Jan 15, 2026
5 checks passed
@thoreinstein thoreinstein deleted the rig-0ib branch January 15, 2026 20:54
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