Skip to content

Instantly share code, notes, and snippets.

@blackrobot
Forked from m1yag1/migrate_zenhub_issues.py
Created February 14, 2023 18:27
Show Gist options
  • Save blackrobot/9db9578304d024ec7fc9c134a1d3c1ef to your computer and use it in GitHub Desktop.
Save blackrobot/9db9578304d024ec7fc9c134a1d3c1ef to your computer and use it in GitHub Desktop.
Migrate Issues from ZenHub to GH Projects
# We first create a spreadsheet with the ZenHub Pipeline and GitHub Project Column mapping
# Then, we moved all the issues manually into an "Unsorted" column in our GitHub Project
# Read through each of the issues that is in the Unsorted column and move them to the correct column
# if it's in the spreadsheet.
# To run:
# pip install python-dotenv ghzh-clients
import csv
import os
from dotenv import load_dotenv
from ghzh import GitHubClient
from github3 import issue, login
def get_rows(filename, encoding='ISO-8859-1'):
with open(filename, 'r', encoding=encoding) as csvfile:
datareader = csv.DictReader(csvfile)
for row in datareader:
yield {key: value for key, value in row.items()}
def retrieve_issue_from_csv(issue_number, csv_filepath):
"""Look in the csv until we find the issue_number. Return None if not found
"""
for row in get_rows(csv_filepath):
if row["issue_number"] == str(issue_number):
return row
break
return None
if __name__ == "__main__":
load_dotenv()
# Settings for script
GITHUB_API_TOKEN = os.environ.get("GITHUB_API_TOKEN")
GITHUB_USER = os.environ.get("GITHUB_USER")
GITHUB_PROJECT_ID = os.environ.get("GITHUB_PROJECT_ID")
GITHUB_REPO_OWNER = os.environ.get("GITHUB_REPO_OWNER")
GITHUB_PROJECT_INBOX_ID = os.environ.get("GITHUB_PROJECT_INBOX_ID")
gh_client = login(GITHUB_USER, token=GITHUB_API_TOKEN)
# Find the GitHub Project
org = gh_client.organization(username=GITHUB_REPO_OWNER)
project = org.project(id=GITHUB_PROJECT_ID)
# Get the "Unsorted" column to be used in the migration
column = project.column(id=GITHUB_PROJECT_INBOX_ID)
for card in column.cards():
# Determine if card is attached to an issue or pull request
if card.content_url is None:
continue
else:
gh_issue = card.retrieve_issue_from_content()
zh_issue_data = retrieve_issue_from_csv(issue_number=gh_issue.number, csv_filepath="./input/all_open_issues.csv")
# If we return an issue from our spreadsheet, move the card to the column specified in the spreadsheet
if zh_issue_data:
gh_project_column_id = zh_issue_data["github_project_column_id"]
card.move(position="top", column_id=int(gh_project_column_id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment