Last active
July 25, 2024 21:04
-
-
Save oilsinwater/df1e803196dab824d12d3d5d4adb453d to your computer and use it in GitHub Desktop.
Create CSV from Github Issues
This file contains 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 os | |
import csv | |
import requests | |
def fetch_issues(repo_owner, repo_name): | |
# Initialize an empty list to store issues | |
issues = [] | |
# Start with the first page of results | |
page = 1 | |
# Set the number of issues to fetch per page | |
per_page = 100 | |
# Define headers for the GitHub API request | |
headers = {"Accept": "application/vnd.github.v3+json"} | |
while True: | |
# Construct the URL for the GitHub API request | |
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues?page={page}&per_page={per_page}" | |
# Make the GET request to the GitHub API | |
response = requests.get(url, headers=headers) | |
# Raise an error if the request was not successful | |
response.raise_for_status() | |
# Parse the JSON response | |
page_issues = response.json() | |
# Break the loop if there are no more issues to fetch | |
if not page_issues: | |
break | |
# Add the fetched issues to the list | |
issues.extend(page_issues) | |
# Increment the page number to fetch the next set of issues | |
page += 1 | |
return issues | |
def write_issues_to_csv(issues, csv_filename): | |
# Open the CSV file for writing | |
with open(csv_filename, 'w', newline='', encoding='utf-8') as csvfile: | |
# Define the field names for the CSV file | |
fieldnames = ['number', 'title', 'state', 'labels', 'body', 'created_at', 'updated_at', 'url'] | |
# Create a CSV DictWriter object | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
# Write the header row to the CSV file | |
writer.writeheader() | |
# Write each issue to the CSV file | |
for issue in issues: | |
# Concatenate all labels into a single string | |
labels = ",".join(label['name'] for label in issue.get('labels', [])) | |
# Write the issue details to the CSV file | |
writer.writerow({ | |
'number': issue['number'], | |
'title': issue['title'], | |
'state': issue['state'], | |
'labels': labels, | |
'body': issue['body'], | |
'created_at': issue['created_at'], | |
'updated_at': issue['updated_at'], | |
'url': issue['html_url'] | |
}) | |
def main(): | |
# Prompt the user to enter the repository owner | |
repo_owner = input("Enter the repository owner: ") | |
# Prompt the user to enter the repository name | |
repo_name = input("Enter the repository name: ") | |
# Define the CSV filename to save the issues | |
csv_filename = "issues.csv" | |
# Fetch issues from the specified repository | |
issues = fetch_issues(repo_owner, repo_name) | |
# Write the fetched issues to the CSV file | |
write_issues_to_csv(issues, csv_filename) | |
# Print a message indicating the issues were saved | |
print(f"Issues fetched and saved to {csv_filename}") | |
# Check if the script is being run directly | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment