Skip to content

Instantly share code, notes, and snippets.

@tofran
Last active May 3, 2024 15:35
Show Gist options
  • Select an option

  • Save tofran/dbfa4bf71d8b8d1b72308380ea45b390 to your computer and use it in GitHub Desktop.

Select an option

Save tofran/dbfa4bf71d8b8d1b72308380ea45b390 to your computer and use it in GitHub Desktop.
# Go thru all vercel projects in any of your organizatons and remove the development "target"
# from all secrets that have other targets
import requests
VERCEL_TOKEN = "YOUT_TOKEN"
HEADERS = {"Authorization": f"Bearer {VERCEL_TOKEN}"}
BASE_API_URL = "https://api.vercel.com"
def get_organizations():
teams_response = requests.get(f"{BASE_API_URL}/v2/teams", headers=HEADERS)
if teams_response.status_code != 200:
print("Failed to fetch organizations:", teams_response.text)
return []
teams = teams_response.json()["teams"]
return teams
def fix_secrets(org_id):
projects_response = requests.get(f"{BASE_API_URL}/v9/projects?slug={org_id}&limit=100", headers=HEADERS)
if projects_response.status_code != 200:
print("Failed to fetch projects for organization:", org_id)
return
projects = projects_response.json()["projects"]
for project in projects:
project_name = project["name"]
project_response = requests.get(
f"{BASE_API_URL}/v9/projects/{project_name}?slug={org_id}",
headers=HEADERS,
)
if project_response.status_code != 200:
print(f"Failed to fetch project {project_name}:", project_response.text)
project_response.raise_for_status()
secrets = project_response.json()["env"]
for secret in secrets:
current_target = secret["target"]
if "development" not in current_target:
continue
new_targets = [
target # fmt: skip
for target in current_target
if target != "development"
]
if len(new_targets) == 0:
print(f"Skipping {secret['key']} as not targets would be left")
continue
print(f"{project_name} Updating secret {secret['key']} from {current_target} to {new_targets}")
secret_id = secret["id"]
response = requests.patch(
f"https://vercel.com/api/v7/projects/project_name/env/{secret_id}?slug={org_id}",
json={
"target": new_targets,
},
headers=HEADERS,
)
if project_response.status_code != 200:
print(f"Failed to update secret {secret['key']}:", response.text)
response.raise_for_status()
print("Updated!")
if __name__ == "__main__":
organizations = get_organizations()
for org in organizations:
org_id = org["slug"]
print(f"Organization: {org_id}")
fix_secrets(org_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment