Skip to content

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

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

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

@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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot removed the needs-triage needs to be reviewed label May 12, 2026
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 <[email protected]>
@maxbeizer
maxbeizer force-pushed the fix/pr-edit-conditional-project-fetch branch from 6895b65 to 93d6362 Compare June 30, 2026 20:18

@williammartin williammartin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't quite figure out under what circumstances this error would arise @maxbeizer sorry. Do you have reproduction steps? The only way I can sort of squint at this is if it was running on an older version of GHES than we support where project v1 was still running?

@maxbeizer

Copy link
Copy Markdown
Contributor Author

I believe my original repro was in Codespaces. The injected token didn't have Projects access, and gh pr edit --body ... still queried project data, resulting in Resource not accessible by integration when the PR was attached to a project. So this was on GitHub.com rather than old GHES.

I haven't reproduced it freshly, though, and #13281 may now prevent the ProjectsV2 error from surfacing. This change would still avoid the unnecessary query rather than relying on that error being ignored, but I don't think that cleanup alone warrants keeping this open. Closing this out - thanks for taking a look.

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 ready-for-review

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

4 participants