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

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?

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"}'

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"}'

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

Related

How to Update collaborator permission from admin to pull (Read) using github api?

I'm trying to update the permission of collaborators across all repos in a Github Organisation. The collaborators are also Organisation members. I want to change permisssion from admin to pull (read). Unable to do so using the github api (v3). Tried with the go package github.com/google/go-github/v28/github and also CURL like mentioned in the doc . Got 204 No Content Response but the changes did not happen.
curl -H "Authorization: token $TOKEN" https://github.com/api/v3/repos/$ORG/$REPO/collaborators/$USER -X PUT -d '{"permission":"pull"}'
Is there any other better way to change the collaborator permissions from admin to pull ?
Please note: the same api allows to change the permission if they are Outside Collaborators (non organisation members)

Is it possible to change the visibility of a private repo using the GitHub API?

I've been looking for a way to convert my private repositories to public ones, but since there are so many of them, I preferred to do it the easy way and resorted to the GitHub API, but this one, as far as I could find, only allows editing the visibility of repositories that are public. Looking through the most up-to-date documentation, I realized that the only endpoints available for access while authenticated are (Repositories, Endpoints available):
GET /user/repos
POST /user/repos
POST /user/projects
I also tried using the browser console and generating a log when I make a request to change visibility, but it doesn't seem to make any requests to the GitHub API.
You can sets a repo's visibility to public (or private) with this REST endpoint:
PATCH /repos/{owner}/{repo}
This does work for repos that are private (despite your comment). And if the repo already has the visibility being requested, the API will return an error message indicating that status.
visibility Parameter
Can be public or private. If your organization is associated with an enterprise account using GitHub Enterprise Cloud or GitHub Enterprise Server 2.20+, visibility can also be internal. The visibility parameter overrides the private parameter when you use both along with the nebula-preview preview header.
Examples
curl
curl \
-X PATCH \
-H "Accept: application/vnd.github.nebula-preview+json" \
https://api.github.com/repos/octocat/hello-world \
-d '{"visibility":"public"}'
gh
gh api -X PATCH /repos/octocat/hello-world -fvisibility=public --preview nebula
2022: I would use the GitHub CLI and its gh repo edit command:
cd /path/to/local/repo
gh repo edit --visibility=public
2020: Yes, you can, but as mention in the repository API, this is still in beta:
You can set the visibility of a repository using the new visibility parameter in the Repositories API, and get a repository's visibility with a new response key.
For more information, see the blog post.
To access repository visibility during the preview period, you must provide a custom media type in the Accept header:
application/vnd.github.nebula-preview+json

How to invite a user to a private github repo within an organisation using the command line

I am trying to add users to a private Github repo within an organisation. Starting from this post, I've simply changed the API endpoint to cope with organizations (as explained here), and I end up with the following command:
gh api orgs/MY_ORG/repos/MY_USER_NAME/MY_REPO/collaborators/COLLABORATOR_USER_NAME -f '{"permission":"maintain"}';
This command systematically returns a 404 error (note that I also get a 404 when I just try to check if a user has access to a repo, i.e. the GET version of the above command).
I also need to mention that this doesn't seem to be a trivial gh auth login issue since a command like gh repo create MY_ORG/MY_REPO works fine.
Here is also some technical details:
os: macosx 10.15.16
git: 2.24.3
gh: 1.1.0
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
You can set organization membership for a user
put /orgs/{org}/memberships/{username}
You can add a collaborator to a repo
put /repos/{owner}/{repo}/collaborators/{username}
But I don't think you can combine the two (add a collaborator to an org repo)
That is because that collaborator need to be a member of the organisation first (so receive and accept the invitation), before being added to a repository.

Watch all starred GitHub projects

I have hundreds of starred GitHub repos, but none of them are watched. How can I easily watch all my starred repos without enabling watching one at a time? I'm sure this is possible with the GitHub API.
Yes - you can do this with the API. You can use the list repositories being starred endpoint to generate a list, e.g.:
curl -u <USER>:<API_KEY> https://api.github.com/user/starred
Then use the set a repository subscription endpoint to watch your list of starred repositories by setting subscribed to true:
curl -X PUT -d {"subscribed":true} -u <USER>:<API_KEY> https://api.github.com/repos/:owner/:repo/subscription

No repository found for hub.topic - Scope issue?

I am trying to subscribe to changes in a particular repository but I'm getting a "No repository found for hub.topic".
Here's my request:
curl -u "user" -i https://api.github.com/hub -F "hub.mode=subscribe" -F "hub.topic=https://github.com/iOS-Goodies/iOS-Goodies/events/watch" -F "hub.callback=callback"
Given that this curl request follows Github's documentation guidelines and that it works for my own repositories, I assume this is a matter of scope.
Am I unable to subscribe to events on repositories I do not own?
Thanks
You have to be an administrator of the repository in order to perform that API request. The PubSubHubbub API follows the same permission rules as the Webhooks API: "The Repository Webhooks API allows repository admins to manage the post-receive hooks for a repository. Webhooks can be managed using the JSON HTTP API, or the PubSubHubbub API."