使用 Github API 将用户添加到组织团队

问题描述 投票:0回答:2

我正在尝试使用curl和github API将多个用户添加到私人小组团队中的团队中。然而,我的语法有些问题,我不确定它是什么。

我已经尝试过:

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

github 文档,例如 here 很有帮助,但我遗漏了一些东西。看着 here 似乎可能有另一种语法。

curl github-api
2个回答
7
投票

摆脱尾部斜杠并使用团队成员 API 应该可以解决问题:

curl --user "groupowner:password" -X PUT -d "" "https://api.github.com/teams/TEAMID/memberships/USERNAMETOBEADDED"

3
投票

OAuth、组织等

基于 Hans Z 的答案。我将向您展示如何邀请电子邮件地址加入组织并将其分配给团队,这一切都一次性完成。您还会注意到,我使用 OAuth“个人访问”令牌 标头而不是用户名和密码。 无论您是作为组织还是个人账户持有人,您都可以(并且应该)这样做。

# 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"

输出

{ "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" }

您可能会注意到发送邀请的

curl

 中的额外标头。 
这里有解释

奖金

获取组织的待处理邀请

(您可以使用它来验证上面命令的结果。)

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

输出

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

获取组织的团队

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

输出

[ { "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" } ]

过滤掉组织中团队的id

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'

输出

123456

获取您所属的组织:

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

输出

[ { "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" } ]
    
© www.soinside.com 2019 - 2024. All rights reserved.