/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/propose/gitlabs.py

  • Committer: Jelmer Vernooij
  • Date: 2019-07-12 01:01:45 UTC
  • mto: This revision was merged to the branch mainline in revision 7375.
  • Revision ID: jelmer@jelmer.uk-20190712010145-m7224qumb8w068zw
Fix importing from remote git repositories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2018 Breezy Developers
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Support for GitLab."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
import json
 
22
import os
 
23
 
 
24
from ... import (
 
25
    bedding,
 
26
    branch as _mod_branch,
 
27
    controldir,
 
28
    errors,
 
29
    urlutils,
 
30
    )
 
31
from ...git.urls import git_url_to_bzr_url
 
32
from ...sixish import PY3
 
33
from ...transport import get_transport
 
34
 
 
35
from .propose import (
 
36
    Hoster,
 
37
    MergeProposal,
 
38
    MergeProposalBuilder,
 
39
    MergeProposalExists,
 
40
    NoSuchProject,
 
41
    PrerequisiteBranchUnsupported,
 
42
    UnsupportedHoster,
 
43
    )
 
44
 
 
45
 
 
46
_DEFAULT_FILES = ['/etc/python-gitlab.cfg', '~/.python-gitlab.cfg']
 
47
 
 
48
 
 
49
def mp_status_to_status(status):
 
50
    return {
 
51
        'all': 'all',
 
52
        'open': 'opened',
 
53
        'merged': 'merged',
 
54
        'closed': 'closed'}[status]
 
55
 
 
56
 
 
57
class NotGitLabUrl(errors.BzrError):
 
58
 
 
59
    _fmt = "Not a GitLab URL: %(url)s"
 
60
 
 
61
    def __init__(self, url):
 
62
        errors.BzrError.__init__(self)
 
63
        self.url = url
 
64
 
 
65
 
 
66
class NotMergeRequestUrl(errors.BzrError):
 
67
 
 
68
    _fmt = "Not a merge proposal URL: %(url)s"
 
69
 
 
70
    def __init__(self, host, url):
 
71
        errors.BzrError.__init__(self)
 
72
        self.host = host
 
73
        self.url = url
 
74
 
 
75
 
 
76
class DifferentGitLabInstances(errors.BzrError):
 
77
 
 
78
    _fmt = ("Can't create merge proposals across GitLab instances: "
 
79
            "%(source_host)s and %(target_host)s")
 
80
 
 
81
    def __init__(self, source_host, target_host):
 
82
        self.source_host = source_host
 
83
        self.target_host = target_host
 
84
 
 
85
 
 
86
class GitLabLoginMissing(errors.BzrError):
 
87
 
 
88
    _fmt = ("Please log into GitLab")
 
89
 
 
90
 
 
91
class GitlabLoginError(errors.BzrError):
 
92
 
 
93
    _fmt = ("Error logging in: %(error)s")
 
94
 
 
95
    def __init__(self, error):
 
96
        self.error = error
 
97
 
 
98
 
 
99
def default_config_path():
 
100
    return os.path.join(bedding.config_dir(), 'gitlab.conf')
 
101
 
 
102
 
 
103
def store_gitlab_token(name, url, private_token):
 
104
    """Store a GitLab token in a configuration file."""
 
105
    import configparser
 
106
    config = configparser.ConfigParser()
 
107
    path = default_config_path()
 
108
    config.read([path])
 
109
    config.add_section(name)
 
110
    config[name]['url'] = url
 
111
    config[name]['private_token'] = private_token
 
112
    with open(path, 'w') as f:
 
113
        config.write(f)
 
114
 
 
115
 
 
116
def iter_tokens():
 
117
    import configparser
 
118
    config = configparser.ConfigParser()
 
119
    config.read(
 
120
        [os.path.expanduser(p) for p in _DEFAULT_FILES] +
 
121
        [default_config_path()])
 
122
    for name, section in config.items():
 
123
        yield name, section
 
124
 
 
125
 
 
126
def get_credentials_by_url(url):
 
127
    for name, credentials in iter_tokens():
 
128
        if 'url' not in credentials:
 
129
            continue
 
130
        if credentials['url'].rstrip('/') == url.rstrip('/'):
 
131
            return credentials
 
132
    else:
 
133
        return None
 
134
 
 
135
 
 
136
def parse_gitlab_url(url):
 
137
    (scheme, user, password, host, port, path) = urlutils.parse_url(
 
138
        url)
 
139
    if scheme not in ('git+ssh', 'https', 'http'):
 
140
        raise NotGitLabUrl(url)
 
141
    if not host:
 
142
        raise NotGitLabUrl(url)
 
143
    path = path.strip('/')
 
144
    if path.endswith('.git'):
 
145
        path = path[:-4]
 
146
    return host, path
 
147
 
 
148
 
 
149
def parse_gitlab_branch_url(branch):
 
150
    url = urlutils.split_segment_parameters(branch.user_url)[0]
 
151
    host, path = parse_gitlab_url(url)
 
152
    return host, path, branch.name
 
153
 
 
154
 
 
155
def parse_gitlab_merge_request_url(url):
 
156
    (scheme, user, password, host, port, path) = urlutils.parse_url(
 
157
        url)
 
158
    if scheme not in ('git+ssh', 'https', 'http'):
 
159
        raise NotGitLabUrl(url)
 
160
    if not host:
 
161
        raise NotGitLabUrl(url)
 
162
    path = path.strip('/')
 
163
    parts = path.split('/')
 
164
    if parts[-2] != 'merge_requests':
 
165
        raise NotMergeRequestUrl(host, url)
 
166
    return host, '/'.join(parts[:-2]), int(parts[-1])
 
167
 
 
168
 
 
169
class GitLabMergeProposal(MergeProposal):
 
170
 
 
171
    def __init__(self, gl, mr):
 
172
        self.gl = gl
 
173
        self._mr = mr
 
174
 
 
175
    @property
 
176
    def url(self):
 
177
        return self._mr['web_url']
 
178
 
 
179
    def get_description(self):
 
180
        return self._mr['description']
 
181
 
 
182
    def set_description(self, description):
 
183
        self._mr['description'] = description
 
184
        self.gl._update_merge_requests(self._mr)
 
185
 
 
186
    def get_commit_message(self):
 
187
        return None
 
188
 
 
189
    def _branch_url_from_project(self, project_id, branch_name):
 
190
        project = self.gl._get_project(project_id)
 
191
        return gitlab_url_to_bzr_url(project['http_url_to_repo'], branch_name)
 
192
 
 
193
    def get_source_branch_url(self):
 
194
        return self._branch_url_from_project(
 
195
            self._mr['source_project_id'], self._mr['source_branch'])
 
196
 
 
197
    def get_target_branch_url(self):
 
198
        return self._branch_url_from_project(
 
199
            self._mr['target_project_id'], self._mr['target_branch'])
 
200
 
 
201
    def is_merged(self):
 
202
        return (self._mr['state'] == 'merged')
 
203
 
 
204
    def close(self):
 
205
        self._mr['state_event'] = 'close'
 
206
        self.gl._update_merge_requests(self._mr)
 
207
 
 
208
    def merge(self, commit_message=None):
 
209
        # https://docs.gitlab.com/ee/api/merge_requests.html#accept-mr
 
210
        self._mr.merge(merge_commit_message=commit_message)
 
211
 
 
212
 
 
213
def gitlab_url_to_bzr_url(url, name):
 
214
    if not PY3:
 
215
        name = name.encode('utf-8')
 
216
    return urlutils.join_segment_parameters(
 
217
        git_url_to_bzr_url(url), {"branch": name})
 
218
 
 
219
 
 
220
class GitLab(Hoster):
 
221
    """GitLab hoster implementation."""
 
222
 
 
223
    supports_merge_proposal_labels = True
 
224
    supports_merge_proposal_commit_message = False
 
225
 
 
226
    def __repr__(self):
 
227
        return "<GitLab(%r)>" % self.base_url
 
228
 
 
229
    @property
 
230
    def base_url(self):
 
231
        return self.transport.base
 
232
 
 
233
    def _api_request(self, method, path):
 
234
        return self.transport.request(
 
235
            method, urlutils.join(self.base_url, 'api', 'v4', path),
 
236
            headers=self.headers)
 
237
 
 
238
    def __init__(self, transport, private_token):
 
239
        self.transport = transport
 
240
        self.headers = {"Private-Token": private_token}
 
241
        self.check()
 
242
 
 
243
    def _get_project(self, project_name):
 
244
        path = 'projects/%s' % urlutils.quote(str(project_name), '')
 
245
        response = self._api_request('GET', path)
 
246
        if response.status == 404:
 
247
            raise NoSuchProject(project_name)
 
248
        if response.status == 200:
 
249
            return json.loads(response.data)
 
250
        raise InvalidHttpResponse(path, response.text)
 
251
 
 
252
    def _fork_project(self, project_name):
 
253
        path = 'projects/%s/fork' % urlutils.quote(str(project_name), '')
 
254
        response = self._api_request('POST', path)
 
255
        if response != 201:
 
256
            raise InvalidHttpResponse(path, response.text)
 
257
        return json.loads(response.data)
 
258
 
 
259
    def _get_logged_in_username(self):
 
260
        return self._current_user['username']
 
261
 
 
262
    def _list_merge_requests(self, owner=None, project=None, state=None):
 
263
        if project is not None:
 
264
            path = 'projects/%s/merge_requests' % urlutils.quote(str(project_name), '')
 
265
        else:
 
266
            path = 'merge_requests'
 
267
        parameters = {}
 
268
        if state:
 
269
            parameters['state'] = state
 
270
        if owner:
 
271
            parameters['owner_id'] = urlutils.quote(owner, '')
 
272
        response = self._api_request(
 
273
            'GET', path + '?' +
 
274
            ';'.join(['%s=%s' % item for item in parameters.items()]))
 
275
        if response.status == 403:
 
276
            raise errors.PermissionDenied(response.text)
 
277
        if response.status == 200:
 
278
            return json.loads(response.data)
 
279
        raise InvalidHttpResponse(path, response.text)
 
280
 
 
281
    def _create_mergerequest(
 
282
            self, title, source_project_id, target_project_id,
 
283
            source_branch_name, target_branch_name, description,
 
284
            labels=None):
 
285
        path = 'projects/%s/merge_requests' % source_project_id
 
286
        response = self._api_request(
 
287
            'POST', path, fields={
 
288
                'title': title,
 
289
                'source_branch': source_branch_name,
 
290
                'target_branch': target_branch_name,
 
291
                'target_project_id': target_project_id,
 
292
                'description': description,
 
293
                'labels': labels})
 
294
        if response.status == 403:
 
295
            raise errors.PermissionDenied(response.text)
 
296
        if response.status == 409:
 
297
            raise MergeProposalExists(self.source_branch.user_url)
 
298
        if response.status == 200:
 
299
            raise InvalidHttpResponse(path, response.text)
 
300
        return json.loads(response.data)
 
301
 
 
302
    def get_push_url(self, branch):
 
303
        (host, project_name, branch_name) = parse_gitlab_branch_url(branch)
 
304
        project = self._get_project(project_name)
 
305
        return gitlab_url_to_bzr_url(
 
306
            project['ssh_url_to_repo'], branch_name)
 
307
 
 
308
    def publish_derived(self, local_branch, base_branch, name, project=None,
 
309
                        owner=None, revision_id=None, overwrite=False,
 
310
                        allow_lossy=True):
 
311
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
 
312
        if owner is None:
 
313
            owner = self._get_logged_in_username()
 
314
        if project is None:
 
315
            project = self._get_project(base_project)['path']
 
316
        try:
 
317
            target_project = self._get_project('%s/%s' % (owner, project))
 
318
        except NoSuchProject:
 
319
            target_project = self._fork_project(base_project)
 
320
            # TODO(jelmer): Spin and wait until import_status for new project
 
321
            # is complete.
 
322
        remote_repo_url = git_url_to_bzr_url(target_project['ssh_url_to_repo'])
 
323
        remote_dir = controldir.ControlDir.open(remote_repo_url)
 
324
        try:
 
325
            push_result = remote_dir.push_branch(
 
326
                local_branch, revision_id=revision_id, overwrite=overwrite,
 
327
                name=name)
 
328
        except errors.NoRoundtrippingSupport:
 
329
            if not allow_lossy:
 
330
                raise
 
331
            push_result = remote_dir.push_branch(
 
332
                local_branch, revision_id=revision_id, overwrite=overwrite,
 
333
                name=name, lossy=True)
 
334
        public_url = gitlab_url_to_bzr_url(
 
335
            target_project['http_url_to_repo'], name)
 
336
        return push_result.target_branch, public_url
 
337
 
 
338
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
 
339
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
 
340
        if owner is None:
 
341
            owner = self._get_logged_in_username()
 
342
        if project is None:
 
343
            project = self._get_project(base_project)['path']
 
344
        try:
 
345
            target_project = self._get_project('%s/%s' % (owner, project))
 
346
        except NoSuchProject:
 
347
            raise errors.NotBranchError('%s/%s/%s' % (self.base_url, owner, project))
 
348
        return _mod_branch.Branch.open(gitlab_url_to_bzr_url(
 
349
            target_project['ssh_url_to_repo'], name))
 
350
 
 
351
    def get_proposer(self, source_branch, target_branch):
 
352
        return GitlabMergeProposalBuilder(self, source_branch, target_branch)
 
353
 
 
354
    def iter_proposals(self, source_branch, target_branch, status):
 
355
        (source_host, source_project_name, source_branch_name) = (
 
356
            parse_gitlab_branch_url(source_branch))
 
357
        (target_host, target_project_name, target_branch_name) = (
 
358
            parse_gitlab_branch_url(target_branch))
 
359
        if source_host != target_host:
 
360
            raise DifferentGitLabInstances(source_host, target_host)
 
361
        source_project = self._get_project(source_project_name)
 
362
        target_project = self._get_project(target_project_name)
 
363
        state = mp_status_to_status(status)
 
364
        for mr in self._list_merge_requests(
 
365
                project=target_project['id'], state=state):
 
366
            if (mr['source_project_id'] != source_project['id'] or
 
367
                    mr['source_branch'] != source_branch_name or
 
368
                    mr['target_project_id'] != target_project['id'] or
 
369
                    mr['target_branch'] != target_branch_name):
 
370
                continue
 
371
            yield GitLabMergeProposal(self, mr)
 
372
 
 
373
    def hosts(self, branch):
 
374
        try:
 
375
            (host, project, branch_name) = parse_gitlab_branch_url(branch)
 
376
        except NotGitLabUrl:
 
377
            return False
 
378
        return (self.base_url == ('https://%s' % host))
 
379
 
 
380
    def check(self):
 
381
        response = self._api_request('GET', 'user')
 
382
        if response.status == 200:
 
383
            self._current_user = json.loads(response.data)
 
384
            return
 
385
        if response == 401:
 
386
            if json.loads(response.data) == {"message": "401 Unauthorized"}:
 
387
                raise GitLabLoginMissing()
 
388
            else:
 
389
                raise GitlabLoginError(response.text)
 
390
        raise UnsupportedHoster(url)
 
391
 
 
392
    @classmethod
 
393
    def probe_from_url(cls, url, possible_transports=None):
 
394
        try:
 
395
            (host, project) = parse_gitlab_url(url)
 
396
        except NotGitLabUrl:
 
397
            raise UnsupportedHoster(url)
 
398
        transport = get_transport(
 
399
            'https://%s' % host, possible_transports=possible_transports)
 
400
        credentials = get_credentials_by_url(transport.base)
 
401
        if credentials is not None:
 
402
            return cls(transport, credentials.get('private_token'))
 
403
        raise UnsupportedHoster(url)
 
404
 
 
405
    @classmethod
 
406
    def iter_instances(cls):
 
407
        for name, credentials in iter_tokens():
 
408
            if 'url' not in credentials:
 
409
                continue
 
410
            yield cls(
 
411
                get_transport(credentials['url']),
 
412
                private_token=credentials.get('private_token'))
 
413
 
 
414
    def iter_my_proposals(self, status='open'):
 
415
        state = mp_status_to_status(status)
 
416
        for mp in self._list_merge_requests(
 
417
                owner=self._get_logged_in_username(), state=state):
 
418
            yield GitLabMergeProposal(self, mp)
 
419
 
 
420
    def get_proposal_by_url(self, url):
 
421
        try:
 
422
            (host, project, merge_id) = parse_gitlab_merge_request_url(url)
 
423
        except NotGitLabUrl:
 
424
            raise UnsupportedHoster(url)
 
425
        except NotMergeRequestUrl as e:
 
426
            if self.base_url == ('https://%s' % e.host):
 
427
                raise
 
428
            else:
 
429
                raise UnsupportedHoster(url)
 
430
        if self.base_url != ('https://%s' % host):
 
431
            raise UnsupportedHoster(url)
 
432
        project = self._get_project(project)
 
433
        mr = project.mergerequests.get(merge_id)
 
434
        return GitLabMergeProposal(mr)
 
435
 
 
436
 
 
437
class GitlabMergeProposalBuilder(MergeProposalBuilder):
 
438
 
 
439
    def __init__(self, l, source_branch, target_branch):
 
440
        self.gl = gl
 
441
        self.source_branch = source_branch
 
442
        (self.source_host, self.source_project_name, self.source_branch_name) = (
 
443
            parse_gitlab_branch_url(source_branch))
 
444
        self.target_branch = target_branch
 
445
        (self.target_host, self.target_project_name, self.target_branch_name) = (
 
446
            parse_gitlab_branch_url(target_branch))
 
447
        if self.source_host != self.target_host:
 
448
            raise DifferentGitLabInstances(self.source_host, self.target_host)
 
449
 
 
450
    def get_infotext(self):
 
451
        """Determine the initial comment for the merge proposal."""
 
452
        info = []
 
453
        info.append("Gitlab instance: %s\n" % self.target_host)
 
454
        info.append("Source: %s\n" % self.source_branch.user_url)
 
455
        info.append("Target: %s\n" % self.target_branch.user_url)
 
456
        return ''.join(info)
 
457
 
 
458
    def get_initial_body(self):
 
459
        """Get a body for the proposal for the user to modify.
 
460
 
 
461
        :return: a str or None.
 
462
        """
 
463
        return None
 
464
 
 
465
    def create_proposal(self, description, reviewers=None, labels=None,
 
466
                        prerequisite_branch=None, commit_message=None):
 
467
        """Perform the submission."""
 
468
        # https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
 
469
        if prerequisite_branch is not None:
 
470
            raise PrerequisiteBranchUnsupported(self)
 
471
        # Note that commit_message is ignored, since Gitlab doesn't support it.
 
472
        # TODO(jelmer): Support reviewers
 
473
        source_project = self.gl._get_project(self.source_project_name)
 
474
        target_project = self.gl._get_project(self.target_project_name)
 
475
        # TODO(jelmer): Allow setting title explicitly
 
476
        title = description.splitlines()[0]
 
477
        # TODO(jelmer): Allow setting allow_collaboration field
 
478
        # TODO(jelmer): Allow setting milestone field
 
479
        # TODO(jelmer): Allow setting squash field
 
480
        kwargs = {
 
481
            'title': title,
 
482
            'source_project_id': source_project['id'],
 
483
            'target_project_id': target_project['id'],
 
484
            'source_branch': self.source_branch_name,
 
485
            'target_branch': self.target_branch_name,
 
486
            'description': description}
 
487
        if labels:
 
488
            kwargs['labels'] = ','.join(labels)
 
489
        merge_request = self.gl._create_mergerequest(**kwargs)
 
490
        return GitLabMergeProposal(self.gl, merge_request)
 
491
 
 
492
 
 
493
def register_gitlab_instance(shortname, url):
 
494
    """Register a gitlab instance.
 
495
 
 
496
    :param shortname: Short name (e.g. "gitlab")
 
497
    :param url: URL to the gitlab instance
 
498
    """
 
499
    from breezy.bugtracker import (
 
500
        tracker_registry,
 
501
        ProjectIntegerBugTracker,
 
502
        )
 
503
    tracker_registry.register(
 
504
        shortname, ProjectIntegerBugTracker(
 
505
            shortname, url + '/{project}/issues/{id}'))