Coverage for scripts / add_to_organization.py: 0%

42 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-25 18:08 -0500

1#!/usr/bin/env python3 

2""" 

3Add users to a JSM organization. 

4 

5Usage: 

6 python add_to_organization.py 12345 --account-id 5b10ac8d82e05b22cc7d4ef5 

7 python add_to_organization.py 12345 --account-id "id1,id2,id3" 

8 python add_to_organization.py 12345 --account-id "id1,id2" --dry-run 

9""" 

10 

11import sys 

12import os 

13import argparse 

14from pathlib import Path 

15 

16sys.path.insert(0, str(Path(__file__).parent.parent.parent / 'shared' / 'scripts' / 'lib')) 

17 

18from config_manager import get_jira_client 

19from error_handler import print_error, JiraError 

20from formatters import print_success 

21 

22 

23def parse_account_ids(account_id_string: str) -> list: 

24 """ 

25 Parse comma-separated account IDs. 

26 

27 Args: 

28 account_id_string: Comma-separated account IDs 

29 

30 Returns: 

31 List of account IDs 

32 """ 

33 return [id.strip() for id in account_id_string.split(',') if id.strip()] 

34 

35 

36def add_users_to_organization_func(organization_id: int, account_ids: list, 

37 profile: str = None) -> None: 

38 """ 

39 Add users to an organization. 

40 

41 Args: 

42 organization_id: Organization ID 

43 account_ids: List of user account IDs 

44 profile: JIRA profile to use 

45 """ 

46 with get_jira_client(profile) as client: 

47 client.add_users_to_organization(organization_id, account_ids) 

48 

49 

50def main(): 

51 """Main entry point.""" 

52 parser = argparse.ArgumentParser( 

53 description='Add users to a JSM organization', 

54 formatter_class=argparse.RawDescriptionHelpFormatter, 

55 epilog=""" 

56Examples: 

57 Add single user: 

58 %(prog)s 12345 --account-id 5b10ac8d82e05b22cc7d4ef5 

59 

60 Add multiple users: 

61 %(prog)s 12345 --account-id "id1,id2,id3" 

62 

63 Dry-run: 

64 %(prog)s 12345 --account-id "id1,id2" --dry-run 

65 """ 

66 ) 

67 

68 parser.add_argument('organization_id', type=int, 

69 help='Organization ID') 

70 parser.add_argument('--account-id', required=True, 

71 help='User account ID(s) (comma-separated)') 

72 parser.add_argument('--dry-run', action='store_true', 

73 help='Show what would be added without adding') 

74 parser.add_argument('--profile', 

75 help='JIRA profile to use from config') 

76 

77 args = parser.parse_args() 

78 

79 try: 

80 account_ids = parse_account_ids(args.account_id) 

81 

82 if not account_ids: 

83 print_error("Invalid or empty account IDs") 

84 return 1 

85 

86 if args.dry_run: 

87 print("DRY RUN MODE - No changes will be made\n") 

88 print(f"Would add {len(account_ids)} user(s) to organization {args.organization_id}:") 

89 for account_id in account_ids: 

90 print(f" - {account_id}") 

91 return 0 

92 

93 add_users_to_organization_func( 

94 organization_id=args.organization_id, 

95 account_ids=account_ids, 

96 profile=args.profile 

97 ) 

98 

99 print_success(f"Successfully added {len(account_ids)} user(s) to organization {args.organization_id}") 

100 

101 return 0 

102 

103 except JiraError as e: 

104 print_error(f"Failed to add users to organization: {e}") 

105 return 1 

106 except Exception as e: 

107 print_error(f"Unexpected error: {e}") 

108 return 1 

109 

110 

111if __name__ == '__main__': 

112 sys.exit(main())