Coverage for scripts / create_customer.py: 0%

63 statements  

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

1#!/usr/bin/env python3 

2""" 

3Create a JSM customer account. 

4 

5Usage: 

6 python create_customer.py --email customer@example.com --name "John Customer" 

7 python create_customer.py --email jane@example.com 

8 python create_customer.py --email user@example.com --output json 

9 python create_customer.py --email test@example.com --dry-run 

10""" 

11 

12import sys 

13import os 

14import argparse 

15import json 

16import re 

17from pathlib import Path 

18 

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

20 

21from config_manager import get_jira_client 

22from error_handler import print_error, JiraError 

23from formatters import print_success 

24 

25 

26def validate_email(email: str) -> bool: 

27 """ 

28 Validate email format. 

29 

30 Args: 

31 email: Email address to validate 

32 

33 Returns: 

34 True if valid, False otherwise 

35 """ 

36 pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' 

37 return re.match(pattern, email) is not None 

38 

39 

40def create_customer_account(email: str, display_name: str = None, 

41 profile: str = None) -> dict: 

42 """ 

43 Create a customer account. 

44 

45 Args: 

46 email: Customer email address 

47 display_name: Display name (defaults to email) 

48 profile: JIRA profile to use 

49 

50 Returns: 

51 Created customer data 

52 """ 

53 if not validate_email(email): 

54 raise ValueError(f"Invalid email format: {email}") 

55 

56 with get_jira_client(profile) as client: 

57 return client.create_customer(email, display_name) 

58 

59 

60def main(): 

61 """Main entry point.""" 

62 parser = argparse.ArgumentParser( 

63 description='Create a JSM customer account', 

64 formatter_class=argparse.RawDescriptionHelpFormatter, 

65 epilog=""" 

66Examples: 

67 Create customer with name: 

68 %(prog)s --email customer@example.com --name "John Customer" 

69 

70 Create customer (email only): 

71 %(prog)s --email jane@example.com 

72 

73 JSON output: 

74 %(prog)s --email user@example.com --output json 

75 

76 Dry-run: 

77 %(prog)s --email test@example.com --dry-run 

78 """ 

79 ) 

80 

81 parser.add_argument('--email', required=True, 

82 help='Customer email address') 

83 parser.add_argument('--name', '--display-name', 

84 help='Display name (defaults to email)') 

85 parser.add_argument('--output', choices=['text', 'json'], default='text', 

86 help='Output format (default: text)') 

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

88 help='Show what would be created without creating') 

89 parser.add_argument('--profile', 

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

91 parser.add_argument('--verbose', '-v', action='store_true', 

92 help='Show full API response') 

93 

94 args = parser.parse_args() 

95 

96 try: 

97 if not validate_email(args.email): 

98 print_error(f"Invalid email format: {args.email}") 

99 return 1 

100 

101 if args.dry_run: 

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

103 print(f"Would create customer:") 

104 print(f" Email: {args.email}") 

105 print(f" Display Name: {args.name or args.email}") 

106 return 0 

107 

108 customer = create_customer_account( 

109 email=args.email, 

110 display_name=args.name, 

111 profile=args.profile 

112 ) 

113 

114 if args.output == 'json': 

115 if args.verbose: 

116 print(json.dumps(customer, indent=2)) 

117 else: 

118 print(json.dumps({ 

119 'accountId': customer.get('accountId'), 

120 'emailAddress': customer.get('emailAddress'), 

121 'displayName': customer.get('displayName') 

122 }, indent=2)) 

123 else: 

124 print_success("Customer created successfully!") 

125 print() 

126 print(f"Account ID: {customer.get('accountId')}") 

127 print(f"Email: {customer.get('emailAddress')}") 

128 print(f"Display Name: {customer.get('displayName')}") 

129 

130 if args.verbose: 

131 print() 

132 print("Full response:") 

133 print(json.dumps(customer, indent=2)) 

134 

135 return 0 

136 

137 except ValueError as e: 

138 print_error(str(e)) 

139 return 1 

140 except JiraError as e: 

141 print_error(f"Failed to create customer: {e}") 

142 return 1 

143 except Exception as e: 

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

145 return 1 

146 

147 

148if __name__ == '__main__': 

149 sys.exit(main())