Use Python and GraphQL APIs to add draft issues to a GitHub project
This posts builds on the previous post “Finding your GitHub org (beta) project ID”
At $DAY_JOB, someone had asked if GitHub allowed users to quickly create draft issues for a GitHub project (the new kind). There was no quick way to easily do this. There is no upload CSV option and using the GitHub CLI to create issues wouldn’t add them to the board.
But, a quick look at GitHub’s GraphQL API shows us there is a addProjectV2DraftIssue
mutation that we can use.
Here’s a quick bit of Python you can borrow that quickly creates draft issues in a project using the mutation above.
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
GH_TOKEN = "MY_TOKEN"
transport = RequestsHTTPTransport(url="https://api.github.com/graphql",
headers={'Authorization': 'token ' + GH_TOKEN})
client = Client(transport=transport)
bunch_of_issues = ["add infra", "add testing", "add docs", "add code"]
for issue in bunch_of_issues:
mutation = gql(
"""
mutation ($projectId: ID!, $title: String!){
addProjectV2DraftIssue(input:{projectId:$projectId, title:$title}) {
clientMutationId
projectItem {
id
}
}
}
"""
)
params = {
"projectId": "MY_PROJECT_ID",
"title": issue
}
response = client.execute(mutation, variable_values=params)
print(response)
You should now see the issues in the project, like the image below.
That’s it! Hope this might be useful to you.