Skip to content

Commit

Permalink
Merge pull request #160 from FDA-AI/main
Browse files Browse the repository at this point in the history
Automating Task Creation
  • Loading branch information
mikepsinn authored Mar 23, 2024
2 parents 6b43ebc + 21f292e commit 709a82e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/contributing/task-management/create-a-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,27 @@ dateCreated: 2022-07-27T21:36:24.019Z
3. If it already exists, click it and up-vote it as described 👉 [here](vote-on-tasks-and-sort-by-priority.md)
4. If not, click the `New issue` button
5. Describe in as much detail as possible and add any tags that you think will be helpful in filtering and sorting

# Automating Task Creation

To automate the creation of GitHub issues for tasks listed in the `docs/treaty/strategy.md` document, you can use the `create_issues_from_strategy_doc.py` script. This script allows for bulk creation of issues, which can save time when dealing with multiple tasks. However, it should be used with caution to avoid creating duplicate issues.

## Prerequisites

- **Python Installation**: Ensure you have Python installed on your system. You can download it from [python.org](https://www.python.org/downloads/).
- **Necessary Python Packages**: The script requires the `requests` package. Install it using pip:
```
pip install requests
```
- **GitHub API Token**: You need a GitHub API token with the necessary permissions to create issues in the repository. Follow the instructions on GitHub to create your token.

## Running the Script

1. Ensure all prerequisites are met.
2. Navigate to the directory containing the `create_issues_from_strategy_doc.py` script.
3. Run the script with the following command:
```
python create_issues_from_strategy_doc.py
```

Remember to use this script responsibly to avoid creating duplicate issues.
61 changes: 61 additions & 0 deletions scripts/jobs/create_issues_from_strategy_doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import json
import os
import re
import time

import requests


def read_strategy_md(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()

def parse_tasks(markdown_content):
tasks = []
task_pattern = re.compile(r'###\s(.+)\n+([\s\S]+?)(?=\n###|$)')
matches = task_pattern.findall(markdown_content)
for match in matches:
tasks.append({'title': match[0], 'body': match[1].strip()})
return tasks

def create_github_issue(title, body, token, repo):
url = f"https://api.github.com/repos/{repo}/issues"
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json"
}
data = {
"title": title,
"body": body
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 201:
print(f"Issue '{title}' created successfully.")
elif response.status_code == 429:
print("Rate limit exceeded. Waiting 60 seconds before retrying...")
time.sleep(60)
create_github_issue(title, body, token, repo)
elif response.status_code == 401:
raise Exception("Authentication failed. Check your GitHub token.")
else:
raise Exception(f"Failed to create issue '{title}': {response.content}")

def main():
file_path = "docs/treaty/strategy.md"
repo = "FDA-AI/FDAi"
token = os.getenv("GITHUB_TOKEN")
if not token:
raise Exception("GitHub token not found. Set the GITHUB_TOKEN environment variable.")

try:
markdown_content = read_strategy_md(file_path)
tasks = parse_tasks(markdown_content)
for task in tasks:
create_github_issue(task['title'], task['body'], token, repo)
except FileNotFoundError:
print(f"File {file_path} not found.")
except Exception as e:
print(f"Error: {str(e)}")

if __name__ == "__main__":
main()

0 comments on commit 709a82e

Please sign in to comment.