Created
April 25, 2025 18:35
-
-
Save dcazrael/66047eda02ed26b01fba49cdfbc8cd2f to your computer and use it in GitHub Desktop.
Github Label and Issue Importer
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
| import csv | |
| import requests | |
| # Ask the user for necessary inputs | |
| GITHUB_TOKEN = "GitHubToken" # Replace with your GitHub token | |
| # You can generate a token with repo scope from github.com/settings/tokens | |
| # Make sure to keep it secret and not share it with anyone | |
| # Replace with your repository owner and name | |
| REPO_OWNER = "RepoOwner" | |
| REPO_NAME = "RepoName" | |
| CSV_ISSUES = input("Path to CSV file containing issues: ") # Path to the CSV file containing Issues | |
| # Example CSV format for issues: | |
| # title,body,milestone,labels | |
| # Example: | |
| # title,body,milestone,labels | |
| # Fix bug in feature X,1,bug,feature | |
| CSV_LABELS = input("Path to CSV file containing labels: ") # Path to the CSV file containing Labels | |
| # Example CSV format for labels: | |
| # name,color | |
| # Example: | |
| # name,color | |
| # bug,ff0000 | |
| # feature,00ff00 | |
| headers = {'Authorization': f'token {GITHUB_TOKEN}', 'Accept': 'application/vnd.github.v3+json'} | |
| def create_label(name, color): | |
| url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/labels' | |
| data = {'name': name, 'color': color} | |
| response = requests.post(url, json=data, headers=headers) | |
| if response.status_code == 201: | |
| print(f'✅ Created label: {name}') | |
| else: | |
| print(f'❌ Failed to create label: {name} | {response.status_code} - {response.text}') | |
| def create_issue(title, body, milestone, labels): | |
| url = f'https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/issues' | |
| data = {'title': title, 'body': body, 'milestone': milestone, 'labels': labels} | |
| response = requests.post(url, json=data, headers=headers) | |
| if response.status_code == 201: | |
| print(f'✅ Created: {title}') | |
| else: | |
| print(f'❌ Failed: {title} | {response.status_code} - {response.text}') | |
| def import_data(import_issues=True): | |
| if import_issues: | |
| csv_file = CSV_ISSUES | |
| with open(csv_file, newline='', encoding='utf-8') as f: | |
| reader = csv.DictReader(f) | |
| for row in reader: | |
| labels = [label.strip() for label in row['labels'].split(",")] | |
| create_issue(row['title'], row['body'], int(row['milestone']), labels) | |
| return | |
| csv_file = CSV_LABELS | |
| with open(csv_file, newline='', encoding='utf-8') as f: | |
| reader = csv.DictReader(f) | |
| for row in reader: | |
| create_label(row['name'], row['color']) | |
| if __name__ == '__main__': | |
| if CSV_ISSUES: | |
| import_issues = input("Do you want to import issues? (y/n): ").strip().lower() == 'y' | |
| import_data(import_issues) | |
| if CSV_LABELS: | |
| import_labels = input("Do you want to import labels? (y/n): ").strip().lower() == 'y' | |
| if import_labels: | |
| import_data(import_issues=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment