Created
April 18, 2023 02:51
-
-
Save priyanshujain/d3b3a8c65ab4f7a234e7c77824333e07 to your computer and use it in GitHub Desktop.
Here's a Python script that you can use to check and replace the Ubuntu version in a GitHub workflow file for all your repositories.
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 requests | |
import base64 | |
# Set your GitHub API token | |
GITHUB_TOKEN = "GITHUB_TOKEN" | |
# Set the Ubuntu version you want to replace | |
old_ubuntu_versions = ["ubuntu-18.04", "ubuntu-20.04"] | |
# Set the new Ubuntu version you want to use | |
new_ubuntu_version = "ubuntu-22.04" | |
# Get a list of all your repositories | |
headers = { | |
"Authorization": f"token {GITHUB_TOKEN}", | |
"Accept": "application/vnd.github.v3+json", | |
} | |
# Set the organization whose repositories you want to retrieve | |
ORGANIZATION = "ORGANIZATION" | |
# Set the base URL for the GitHub API | |
base_url = "https://api.github.com" | |
# Set the parameters for the API request | |
params = { | |
"per_page": 100, # Maximum number of results per page | |
"page": 1, # Start with the first page of results | |
"sort": "updated", # Sort the results by the date they were last updated | |
} | |
all_repos = [] | |
# get repos of an organization for all pages | |
# Loop through all pages of results until no more pages are returned | |
while True: | |
# Send the API request to retrieve the repositories | |
response = requests.get( | |
f"{base_url}/orgs/{ORGANIZATION}/repos", params=params, headers=headers | |
) | |
# If the response was successful, add the repositories to the list | |
if response.status_code == 200: | |
repos = response.json() | |
for repo in repos: | |
all_repos.append(repo["name"]) | |
# If there are more pages of results, increase the page number and send another API request | |
if "next" in response.links: | |
params["page"] += 1 | |
else: | |
break | |
# If the response was not successful, print an error message and stop looping | |
else: | |
print(f"Failed to retrieve repositories: {response.status_code}") | |
break | |
old_repos = [] | |
# Loop through each repository and replace the Ubuntu version in its workflow file | |
for repo in all_repos: | |
# Get the contents of the workflow file | |
response = requests.get( | |
f"{base_url}/repos/okcredit/{repo}/contents/.github/workflows/deploy.yml", | |
headers=headers, | |
) | |
if response.status_code == 200: | |
content = response.json()["content"] | |
workflow_file = base64.b64decode(content).decode("utf-8") | |
# check if the workflow file contains the old Ubuntu version | |
for old_ubuntu_version in old_ubuntu_versions: | |
if old_ubuntu_version in workflow_file: | |
old_repos.append( | |
{"repo": repo, "old_ubuntu_version": old_ubuntu_version} | |
) | |
for old_ubuntu_version in old_ubuntu_versions: | |
# print the list of repositories that contain the old Ubuntu version | |
print(f"Repositories with {old_ubuntu_version}:") | |
for repo in old_repos: | |
if repo["old_ubuntu_version"] == old_ubuntu_version: | |
print(repo["repo"]) |
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 requests | |
import base64 | |
# Set your GitHub API token | |
GITHUB_TOKEN = "GITHUB_TOKEN" | |
# Set the Ubuntu version you want to replace | |
old_ubuntu_versions = ["ubuntu-18.04", "ubuntu-20.04"] | |
# Set the new Ubuntu version you want to use | |
new_ubuntu_version = "ubuntu-22.04" | |
# Get a list of all your repositories | |
headers = { | |
"Authorization": f"token {GITHUB_TOKEN}", | |
"Accept": "application/vnd.github.v3+json", | |
} | |
# Set the organization whose repositories you want to retrieve | |
ORGANIZATION = "ORGANIZATION" | |
# Set the base URL for the GitHub API | |
base_url = "https://api.github.com" | |
# Set the parameters for the API request | |
params = { | |
"per_page": 100, # Maximum number of results per page | |
"page": 1, # Start with the first page of results | |
"sort": "updated", # Sort the results by the date they were last updated | |
} | |
all_repos = [] | |
# get repos of an organization for all pages | |
# Loop through all pages of results until no more pages are returned | |
while True: | |
# Send the API request to retrieve the repositories | |
response = requests.get( | |
f"{base_url}/orgs/{ORGANIZATION}/repos", params=params, headers=headers | |
) | |
# If the response was successful, add the repositories to the list | |
if response.status_code == 200: | |
repos = response.json() | |
for repo in repos: | |
all_repos.append(repo["name"]) | |
# If there are more pages of results, increase the page number and send another API request | |
if "next" in response.links: | |
params["page"] += 1 | |
else: | |
break | |
# If the response was not successful, print an error message and stop looping | |
else: | |
print(f"Failed to retrieve repositories: {response.status_code}") | |
break | |
# Loop through each repository and replace the Ubuntu version in its workflow file | |
for repo in all_repos: | |
# Get the contents of the workflow file | |
response = requests.get( | |
f"{base_url}/repos/okcredit/{repo}/contents/.github/workflows/deploy.yml", | |
headers=headers, | |
) | |
if response.status_code == 200: | |
content = response.json()["content"] | |
workflow_file = base64.b64decode(content).decode("utf-8") | |
# check if the workflow file contains the old Ubuntu version | |
for old_ubuntu_version in old_ubuntu_versions: | |
if old_ubuntu_version in workflow_file: | |
workflow_file = base64.b64decode(content).decode("utf-8") | |
# Replace the old Ubuntu version with the new one | |
new_workflow_file = workflow_file.replace(old_ubuntu_version, new_ubuntu_version) | |
# Create a branch to make the changes | |
branch_name = f"update-ubuntu-version-{new_ubuntu_version}" | |
data = { | |
"ref": "refs/heads/main" | |
} | |
response = requests.post( | |
f"{base_url}/repos/{ORGANIZATION}/{repo}/git/refs", | |
headers=headers, | |
json=data | |
) | |
if response.status_code == 201: | |
sha = response.json()["object"]["sha"] | |
# Create a new file with the updated contents | |
data = { | |
"message": "Update Ubuntu version in GitHub Actions workflow", | |
"branch": branch_name, | |
"content": base64.b64encode(new_workflow_file.encode("utf-8")).decode("utf-8"), | |
"path": ".github/workflows/main.yml", | |
"sha": sha | |
} | |
response = requests.put( | |
f"{base_url}/repos/{ORGANIZATION}/{repo}/contents/.github/workflows/main.yml", | |
headers=headers, | |
json=data | |
) | |
if response.status_code == 201: | |
print(f"Successfully created new file in {repo}") | |
# Create a pull request for the changes | |
data = { | |
"title": f"Update Ubuntu version to {new_ubuntu_version}", | |
"body": f"This pull request updates the Ubuntu version used in the GitHub Actions workflow from {old_ubuntu_version} to {new_ubuntu_version}.", | |
"head": branch_name, | |
"base": "main" | |
} | |
response = requests.post( | |
f"{base_url}/repos/{ORGANIZATION}/{repo}/pulls", | |
headers=headers, | |
json=data | |
) | |
if response.status_code == 201: | |
print(f"Successfully created pull request for {repo}") | |
else: | |
print(f"Failed to create pull request for {repo}") | |
else: | |
print(f"Failed to create new file in {repo}") | |
else: | |
print(f"Failed to create branch in {repo}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment