Detect missing fine grain permission scopes, notify user on steps to take to refresh scopes
- Dominant language
- Go
- Stars
- 46.3k
- Forks
- 9k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 89
Description
### Describe the bug
The Github REST API docs specify that creating a PR using a fine-grained permissions token only requires the
> ["Pull requests" repository permissions (write)](https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#create-a-pull-request)
Adding this permission to the fine-grained token also automatically adds the "Metadata" permission as mandatory.
However, this results in an error along the lines of: `GraphQL: Resource not accessible by personal access token (repository.defaultBranchRef)`.
### Steps to reproduce the behavior
1. Create a fine-grained personal access token with the permissions:
- Pull Requests: Read and write
- Metadata (mandatory): Read-only
3. Locally, create a new branch in a repository, make a commit to it and push the branch to the remote
4. Run `gh pr create`, either interactively, or with flags
5. See an error like: `GraphQL: Resource not accessible by personal access token (repository.defaultBranchRef)`
If the "Contents: Read-only" permission is also added to the fine-grained personal access token, this issue goes away.
### Expected vs actual behavior
**Expected behaviour:** Able to create a PR using the GH CLI using a fine grained permissions token with permissions that are defined in the docs.
**Actual behaviour:** Extra permissions are required.
This seems to be either:
- An error in the docs
- REST API docs don't line up with the equivalent action(s) in the GH CLI
- A bug in the scope of the "Pull Requests: Read and write" permission.
### Logs
```
>> DEBUG=api gh pr create
[git remote -v]
[git config --get-regexp ^remote\..*\.gh-resolved$]
* Request at 2024-08-01 11:47:35.435635 +0100 BST m=+0.183569918
* Request to https://api.github.com/graphql
> POST /graphql HTTP/1.1
> Host: api.github.com
> Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
> Authorization: token [READACTED]
> Content-Length: 405
> Content-Type: application/json; charset=utf-8
> Graphql-Features: merge_queue
> Time-Zone: Europe/London
> User-Agent: GitHub CLI 2.53.0
GraphQL query:
fragment repo on Repository {
id
name
owner { login }
viewerPermission
defaultBranchRef {
name
}
isPrivate
}
query RepositoryNetwork {
viewer { login }
[READACTED]: repository(owner: "[READACTED]", name: "[READACTED]") {
...repo
parent {
...repo
}
}
}
GraphQL variables: null
< HTTP/2.0 200 OK
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
< Content-Security-Policy: default-src 'none'
< Content-Type: application/json; charset=utf-8
< Date: Thu, 01 Aug 2024 10:47:35 GMT
< Github-Authentication-Token-Expiration: 2024-08-31 11:36:27 +0100
< Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
< Server: github.com
< Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
< Vary: Accept-Encoding, Accept, X-Requested-With
< X-Content-Type-Options: nosniff
< X-Frame-Options: deny
< X-Github-Media-Type: github.v4; param=merge-info-preview.nebula-preview; format=json
< X-Github-Request-Id: EA39:9537D:291A3FF:2BBE2CD:66AB67C7
< X-Ratelimit-Limit: 5000
< X-Ratelimit-Remaining: 4980
< X-Ratelimit-Reset: 1722510409
< X-Ratelimit-Resource: graphql
< X-Ratelimit-Used: 20
< X-Xss-Protection: 0
{
"data": {
"viewer": {
"login": "[READACTED]"
},
"[READACTED]": {
"id": "[READACTED]=",
"name": "[READACTED]",
"owner": {
"login": "[READACTED]"
},
"viewerPermission": "ADMIN",
"defaultBranchRef": null,
"isPrivate": true,
"parent": null
}
},
"errors": [
{
"type": "FORBIDDEN",
"path": [
"[READACTED]",
"defaultBranchRef"
],
"extensions": {
"saml_failure": false
},
"locations": [
{
"line": 7,
"column": 3
}
],
"message": "Resource not accessible by personal access token"
}
]
}
* Request took 331.800666ms
GraphQL: Resource not accessible by personal access token ([READACTED].defaultBranchRef)
```
Contributor guide
Assessment
This issue has not been assessed yet.