fix(tools): scan all branches for workflow files
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
sync-registry-secrets: verbose-mode version.
|
||||
sync-registry-secrets: verbose-mode version with full-branch scan.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
@@ -50,49 +50,77 @@ def list_my_repos(limit):
|
||||
return out[:limit]
|
||||
|
||||
|
||||
def list_workflow_files(full_name):
|
||||
files = []
|
||||
for prefix in (".gitea/workflows", ".github/workflows"):
|
||||
url = f"{API}/repos/{full_name}/contents/{prefix}?ref=main"
|
||||
def list_repo_branches(full_name):
|
||||
branches = []
|
||||
page = 1
|
||||
while True:
|
||||
url = f"{API}/repos/{full_name}/branches?limit=50&page={page}"
|
||||
status, body = http("GET", url)
|
||||
if status != 200:
|
||||
for ref in ("master", "default"):
|
||||
url = f"{API}/repos/{full_name}/contents/{prefix}?ref={ref}"
|
||||
status, body = http("GET", url)
|
||||
if status == 200:
|
||||
break
|
||||
else:
|
||||
continue
|
||||
break
|
||||
try:
|
||||
entries = json.loads(body)
|
||||
arr = json.loads(body)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
if not isinstance(entries, list):
|
||||
continue
|
||||
for entry in entries:
|
||||
name = entry.get("name", "")
|
||||
if name.endswith((".yml", ".yaml")):
|
||||
files.append((prefix + "/" + name, entry.get("sha")))
|
||||
break
|
||||
if not arr:
|
||||
break
|
||||
branches.extend([b["name"] for b in arr if isinstance(b, dict) and "name" in b])
|
||||
if len(arr) < 50:
|
||||
break
|
||||
page += 1
|
||||
return branches
|
||||
|
||||
|
||||
def list_workflow_files(full_name):
|
||||
files = []
|
||||
seen = set()
|
||||
branches_to_check = list_repo_branches(full_name)
|
||||
branches_to_check = [b for b in branches_to_check if b not in seen and not seen.add(b)]
|
||||
if not branches_to_check:
|
||||
try:
|
||||
r = http("GET", f"{API}/repos/{full_name}")
|
||||
if isinstance(r, tuple) and r[0] == 200:
|
||||
data = json.loads(r[1])
|
||||
if isinstance(data, dict) and data.get("default_branch"):
|
||||
branches_to_check.append(data["default_branch"])
|
||||
except Exception:
|
||||
pass
|
||||
if not branches_to_check:
|
||||
branches_to_check = ["main", "master", "default"]
|
||||
branches_to_check = branches_to_check[:25]
|
||||
for prefix in (".gitea/workflows", ".github/workflows"):
|
||||
for branch in branches_to_check:
|
||||
url = f"{API}/repos/{full_name}/contents/{prefix}?ref={branch}"
|
||||
status, body = http("GET", url)
|
||||
if status != 200:
|
||||
continue
|
||||
try:
|
||||
entries = json.loads(body)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
if not isinstance(entries, list):
|
||||
continue
|
||||
for entry in entries:
|
||||
name = entry.get("name", "")
|
||||
if name.endswith((".yml", ".yaml")):
|
||||
files.append((prefix + "/" + name, entry.get("sha")))
|
||||
return files
|
||||
|
||||
|
||||
def workflow_uses_secret(full_name, path, secret_name):
|
||||
url = f"{API}/repos/{full_name}/raw/main/{path}"
|
||||
status, body = http("GET", url)
|
||||
if status != 200:
|
||||
return False
|
||||
needle = f"secrets.{secret_name}"
|
||||
return needle in body
|
||||
def workflow_uses_secret(full_name, path, ref):
|
||||
for branch in ("main", "master", "default", ref):
|
||||
url = f"{API}/repos/{full_name}/raw/{branch}/{path}"
|
||||
status, body = http("GET", url)
|
||||
if status == 200:
|
||||
needle = f"secrets.{ref}"
|
||||
return needle in body
|
||||
return False
|
||||
|
||||
|
||||
def secret_exists(full_name, secret_name):
|
||||
url = f"{API}/repos/{full_name}/actions/secrets/{secret_name}"
|
||||
status, body = http("GET", url)
|
||||
if status == 200:
|
||||
return True
|
||||
if status in (404, 422, 405):
|
||||
return False
|
||||
return False
|
||||
status, _ = http("GET", url)
|
||||
return status == 200
|
||||
|
||||
|
||||
def put_secret(full_name, secret_name, value):
|
||||
|
||||
Reference in New Issue
Block a user