Skip to content

Instantly share code, notes, and snippets.

@madhurprash
Created September 17, 2024 18:41
Show Gist options
  • Select an option

  • Save madhurprash/55d2d6cde716135d4d678a518a1efac7 to your computer and use it in GitHub Desktop.

Select an option

Save madhurprash/55d2d6cde716135d4d678a518a1efac7 to your computer and use it in GitHub Desktop.
This gist converts github issues into csv that can be imported into Asana for sprint planning
import os
import csv
import logging
import requests
from datetime import datetime
from typing import Optional, List
logging.basicConfig(format='[%(asctime)s] p%(process)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def get_github_data(repo_owner: str, repo_name: str, auth_token: str):
try:
all_issues: List = []
headers: str = {"Authorization": f"token {auth_token}"}
url: str = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues"
page: int = 1
while True:
logger.info(f"The url that is being accessed for Github issues/PRs: {url}")
response = requests.get(f"{url}?page={page}&per_page=100", headers=headers)
if response.status_code != 200:
raise Exception(f"API request failed with status code {response.status_code}: {response.text}")
issues = response.json()
if not issues:
break
all_issues.extend(issues)
page += 1
except Exception as e:
logger.error(f"Error occurred while fetching the issues: {e}")
all_issues=None
return all_issues
def export_to_csv(data, filename: str):
with open(filename, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Title", "State", "Assignee", "Labels", "Created_at"])
for issue in data:
labels_list = [label["name"] for label in issue["labels"]]
writer.writerow([
issue["title"],
issue["state"],
issue["assignee"]["login"] if issue["assignee"] else None,
", ".join(labels_list),
issue["created_at"]
])
def main():
repo_owner = input("Enter the repository owner: ")
repo_name = input("Enter the repository name: ")
# Use environment variable for the auth token
auth_token = os.environ.get("GITHUB_TOKEN")
if not auth_token:
print("Please set the GITHUB_TOKEN environment variable.")
return
try:
issues_data = get_github_data(repo_owner, repo_name, auth_token)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"issues_data_{repo_owner}_{repo_name}_{timestamp}.csv"
export_to_csv(issues_data, filename)
print(f"Data exported to {filename}")
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment