Skip to content

Instantly share code, notes, and snippets.

@BigLep
Created May 25, 2023 22:36
Show Gist options
  • Save BigLep/23deef01d3d1a707390d86f769fd4828 to your computer and use it in GitHub Desktop.
Save BigLep/23deef01d3d1a707390d86f769fd4828 to your computer and use it in GitHub Desktop.
Bulk adding issues/prs from repos to a project board for boxo repo consolidation
###
# Context: https://github.com/ipfs/boxo/issues/202
# Most of this was generated with a couple of ChatGPT prompts
###
##############
# ChatGPT:
# write a python script to add open github issues and PRs in a set of github repos to a v2 github project board using the graphql api and addProjectV2ItemById
# did some tweaks based on https://docs.github.com/en/issues/planning-and-tracking-with-projects/automating-your-project/using-the-api-to-manage-projects#updating-projects
##############
import requests
import json
base_url = "https://api.github.com/graphql"
token = "FILL_THIS_IN"
headers = {
"Authorization": f"Bearer {token}",
}
#######
# Generated with ChatGPT:
# write a python script to find the node ID of https://github.com/orgs/ipfs/projects/27 using the graphql api
#######
organization = "ipfs"
project_number = "27"
# GraphQL query to fetch project details
query = """
query($owner: String!, $number: Int!) {
organization(login: $owner) {
projectV2(number: $number) {
id
}
}
}
"""
# Send the GraphQL request
response = requests.post(
base_url,
headers=headers,
json={
"query": query,
"variables": {
"owner": organization,
"number": int(project_number),
}
},
)
print(response.text)
# Process the response
data = response.json()
print(data)
# Extract the node ID
node_id = data["data"]["organization"]["project"]["id"]
print(f"The node ID of the project is: {node_id}")
project_id = "PVT_kwDOAKDGrc4AQsyn"
repo_list = [
"ipfs/interface-go-ipfs-core",
"ipfs/go-unixfs",
"ipfs/go-pinning-service-http-client",
"ipfs/go-path",
"ipfs/go-namesys",
"ipfs/go-mfs",
"ipfs/go-ipfs-provider",
"ipfs/go-ipfs-pinner",
"ipfs/go-ipfs-keystore",
"ipfs/go-filestore",
"ipfs/go-ipns",
"ipfs/go-blockservice",
"ipfs/go-ipfs-chunker",
"ipfs/go-fetcher",
"ipfs/go-ipfs-blockstore",
"ipfs/go-ipfs-posinfo",
"ipfs/go-ipfs-util",
"ipfs/go-ipfs-ds-help",
"ipfs/go-verifcid",
"ipfs/go-ipfs-exchange-offline",
"ipfs/go-ipfs-routing",
"ipfs/go-ipfs-exchange-interface",
]
# GraphQL mutation to add an issue or PR to the project board
mutation = """
mutation($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: {projectId: $projectId, contentId: $contentId}) {
item {
id
}
}
}
"""
for repo in repo_list:
owner, repo_name = repo.split("/")
# GraphQL query to fetch open issues and PRs
query = """
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
issues(first: 100, states: OPEN) {
nodes {
id
number
__typename
}
}
pullRequests(first: 100, states: OPEN) {
nodes {
id
number
__typename
}
}
}
}
"""
# Send the GraphQL request to fetch open issues and PRs
response = requests.post(
base_url,
headers=headers,
json={
"query": query,
"variables": {
"owner": owner,
"repo": repo_name,
},
},
)
# Process the response
data = response.json()
# Extract issues and PRs from the response
issues = data["data"]["repository"]["issues"]["nodes"]
pull_requests = data["data"]["repository"]["pullRequests"]["nodes"]
# Add issues to the project board
for issue in issues:
issue_id = issue["id"]
response = requests.post(
base_url,
headers=headers,
json={
"query": mutation,
"variables": {
"projectId": project_id,
"contentId": issue_id
},
},
)
print(response.text)
print(f"Added issue #{issue['number']} to the project board.")
# Add PRs to the project board
for pr in pull_requests:
pr_id = pr["id"]
response = requests.post(
base_url,
headers=headers,
json={
"query": mutation,
"variables": {
"projectId": project_id,
"contentId": pr_id,
"contentType": "PULL_REQUEST",
},
},
)
print(f"Added PR #{pr['number']} to the project board.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment