I take the initiative to answer my own question here since after some investigations (thanks to mislav for his help) and trials and errors, I ve found the proper way to add collaborators to a GitHub repo within an organization with the CLI. I think it is worth posting it, hopefully this will help others.
Invite an outside collaborator to a repo within an organization
gh api -X PUT repos/:org/:repo/collaborators/:username -f permission=:perm
the -X PUT
specifies that the request is a PUT
and not a GET
(default request). The repo's identifier is specified by :org/:repo
(note that if the repo is not under an organization, the identifier will be :owner/:repo
). The :perm
argument indicates the type of access, the default value is push
(see here)
So assume I want to provide admin
access to jonsnow
to the repo winterfell
under the organization got
, I will use the following command
gh api -X PUT repos/got/winterfell/collaborators/jonsnow -f permission=admin
Note that if you send an invite for the repo directly, the user will appear as an outside collaborator (not as an organization member)
Add a member to the organization and invite him to a repo
You just need to include the user as a member to the organisation beforehand with
gh api -X PUT /orgs/:org/memberships/:username -f role=:role
and then you can provide him access to a specific repo with the same command as above, i.e.
gh api -X PUT repos/:org/:repo/collaborators/:username -f permission=:perm
Note that the value for the various :role
can be found here
gh api repos/MY_USER_NAME/MY_REPO/coll...
instead ofgh api orgs/MY_ORG/repos/MY_USER_NAME/MY_REPO/coll...
? – Screamorgs/MY_ORG
as you suggested but this doesn't work. Interestingly, the command you suggested return a 404 with the body containing"documentation_url": "https://docs.github.com/rest/reference/repos#check-if-a-user-is-a-repository-collaborator"
(showing that GitHub is somehow able to parse the endpoint) while the initial command returns"documentation_url": "https://docs.github.com/rest"
. So I am now wondering if it is actually possible to add user on a repo within an organisation, maybe the API doesn't support such a feature at this stage. – Neoma