Coverage for scripts / suggest_kb.py: 28%
43 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"""
3Suggest relevant KB articles for a service request.
5Analyzes request summary/description and suggests relevant KB articles.
6Useful for automated deflection and self-service suggestions.
8Usage:
9 suggest_kb.py --request REQ-123
10 suggest_kb.py --request REQ-123 --max-suggestions 3
11 suggest_kb.py --request REQ-123 --output json
12"""
14import argparse
15import sys
16import json
17from pathlib import Path
19# Add shared lib to path
20shared_lib_path = str(Path(__file__).parent.parent.parent.parent / 'shared' / 'scripts' / 'lib')
21if shared_lib_path not in sys.path:
22 sys.path.insert(0, shared_lib_path)
24from config_manager import get_jira_client
27def suggest_kb(issue_key: str, max_suggestions: int = 5):
28 """
29 Suggest KB articles for a request.
31 Args:
32 issue_key: Request issue key
33 max_suggestions: Maximum suggestions to return
35 Returns:
36 List of suggested KB articles
37 """
38 with get_jira_client() as client:
39 return client.suggest_kb_for_request(issue_key, max_suggestions)
42def format_text(suggestions: list, issue_key: str) -> str:
43 """Format KB suggestions as human-readable text."""
44 if not suggestions:
45 return f"No KB article suggestions found for {issue_key}."
47 output = [f"KB Article Suggestions for {issue_key} ({len(suggestions)} suggestions):\n"]
49 for i, article in enumerate(suggestions, 1):
50 output.append(f"{i}. {article['title']}")
51 if 'excerpt' in article:
52 excerpt = article['excerpt'].replace('<em>', '').replace('</em>', '')
53 output.append(f" Excerpt: {excerpt}")
54 if '_links' in article and 'self' in article['_links']:
55 output.append(f" URL: {article['_links']['self']}")
56 output.append("")
58 return "\n".join(output)
61def format_json(suggestions: list) -> str:
62 """Format KB suggestions as JSON."""
63 return json.dumps(suggestions, indent=2)
66def main():
67 parser = argparse.ArgumentParser(
68 description="Suggest KB articles for a request",
69 formatter_class=argparse.RawDescriptionHelpFormatter,
70 epilog=__doc__
71 )
72 parser.add_argument('--request', required=True,
73 help='Request issue key (e.g., REQ-123)')
74 parser.add_argument('--max-suggestions', type=int, default=5,
75 help='Maximum suggestions to return (default: 5)')
76 parser.add_argument('--output', choices=['text', 'json'], default='text',
77 help='Output format (default: text)')
78 parser.add_argument('--profile', help='JIRA profile to use')
80 args = parser.parse_args()
82 try:
83 suggestions = suggest_kb(args.request, args.max_suggestions)
85 if args.output == 'json':
86 print(format_json(suggestions))
87 else:
88 print(format_text(suggestions, args.request))
90 except Exception as e:
91 print(f"Error suggesting KB articles: {str(e)}", file=sys.stderr)
92 sys.exit(1)
95if __name__ == "__main__":
96 main()