Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add user to organization repository github API

I'm trying to add multiple users to a team in a private group team using curl and the github API. However, something is off with my syntax and I'm not sure what it is.

I've tried:

curl --user "groupowner:password" -X PUT -d "" "https://api.github.com/orgs/ORGNAME/teams/TEAMNAME/members/USERNAMETOBEADDED/"

The github documentation, e.g., here is helpful, but I'm missing something.Looking here it seems that another syntax may be possible.

like image 623
d-cubed Avatar asked Nov 11 '15 16:11

d-cubed


People also ask

How do I add users to my GitHub organization?

Inviting a user to join your organizationClick the name of your organization. Under your organization name, click People. On the People tab, click Invite member. Type the username, full name, or email address of the person you want to invite and click Invite.

How do I add an existing GitHub repository to an organization?

In the upper-right corner of any page, click , and then click Import repository. Under "Your old repository's clone URL", type the URL of the project you want to import. Choose your personal account or an organization to own the repository, then type a name for the repository on GitHub.

Can you follow an organization on GitHub?

You can follow organizations on GitHub to receive notifications about their activity. Note: The ability to follow organizations is currently in public beta and subject to change.

Can a GitHub organization have multiple owners?

About maintaining ownership continuity for your organizationWe recommend an organization have at least two people with owner permissions to ensure no one will lose access to a project. Organization owners have full administrative access to the organization. For more information, see "Roles in an organization."


2 Answers

Getting rid of the trailing slash and using the team membership API should do the trick:

curl --user "groupowner:password" -X PUT -d "" "https://api.github.com/teams/TEAMID/memberships/USERNAMETOBEADDED"
like image 143
Hans Z. Avatar answered Sep 30 '22 21:09

Hans Z.


OAuth, Organizations, etc.

Building on the answer from Hans Z. I'll show you how to invite an email address to join an organization and assign them to a team, all in one shot. You will also notice that I use an OAuth "Personal Access" Token header instead of a username and password. You can (and should) do this whether you are acting as an organization or a personal account holder.

# Assuming you have `export GITHUB_OAUTH_TOKEN=...` in your .bash_profile

# Define inputs
email="[email protected]"
org_name=DecaturMakers
team_name=Administrators

# Derive request data
team_id="$(
    curl -s                                                               \
         -H "Authorization: token $GITHUB_OAUTH_TOKEN"                    \
         "https://api.github.com/orgs/$org_name/teams"                   |\
      jq 'map(select(.name=="'$team_name'")) | .[].id'
)"
json='{
  "role": "direct_member",
  "team_ids":['$team_id'],
  "email":"'$email'"
}'

# Send invitation
curl -s                                                                   \
     -H "Authorization: token $GITHUB_OAUTH_TOKEN"                        \
     -H "Accept: application/vnd.github.dazzler-preview+json"             \
     -d "$json"                                                           \
     "https://api.github.com/orgs/$org_name/invitations"

output

{
  "id": 12345678,
  "node_id": "MDIyOk9yZ2FuaXphdGlvbkludml0YXRpb24xNjcwNTEwNg==",
  "login": null,
  "email": "[email protected]",
  "role": "direct_member",
  "created_at": "2019-08-20T20:53:49Z",
  "inviter": {
    "login": "RichardBronosky",
    "id": 12345,
    "node_id": "MDQ6VXNlcjEzNjIw",
    "avatar_url": "https://avatars3.githubusercontent.com/u/12345?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/RichardBronosky",
    "html_url": "https://github.com/RichardBronosky",
    "followers_url": "https://api.github.com/users/RichardBronosky/followers",
    "following_url": "https://api.github.com/users/RichardBronosky/following{/other_user}",
    "gists_url": "https://api.github.com/users/RichardBronosky/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/RichardBronosky/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/RichardBronosky/subscriptions",
    "organizations_url": "https://api.github.com/users/RichardBronosky/orgs",
    "repos_url": "https://api.github.com/users/RichardBronosky/repos",
    "events_url": "https://api.github.com/users/RichardBronosky/events{/privacy}",
    "received_events_url": "https://api.github.com/users/RichardBronosky/received_events",
    "type": "User",
    "site_admin": false
  },
  "team_count": 1,
  "invitation_teams_url": "https://api.github.com/organizations/01234567/invitations/12345678/teams"
}

You may notice the extra header in the curl that sends the invitation. That is explained here.

Bonus

Get pending invitations for an organization:

(You can use this to verify the results of the command above.)

# Verify results
curl -s                                                                   \
     -H "Authorization: token $GITHUB_OAUTH_TOKEN"                        \
      "https://api.github.com/orgs/$org_name/invitations"                |\
  jq '[.[].email]'

output

[
  "[email protected]",
  "[email protected]"
]

Get the teams for an organization:

org_name=DecaturMakers
curl -sH "Authorization: token $GITHUB_OAUTH_TOKEN"   \
     "https://api.github.com/orgs/$org_name/teams"

output

[
  {
    "name": "Administrators",
    "id": 123456,
    "node_id": "H4ShH4ShH4ShH4ShH4==",
    "slug": "administrators",
    "description": null,
    "privacy": "secret",
    "url": "https://api.github.com/teams/123456",
    "html_url": "https://github.com/orgs/DecaturMakers/teams/administrators",
    "members_url": "https://api.github.com/teams/123456/members{/member}",
    "repositories_url": "https://api.github.com/teams/123456/repos",
    "permission": "pull"
  }
]

Filter out the id of a team in an organization:

team_name=Administrators
curl -sH "Authorization: token $GITHUB_OAUTH_TOKEN"   \
     "https://api.github.com/orgs/$org_name/teams"   |\
     jq 'map(select(.name=="'$team_name'")) | .[].id'

output

123456

Get orgs you are a member of:

curl -sH "Authorization: token $GITHUB_OAUTH_TOKEN"   \
     "https://api.github.com/user/orgs"

output

[
  {
    "login": "DecaturMakers",
    "id": 1234567,
    "node_id": "H4ShH4ShH4ShH4ShH4ShH4ShH4ShH4S=",
    "url": "https://api.github.com/orgs/DecaturMakers",
    "repos_url": "https://api.github.com/orgs/DecaturMakers/repos",
    "events_url": "https://api.github.com/orgs/DecaturMakers/events",
    "hooks_url": "https://api.github.com/orgs/DecaturMakers/hooks",
    "issues_url": "https://api.github.com/orgs/DecaturMakers/issues",
    "members_url": "https://api.github.com/orgs/DecaturMakers/members{/member}",
    "public_members_url": "https://api.github.com/orgs/DecaturMakers/public_members{/member}",
    "avatar_url": "https://avatars1.githubusercontent.com/u/1234567?v=4",
    "description": "Code projects associated with Decatur Makers"
  }
]
like image 27
Bruno Bronosky Avatar answered Sep 30 '22 19:09

Bruno Bronosky