What level of access do I have on that GitHub repository?
Earlier today, I was trying to work out if I had sufficient privileges to make some changes on a repository.
Without access to the Settings
panel as a non-Admin
on the repo made it a little difficult, so I went looking for API access, and it turns out there are some good APIs for checking collaborator access, such as Get repository permissions for a user as well as Check if a user is a repository collaborator.
We can use this to check i.e. if I personally have access to oapi-codegen
repo:
$ gh api /repos/oapi-codegen/oapi-codegen/collaborators/jamietanna/permission
{
"permission": "admin",
"role_name": "admin",
"user": {
"login": "jamietanna",
"permissions": {
"admin": true,
"maintain": true,
"pull": true,
"push": true,
"triage": true
},
"role_name": "admin",
}
}
(slightly omitted for brevity)
Alternatively, for a user that'll only have read-only access:
$ gh api /repos/oapi-codegen/oapi-codegen/collaborators/jamietanna-bot/permission
{
"permission": "read",
"role_name": "read",
"user": {
"login": "jamietanna-bot",
"permissions": {
"admin": false,
"maintain": false,
"pull": true,
"push": false,
"triage": false
},
"role_name": "read",
}
}
This only works when you have at least push
access to a repository:
% gh api /repos/wiremock/wiremock.org/collaborators/jamietanna/permission
gh: Must have push access to view collaborator permission. (HTTP 403)
{
"message": "Must have push access to view collaborator permission.",
"documentation_url": "https://docs.github.com/rest/collaborators/collaborators#get-repository-permissions-for-a-user",
"status": "403"
}
This also only works for a GitHub User not an App.