|
7 | 7 | from ...exceptions import ArgumentError, ResourceError, ResourceExistsError, ResourceNotFoundError
|
8 | 8 |
|
9 | 9 | import gitlab
|
10 |
| -from gitlab.exceptions import GitlabCreateError, GitlabGetError |
| 10 | +from gitlab.exceptions import GitlabListError, GitlabCreateError, GitlabGetError |
11 | 11 |
|
| 12 | +from git.exc import GitCommandError |
| 13 | + |
| 14 | +import os |
12 | 15 | import json, time
|
13 | 16 |
|
14 | 17 | @register_target('lab', 'gitlab')
|
@@ -131,14 +134,198 @@ def get_repository(self, user, repo):
|
131 | 134 | return self.gl.projects.get('{}/{}'.format(user, repo))
|
132 | 135 | except GitlabGetError as err:
|
133 | 136 | if err.response_code == 404:
|
134 |
| - raise ResourceNotFoundError("Cannot delete: repository {}/{} does not exists.".format(user, repo)) from err |
| 137 | + raise ResourceNotFoundError("Cannot get: repository {}/{} does not exists.".format(user, repo)) from err |
135 | 138 |
|
136 | 139 | @classmethod
|
137 | 140 | def get_auth_token(cls, login, password, prompt=None):
|
138 | 141 | gl = gitlab.Gitlab(url='https://{}'.format(cls.fqdn), email=login, password=password)
|
139 | 142 | gl.auth()
|
140 | 143 | return gl.user.private_token
|
141 | 144 |
|
| 145 | + def _deconstruct_snippet_uri(self, uri): |
| 146 | + path = uri.split('https://{}/'.format(self.fqdn))[-1].split('/') |
| 147 | + if 4 == len(path): |
| 148 | + user, project_name, _, snippet_id = path |
| 149 | + elif 2 == len(path): |
| 150 | + _, snippet_id = path |
| 151 | + project_name = None |
| 152 | + user = None |
| 153 | + elif 1 == len(path): |
| 154 | + snippet_id = path[0] |
| 155 | + project_name = None |
| 156 | + user = None |
| 157 | + else: |
| 158 | + raise ResourceNotFoundError('URL is not of a snippet') |
| 159 | + return (user, project_name, snippet_id) |
| 160 | + |
| 161 | + def gist_list(self, project=None): |
| 162 | + if not project: |
| 163 | + try: |
| 164 | + for snippet in self.gl.snippets.list(): |
| 165 | + yield (snippet.web_url, snippet.title) |
| 166 | + except GitlabListError as err: |
| 167 | + if err.response_code == 404: |
| 168 | + raise ResourceNotFoundError('Feature not available, please upgrade your gitlab instance.') from err |
| 169 | + raise ResourceError('Cannot list snippet') from err |
| 170 | + else: |
| 171 | + if '/' not in project: |
| 172 | + project = '/'.join([self.username, project]) |
| 173 | + try: |
| 174 | + project = self.gl.projects.get(project) |
| 175 | + for snippet in project.snippets.list(): |
| 176 | + yield (snippet.web_url, 0, snippet.title) |
| 177 | + except GitlabGetError as err: |
| 178 | + raise ResourceNotFoundError('Could not retrieve project "{}".'.format(project)) from err |
| 179 | + |
| 180 | + def gist_fetch(self, snippet, fname=None): |
| 181 | + if fname: |
| 182 | + raise ArgumentError('Snippets contain only single file in gitlab.') |
| 183 | + try: |
| 184 | + *_, snippet_id = self._deconstruct_snippet_uri(snippet) |
| 185 | + snippet = self.gl.snippets.get(id=snippet_id) |
| 186 | + except GitlabGetError as err: |
| 187 | + if err.response_code == 404: |
| 188 | + if "The page you're looking for could not be found." in err.response_body.decode('utf-8'): |
| 189 | + raise ResourceNotFoundError('Feature not available, please upgrade your gitlab instance.') from err |
| 190 | + raise ResourceNotFoundError('Cannot fetch snippet') from err |
| 191 | + raise ResourceError('Cannot fetch snippet') from err |
| 192 | + except Exception as err: |
| 193 | + raise ResourceNotFoundError('Could not find snippet') from err |
| 194 | + |
| 195 | + return snippet.raw().decode('utf-8') |
| 196 | + |
| 197 | + def gist_clone(self, gist): |
| 198 | + raise ArgumentError('Snippets cannot be cloned in gitlab.') |
| 199 | + |
| 200 | + def gist_create(self, gist_pathes, description, secret=False): |
| 201 | + def load_file(fname, path='.'): |
| 202 | + with open(os.path.join(path, fname), 'r') as f: |
| 203 | + return f.read() |
| 204 | + |
| 205 | + if len(gist_pathes) > 2: |
| 206 | + raise ArgumentError('Snippets contain only single file in gitlab.') |
| 207 | + |
| 208 | + data = { |
| 209 | + 'title': description, |
| 210 | + 'visibility_level': 0 if secret else 20 |
| 211 | + } |
| 212 | + |
| 213 | + try: |
| 214 | + |
| 215 | + if len(gist_pathes) == 2: |
| 216 | + project = gist_pathes[0] |
| 217 | + if '/' in project: |
| 218 | + *namespace, project = project.split('/') |
| 219 | + namespace = '/'.join(namespace) |
| 220 | + else: |
| 221 | + namespace = self.username |
| 222 | + gist_path = gist_pathes[1] |
| 223 | + data.update({ |
| 224 | + 'project_id': '/'.join([namespace, project]), |
| 225 | + 'code': load_file(gist_path), |
| 226 | + 'file_name': os.path.basename(gist_path), |
| 227 | + } |
| 228 | + ) |
| 229 | + gist = self.gl.project_snippets.create(data) |
| 230 | + |
| 231 | + elif len(gist_pathes) == 1: |
| 232 | + gist_path = gist_pathes[0] |
| 233 | + data.update({ |
| 234 | + 'content': load_file(gist_path), |
| 235 | + 'file_name': os.path.basename(gist_path), |
| 236 | + } |
| 237 | + ) |
| 238 | + gist = self.gl.snippets.create(data) |
| 239 | + |
| 240 | + return gist.web_url |
| 241 | + except GitlabCreateError as err: |
| 242 | + if err.response_code == 422: |
| 243 | + raise ResourceNotFoundError('Feature not available, please upgrade your gitlab instance.') from err |
| 244 | + raise ResourceError('Cannot create snippet') from err |
| 245 | + |
| 246 | + def gist_delete(self, snippet): |
| 247 | + try: |
| 248 | + _, project, snippet_id = self._deconstruct_snippet_uri(snippet) |
| 249 | + if project: |
| 250 | + if '/' in project: |
| 251 | + *namespace, project = project.split('/') |
| 252 | + namespace = '/'.join(namespace) |
| 253 | + else: |
| 254 | + namespace = self.username |
| 255 | + snippet = self.gl.projects.get( |
| 256 | + '/'.join([namespace, project]) |
| 257 | + ).snippets.get(id=snippet_id) |
| 258 | + else: |
| 259 | + snippet = self.gl.snippets.get(id=snippet_id) |
| 260 | + except GitlabCreateError as err: |
| 261 | + if err.response_code == 422: |
| 262 | + raise ResourceNotFoundError('Cannot delete snippet, please upgrade your gitlab instance.') from err |
| 263 | + raise ResourceError('Cannot delete snippet') from err |
| 264 | + except Exception as err: |
| 265 | + raise ResourceNotFoundError('Could not find snippet') from err |
| 266 | + |
| 267 | + return snippet.delete() |
| 268 | + |
| 269 | + def request_create(self, user, repo, local_branch, remote_branch, title, description=None): |
| 270 | + try: |
| 271 | + repository = self.gl.projects.get('/'.join([user, repo])) |
| 272 | + if not repository: |
| 273 | + raise ResourceNotFoundError('Could not find repository `{}/{}`!'.format(user, repo)) |
| 274 | + if not local_branch: |
| 275 | + remote_branch = self.repository.active_branch.name or self.repository.active_branch.name |
| 276 | + if not remote_branch: |
| 277 | + local_branch = repository.master_branch or 'master' |
| 278 | + request = self.gl.project_mergerequests.create( |
| 279 | + project_id=repository.id, |
| 280 | + data= { |
| 281 | + 'source_branch':local_branch, |
| 282 | + 'target_branch':remote_branch, |
| 283 | + 'title':title, |
| 284 | + 'description':description |
| 285 | + } |
| 286 | + ) |
| 287 | + except GitlabGetError as err: |
| 288 | + raise ResourceNotFoundError(err) from err |
| 289 | + except Exception as err: |
| 290 | + raise ResourceError("Unhandled error: {}".format(err)) from err |
| 291 | + |
| 292 | + return {'local': local_branch, |
| 293 | + 'remote': remote_branch, |
| 294 | + 'ref': request.iid} |
| 295 | + |
| 296 | + def request_list(self, user, repo): |
| 297 | + project = self.gl.projects.get('/'.join([user, repo])) |
| 298 | + for mr in self.gl.project_mergerequests.list(project_id=project.id): |
| 299 | + yield ( str(mr.iid), |
| 300 | + mr.title, |
| 301 | + 'https://{}/{}/{}/merge_requests/{}'.format( |
| 302 | + self.fqdn, |
| 303 | + project.namespace.name, |
| 304 | + project.name, |
| 305 | + mr.iid |
| 306 | + ) |
| 307 | + ) |
| 308 | + |
| 309 | + def request_fetch(self, user, repo, request, pull=False): |
| 310 | + if pull: |
| 311 | + raise NotImplementedError('Pull operation on requests for merge are not yet supported') |
| 312 | + try: |
| 313 | + for remote in self.repository.remotes: |
| 314 | + if remote.name == self.name: |
| 315 | + local_branch_name = 'requests/gitlab/{}'.format(request) |
| 316 | + self.fetch( |
| 317 | + remote, |
| 318 | + 'merge_requests/{}/head'.format(request), |
| 319 | + local_branch_name |
| 320 | + ) |
| 321 | + return local_branch_name |
| 322 | + else: |
| 323 | + raise ResourceNotFoundError('Could not find remote {}'.format(self.name)) |
| 324 | + except GitCommandError as err: |
| 325 | + if 'Error when fetching: fatal: Couldn\'t find remote ref' in err.command[0]: |
| 326 | + raise ResourceNotFoundError('Could not find opened request #{}'.format(request)) from err |
| 327 | + raise err |
| 328 | + |
142 | 329 | @property
|
143 | 330 | def user(self):
|
144 | 331 | return self.gl.user.username
|
|
0 commit comments