Skip to content

ride-workspace: detect existing PRs and use their branch instead of creating new ones #3

@dhilgaertner

Description

@dhilgaertner

Problem

When /ride-workspace is invoked with a GitHub/GitLab issue URL that already has an open PR, the skill creates a fresh branch from origin/main. This means the existing work on the PR is ignored and a duplicate branch is created, requiring the user to manually switch to the correct branch.

Expected Behavior

Before creating a worktree, the skill should check if the issue already has an open PR. If so, it should:

  1. Use the PR's branch (headRefName) instead of generating a new feature/{repo}-{number}-{slug} branch
  2. Create the worktree tracking that existing remote branch
  3. Add the PR URL as a session link (ride add-link ... --type pr)
  4. Include an "Existing Pull Request" section in the Claude Code prompt so the spawned session knows there's prior work

Proposed Changes to SKILL.md

1. Add PR detection step (between ticket metadata and worktree creation)

GitHub:

gh pr list --repo {owner}/{repo} --search "{issue_number}" --state open \
  --json number,title,headRefName,url --limit 5

GitLab:

GITLAB_HOST={host} glab mr list --repo {org/repo} --search "{issue_number}"

If a PR is found, prefer PRs whose headRefName contains the issue number. Use the PR branch for the worktree instead of creating a new one.

2. Add third git worktree path

# If PR found — use PR's branch:
git -C {repo_path} worktree add {path} \
  -b {pr_branch_name} \
  --track origin/{pr_branch_name}

3. Add PR link to session

ride add-link --session {session_id} --label "Pull Request" --url "{pr_url}" --type pr

4. Update prompt template

When a PR exists, add to the prompt:

## Existing Pull Request

There is an existing open PR for this issue. Review it before planning:

\`\`\`bash
gh pr view {pr_url} --comments
\`\`\`

This workspace is checked out on the PR's branch. Review existing changes with `git log origin/main..HEAD` before adding new work.

And update instructions to include "Review the existing PR and its changes" before planning.

Acceptance Criteria

  • When an issue has an open PR, the worktree uses the PR's branch
  • PR URL is added as a session link
  • Claude Code prompt includes the existing PR context
  • When no PR exists, behavior is unchanged (new branch from main)
  • Works for both GitHub (gh) and GitLab (glab) providers

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions