Created
September 25, 2024 12:34
-
-
Save intrd/effa991a425471e0ae52d2a88577a2a9 to your computer and use it in GitHub Desktop.
Python3 Convert all your Github issues into Obsidian markdown format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Python3 Convert all your Github issues into Obsidian markdown format. | |
# Author: [email protected] | |
import os | |
import requests | |
import re | |
# Replace with your repository and username | |
GITHUB_API = "https://api.github.com" | |
USERNAME = "intrd" | |
REPO = "personal" | |
TOKEN = "github_pat_.." # You can create a personal access token in GitHub | |
headers = { | |
"Authorization": f"token {TOKEN}" | |
} | |
def get_issues(): | |
issues = [] | |
page = 1 | |
per_page = 100 # Max items per page allowed by GitHub API | |
while True: | |
url = f"{GITHUB_API}/repos/{USERNAME}/{REPO}/issues" | |
params = {'page': page, 'per_page': per_page} | |
response = requests.get(url, headers=headers, params=params) | |
if response.status_code == 200: | |
batch = response.json() | |
if not batch: | |
# No more issues, stop pagination | |
break | |
issues.extend(batch) | |
page += 1 | |
else: | |
print(f"Failed to fetch issues: {response.status_code}") | |
break | |
return issues | |
def sanitize_title(title): | |
# Replace spaces with underscores and remove or replace problematic characters | |
title = title.lower().strip() | |
title = re.sub(r'[^\w\s-]', '', title) # Remove special characters | |
title = re.sub(r'\s+', '_', title) # Replace spaces with underscores | |
return title | |
def write_issue_to_md(issue): | |
title = issue["title"] | |
issue_number = issue["number"] | |
labels = [label["name"] for label in issue["labels"]] | |
body = issue["body"] or "" | |
# Sanitize the title to use as a filename | |
sanitized_title = sanitize_title(title) | |
# Move properties other than 'tags' into the body | |
additional_info = f"\n\n---\nIssue Number: {issue_number}\n" | |
# Add this info to the body | |
full_body = f"{body}{additional_info}" | |
# Create the markdown file content | |
md_content = f"---\n" | |
md_content += f"tags: {', '.join(labels)}\n" | |
md_content += f"---\n\n" | |
md_content += full_body | |
filename = f"{sanitized_title}.md" | |
with open(filename, "w") as md_file: | |
md_file.write(md_content) | |
print(f"Created {filename}") | |
def main(): | |
issues = get_issues() | |
for issue in issues: | |
write_issue_to_md(issue) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment