Coverage for scripts / add_request_comment.py: 28%

40 statements  

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

1#!/usr/bin/env python3 

2""" 

3Add a comment to a JSM request with public/internal visibility. 

4 

5This script adds comments to JSM requests using the Service Desk API, 

6which differs from standard JIRA comments by supporting customer portal visibility. 

7Public comments are visible to customers in the portal, while internal comments 

8are only visible to agents. 

9""" 

10 

11import sys 

12import argparse 

13from pathlib import Path 

14 

15# Add shared lib to path 

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

17 

18from config_manager import get_jira_client 

19from error_handler import handle_errors 

20 

21 

22def get_jira_client(profile=None): 

23 """Get JIRA client (overridable for testing).""" 

24 from config_manager import get_jira_client as _get_client 

25 return _get_client(profile) 

26 

27 

28def format_comment_output(comment: dict, issue_key: str) -> None: 

29 """Format and print comment creation result.""" 

30 comment_id = comment.get('id', 'unknown') 

31 is_public = comment.get('public', True) 

32 body = comment.get('body', '') 

33 

34 visibility = "Public (Customer Portal)" if is_public else "Internal (Agents Only)" 

35 

36 print(f"\nAdded comment to {issue_key} (ID: {comment_id})") 

37 print(f"\nVisibility: {visibility}") 

38 print("Body:") 

39 for line in body.split('\n'): 

40 print(f" {line}") 

41 

42 if is_public: 

43 print("\nNote: This comment is visible to customers in the service portal.") 

44 else: 

45 print("\nNote: This comment is NOT visible to customers.") 

46 

47 

48@handle_errors 

49def main(args=None): 

50 """Main function.""" 

51 parser = argparse.ArgumentParser( 

52 description='Add comment to JSM request (public/internal)', 

53 formatter_class=argparse.RawDescriptionHelpFormatter, 

54 epilog=""" 

55Examples: 

56 # Add public (customer-visible) comment 

57 %(prog)s REQ-123 --body "Your issue has been resolved." 

58 

59 # Add internal (agent-only) comment 

60 %(prog)s REQ-123 --body "Escalating to Tier 2" --internal 

61 

62 # Multiline comment 

63 %(prog)s REQ-123 --body "Issue resolved. 

64 

65Root cause: Database connection timeout. 

66Fix: Increased timeout to 30s." 

67 

68 # From stdin 

69 echo "Waiting for vendor response" | %(prog)s REQ-123 --internal --body - 

70 """ 

71 ) 

72 

73 parser.add_argument('issue_key', 

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

75 parser.add_argument('--body', required=True, 

76 help='Comment body (use - for stdin)') 

77 parser.add_argument('--internal', action='store_true', 

78 help='Internal comment (agent-only, not visible to customers)') 

79 parser.add_argument('--profile', 

80 help='JIRA profile to use') 

81 

82 parsed_args = parser.parse_args(args) 

83 

84 # Read body from stdin if needed 

85 body = parsed_args.body 

86 if body == '-': 

87 body = sys.stdin.read() 

88 

89 # Get JIRA client 

90 jira = get_jira_client(parsed_args.profile) 

91 

92 # Add comment with visibility flag 

93 public = not parsed_args.internal 

94 

95 comment = jira.add_request_comment( 

96 parsed_args.issue_key, 

97 body, 

98 public=public 

99 ) 

100 

101 # Format and display output 

102 format_comment_output(comment, parsed_args.issue_key) 

103 

104 return 0 

105 

106 

107if __name__ == '__main__': 

108 sys.exit(main())