Skip to content

Only fetch project data in pr edit when editing projects#13282

Open
maxbeizer wants to merge 1 commit into
cli:trunkfrom
maxbeizer:fix/pr-edit-conditional-project-fetch
Open

Only fetch project data in pr edit when editing projects#13282
maxbeizer wants to merge 1 commit into
cli:trunkfrom
maxbeizer:fix/pr-edit-conditional-project-fetch

Conversation

@maxbeizer
Copy link
Copy Markdown
Contributor

gh pr edit unconditionally fetches projectCards and projectItems fields from the GraphQL API, even when the user is only editing non-project fields like --body or --title. This causes the command to fail with permission errors when the token lacks project scope:

GraphQL: Resource not accessible by integration (repository.pullRequest.projectItems.nodes.0),
         Resource not accessible by integration (repository.pullRequest.projectItems.nodes.1)

This affects GitHub Apps, GITHUB_TOKEN in Actions, and fine-grained PATs that don't have the project permission.

Changes

Restructure editRun to:

  1. Prompt before fetching — In interactive mode, ask the user which fields to edit before fetching the PR. This matches the pattern already used by gh issue edit and avoids querying project data when the user doesn't intend to edit projects.

  2. Conditionally include project fields — Only add projectCards and projectItems to the finder fields when editable.Projects.Edited is true (i.e., when --add-project/--remove-project flags are used, or the user selects "Projects" in the interactive prompt).

  3. Guard project defaults — Only populate editable.Projects.Default and editable.Projects.ProjectItems when project data was actually fetched.

Testing

  • Updated TestProjectsV1Deprecation to set Projects.Edited so project fields are still tested when relevant
  • Added TestEditSkipsProjectFieldsWhenNotEditingProjects to verify project fields are excluded from the query when not editing projects
  • All 21 existing tests continue to pass

Closes #6274
Related: #8784, #13280

When running gh pr edit with non-project flags (e.g. --body, --title),
the command unconditionally fetched projectCards and projectItems fields
from the GraphQL API. This caused the command to fail with "Resource not
accessible by integration" when the token lacked project permissions,
even though no project changes were requested.

Restructure editRun to:
- Prompt for which fields to edit before fetching the PR (in interactive
  mode), matching the pattern already used by gh issue edit
- Only include projectCards and projectItems in the query when
  editable.Projects.Edited is true
- Guard project defaults population behind the same condition

Closes cli#6274

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added external pull request originating outside of the CLI core team needs-triage needs to be reviewed labels Apr 24, 2026
@maxbeizer maxbeizer changed the title Only fetch project data in pr edit when editing projects test Apr 24, 2026
@maxbeizer maxbeizer changed the title test Only fetch project data in pr edit when editing projects Apr 24, 2026
@maxbeizer maxbeizer marked this pull request as ready for review April 25, 2026 16:53
@maxbeizer maxbeizer requested a review from a team as a code owner April 25, 2026 16:53
Copy link
Copy Markdown
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 updates gh pr edit to avoid fetching GitHub Projects data from GraphQL unless the user is actually editing project associations, preventing permission failures for tokens without project scope.

Changes:

  • Prompts for which fields to edit before fetching the PR in interactive mode, so project fields are only queried when selected.
  • Conditionally includes projectCards/projectItems finder fields only when Editable.Projects.Edited is true.
  • Adds/updates tests to ensure project fields are present only when relevant, including a new test that asserts they’re skipped for non-project edits.
Show a summary per file
File Description
pkg/cmd/pr/edit/edit.go Restructures editRun to prompt earlier (interactive) and to conditionally fetch/populate project-related fields only when editing projects.
pkg/cmd/pr/edit/edit_test.go Updates existing project deprecation test setup and adds coverage ensuring project fields are excluded when not editing projects.

Copilot's findings

Tip

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

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team needs-triage needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh pr edit does not work with only pull-requests: write permissions

2 participants