Coverage for scripts / add_participant.py: 0%

47 statements  

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

1#!/usr/bin/env python3 

2""" 

3Add participants to a request. 

4 

5Usage: 

6 python add_participant.py REQ-123 --account-id 5b10ac8d82e05b22cc7d4ef5 

7 python add_participant.py REQ-123 --account-id "id1,id2,id3" 

8 python add_participant.py REQ-123 --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_participant_func(issue_key: str, account_ids: list = None, 

37 usernames: list = None, profile: str = None) -> dict: 

38 """ 

39 Add participants to a request. 

40 

41 Args: 

42 issue_key: Request issue key 

43 account_ids: List of user account IDs 

44 usernames: List of usernames (legacy) 

45 profile: JIRA profile to use 

46 

47 Returns: 

48 Updated participants data 

49 """ 

50 with get_jira_client(profile) as client: 

51 return client.add_request_participants(issue_key, account_ids=account_ids, 

52 usernames=usernames) 

53 

54 

55def main(): 

56 """Main entry point.""" 

57 parser = argparse.ArgumentParser( 

58 description='Add participants to a request', 

59 formatter_class=argparse.RawDescriptionHelpFormatter, 

60 epilog=""" 

61Examples: 

62 Add single participant: 

63 %(prog)s REQ-123 --account-id 5b10ac8d82e05b22cc7d4ef5 

64 

65 Add multiple participants: 

66 %(prog)s REQ-123 --account-id "id1,id2,id3" 

67 

68 Dry-run: 

69 %(prog)s REQ-123 --account-id "id1,id2" --dry-run 

70 """ 

71 ) 

72 

73 parser.add_argument('issue_key', 

74 help='Request issue key (e.g., REQ-123)') 

75 parser.add_argument('--account-id', 

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

77 parser.add_argument('--username', 

78 help='Username(s) (comma-separated, legacy)') 

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

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

81 parser.add_argument('--profile', 

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

83 

84 args = parser.parse_args() 

85 

86 try: 

87 if not args.account_id and not args.username: 

88 print_error("Either --account-id or --username is required") 

89 return 1 

90 

91 account_ids = parse_account_ids(args.account_id) if args.account_id else None 

92 usernames = parse_account_ids(args.username) if args.username else None 

93 

94 if args.dry_run: 

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

96 print(f"Would add participants to request {args.issue_key}:") 

97 if account_ids: 

98 print(f" Account IDs: {', '.join(account_ids)}") 

99 if usernames: 

100 print(f" Usernames: {', '.join(usernames)}") 

101 return 0 

102 

103 add_participant_func( 

104 issue_key=args.issue_key, 

105 account_ids=account_ids, 

106 usernames=usernames, 

107 profile=args.profile 

108 ) 

109 

110 count = (len(account_ids) if account_ids else 0) + (len(usernames) if usernames else 0) 

111 print_success(f"Successfully added {count} participant(s) to request {args.issue_key}") 

112 

113 return 0 

114 

115 except JiraError as e: 

116 print_error(f"Failed to add participants: {e}") 

117 return 1 

118 except Exception as e: 

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

120 return 1 

121 

122 

123if __name__ == '__main__': 

124 sys.exit(main())