|
| 1 | +"""Resolve contribute-patch workflow metadata for host repositories.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import pathlib |
| 7 | +from urllib.parse import urlparse |
| 8 | + |
| 9 | +from syncweaver.lockfile import ( |
| 10 | + load_existing_lockfile, |
| 11 | + resolve_source_path_from_lockfile, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def _repo_slug_from_url(url: str) -> str: |
| 16 | + """Extract OWNER/REPO slug from a normalized repository URL.""" |
| 17 | + parsed = urlparse(url.strip()) |
| 18 | + if parsed.scheme not in {"http", "https"}: |
| 19 | + raise ValueError(f"Cannot derive repository slug from: {url}") |
| 20 | + |
| 21 | + path_parts = [part for part in parsed.path.split("/") if part] |
| 22 | + if len(path_parts) < 2: |
| 23 | + raise ValueError(f"Cannot derive repository slug from: {url}") |
| 24 | + |
| 25 | + owner = path_parts[0] |
| 26 | + repo = path_parts[1] |
| 27 | + slug = f"{owner}/{repo}" |
| 28 | + return slug |
| 29 | + |
| 30 | + |
| 31 | +def resolve_contribute_patch_metadata( |
| 32 | + *, |
| 33 | + lockfile: pathlib.Path, |
| 34 | + host_cwd: pathlib.Path, |
| 35 | + source_path: str | None, |
| 36 | + repo_url: str | None, |
| 37 | + source_repository: str | None, |
| 38 | + patch_path: str | None, |
| 39 | + source_base_ref: str | None, |
| 40 | +) -> dict[str, str]: |
| 41 | + """Resolve workflow inputs into concrete source and patch metadata. |
| 42 | +
|
| 43 | + Args: |
| 44 | + lockfile: Path to the syncweaver lockfile in the host repository. |
| 45 | + host_cwd: Host repository working directory used for resolving relative paths. |
| 46 | + source_path: Optional tracked source path override. |
| 47 | + repo_url: Optional source repository URL or OWNER/REPO shorthand. |
| 48 | + source_repository: Optional explicit OWNER/REPO source repository. |
| 49 | + patch_path: Optional explicit patch file path. |
| 50 | + source_base_ref: Optional explicit source repository base ref. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + dict[str, str]: Resolved metadata fields required by the workflow. |
| 54 | +
|
| 55 | + Raises: |
| 56 | + ValueError: If lockfile metadata is missing or ambiguous. |
| 57 | + FileNotFoundError: If resolved patch file does not exist. |
| 58 | + """ |
| 59 | + source_path_input = "" |
| 60 | + repo_url_input = "" |
| 61 | + source_repository_input = "" |
| 62 | + patch_path_input = "" |
| 63 | + source_base_ref_input = "" |
| 64 | + |
| 65 | + if source_path is not None: |
| 66 | + source_path_input = source_path.strip() |
| 67 | + if repo_url is not None: |
| 68 | + repo_url_input = repo_url.strip() |
| 69 | + if source_repository is not None: |
| 70 | + source_repository_input = source_repository.strip() |
| 71 | + if patch_path is not None: |
| 72 | + patch_path_input = patch_path.strip() |
| 73 | + if source_base_ref is not None: |
| 74 | + source_base_ref_input = source_base_ref.strip() |
| 75 | + |
| 76 | + repo_selector = repo_url_input |
| 77 | + if not repo_selector and source_repository_input: |
| 78 | + repo_selector = source_repository_input |
| 79 | + |
| 80 | + resolved_source_path = resolve_source_path_from_lockfile( |
| 81 | + lockfile=lockfile, |
| 82 | + source_path=source_path_input, |
| 83 | + repo_url=repo_selector, |
| 84 | + ) |
| 85 | + |
| 86 | + lock_data = load_existing_lockfile(lockfile) |
| 87 | + sources = lock_data.get("sources", {}) |
| 88 | + source_entry_raw = sources.get(resolved_source_path) |
| 89 | + if not isinstance(source_entry_raw, dict): |
| 90 | + raise ValueError( |
| 91 | + f"lockfile source entry is invalid for source_path: {resolved_source_path}" |
| 92 | + ) |
| 93 | + |
| 94 | + resolved_repo_url = str(source_entry_raw.get("repo_url", "")).strip() |
| 95 | + if not resolved_repo_url: |
| 96 | + raise ValueError( |
| 97 | + "lockfile source entry is missing repo_url for source_path: " |
| 98 | + f"{resolved_source_path}" |
| 99 | + ) |
| 100 | + |
| 101 | + resolved_source_repository = source_repository_input |
| 102 | + if not resolved_source_repository: |
| 103 | + resolved_source_repository = _repo_slug_from_url(resolved_repo_url) |
| 104 | + |
| 105 | + resolved_patch_path = patch_path_input |
| 106 | + if not resolved_patch_path: |
| 107 | + resolved_patch_path = str(source_entry_raw.get("patch", "")).strip() |
| 108 | + if not resolved_patch_path: |
| 109 | + raise ValueError( |
| 110 | + "no patch path was provided and no tracked patch exists in lockfile " |
| 111 | + f"for source_path: {resolved_source_path}" |
| 112 | + ) |
| 113 | + |
| 114 | + patch_file = (host_cwd / pathlib.Path(resolved_patch_path)).resolve() |
| 115 | + if not patch_file.exists(): |
| 116 | + raise FileNotFoundError( |
| 117 | + "resolved patch file does not exist in host repository: " |
| 118 | + f"{resolved_patch_path}" |
| 119 | + ) |
| 120 | + |
| 121 | + resolved_source_base_ref = source_base_ref_input |
| 122 | + if not resolved_source_base_ref: |
| 123 | + resolved_source_base_ref = ( |
| 124 | + str(source_entry_raw.get("ref", "")).strip() or "main" |
| 125 | + ) |
| 126 | + |
| 127 | + result = { |
| 128 | + "source_path": resolved_source_path, |
| 129 | + "repo_url": resolved_repo_url, |
| 130 | + "source_repository": resolved_source_repository, |
| 131 | + "patch_path": resolved_patch_path, |
| 132 | + "source_base_ref": resolved_source_base_ref, |
| 133 | + } |
| 134 | + return result |
| 135 | + |
| 136 | + |
| 137 | +def write_github_output(outputs: dict[str, str], output_path: pathlib.Path) -> None: |
| 138 | + """Write key-value pairs to the GitHub Actions output file.""" |
| 139 | + with output_path.open("a", encoding="utf-8") as handle: |
| 140 | + for key, value in outputs.items(): |
| 141 | + handle.write(f"{key}={value}\n") |
| 142 | + |
| 143 | + |
| 144 | +def main() -> int: |
| 145 | + """Resolve metadata from workflow environment and emit GitHub outputs.""" |
| 146 | + lockfile = pathlib.Path(os.environ.get("LOCKFILE_PATH", ".syncweaver-lock.json")) |
| 147 | + host_cwd = pathlib.Path(os.environ.get("SYNCWEAVER_HOST_CWD", ".")).resolve() |
| 148 | + source_path = os.environ.get("SOURCE_PATH_INPUT", "") |
| 149 | + repo_url = os.environ.get("REPO_URL_INPUT", "") |
| 150 | + source_repository = os.environ.get("SOURCE_REPOSITORY_INPUT", "") |
| 151 | + patch_path = os.environ.get("PATCH_PATH_INPUT", "") |
| 152 | + source_base_ref = os.environ.get("SOURCE_BASE_REF_INPUT", "") |
| 153 | + output_file = os.environ.get("GITHUB_OUTPUT", "") |
| 154 | + |
| 155 | + if not output_file: |
| 156 | + raise RuntimeError("GITHUB_OUTPUT is not set") |
| 157 | + |
| 158 | + resolved = resolve_contribute_patch_metadata( |
| 159 | + lockfile=lockfile, |
| 160 | + host_cwd=host_cwd, |
| 161 | + source_path=source_path, |
| 162 | + repo_url=repo_url, |
| 163 | + source_repository=source_repository, |
| 164 | + patch_path=patch_path, |
| 165 | + source_base_ref=source_base_ref, |
| 166 | + ) |
| 167 | + write_github_output(resolved, pathlib.Path(output_file)) |
| 168 | + return 0 |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + raise SystemExit(main()) |
0 commit comments