Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept a Github repository invitation through API?

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

like image 387
Yenthe Avatar asked Apr 17 '20 16:04

Yenthe


People also ask

How do I connect to GitHub API?

Go to Developer Settings ->Personal Access Tokens. Add a name and select the scope for the API access and click on Create Token. In the next screen, make sure to copy the token and save it in a file. This token will be used in the command line to access GitHub API.

How do I accept a private GitHub repository?

In the search field, start typing the name of person you want to invite, then click a name in the list of matches. Click Add NAME to REPOSITORY. The user will receive an email inviting them to the repository. Once they accept your invitation, they will have collaborator access to your repository.

Is there an API for GitHub?

Learn about GitHub's APIs to extend and customize your GitHub experience. There are two stable versions of the GitHub API: the REST API and the GraphQL API.


1 Answers

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.

like image 187
Yenthe Avatar answered Oct 17 '22 13:10

Yenthe