Coverage for scripts / remove_participant.py: 0%

51 statements  

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

1#!/usr/bin/env python3 

2""" 

3Remove participants from a request. 

4 

5Usage: 

6 python remove_participant.py REQ-123 --account-id 5b10ac8d82e05b22cc7d4ef5 --yes 

7 python remove_participant.py REQ-123 --account-id "id1,id2" --yes 

8 python remove_participant.py REQ-123 --account-id id1 --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 remove_participant_func(issue_key: str, account_ids: list = None, 

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

38 """ 

39 Remove participants from 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.remove_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='Remove participants from a request', 

59 formatter_class=argparse.RawDescriptionHelpFormatter, 

60 epilog=""" 

61Examples: 

62 Remove single participant (with confirmation skip): 

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

64 

65 Remove multiple participants: 

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

67 

68 Dry-run: 

69 %(prog)s REQ-123 --account-id id1 --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('--yes', '-y', action='store_true', 

80 help='Skip confirmation prompt') 

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

82 help='Show what would be removed without removing') 

83 parser.add_argument('--profile', 

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

85 

86 args = parser.parse_args() 

87 

88 try: 

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

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

91 return 1 

92 

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

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

95 

96 if args.dry_run: 

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

98 print(f"Would remove participants from request {args.issue_key}:") 

99 if account_ids: 

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

101 if usernames: 

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

103 return 0 

104 

105 if not args.yes: 

106 print_error("Confirmation required. Use --yes flag to confirm removal.") 

107 return 1 

108 

109 remove_participant_func( 

110 issue_key=args.issue_key, 

111 account_ids=account_ids, 

112 usernames=usernames, 

113 profile=args.profile 

114 ) 

115 

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

117 print_success(f"Successfully removed {count} participant(s) from request {args.issue_key}") 

118 

119 return 0 

120 

121 except JiraError as e: 

122 print_error(f"Failed to remove participants: {e}") 

123 return 1 

124 except Exception as e: 

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

126 return 1 

127 

128 

129if __name__ == '__main__': 

130 sys.exit(main())