Coverage for scripts / list_organizations.py: 0%
49 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-25 18:08 -0500
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-25 18:08 -0500
1#!/usr/bin/env python3
2"""
3List JSM organizations.
5Usage:
6 python list_organizations.py
7 python list_organizations.py --output json
8 python list_organizations.py --start 0 --limit 25
9"""
11import sys
12import os
13import argparse
14import json
15from pathlib import Path
17sys.path.insert(0, str(Path(__file__).parent.parent.parent / 'shared' / 'scripts' / 'lib'))
19from config_manager import get_jira_client
20from error_handler import print_error, JiraError
23def list_organizations_func(start: int = 0, limit: int = 50,
24 profile: str = None) -> dict:
25 """
26 List all organizations.
28 Args:
29 start: Starting index for pagination
30 limit: Maximum results per page
31 profile: JIRA profile to use
33 Returns:
34 Organizations data
35 """
36 with get_jira_client(profile) as client:
37 return client.get_organizations(start=start, limit=limit)
40def main():
41 """Main entry point."""
42 parser = argparse.ArgumentParser(
43 description='List JSM organizations',
44 formatter_class=argparse.RawDescriptionHelpFormatter,
45 epilog="""
46Examples:
47 List all organizations:
48 %(prog)s
50 JSON output:
51 %(prog)s --output json
53 Pagination:
54 %(prog)s --start 0 --limit 25
55 """
56 )
58 parser.add_argument('--start', type=int, default=0,
59 help='Starting index for pagination (default: 0)')
60 parser.add_argument('--limit', type=int, default=50,
61 help='Maximum results per page (default: 50)')
62 parser.add_argument('--output', choices=['text', 'json', 'csv'], default='text',
63 help='Output format (default: text)')
64 parser.add_argument('--count', action='store_true',
65 help='Show only count')
66 parser.add_argument('--profile',
67 help='JIRA profile to use from config')
69 args = parser.parse_args()
71 try:
72 data = list_organizations_func(
73 start=args.start,
74 limit=args.limit,
75 profile=args.profile
76 )
78 organizations = data.get('values', [])
80 if args.count:
81 print(len(organizations))
82 return 0
84 if args.output == 'json':
85 print(json.dumps(organizations, indent=2))
86 elif args.output == 'csv':
87 print("ID,Name")
88 for org in organizations:
89 print(f"{org.get('id')},{org.get('name')}")
90 else:
91 if not organizations:
92 print("No organizations found.")
93 return 0
95 print("Organizations:\n")
96 print(f"{'ID':<10} {'Name'}")
97 print("-" * 60)
98 for org in organizations:
99 print(f"{org.get('id'):<10} {org.get('name')}")
101 print(f"\nTotal: {len(organizations)} organization(s)")
103 return 0
105 except JiraError as e:
106 print_error(f"Failed to list organizations: {e}")
107 return 1
108 except Exception as e:
109 print_error(f"Unexpected error: {e}")
110 return 1
113if __name__ == '__main__':
114 sys.exit(main())