Search code examples
githubgithub-api

Is there a github api endpoint to give Team access to a repo?


It is possible to add collaborators via the api as described here: https://developer.github.com/v3/repos/collaborators/#add-user-as-a-collaborator

Endpoint: /repos/:owner/:repo/collaborators/:username

But what about adding team access, which is definitely possible via web interface in "Settings > Collaborators & Teams"


Solution

  • Actually i figured out this CAN be done via the api in this way, it just requires headers and data indicating what permissions.:

    curl -H "Accept: application/vnd.github.v3+json" -u YourUserName:YourPersonalAccessToken -X PUT -d '{"permission":"write"}' https://api.github.com/teams/$team_id/repos/$org_name/$repo
    

    Alternatively, in Python:

    import requests, json
    
    data = json.dumps({"permission": 'read'}) . #could be 'write', etc..
    headers = {
        'content-type': 'application/json',
        'accept': 'application/vnd.github.v3+json, text/plain, */*'
    }
    auth_tuple = (username, access_token)
    url = f"https://api.github.com/teams/{team_id}/repos/{org_name}/{repo}"
    requests.put(url, auth=auth_tuple, data=data, headers=headers)