Search code examples
githubgithub-api

Add member to GitHub repo using CLI or REST


I have a private repo that belongs to my organization on GitHub. Is there any way to add members to this repo (or to a team) using GitHub CLI or any other API that allows me to perform this action programmatically?


Solution

  • Add collaborator to repo by CLI:

    # GitHub CLI api
    # https://cli.github.com/manual/gh_api
    
    gh api \
      --method PUT \
      -H "Accept: application/vnd.github+json" \
      -H "X-GitHub-Api-Version: 2022-11-28" \
      /repos/OWNER/REPO/collaborators/USERNAME \
      -f permission='triage'
    

    See https://docs.github.com/en/rest/collaborators/collaborators?apiVersion=2022-11-28#add-a-repository-collaborator

    Different permission levels can be given by changing the last line, swapping out triage.

    The permission to grant the collaborator. Only valid on organization-owned repositories. We accept the following permissions to be set:

    • pull
    • triage
    • push
    • maintain
    • admin

    and you can also specify a custom repository role name, if the owning organization has defined any.

    Default: push