Coverage for scripts / search_kb.py: 27%
44 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"""
3Search Knowledge Base articles for a service desk.
5Searches KB articles using query terms and displays results with excerpts.
6Useful for finding documentation and self-service solutions.
8Usage:
9 search_kb.py --service-desk 1 --query "password reset"
10 search_kb.py --service-desk 1 --query "vpn" --max-results 10
11 search_kb.py --service-desk 1 --query "login" --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 search_kb(service_desk_id: int, query: str, max_results: int = 50):
28 """
29 Search KB articles for a service desk.
31 Args:
32 service_desk_id: Service desk ID
33 query: Search query string
34 max_results: Maximum results to return
36 Returns:
37 List of matching KB articles
38 """
39 with get_jira_client() as client:
40 return client.search_kb_articles(service_desk_id, query, max_results)
43def format_text(articles: list) -> str:
44 """Format KB search results as human-readable text."""
45 if not articles:
46 return "No KB articles found matching your query."
48 output = [f"Knowledge Base Search Results ({len(articles)} articles):\n"]
50 for article in articles:
51 output.append(f"Title: {article['title']}")
52 if 'excerpt' in article:
53 # Remove HTML tags from excerpt
54 excerpt = article['excerpt'].replace('<em>', '').replace('</em>', '')
55 output.append(f"Excerpt: {excerpt}")
56 if '_links' in article and 'self' in article['_links']:
57 output.append(f"URL: {article['_links']['self']}")
58 output.append("")
60 return "\n".join(output)
63def format_json(articles: list) -> str:
64 """Format KB search results as JSON."""
65 return json.dumps(articles, indent=2)
68def main():
69 parser = argparse.ArgumentParser(
70 description="Search Knowledge Base articles",
71 formatter_class=argparse.RawDescriptionHelpFormatter,
72 epilog=__doc__
73 )
74 parser.add_argument('--service-desk', type=int, required=True,
75 help='Service desk ID')
76 parser.add_argument('--query', required=True,
77 help='Search query string')
78 parser.add_argument('--max-results', type=int, default=50,
79 help='Maximum results to return (default: 50)')
80 parser.add_argument('--output', choices=['text', 'json'], default='text',
81 help='Output format (default: text)')
82 parser.add_argument('--profile', help='JIRA profile to use')
84 args = parser.parse_args()
86 try:
87 articles = search_kb(args.service_desk, args.query, args.max_results)
89 if args.output == 'json':
90 print(format_json(articles))
91 else:
92 print(format_text(articles))
94 except Exception as e:
95 print(f"Error searching KB: {str(e)}", file=sys.stderr)
96 sys.exit(1)
99if __name__ == "__main__":
100 main()