|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2026 Apollo Authors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Resolve Sonatype repository context for release deployments.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import base64 |
| 20 | +import json |
| 21 | +import os |
| 22 | +import urllib.error |
| 23 | +import urllib.parse |
| 24 | +import urllib.request |
| 25 | +from pathlib import Path |
| 26 | +from typing import Any |
| 27 | + |
| 28 | +from github_actions_utils import write_output |
| 29 | + |
| 30 | +OSSRH_BASE = "https://ossrh-staging-api.central.sonatype.com" |
| 31 | + |
| 32 | + |
| 33 | +def request_json(url: str, headers: dict[str, str]) -> tuple[int | None, dict[str, Any]]: |
| 34 | + request = urllib.request.Request(url=url, method="GET", headers=headers) |
| 35 | + try: |
| 36 | + with urllib.request.urlopen(request, timeout=30) as response: |
| 37 | + body = response.read().decode("utf-8") |
| 38 | + if not body: |
| 39 | + return response.status, {} |
| 40 | + try: |
| 41 | + return response.status, json.loads(body) |
| 42 | + except json.JSONDecodeError: |
| 43 | + return response.status, {"raw": body} |
| 44 | + except urllib.error.HTTPError as error: |
| 45 | + body = error.read().decode("utf-8") |
| 46 | + try: |
| 47 | + payload = json.loads(body) if body else {} |
| 48 | + except json.JSONDecodeError: |
| 49 | + payload = {"raw": body} |
| 50 | + payload.setdefault("error", f"HTTP {error.code}") |
| 51 | + return error.code, payload |
| 52 | + except Exception as error: # noqa: BLE001 |
| 53 | + return None, {"error": str(error)} |
| 54 | + |
| 55 | + |
| 56 | +def main() -> int: |
| 57 | + target_repository = os.environ.get("TARGET_REPOSITORY", "").strip() |
| 58 | + namespace = os.environ.get("TARGET_NAMESPACE", "").strip() |
| 59 | + username = os.environ.get("MAVEN_USERNAME", "") |
| 60 | + password = os.environ.get("MAVEN_CENTRAL_TOKEN", "") |
| 61 | + context_path = Path( |
| 62 | + os.environ.get("REPOSITORY_CONTEXT_FILE", "repository-context.json") |
| 63 | + ) |
| 64 | + |
| 65 | + context: dict[str, Any] = { |
| 66 | + "target_repository": target_repository, |
| 67 | + "namespace": namespace, |
| 68 | + "status": "not_applicable", |
| 69 | + "reason": "repository input is not releases", |
| 70 | + "repository_key": "", |
| 71 | + "portal_deployment_id": "", |
| 72 | + "search_candidates": [], |
| 73 | + } |
| 74 | + |
| 75 | + if target_repository == "releases": |
| 76 | + if not username or not password: |
| 77 | + context["status"] = "manual_required" |
| 78 | + context["reason"] = "Missing MAVEN_USERNAME/MAVEN_CENTRAL_TOKEN" |
| 79 | + else: |
| 80 | + token = base64.b64encode(f"{username}:{password}".encode("utf-8")).decode("utf-8") |
| 81 | + headers = { |
| 82 | + "Authorization": f"Bearer {token}", |
| 83 | + "Accept": "application/json", |
| 84 | + } |
| 85 | + |
| 86 | + searches = [ |
| 87 | + ("open", "client"), |
| 88 | + ("closed", "client"), |
| 89 | + ("open", "any"), |
| 90 | + ("closed", "any"), |
| 91 | + ] |
| 92 | + selected: dict[str, Any] | None = None |
| 93 | + last_error = "" |
| 94 | + |
| 95 | + for state, ip in searches: |
| 96 | + url = ( |
| 97 | + f"{OSSRH_BASE}/manual/search/repositories?" |
| 98 | + f"profile_id={urllib.parse.quote(namespace)}" |
| 99 | + f"&state={urllib.parse.quote(state)}" |
| 100 | + f"&ip={urllib.parse.quote(ip)}" |
| 101 | + ) |
| 102 | + status, payload = request_json(url, headers) |
| 103 | + if status is None: |
| 104 | + last_error = payload.get("error", "unknown error") |
| 105 | + context["search_candidates"].append( |
| 106 | + { |
| 107 | + "state": state, |
| 108 | + "ip": ip, |
| 109 | + "status": None, |
| 110 | + "count": 0, |
| 111 | + "error": last_error, |
| 112 | + } |
| 113 | + ) |
| 114 | + continue |
| 115 | + |
| 116 | + if status < 200 or status >= 300: |
| 117 | + http_error = payload.get("error", f"HTTP {status}") |
| 118 | + last_error = http_error |
| 119 | + context["search_candidates"].append( |
| 120 | + { |
| 121 | + "state": state, |
| 122 | + "ip": ip, |
| 123 | + "status": status, |
| 124 | + "count": 0, |
| 125 | + "error": http_error, |
| 126 | + } |
| 127 | + ) |
| 128 | + continue |
| 129 | + |
| 130 | + repositories = ( |
| 131 | + payload.get("repositories", []) if isinstance(payload, dict) else [] |
| 132 | + ) |
| 133 | + context["search_candidates"].append( |
| 134 | + {"state": state, "ip": ip, "status": status, "count": len(repositories)} |
| 135 | + ) |
| 136 | + if repositories: |
| 137 | + selected = repositories[0] |
| 138 | + break |
| 139 | + |
| 140 | + if selected: |
| 141 | + context["status"] = "resolved" |
| 142 | + context["reason"] = "" |
| 143 | + context["repository_key"] = selected.get("key", "") or "" |
| 144 | + context["portal_deployment_id"] = ( |
| 145 | + selected.get("portal_deployment_id", "") or "" |
| 146 | + ) |
| 147 | + else: |
| 148 | + context["status"] = "manual_required" |
| 149 | + context["reason"] = last_error or "No staging repository key found" |
| 150 | + |
| 151 | + context_path.write_text(json.dumps(context, indent=2) + "\n", encoding="utf-8") |
| 152 | + write_output("repository_key", context.get("repository_key", "")) |
| 153 | + write_output("portal_deployment_id", context.get("portal_deployment_id", "")) |
| 154 | + write_output("status", context.get("status", "")) |
| 155 | + write_output("reason", context.get("reason", "")) |
| 156 | + return 0 |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + raise SystemExit(main()) |
0 commit comments