77from requests import HTTPError
88
99from bitbucklet .token import get_access_token , BearerAuth
10- from bitbucklet .urls import teams_url
10+ from bitbucklet .urls import teams_url , team_invitations_url
1111from bitbucklet .groups_cli import __group_add_user
1212
1313@click .group (name = 'users' , help = 'Managing users' )
@@ -17,6 +17,8 @@ def users_cli():
1717@click .command (name = 'list' , help = 'List all the users (sort of)' )
1818@click .option ('--verbose' , is_flag = True , default = False )
1919def list_users (verbose : bool ):
20+ from tabulate import tabulate
21+
2022 bitbucket_team_name = os .getenv ('BITBUCKET_TEAM' )
2123
2224 access_token = get_access_token ()
@@ -31,15 +33,77 @@ def list_users(verbose: bool):
3133 return
3234
3335 members = response .json ()
34- [print (member ['username' ]) for member in members ['values' ]]
36+
37+ table = [[member ['display_name' ], member ['account_id' ], member ['uuid' ]] for member in members ['values' ] ]
38+ headers = ['display_name' , 'account_id' , 'uuid' ]
39+ print (tabulate (table , headers = headers , showindex = range (1 , len (table ) + 1 ), tablefmt = 'github' ))
3540
36- @click .command (name = 'add' , help = 'Add an user' )
37- @click .argument ('username' )
38- def add_user (username : str ):
39- # Normally, adding a user into any group making it a member of the Team.
40- # By default, BitBucket creates two groups `administrators` and `developers`.
41- # So obviously, adding a new user into `developers` makes sense here.
42- __group_add_user ('developers' , username )
41+ @click .command (name = 'invite' , help = 'Invite an user by their primary email' )
42+ @click .argument ('email' )
43+ @click .argument ('group' , default = 'developers' )
44+ def add_user (email : str , group : str ):
45+ import json
46+ # Since June 2019, BitBucket changed their policy which a new user
47+ # can only be invited via their email address. Once they accept the
48+ # invitation email address, their username will be added into the team
49+ # `developers`.
50+
51+ bitbucket_team_name = os .getenv ('BITBUCKET_TEAM' )
52+ access_token = get_access_token ()
53+
54+ response = requests .put (
55+ f"{ team_invitations_url ()} "
56+ .format (team = bitbucket_team_name ),
57+ auth = BearerAuth (access_token ),
58+ headers = {
59+ 'Content-Type' : 'application/json'
60+ },
61+ data = json .dumps ({
62+ 'email' : email ,
63+ 'group_slug' : group
64+ })
65+ )
66+
67+ print (response .text )
68+
69+ @click .command (name = 'list-pending' , help = 'List unaccepted invitations' )
70+ def list_pending_users ():
71+ from tabulate import tabulate
72+
73+ bitbucket_team_name = os .getenv ('BITBUCKET_TEAM' )
74+ access_token = get_access_token ()
75+
76+ response = requests .get (
77+ f"{ team_invitations_url ()} "
78+ .format (team = bitbucket_team_name ),
79+ auth = BearerAuth (access_token )
80+ )
81+
82+ invitations = response .json ()
83+ headers = ['email' , 'invited_by' , 'utc_sent_on' ]
84+ table = [[i ['email' ], i ['invited_by' ]['display_name' ], i ['utc_sent_on' ]] for i in invitations ]
85+ print (tabulate (table , headers = headers , showindex = range (1 , len (table ) + 1 ), tablefmt = 'github' ))
86+
87+ @click .command (name = 'del-invitation' , help = 'Delete an unaccepted invitation' )
88+ @click .argument ('email' )
89+ def delete_invitation (email : str ):
90+ import json
91+ bitbucket_team_name = os .getenv ('BITBUCKET_TEAM' )
92+ access_token = get_access_token ()
93+
94+ response = requests .delete (
95+ f"{ team_invitations_url ()} "
96+ .format (team = bitbucket_team_name ),
97+ auth = BearerAuth (access_token ),
98+ headers = {
99+ 'Content-Type' : 'application/json'
100+ },
101+ data = json .dumps ({
102+ 'email' : email
103+ })
104+ )
105+
106+ print (response )
43107
44108@click .command (name = 'del' , help = 'Delete an user' )
45109@click .argument ('username' )
@@ -50,4 +114,6 @@ def del_user(username: str):
50114
51115users_cli .add_command (list_users )
52116users_cli .add_command (add_user )
117+ users_cli .add_command (list_pending_users )
118+ users_cli .add_command (delete_invitation )
53119users_cli .add_command (del_user )
0 commit comments