Created
April 9, 2023 11:01
-
-
Save sa-/d0e264777d42975d6b98df9e564827cc to your computer and use it in GitHub Desktop.
Create mermaid dependency chart of github issues
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
| """ | |
| initial prompt: | |
| assuming github issues will have their first line in the body as "#dep: #1, #2, #4", | |
| write a python script to get all open issues in a repo using the github rest API that | |
| will parse this information and create a flowchart using mermaid.js to show the issue | |
| dependenices. Also if there are no dependencies for an issue, add it to the flowchart | |
| anyway. Remember to do a null check on the body before checking if a string exists | |
| inside it. Use the requests library and not the github library | |
| """ | |
| import requests | |
| import json | |
| import re | |
| # Set the repo and owner | |
| owner = '..' | |
| repo = '...' | |
| # Set the authorization token | |
| headers = {'Authorization': 'Bearer ...'} | |
| # Get all open issues | |
| url = f'https://api.github.com/repos/{owner}/{repo}/issues?state=open' | |
| response = requests.get(url, headers=headers) | |
| issues = json.loads(response.text) | |
| def get_issues(owner, repo, access_token): | |
| url = f'https://api.github.com/repos/{owner}/{repo}/issues?state=open' | |
| response = requests.get(url, headers=headers) | |
| issues = response.json() | |
| return issues | |
| def get_dependencies(issue): | |
| """ | |
| Look for a line with the format | |
| #dep: #9, #10, #11 | |
| """ | |
| body = issue.get('body') | |
| if body: | |
| match = re.search(r'^#dep:\s+((#\d+,?\s*)+)', body) | |
| if match: | |
| dependencies = match.group(1).replace('#', '').split(',') | |
| dependencies = [int(dep.strip()) for dep in dependencies] | |
| return dependencies | |
| return [] | |
| def generate_mermaid(issues): | |
| mermaid = 'graph LR\n' | |
| for issue in issues: | |
| issue_number = issue['number'] | |
| issue_title = re.sub(r"[()]", "", issue['title']) | |
| dependencies = get_dependencies(issue) | |
| if dependencies: | |
| for dep in dependencies: | |
| mermaid += f'{dep} --> {issue_number}[{issue_number}: {issue_title}]\n' | |
| else: | |
| mermaid += f'{issue_number}[{issue_number}: {issue_title}]\n' | |
| return mermaid | |
| issues = get_issues(owner, repo, headers) | |
| mermaid = generate_mermaid(issues) | |
| print(mermaid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment