|
1 | 1 | import os |
2 | 2 | import pathlib |
| 3 | +import re |
3 | 4 | import shlex |
| 5 | +import shutil |
4 | 6 | import subprocess |
5 | 7 |
|
6 | 8 | CI_PROVIDER = "{{ copier__ci_provider }}" |
7 | 9 | SEMANTIC_RELEASE = {{ "True" if copier__enable_semantic_release else "False" }} |
8 | 10 | SECRET_SCANNING = {{ "True" if copier__enable_secret_scanning else "False" }} |
9 | 11 | TASK_RUNNER = "{{ copier__task_runner }}" |
| 12 | +CREATE_REPO = {{ "True" if copier__create_repo else "False" }} |
| 13 | +REPO_VISIBILITY = "{{ copier__repo_visibility }}" |
10 | 14 |
|
11 | 15 | ROOT = pathlib.Path(".") |
12 | 16 | TERMINATOR = "\x1b[0m" |
@@ -76,8 +80,124 @@ def configure_git_remote() -> None: |
76 | 80 | ) |
77 | 81 |
|
78 | 82 |
|
| 83 | +def parse_repo_url(repo_url: str) -> tuple[str, str] | None: |
| 84 | + patterns = [ |
| 85 | + r"^git@(?P<host>[^:]+):(?P<path>[^ ]+?)(?:\.git)?$", |
| 86 | + r"^https?://(?P<host>[^/]+)/(?P<path>[^ ]+?)(?:\.git)?/?$", |
| 87 | + ] |
| 88 | + for pattern in patterns: |
| 89 | + match = re.match(pattern, repo_url) |
| 90 | + if match: |
| 91 | + host = match.group("host") |
| 92 | + path = match.group("path").strip("/") |
| 93 | + if path.count("/") >= 1: |
| 94 | + return (host, path) |
| 95 | + return None |
| 96 | + |
| 97 | + |
| 98 | +def maybe_create_repo() -> None: |
| 99 | + if not CREATE_REPO: |
| 100 | + return |
| 101 | + |
| 102 | + repo_url = "{{ copier__repo_url }}".strip() |
| 103 | + if not repo_url: |
| 104 | + print(WARNING + "Repo creation requested but no repo_url was provided." + TERMINATOR) |
| 105 | + return |
| 106 | + |
| 107 | + parsed = parse_repo_url(repo_url) |
| 108 | + if not parsed: |
| 109 | + print( |
| 110 | + WARNING |
| 111 | + + f"Repo URL is not in a supported SSH/HTTPS format ({repo_url}). Skipping repo creation." |
| 112 | + + TERMINATOR |
| 113 | + ) |
| 114 | + return |
| 115 | + host, repo_name = parsed |
| 116 | + |
| 117 | + if CI_PROVIDER == "github": |
| 118 | + if not shutil.which("gh"): |
| 119 | + print(WARNING + "gh CLI is not installed. Skipping repo creation." + TERMINATOR) |
| 120 | + return |
| 121 | + |
| 122 | + repo_exists = subprocess.run( |
| 123 | + ["gh", "repo", "view", repo_name, "--hostname", host], |
| 124 | + stdout=subprocess.DEVNULL, |
| 125 | + stderr=subprocess.DEVNULL, |
| 126 | + ) |
| 127 | + if repo_exists.returncode == 0: |
| 128 | + print(INFO + f"GitHub repository {repo_name} already exists." + TERMINATOR) |
| 129 | + return |
| 130 | + |
| 131 | + print( |
| 132 | + INFO |
| 133 | + + f"Creating GitHub repository {repo_name} ({REPO_VISIBILITY})..." |
| 134 | + + TERMINATOR |
| 135 | + ) |
| 136 | + create_repo = subprocess.run( |
| 137 | + ["gh", "repo", "create", repo_name, f"--{REPO_VISIBILITY}", "--hostname", host], |
| 138 | + capture_output=True, |
| 139 | + text=True, |
| 140 | + ) |
| 141 | + if create_repo.returncode != 0: |
| 142 | + error = create_repo.stderr.strip() or create_repo.stdout.strip() or "unknown error" |
| 143 | + print( |
| 144 | + WARNING |
| 145 | + + f"Failed to create GitHub repository {repo_name}: {error}" |
| 146 | + + TERMINATOR |
| 147 | + ) |
| 148 | + return |
| 149 | + print(SUCCESS + f"GitHub repository {repo_name} created." + TERMINATOR) |
| 150 | + return |
| 151 | + |
| 152 | + if CI_PROVIDER == "gitlab": |
| 153 | + if not shutil.which("glab"): |
| 154 | + print(WARNING + "glab CLI is not installed. Skipping repo creation." + TERMINATOR) |
| 155 | + return |
| 156 | + |
| 157 | + glab_env = os.environ.copy() |
| 158 | + glab_env["GITLAB_HOST"] = host |
| 159 | + repo_exists = subprocess.run( |
| 160 | + ["glab", "repo", "view", repo_name], |
| 161 | + stdout=subprocess.DEVNULL, |
| 162 | + stderr=subprocess.DEVNULL, |
| 163 | + env=glab_env, |
| 164 | + ) |
| 165 | + if repo_exists.returncode == 0: |
| 166 | + print(INFO + f"GitLab repository {repo_name} already exists." + TERMINATOR) |
| 167 | + return |
| 168 | + |
| 169 | + print( |
| 170 | + INFO |
| 171 | + + f"Creating GitLab repository {repo_name} ({REPO_VISIBILITY})..." |
| 172 | + + TERMINATOR |
| 173 | + ) |
| 174 | + create_repo = subprocess.run( |
| 175 | + ["glab", "repo", "create", repo_name, f"--{REPO_VISIBILITY}"], |
| 176 | + capture_output=True, |
| 177 | + text=True, |
| 178 | + env=glab_env, |
| 179 | + ) |
| 180 | + if create_repo.returncode != 0: |
| 181 | + error = create_repo.stderr.strip() or create_repo.stdout.strip() or "unknown error" |
| 182 | + print( |
| 183 | + WARNING |
| 184 | + + f"Failed to create GitLab repository {repo_name}: {error}" |
| 185 | + + TERMINATOR |
| 186 | + ) |
| 187 | + return |
| 188 | + print(SUCCESS + f"GitLab repository {repo_name} created." + TERMINATOR) |
| 189 | + return |
| 190 | + |
| 191 | + print( |
| 192 | + WARNING |
| 193 | + + f"Repo creation not implemented for provider '{CI_PROVIDER}'. Skipping." |
| 194 | + + TERMINATOR |
| 195 | + ) |
| 196 | + |
| 197 | + |
79 | 198 | def main() -> None: |
80 | 199 | init_git_repo() |
| 200 | + maybe_create_repo() |
81 | 201 | configure_git_remote() |
82 | 202 | run_init_script() |
83 | 203 |
|
|
0 commit comments