hosted/scripts/accept_invitations
2023-04-04 15:21:59 +02:00

53 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python3
"""
Simple script to accept all invitations on GitHub.
It takes GitHub access token as single parameter.
"""
import sys
import requests
def main():
headers = {
"Content-Type": "application/json",
"Accept": "application/vnd.github.v3+json",
"Authorization": f"token {sys.argv[1]}",
}
url = "https://api.github.com/user/memberships/orgs"
accept_url = "https://api.github.com/user/memberships/orgs/{}"
orgs = requests.get(url, headers=headers)
orgs.raise_for_status()
while 1:
orgs = requests.get(url, headers=headers)
orgs.raise_for_status()
for org in orgs.json():
if org["state"] == "pending":
name = org["organization"]["login"]
print(f"Accepting org {name}")
response = requests.patch(
accept_url.format(name), '{"state":"active"}', headers=headers
)
response.raise_for_status()
if "next" not in orgs.links:
break
url = orgs.links["next"]["url"]
url = "https://api.github.com/user/repository_invitations"
invitations = requests.get(url, headers=headers)
invitations.raise_for_status()
for invitation in invitations.json():
print("Accepting {}".format(invitation["repository"]["html_url"]))
response = requests.patch(invitation["url"], headers=headers)
response.raise_for_status()
if __name__ == "__main__":
main()