Last active
July 16, 2020 18:47
-
-
Save ostodieck/4c11d9ade2534a9af3f0a3a165ac820c to your computer and use it in GitHub Desktop.
Bulk import tasks as issues into a GitHub Project
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
''' | |
To use the script, you need to create a personal access token in GitHub under | |
Your Profile > Developer Settings > New personal access token | |
Keep this token safe and private. | |
Other prerequisites: | |
The project should already have been created in GitHub, with backlog, in progress and completed columns - set the automation on the backlog as to do and add new issues automatically to this column | |
The labels corresponding to the import tasks should have been created under issues> labels | |
Information about the GitHub GraphQL API can be found here: https://docs.github.com/en/graphql/reference/objects | |
''' | |
import requests | |
import json | |
import pandas as pd | |
# Read csv with issue data from our User Stories tracker | |
import_issues = pd.read_csv("User_stories_Iteration_3.csv") | |
import_issues.head() | |
#-----------------------START user input parameters | |
# set user and token | |
user = "ostodieck" | |
token = "XXXXXXXXXXXXXXXXXXXXX" | |
# repo with project board | |
repo_owner = "daptablade" | |
repo_name = "xopt" | |
project_name = "Iteration 3" | |
#-----------------------END user input parameters | |
# region GET_REPO_ID | |
# query the properties of the project repository on GitHub | |
query = '''query { | |
repository(owner:"''' + repo_owner + '''", name:"''' + repo_name + '''") { | |
id | |
} | |
}''' | |
url = 'https://api.github.com/graphql' | |
r = requests.post(url, json={'query': query}, auth=(user, token)) | |
#print(r.status_code) | |
#print(r.text) | |
repo = json.loads(r.text)['data']['repository']['id'] | |
# endregion | |
# region GET_PROJECT_ID | |
# query project id | |
query = '''query { | |
repository(owner:"''' + repo_owner + '''", name:"''' + repo_name + '''") { | |
projects(last:20) { | |
edges { | |
node { | |
id | |
name | |
} | |
} | |
} | |
} | |
}''' | |
url = 'https://api.github.com/graphql' | |
r = requests.post(url, json={'query': query}, auth=(user, token)) | |
#print(r.status_code) | |
#print(r.text) | |
# Transform json input to python objects | |
input_dict = json.loads(r.text)['data']['repository']['projects']['edges'] | |
# filter by project name | |
output_dict = [x for x in input_dict if x['node']['name']== project_name] | |
output_json = json.dumps(output_dict) | |
project_ID = json.loads(output_json)[0]['node']['id'] | |
#print(project_ID) | |
# endregion | |
# region GET_REPO_LABELS | |
# query the repository labels | |
query = '''query { | |
repository(owner:"''' + repo_owner + '''", name:"''' + repo_name + '''") { | |
labels(last:20) { | |
edges { | |
node { | |
id | |
name | |
} | |
} | |
} | |
} | |
}''' | |
url = 'https://api.github.com/graphql' | |
r = requests.post(url, json={'query': query}, auth=(user, token)) | |
#print(r.status_code) | |
#print(r.text) | |
list_of_labels = json.loads(r.text)['data']['repository']['labels']['edges'] | |
label_ids = [] | |
label_names = [] | |
for label in list_of_labels: | |
label_ids.append(label['node']['id']) | |
label_names.append(label['node']['name']) | |
data = { | |
"id": label_ids, | |
"names": label_names | |
} | |
df = pd.DataFrame(data) | |
# endregion | |
# Create the new issues in Github | |
for issue in range(len(import_issues)): | |
description = import_issues["Task"][issue] | |
title = import_issues["Task"][issue] | |
labels= import_issues["Labels"][issue] | |
label_ids = [] | |
for label in labels.split(', '): | |
label_ids.append(df[df["names"]==label]["id"].to_string(index=False).strip()) | |
label_ids= str(label_ids).replace("'", '"') | |
mutation = """mutation { | |
createIssue(input:{body:\"""" + description \ | |
+ "\",clientMutationId:\"" + user \ | |
+ "\",repositoryId:\"" + repo \ | |
+ "\",title:\"" + title \ | |
+ "\",projectIds:\"" + project_ID \ | |
+ "\",labelIds:" + label_ids \ | |
+ """}) { | |
clientMutationId | |
} | |
}""" | |
r1 = requests.post(url, json={'query': mutation}, auth=(user, token)) | |
print(r1.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment