Created
June 22, 2023 06:40
-
-
Save bjarneo/180915f62d309dbfaa774e99b07eb607 to your computer and use it in GitHub Desktop.
add and remove autolinks from github with python
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
import os | |
import sys | |
import requests | |
import argparse | |
# replace the values below with your GitHub username and organization name | |
github_username = "<your_github_name>" | |
github_org = "<your_github_org>" | |
autolinks = [ | |
{ | |
"url_template": "<url_template>", | |
"key_prefix": "MEN-", | |
}, | |
{ | |
"url_template": "<url_template>", | |
"key_prefix": "VIP-", | |
}, | |
] | |
parser = argparse.ArgumentParser(description="Manage autolinks for GitHub repositories") | |
parser.add_argument( | |
"action", choices=["update", "remove"], help="Action to perform: update or remove" | |
) | |
args = parser.parse_args() | |
def update_autolinks(repo_name): | |
""" | |
Create autolinks for the specified repository. | |
Args: | |
repo_name (str): Name of the repository. | |
Returns: | |
None | |
""" | |
for autolink in autolinks: | |
response = requests.post( | |
f"https://api.github.com/repos/{github_org}/{repo_name}/autolinks", | |
json=autolink, | |
auth=(github_username, os.getenv("GITHUB_TOKEN")), | |
) | |
if response.status_code == 201: | |
print(f"Autolink created for {repo_name}") | |
elif response.status_code == 422: | |
print(f"Autolink already exists for {repo_name}") | |
else: | |
print(f"Error creating autolink for {repo_name}: {response.status_code}") | |
def remove_autolinks(repo_name): | |
""" | |
Remove autolinks from the specified repository. | |
Args: | |
repo_name (str): Name of the repository. | |
Returns: | |
None | |
""" | |
autolinks = get_autolink_id(repo_name) | |
for autolink in autolinks: | |
response = requests.delete( | |
f"https://api.github.com/repos/{github_org}/{repo_name}/autolinks/{autolink['id']}", | |
auth=(github_username, os.getenv("GITHUB_TOKEN")), | |
) | |
if response.status_code == 204: | |
print(f"Autolink removed for {repo_name}") | |
elif response.status_code == 404: | |
print(f"Autolink not found for {repo_name}") | |
else: | |
print(f"Error removing autolink for {repo_name}: {response.status_code}") | |
def get_repositories(): | |
""" | |
Get the list of repositories for the organization. | |
Returns: | |
list: List of repositories. | |
""" | |
pages = 2 | |
repositories = [] | |
for page in range(pages): | |
response = requests.get( | |
f"https://api.github.com/orgs/{github_org}/repos?type=sources&sort=pushed&direction=desc&per_page=100&page={page}", | |
auth=(github_username, os.getenv("GITHUB_TOKEN")), | |
) | |
if response.status_code == 200: | |
repositories.extend(response.json()) | |
else: | |
print(f"Error fetching repositories: {response.status_code}") | |
return repositories | |
def get_autolink_id(repo): | |
""" | |
Get the autolinks for the specified repository. | |
Args: | |
repo (str): Name of the repository. | |
Returns: | |
list: List of autolinks. | |
""" | |
url = f"https://api.github.com/repos/{github_org}/{repo}/autolinks" | |
headers = {"Authorization": f'Token {os.getenv("GITHUB_TOKEN")}'} | |
response = requests.get(url, headers=headers) | |
if response.status_code == 200: | |
autolinks = response.json() | |
return autolinks | |
def main(): | |
""" | |
Main function to update autolinks for all repositories. | |
Returns: | |
None | |
""" | |
repositories = get_repositories() | |
for repo in repositories: | |
if args.action == "remove": | |
remove_autolinks(repo["name"]) | |
elif args.action == "update": | |
update_autolinks(repo["name"]) | |
if __name__ == "__main__": | |
try: | |
main() | |
except KeyboardInterrupt: | |
print("\nProcess interrupted. Exiting...") | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment