Coverage for scripts / list_customers.py: 0%

57 statements  

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

1#!/usr/bin/env python3 

2""" 

3List customers for a JSM service desk. 

4 

5Usage: 

6 python list_customers.py SD-1 

7 python list_customers.py SD-1 --query "john" 

8 python list_customers.py SD-1 --start 0 --limit 25 

9 python list_customers.py SD-1 --output json 

10 python list_customers.py SD-1 --count 

11""" 

12 

13import sys 

14import os 

15import argparse 

16import json 

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 list_service_desk_customers(service_desk_id: str, query: str = None, 

27 start: int = 0, limit: int = 50, 

28 profile: str = None) -> dict: 

29 """ 

30 List customers for a service desk. 

31 

32 Args: 

33 service_desk_id: Service desk ID or key 

34 query: Search query for filtering 

35 start: Starting index for pagination 

36 limit: Maximum results per page 

37 profile: JIRA profile to use 

38 

39 Returns: 

40 Customers data 

41 """ 

42 with get_jira_client(profile) as client: 

43 return client.get_service_desk_customers( 

44 service_desk_id, 

45 query=query, 

46 start=start, 

47 limit=limit 

48 ) 

49 

50 

51def main(): 

52 """Main entry point.""" 

53 parser = argparse.ArgumentParser( 

54 description='List customers for a JSM service desk', 

55 formatter_class=argparse.RawDescriptionHelpFormatter, 

56 epilog=""" 

57Examples: 

58 List all customers: 

59 %(prog)s SD-1 

60 

61 Search customers: 

62 %(prog)s SD-1 --query "john" 

63 

64 Pagination: 

65 %(prog)s SD-1 --start 10 --limit 25 

66 

67 JSON output: 

68 %(prog)s SD-1 --output json 

69 

70 Customer count only: 

71 %(prog)s SD-1 --count 

72 """ 

73 ) 

74 

75 parser.add_argument('service_desk_id', 

76 help='Service desk ID or key (e.g., SD-1)') 

77 parser.add_argument('--query', '-q', 

78 help='Search query for email/name filtering') 

79 parser.add_argument('--start', type=int, default=0, 

80 help='Starting index for pagination (default: 0)') 

81 parser.add_argument('--limit', type=int, default=50, 

82 help='Maximum results per page (default: 50)') 

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

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

85 parser.add_argument('--count', action='store_true', 

86 help='Show customer count only') 

87 parser.add_argument('--profile', 

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

89 

90 args = parser.parse_args() 

91 

92 try: 

93 result = list_service_desk_customers( 

94 service_desk_id=args.service_desk_id, 

95 query=args.query, 

96 start=args.start, 

97 limit=args.limit, 

98 profile=args.profile 

99 ) 

100 

101 customers = result.get('values', []) 

102 total = result.get('size', len(customers)) 

103 

104 if args.count: 

105 print(total) 

106 return 0 

107 

108 if args.output == 'json': 

109 print(json.dumps(result, indent=2)) 

110 else: 

111 if total == 0: 

112 print(f"No customers found for service desk {args.service_desk_id}") 

113 return 0 

114 

115 print(f"Customers for Service Desk: {args.service_desk_id}\n") 

116 print(f"{'Email':<30} {'Display Name':<25} {'Active':<10}") 

117 print("-" * 70) 

118 

119 active_count = 0 

120 for customer in customers: 

121 email = customer.get('emailAddress', 'N/A') 

122 name = customer.get('displayName', 'N/A') 

123 active = customer.get('active', False) 

124 active_str = "Yes" if active else "No" 

125 

126 if active: 

127 active_count += 1 

128 

129 print(f"{email:<30} {name:<25} {active_str:<10}") 

130 

131 print() 

132 print(f"Total: {total} customers ({active_count} active)") 

133 

134 return 0 

135 

136 except JiraError as e: 

137 print_error(f"Failed to list customers: {e}") 

138 return 1 

139 except Exception as e: 

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

141 return 1 

142 

143 

144if __name__ == '__main__': 

145 sys.exit(main())