Sync GitHub metadata by parsing push mirrors dynamically
Sync GitHub mirror metadata / sync-metadata (push) Failing after 3s
Sync GitHub mirror metadata / sync-metadata (push) Failing after 3s
This commit is contained in:
@@ -1,45 +1,45 @@
|
|||||||
name: Sync GitHub mirror metadata
|
name: Sync GitHub mirror metadata
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
schedule:
|
schedule:
|
||||||
- cron: "17 3 * * *"
|
- cron: "17 3 * * *"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
sync-metadata:
|
sync-metadata:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Sync description and topics
|
- name: Sync description and topics
|
||||||
env:
|
env:
|
||||||
GITEA_API_URL: ${{ gitea.api_url }}
|
GITEA_API_URL: ${{ gitea.api_url }}
|
||||||
GITEA_TOKEN: ${{ gitea.token }}
|
GITEA_TOKEN: ${{ gitea.token }}
|
||||||
SOURCE_REPOSITORY: ${{ gitea.repository }}
|
SOURCE_REPOSITORY: ${{ gitea.repository }}
|
||||||
GITHUB_CREDENTIALS: ${{ secrets.CREDENTIALS_FOR_GITHUB }}
|
GITHUB_CREDENTIALS: ${{ secrets.CREDENTIALS_FOR_GITHUB }}
|
||||||
GITHUB_REPO: Tirsvad-Udemy-100_days_of_code
|
run: |
|
||||||
run: |
|
python3 - <<'PY'
|
||||||
python3 - <<'PY'
|
import json
|
||||||
import json
|
import os
|
||||||
import os
|
import urllib.error
|
||||||
import urllib.error
|
import urllib.parse
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
|
||||||
def request_json(url, method="GET", headers=None, body=None):
|
def request_json(url, method="GET", headers=None, body=None):
|
||||||
request = urllib.request.Request(
|
request = urllib.request.Request(
|
||||||
url,
|
url,
|
||||||
data=json.dumps(body).encode("utf-8") if body is not None else None,
|
data=json.dumps(body).encode("utf-8") if body is not None else None,
|
||||||
headers=headers or {},
|
headers=headers or {},
|
||||||
method=method,
|
method=method,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(request, timeout=30) as response:
|
with urllib.request.urlopen(request, timeout=30) as response:
|
||||||
content = response.read()
|
content = response.read()
|
||||||
return json.loads(content) if content else None
|
return json.loads(content) if content else None
|
||||||
except urllib.error.HTTPError as error:
|
except urllib.error.HTTPError as error:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f"API request failed with HTTP {error.code} ({error.reason})"
|
f"API request failed with HTTP {error.code} ({error.reason})"
|
||||||
) from None
|
) from None
|
||||||
|
|
||||||
raw_credentials = os.environ.get("GITHUB_CREDENTIALS", "").strip()
|
raw_credentials = os.environ.get("GITHUB_CREDENTIALS", "").strip()
|
||||||
if not raw_credentials:
|
if not raw_credentials:
|
||||||
raise RuntimeError("CREDENTIALS_FOR_GITHUB is missing or empty.")
|
raise RuntimeError("CREDENTIALS_FOR_GITHUB is missing or empty.")
|
||||||
@@ -57,53 +57,82 @@ jobs:
|
|||||||
)
|
)
|
||||||
|
|
||||||
github_token = credentials.get("GITHUB_PAT")
|
github_token = credentials.get("GITHUB_PAT")
|
||||||
github_user = credentials.get("GITHUB_USER")
|
if not isinstance(github_token, str) or not github_token.strip():
|
||||||
if not github_token or not github_user:
|
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"CREDENTIALS_FOR_GITHUB must contain GITHUB_PAT and GITHUB_USER."
|
"CREDENTIALS_FOR_GITHUB must contain a non-empty GITHUB_PAT."
|
||||||
)
|
)
|
||||||
source_owner, separator, source_repo = os.environ[
|
|
||||||
"SOURCE_REPOSITORY"
|
source_owner, separator, source_repo = os.environ[
|
||||||
].partition("/")
|
"SOURCE_REPOSITORY"
|
||||||
if not separator or not source_owner or not source_repo:
|
].partition("/")
|
||||||
raise RuntimeError("Could not determine the Gitea source repository.")
|
if not separator or not source_owner or not source_repo:
|
||||||
|
raise RuntimeError("Could not determine the Gitea source repository.")
|
||||||
gitea_api_url = os.environ["GITEA_API_URL"].rstrip("/")
|
|
||||||
source_url = (
|
gitea_api_url = os.environ["GITEA_API_URL"].rstrip("/")
|
||||||
f"{gitea_api_url}/repos/{source_owner}/{source_repo}"
|
source_url = f"{gitea_api_url}/repos/{source_owner}/{source_repo}"
|
||||||
)
|
gitea_headers = {
|
||||||
source = request_json(
|
"Authorization": f"token {os.environ['GITEA_TOKEN']}",
|
||||||
source_url,
|
"Accept": "application/json",
|
||||||
headers={
|
}
|
||||||
"Authorization": f"token {os.environ['GITEA_TOKEN']}",
|
source = request_json(source_url, headers=gitea_headers)
|
||||||
"Accept": "application/json",
|
mirrors = request_json(
|
||||||
},
|
f"{source_url}/push_mirrors",
|
||||||
)
|
headers=gitea_headers,
|
||||||
|
)
|
||||||
github_repo = os.environ["GITHUB_REPO"]
|
if not isinstance(mirrors, list):
|
||||||
github_api_url = (
|
raise RuntimeError("Gitea returned an invalid push mirror list.")
|
||||||
f"https://api.github.com/repos/{github_user}/{github_repo}"
|
|
||||||
)
|
github_targets = []
|
||||||
github_headers = {
|
for mirror in mirrors:
|
||||||
"Authorization": f"Bearer {github_token}",
|
remote_address = mirror.get("remote_address", "")
|
||||||
"Accept": "application/vnd.github+json",
|
if remote_address.startswith("git@github.com:"):
|
||||||
"X-GitHub-Api-Version": "2022-11-28",
|
mirror_path = remote_address.split(":", 1)[1]
|
||||||
"Content-Type": "application/json",
|
else:
|
||||||
}
|
parsed_remote = urllib.parse.urlsplit(remote_address)
|
||||||
|
if parsed_remote.hostname != "github.com":
|
||||||
request_json(
|
continue
|
||||||
github_api_url,
|
mirror_path = parsed_remote.path.lstrip("/")
|
||||||
method="PATCH",
|
|
||||||
headers=github_headers,
|
mirror_path = mirror_path.removesuffix(".git").strip("/")
|
||||||
body={"description": source.get("description") or ""},
|
path_parts = mirror_path.split("/")
|
||||||
)
|
if len(path_parts) != 2 or not all(path_parts):
|
||||||
|
raise RuntimeError(
|
||||||
request_json(
|
"Could not determine the GitHub owner and repository "
|
||||||
f"{github_api_url}/topics",
|
"from a configured push mirror."
|
||||||
method="PUT",
|
)
|
||||||
headers=github_headers,
|
github_targets.append(tuple(path_parts))
|
||||||
body={"names": source.get("topics") or []},
|
|
||||||
)
|
if len(github_targets) != 1:
|
||||||
|
raise RuntimeError(
|
||||||
print(f"Synced description and topics to {github_user}/{github_repo}.")
|
"Expected exactly one GitHub push mirror for this repository; "
|
||||||
PY
|
f"found {len(github_targets)}."
|
||||||
|
)
|
||||||
|
|
||||||
|
github_owner, github_repo = github_targets[0]
|
||||||
|
github_api_url = (
|
||||||
|
"https://api.github.com/repos/"
|
||||||
|
f"{urllib.parse.quote(github_owner, safe='')}/"
|
||||||
|
f"{urllib.parse.quote(github_repo, safe='')}"
|
||||||
|
)
|
||||||
|
github_headers = {
|
||||||
|
"Authorization": f"Bearer {github_token.strip()}",
|
||||||
|
"Accept": "application/vnd.github+json",
|
||||||
|
"X-GitHub-Api-Version": "2022-11-28",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
}
|
||||||
|
|
||||||
|
request_json(
|
||||||
|
github_api_url,
|
||||||
|
method="PATCH",
|
||||||
|
headers=github_headers,
|
||||||
|
body={"description": source.get("description") or ""},
|
||||||
|
)
|
||||||
|
request_json(
|
||||||
|
f"{github_api_url}/topics",
|
||||||
|
method="PUT",
|
||||||
|
headers=github_headers,
|
||||||
|
body={"names": source.get("topics") or []},
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"Synced description and topics to {github_owner}/{github_repo}.")
|
||||||
|
PY
|
||||||
Reference in New Issue
Block a user