Implement Organization.invite_user (#880)

Utilize the API preview for the organization invite system to allow
organization owners to invite outside colloborators either via passing
in the user directly, or their email address.
    
Fixes #851
This commit is contained in:
Steve Kowalik
2018-08-28 14:29:23 +08:00
committed by Wan Liuyang
parent ef16702a0d
commit eb80564b01
8 changed files with 143 additions and 0 deletions
+33
View File
@@ -21,6 +21,7 @@
# Copyright 2018 Raihaan <31362124+res0nance@users.noreply.github.com> #
# Copyright 2018 Tim Boring <tboring@hearst.com> #
# Copyright 2018 sfdye <tsfdye@gmail.com> #
# Copyright 2018 Steve Kowalik <steven@wedontsleep.org> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
@@ -52,6 +53,7 @@ import github.Event
import github.Repository
import github.NamedUser
import Consts
class Organization(github.GithubObject.CompletableGithubObject):
"""
@@ -776,6 +778,37 @@ class Organization(github.GithubObject.CompletableGithubObject):
None
)
def invite_user(self, user=github.GithubObject.NotSet, email=github.GithubObject.NotSet, role=github.GithubObject.NotSet, teams=github.GithubObject.NotSet):
"""
:calls: `POST /orgs/:org/invitations <http://developer.github.com/v3/orgs/members>`_
:param user: :class:`github.NamedUser.NamedUser`
:param email: string
:param role: string
:param teams: array of :class:`github.Team.Team`
:rtype: None
"""
assert user is github.GithubObject.NotSet or isinstance(user, github.NamedUser.NamedUser), user
assert email is github.GithubObject.NotSet or isinstance(email, (str, unicode)), email
assert (email is github.GithubObject.NotSet) ^ (user is github.GithubObject.NotSet), "specify only one of email or user"
parameters = {}
if user is not github.GithubObject.NotSet:
parameters["invitee_id"] = user.id
elif email is not github.GithubObject.NotSet:
parameters["email"] = email
if role is not github.GithubObject.NotSet:
assert isinstance(role, (str, unicode)), role
assert role in ['admin', 'direct_member', 'billing_manager']
parameters["role"] = role
if teams is not github.GithubObject.NotSet:
assert all(isinstance(team, github.Team.Team) for team in teams)
parameters["team_ids"] = [t.id for t in teams]
headers, data = self._requester.requestJsonAndCheck(
"POST",
self.url + "/invitations",
headers={'Accept': Consts.mediaTypeOrganizationInvitationPreview},
input=parameters
)
def has_in_members(self, member):
"""
:calls: `GET /orgs/:org/members/:user <http://developer.github.com/v3/orgs/members>`_