Skip to content

Instantly share code, notes, and snippets.

@dariocurr
Created March 12, 2024 11:20
Show Gist options
  • Save dariocurr/b091760b1170ebbb69682dd138eda413 to your computer and use it in GitHub Desktop.
Save dariocurr/b091760b1170ebbb69682dd138eda413 to your computer and use it in GitHub Desktop.
Create a set of labels in every existing repository for a set of users
import requests
# write permissions for issues and pull requests, read for metadata
TOKEN: str = "<your GH token>"
LABELS: set[tuple[str, str, str]] = {
(
"<your new label name>",
"<your new label description>",
"<your new label color>",
)
}
USERS: str[str] = {"<your users>"}
def get_repos(
token: str,
users: set[str],
) -> set[str]:
repos = set()
for user in users:
response = requests.get(
url=f"https://api.github.com/users/{user}/repos",
headers={
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28",
},
)
for repo in response.json():
repos.add(repo["full_name"])
return repos
def create_labels(
token: str,
repos: set[str],
labels: set[tuple[str, str, str]],
):
for repo in repos:
for name, description, color in labels:
requests.post(
url=f"https://api.github.com/repos/{repo}/labels",
headers={
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28",
},
json={
"name": name,
"description": description,
"color": color,
},
)
repos = get_repos(token=TOKEN, users=USERS)
print("Creating labels")
create_labels(token=TOKEN, repos=repos, labels=LABELS)
print("Finished to create labels")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment