Reputation: 2353
I was looking at the Github API and it allows you to fetch all repository invites through an API endpoint (see https://developer.github.com/v3/repos/invitations/#list-invitations-for-a-repository). This works fine like this:
from requests.auth import HTTPBasicAuth
import requests
login = 'xxx'
password = 'yyy'
url = 'https://api.github.com/user/repository_invitations'
repository_invites = requests.get(
url, auth=HTTPBasicAuth(login, password)).json()
print('response: ' + str(repository_invites))
I can then get out each request its url
like this:
for repository_invite in repository_invites:
print('url: ' + repository_invite.get('url'))
Which gives something back like:
url: https://api.github.com/user/repository_invitations/123456789
Github also mentions that you can accept an invite at https://developer.github.com/v3/repos/invitations/#accept-a-repository-invitation which mentions
PATCH /user/repository_invitations/:invitation_id
What I don't get is how I can tell Github how to accept it though. This endpoint seems to be used for both deleting and accepting an invitation. Github talks about PATCH
at https://developer.github.com/v3/#http-verbs which mentions you can use either a POST
or send a PATCH
request, however not how. So the question is, how do I know what I should send in the PATCH
call? I tried this for example:
result = requests.patch(repository_invite.get('url'), json.dumps({'accept': True}))
print('result: ' + str(result.json()))
Which gives back:
result: {'message': 'Invalid request.\n\n"accept" is not a permitted key.', 'documentation_url': 'https://developer.github.com/v3'}
Upvotes: 8
Views: 3093
Reputation: 9175
Here is how I did it with gh cli:
gh api user/repository_invitations | jq .[].id | each "p `gh api --method=PATCH user/repository_invitations/#{it}`"
It lists all the invitations, fetch the invitation ids and for each invitation id it makes a request to accept the invitation
Upvotes: 0
Reputation: 2353
In order to call the API endpoint you will need to have authentication with your Github user and you need to send a Patch
call (which can take data/headers if you would need them). Here's a working sample:
for repository_invite in repository_invites:
repository_id = repository_invite.get('id')
accept_invite = requests.patch('https://api.github.com/user/repository_invitations/'+ str(repository_id),
data={}, headers={},
auth=HTTPBasicAuth(github_username, github_password))
Without the authentication the Patch
call will give back a 404 response code because it is only accessible behind a login for obvious safety purpose. If you call the endpoint user/repository_invitations/
followed by the ID Github will automatically accept the invitation.
Upvotes: 5