Only fetch project data in pr edit when editing projects - #13282
Only fetch project data in pr edit when editing projects#13282maxbeizer wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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/projectItemsfinder fields only whenEditable.Projects.Editedis 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
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]>
6895b65 to
93d6362
Compare
williammartin
left a comment
There was a problem hiding this comment.
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?
|
I believe my original repro was in Codespaces. The injected token didn't have Projects access, and 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. |
gh pr editunconditionally fetchesprojectCardsandprojectItemsfields from the GraphQL API, even when the user is only editing non-project fields like--bodyor--title. This causes the command to fail with permission errors when the token lacks project scope:This affects GitHub Apps,
GITHUB_TOKENin Actions, and fine-grained PATs that don't have theprojectpermission.Changes
Restructure
editRunto: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 editand avoids querying project data when the user doesn't intend to edit projects.Conditionally include project fields — Only add
projectCardsandprojectItemsto the finder fields wheneditable.Projects.Editedis true (i.e., when--add-project/--remove-projectflags are used, or the user selects "Projects" in the interactive prompt).Guard project defaults — Only populate
editable.Projects.Defaultandeditable.Projects.ProjectItemswhen project data was actually fetched.Testing
TestProjectsV1Deprecationto setProjects.Editedso project fields are still tested when relevantTestEditSkipsProjectFieldsWhenNotEditingProjectsto verify project fields are excluded from the query when not editing projectsCloses #6274
Related: #8784, #13280