Skip to content

Commit

Permalink
Add method to convert GitHub issue to markdown
Browse files Browse the repository at this point in the history
Add support for converting GitHub issues to markdown.

* Add `convert_github_issue` method in `src/markitdown/_markitdown.py` to handle GitHub issue conversion.
* Use `PyGithub` to fetch issue details using the provided token.
* Convert the issue details to markdown format and return as `DocumentConverterResult`.
* Add optional GitHub issue support with `IS_GITHUB_ISSUE_CAPABLE` flag.
  • Loading branch information
gagb committed Dec 12, 2024
1 parent b401396 commit 9d04710
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
except ModuleNotFoundError:
pass

# Optional GitHub issue support
try:
from github import Github

IS_GITHUB_ISSUE_CAPABLE = True
except ModuleNotFoundError:
pass


class _CustomMarkdownify(markdownify.MarkdownConverter):
"""
Expand Down Expand Up @@ -1099,3 +1107,37 @@ def _guess_ext_magic(self, path):
def register_page_converter(self, converter: DocumentConverter) -> None:
"""Register a page text converter."""
self._page_converters.insert(0, converter)

def convert_github_issue(
self, issue_url: str, github_token: str
) -> DocumentConverterResult:
if not IS_GITHUB_ISSUE_CAPABLE:
raise ImportError("PyGithub is not installed. Please install it to use this feature.")

# Parse the issue URL
parsed_url = urlparse(issue_url)
path_parts = parsed_url.path.strip("/").split("/")
if len(path_parts) < 4 or path_parts[2] != "issues":
raise ValueError("Invalid GitHub issue URL")

owner, repo, _, issue_number = path_parts[:4]

# Authenticate with GitHub
g = Github(github_token)
repo = g.get_repo(f"{owner}/{repo}")
issue = repo.get_issue(int(issue_number))

# Convert issue details to markdown
markdown_content = f"# {issue.title}\n\n{issue.body}\n\n"
markdown_content += f"**State:** {issue.state}\n"
markdown_content += f"**Created at:** {issue.created_at}\n"
markdown_content += f"**Updated at:** {issue.updated_at}\n"
markdown_content += f"**Comments:**\n"

for comment in issue.get_comments():
markdown_content += f"- {comment.user.login} ({comment.created_at}): {comment.body}\n"

return DocumentConverterResult(
title=issue.title,
text_content=markdown_content,
)

0 comments on commit 9d04710

Please sign in to comment.