import requests import sys import re from datetime import datetime import argparse def get_github_issue(issue_url): # Extract owner, repo, and issue number from URL pattern = r"https://github\.com/([^/]+)/([^/]+)/issues/(\d+)" match = re.match(pattern, issue_url) if not match: raise ValueError("Invalid GitHub issue URL format") owner, repo, issue_number = match.groups() # GitHub API endpoints api_base = f"https://api.github.com/repos/{owner}/{repo}" headers = { "Accept": "application/vnd.github.v3+json", # If you have a token: # "Authorization": "Bearer YOUR_GITHUB_TOKEN" } # Get issue details issue_response = requests.get(f"{api_base}/issues/{issue_number}", headers=headers) if issue_response.status_code != 200: raise Exception(f"Failed to fetch issue: {issue_response.status_code}") issue = issue_response.json() # Get comments comments_response = requests.get(f"{api_base}/issues/{issue_number}/comments", headers=headers) if comments_response.status_code != 200: raise Exception(f"Failed to fetch comments: {comments_response.status_code}") comments = comments_response.json() # Format the markdown content markdown_content = [] # Add issue title markdown_content.append(f"# {issue['title']}\n") # Add issue author markdown_content.append(f"*Posted by @{issue['user']['login']}*\n") # Add issue body markdown_content.append(issue["body"]) markdown_content.append("\n---\n") # Add comments for comment in comments: markdown_content.append(f"\n### Comment by @{comment['user']['login']}\n") markdown_content.append(comment["body"]) markdown_content.append("\n---\n") return "\n".join(markdown_content) def main(): parser = argparse.ArgumentParser(description="Download GitHub issue as markdown") parser.add_argument("issue_url", help="Full GitHub issue URL") parser.add_argument("--output", "-o", help="Output file path (optional)") args = parser.parse_args() try: markdown_content = get_github_issue(args.issue_url) # Determine output filename if args.output: output_file = args.output else: # Extract issue number for default filename issue_number = re.search(r"/issues/(\d+)", args.issue_url).group(1) output_file = f"issue-{issue_number}.md" # Write to file with open(output_file, "w", encoding="utf-8") as f: f.write(markdown_content) print(f"Successfully downloaded issue to {output_file}") except Exception as e: print(f"Error: {str(e)}", file=sys.stderr) sys.exit(1) if __name__ == "__main__": main()