user15181
user15181

Reputation: 171

How to invite people to private GitHub repo through command line interface?

Usually one must click link "Invite teams or people" after accessing "https://github.com///settings/access" in a web browser. But, I wish to do this through a command line interface, because I must invite many persons. Is it possible?

Upvotes: 8

Views: 3917

Answers (3)

botenvouwer
botenvouwer

Reputation: 4432

You can use the github cli or call the github api directly through curl. In this example I add a member to a company repo using the github cli:

gh api "orgs/$target_repo/teams/$team/repos/$target_repo/$repo_new_name" -X PUT -f permission=admin

Also see the docs: https://docs.github.com/en/rest/reference/teams#add-or-update-team-repository-permissions

For your situation you can use this endpoint:

https://docs.github.com/en/rest/reference/repos#add-a-repository-collaborator

Upvotes: 1

VonC
VonC

Reputation: 1323125

You could use the GitHub API in order to add a collaborator

PUT /repos/:owner/:repo/collaborators/:username

See for instance here:

curl -H "Authorization: token YOUR_TOKEN" "https://api.github.com/repos/YOUR_USER_NAME/YOUR_REPO/collaborators/COLLABORATOR_USER_NAME" -X PUT -d '{"permission":"admin"}'

With permission level being one of:

  • pull - can pull, but not push to or administer this repository.
  • push - can pull and push, but not administer this repository.
  • admin - can pull, push and administer this repository.
  • maintain - Recommended for project managers who need to manage the repository without access to sensitive or destructive actions.
  • triage - Recommended for contributors who need to proactively manage issues and pull requests without write access.

(default is "push")

Update Sept. 2020, considering GitHub CLI gh is now 1.0, it could be a good feature to add (a kind of gh repo invite)

In the meantime, you can use gh pi to make a similar API call, automatically authenticated, with -f to add POST fields.

gh api repos/YOUR_USER_NAME/YOUR_REPO/collaborators/COLLABORATOR_USER_NAME" -f '{"permission":"admin"}'

Upvotes: 5

Joaquín
Joaquín

Reputation: 336

An alternative using hub:

1- Check all users with permissions in your repo:

hub api --flat 'repos/YOUR_USER_OR_ORGANIZATION_NAME/YOUR_REPO/collaborators' | grep -E 'login|permissions'

2- Give permission to an user :

hub api 'repos/YOUR_USER_OR_ORGANIZATION_NAME/YOUR_REPO/collaborators/COLLABORATOR_USER_NAME' -H X:PUT -H d:'{"permission":"admin"}'

Upvotes: 1

Related Questions