|
| 1 | +import time |
| 2 | +import functools |
| 3 | +import json |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | + |
| 8 | +class Hub(object): |
| 9 | + def __init__( |
| 10 | + self, src, dst, dst_token, account_type="user", |
| 11 | + clone_style="https", |
| 12 | + src_account_type=None, |
| 13 | + dst_account_type=None, |
| 14 | + api_timeout=60, |
| 15 | + gitea_url=None, |
| 16 | + ): |
| 17 | + self.api_timeout = api_timeout |
| 18 | + self.account_type = account_type |
| 19 | + self.src_account_type = src_account_type or account_type |
| 20 | + self.dst_account_type = dst_account_type or account_type |
| 21 | + self.src_type, self.src_account = src.split('/') |
| 22 | + self.dst_type, self.dst_account = dst.split('/') |
| 23 | + self._validate_account_type( |
| 24 | + self.src_type, self.src_account_type, 'source' |
| 25 | + ) |
| 26 | + self._validate_account_type( |
| 27 | + self.dst_type, self.dst_account_type, 'destination' |
| 28 | + ) |
| 29 | + self.dst_token = dst_token |
| 30 | + self.session = requests.Session() |
| 31 | + if self.dst_type == "gitea": |
| 32 | + self.dst_base = f'{gitea_url}/api/v1' |
| 33 | + elif self.dst_type == "gitee": |
| 34 | + self.dst_base = 'https://gitee.com/api/v5' |
| 35 | + elif self.dst_type == "github": |
| 36 | + self.dst_base = 'https://api.github.com' |
| 37 | + elif self.dst_type == "gitlab": |
| 38 | + self.dst_base = 'https://gitlab.com/api/v4' |
| 39 | + |
| 40 | + prefix = "https://" if clone_style == 'https' else 'git@' |
| 41 | + suffix = "/" if clone_style == 'https' else ':' |
| 42 | + if self.src_type == "gitea": |
| 43 | + self.src_base = f'{gitea_url}/api/v1' |
| 44 | + self.src_repo_base = prefix + gitea_url.replace("http://", "").replace("https://", "") + suffix |
| 45 | + elif self.src_type == "gitee": |
| 46 | + self.src_base = 'https://gitee.com/api/v5' |
| 47 | + self.src_repo_base = prefix + 'gitee.com' + suffix |
| 48 | + elif self.src_type == "github": |
| 49 | + self.src_base = 'https://api.github.com' |
| 50 | + self.src_repo_base = prefix + 'github.com' + suffix |
| 51 | + print(f"DEBUG self.src_repo_base if gitea: {prefix + gitea_url.replace("http://", "").replace("https://", "") + suffix}") |
| 52 | + elif self.src_type == "gitlab": |
| 53 | + self.src_base = 'https://gitlab.com/api/v4' |
| 54 | + self.src_repo_base = prefix + 'gitlab.com' + suffix |
| 55 | + self.src_repo_base = self.src_repo_base + self.src_account |
| 56 | + # TODO: toekn push support |
| 57 | + if self.dst_type == "gitea": |
| 58 | + #gitea@127.0.0.1:minlearn /inst.git |
| 59 | + self.dst_repo_base = "gitea@" + gitea_url.replace("http://", "").replace("https://", "").replace("/gitea", "") + ":" + self.dst_account |
| 60 | + print(f"DEBUG self.dst_repo_base if gitea: {self.dst_repo_base}") |
| 61 | + else: |
| 62 | + prefix = "git@" + self.dst_type + ".com:" |
| 63 | + self.dst_repo_base = prefix + self.dst_account |
| 64 | + |
| 65 | + def _validate_account_type(self, platform_type, account_type, role): |
| 66 | + if platform_type not in ("gitlab", "github", "gitee", "gitea"): |
| 67 | + raise ValueError( |
| 68 | + f"Unsupported platform_type '{platform_type}' for {role}." |
| 69 | + ) |
| 70 | + # gitea ---> user or org |
| 71 | + if platform_type == "gitea": |
| 72 | + if account_type not in ("user", "org"): |
| 73 | + raise ValueError( |
| 74 | + f"For {platform_type}, {role} account_type must be " |
| 75 | + "either 'user' or 'org'." |
| 76 | + ) |
| 77 | + # gitlab ---> user or group |
| 78 | + elif platform_type == "gitlab": |
| 79 | + if account_type not in ("user", "group"): |
| 80 | + raise ValueError( |
| 81 | + f"For {platform_type}, {role} account_type must be " |
| 82 | + "either 'user' or 'group'." |
| 83 | + ) |
| 84 | + # github/gitee ---> user or org |
| 85 | + elif platform_type in ("github", "gitee"): |
| 86 | + if account_type not in ("user", "org"): |
| 87 | + raise ValueError( |
| 88 | + f"For {platform_type}, {role} account_type must be" |
| 89 | + "either 'user' or 'org'." |
| 90 | + ) |
| 91 | + |
| 92 | + def has_dst_repo(self, repo_name): |
| 93 | + # gitlab ---> projects, github/gitee ---> repos |
| 94 | + repo_field = "projects" if self.dst_type == "gitlab" else "repos" |
| 95 | + url = '/'.join( |
| 96 | + [ |
| 97 | + self.dst_base, self.dst_account_type+'s', self.dst_account, |
| 98 | + repo_field, |
| 99 | + ] |
| 100 | + ) |
| 101 | + repo_names = self._get_all_repo_names(url) |
| 102 | + if not repo_names: |
| 103 | + print("Warning: destination repos is []") |
| 104 | + return False |
| 105 | + return repo_name in repo_names |
| 106 | + |
| 107 | + def create_dst_repo(self, repo_name): |
| 108 | + result = None |
| 109 | + # gitlab ---> projects, github/gitee ---> repos |
| 110 | + repo_field = "projects" if self.dst_type == "gitlab" else "repos" |
| 111 | + if self.dst_type == "gitlab": |
| 112 | + url = f"{self.dst_base}/{repo_field}" |
| 113 | + headers = {'PRIVATE-TOKEN': self.dst_token} |
| 114 | + data = {'name': repo_name, 'visibility': 'public'} |
| 115 | + # If creating under a group, add namespace_id |
| 116 | + if self.dst_account_type == "group": |
| 117 | + group_id = self._get_gitlab_group_id(self.dst_account) |
| 118 | + data['namespace_id'] = group_id |
| 119 | + else: |
| 120 | + suffix = f"user/{repo_field}" |
| 121 | + if self.dst_account_type == "org": |
| 122 | + suffix = f"orgs/{self.dst_account}/{repo_field}" |
| 123 | + url = '/'.join( |
| 124 | + [self.dst_base, suffix] |
| 125 | + ) |
| 126 | + if self.dst_type == 'gitee': |
| 127 | + data = {'name': repo_name} |
| 128 | + elif self.dst_type == 'github': |
| 129 | + data = json.dumps({'name': repo_name}) |
| 130 | + if not self.has_dst_repo(repo_name): |
| 131 | + print(repo_name + " doesn't exist, create it...") |
| 132 | + if self.dst_type == "gitea": |
| 133 | + response = self.session.get( |
| 134 | + f"{self.dst_base}/repos/{self.dst_account}/{repo_name}", |
| 135 | + headers={'Authorization': f'token {self.dst_token}'}, |
| 136 | + timeout=self.api_timeout |
| 137 | + ) |
| 138 | + print(f"DEBUG self.create_dst_repo if gitea: {self.dst_base}/repos/{self.dst_account}/{repo_name}") |
| 139 | + return response.status_code == 200 |
| 140 | + elif self.dst_type == "github": |
| 141 | + response = self.session.post( |
| 142 | + url, |
| 143 | + data=data, |
| 144 | + headers={'Authorization': 'token ' + self.dst_token}, |
| 145 | + timeout=self.api_timeout |
| 146 | + ) |
| 147 | + result = response.status_code == 201 |
| 148 | + if result: |
| 149 | + print("Destination repo creating accepted.") |
| 150 | + else: |
| 151 | + print("Destination repo creating failed: " + response.text) |
| 152 | + elif self.dst_type == "gitee": |
| 153 | + response = requests.post( |
| 154 | + url, |
| 155 | + headers={'Content-Type': 'application/json;charset=UTF-8'}, |
| 156 | + params={"name": repo_name, "access_token": self.dst_token}, |
| 157 | + timeout=self.api_timeout |
| 158 | + ) |
| 159 | + result = response.status_code == 201 |
| 160 | + if result: |
| 161 | + print("Destination repo creating accepted.") |
| 162 | + else: |
| 163 | + print("Destination repo creating failed: " + response.text) |
| 164 | + elif self.dst_type == "gitlab": |
| 165 | + response = self.session.post( |
| 166 | + url, |
| 167 | + data=data, |
| 168 | + headers=headers, |
| 169 | + timeout=self.api_timeout |
| 170 | + ) |
| 171 | + result = response.status_code == 201 |
| 172 | + if result: |
| 173 | + print("Destination repo creating accepted.") |
| 174 | + else: |
| 175 | + print("Destination repo creating failed: " + response.text) |
| 176 | + else: |
| 177 | + print(repo_name + " repo exist, skip creating...") |
| 178 | + # TODO(snowyu): Cleanup 2s sleep |
| 179 | + if result: |
| 180 | + time.sleep(2) |
| 181 | + return result |
| 182 | + |
| 183 | + def dynamic_list(self): |
| 184 | + # gitlab ---> projects, github/gitee ---> repos |
| 185 | + repo_field = "projects" if self.src_type == "gitlab" else "repos" |
| 186 | + url = '/'.join( |
| 187 | + [ |
| 188 | + self.src_base, self.src_account_type + 's', self.src_account, |
| 189 | + repo_field, |
| 190 | + ] |
| 191 | + ) |
| 192 | + return self._get_all_repo_names(url) |
| 193 | + |
| 194 | + @functools.lru_cache |
| 195 | + def _get_all_repo_names(self, url, page=1): |
| 196 | + per_page = 60 |
| 197 | + api = url + f"?page={page}&per_page=" + str(per_page) |
| 198 | + # TODO: src_token support |
| 199 | + response = self.session.get(api, timeout=self.api_timeout) |
| 200 | + all_items = [] |
| 201 | + if response.status_code != 200: |
| 202 | + print("Repo getting failed: " + response.text) |
| 203 | + return all_items |
| 204 | + items = response.json() |
| 205 | + if items: |
| 206 | + names = [i['name'] for i in items] |
| 207 | + return names + self._get_all_repo_names(url, page=page+1) |
| 208 | + return all_items |
| 209 | + |
| 210 | + def _get_gitlab_group_id(self, group_name): |
| 211 | + """Helper method to get GitLab group ID""" |
| 212 | + url = f"{self.dst_base}/groups" |
| 213 | + headers = {'PRIVATE-TOKEN': self.dst_token} |
| 214 | + response = self.session.get( |
| 215 | + url, |
| 216 | + headers=headers, |
| 217 | + timeout=self.api_timeout |
| 218 | + ) |
| 219 | + if response.status_code == 200: |
| 220 | + groups = response.json() |
| 221 | + for group in groups: |
| 222 | + if group['path'] == group_name: |
| 223 | + return group['id'] |
| 224 | + print(f"Failed to find group ID for '{group_name}'.") |
| 225 | + else: |
| 226 | + print("Failed to get groups list.") |
| 227 | + print(f"Error message: {response.text}") |
| 228 | + return None |
0 commit comments