/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-06-29 13:16:26 UTC
  • mto: This revision was merged to the branch mainline in revision 7376.
  • Revision ID: jelmer@jelmer.uk-20190629131626-qioafloyemhdbm4w
Remove Tree.get_root_id() in favour of Tree.path2id('').

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 parse_gitlab_url(url):
 
127
    (scheme, user, password, host, port, path) = urlutils.parse_url(
 
128
        url)
 
129
    if scheme not in ('git+ssh', 'https', 'http'):
 
130
        raise NotGitLabUrl(url)
 
131
    if not host:
 
132
        raise NotGitLabUrl(url)
 
133
    path = path.strip('/')
 
134
    if path.endswith('.git'):
 
135
        path = path[:-4]
 
136
    return host, path
 
137
 
 
138
 
 
139
def parse_gitlab_branch_url(branch):
 
140
    url = urlutils.split_segment_parameters(branch.user_url)[0]
 
141
    host, path = parse_gitlab_url(url)
 
142
    return host, path, branch.name
 
143
 
 
144
 
 
145
def parse_gitlab_merge_request_url(url):
 
146
    (scheme, user, password, host, port, path) = urlutils.parse_url(
 
147
        url)
 
148
    if scheme not in ('git+ssh', 'https', 'http'):
 
149
        raise NotGitLabUrl(url)
 
150
    if not host:
 
151
        raise NotGitLabUrl(url)
 
152
    path = path.strip('/')
 
153
    parts = path.split('/')
 
154
    if parts[-2] != 'merge_requests':
 
155
        raise NotMergeRequestUrl(host, url)
 
156
    return host, '/'.join(parts[:-2]), int(parts[-1])
 
157
 
 
158
 
 
159
class GitLabMergeProposal(MergeProposal):
 
160
 
 
161
    def __init__(self, gl, mr):
 
162
        self.gl = gl
 
163
        self._mr = mr
 
164
 
 
165
    @property
 
166
    def url(self):
 
167
        return self._mr['web_url']
 
168
 
 
169
    def get_description(self):
 
170
        return self._mr['description']
 
171
 
 
172
    def set_description(self, description):
 
173
        self._mr['description'] = description
 
174
        self.gl._update_merge_requests(self._mr)
 
175
 
 
176
    def get_commit_message(self):
 
177
        return None
 
178
 
 
179
    def _branch_url_from_project(self, project_id, branch_name):
 
180
        project = self.gl._get_project(project_id)
 
181
        return gitlab_url_to_bzr_url(project['http_url_to_repo'], branch_name)
 
182
 
 
183
    def get_source_branch_url(self):
 
184
        return self._branch_url_from_project(
 
185
            self._mr['source_project_id'], self._mr['source_branch'])
 
186
 
 
187
    def get_target_branch_url(self):
 
188
        return self._branch_url_from_project(
 
189
            self._mr['target_project_id'], self._mr['target_branch'])
 
190
 
 
191
    def is_merged(self):
 
192
        return (self._mr['state'] == 'merged')
 
193
 
 
194
    def close(self):
 
195
        self._mr['state_event'] = 'close'
 
196
        self.gl._update_merge_requests(self._mr)
 
197
 
 
198
    def merge(self, commit_message=None):
 
199
        # https://docs.gitlab.com/ee/api/merge_requests.html#accept-mr
 
200
        self._mr.merge(merge_commit_message=commit_message)
 
201
 
 
202
 
 
203
def gitlab_url_to_bzr_url(url, name):
 
204
    if not PY3:
 
205
        name = name.encode('utf-8')
 
206
    return urlutils.join_segment_parameters(
 
207
        git_url_to_bzr_url(url), {"branch": name})
 
208
 
 
209
 
 
210
class GitLab(Hoster):
 
211
    """GitLab hoster implementation."""
 
212
 
 
213
    supports_merge_proposal_labels = True
 
214
    supports_merge_proposal_commit_message = False
 
215
 
 
216
    def __repr__(self):
 
217
        return "<GitLab(%r)>" % self.base_url
 
218
 
 
219
    @property
 
220
    def base_url(self):
 
221
        return self.transport.base
 
222
 
 
223
    def _api_request(self, method, path):
 
224
        return self.transport.request(
 
225
            method, urlutils.join(self.base_url, 'api', 'v4', path),
 
226
            headers=self.headers)
 
227
 
 
228
    def __init__(self, transport, private_token):
 
229
        self.transport = transport
 
230
        self.headers = {"Private-Token": private_token}
 
231
        self.check()
 
232
 
 
233
    def _get_project(self, project_name):
 
234
        path = 'projects/:%s' % urlutils.quote(str(project_name), '')
 
235
        response = self._api_request('GET', path)
 
236
        if response.status == 404:
 
237
            raise NoSuchProject(project_name)
 
238
        if response.status == 200:
 
239
            return json.loads(response.data)
 
240
        raise InvalidHttpResponse(path, response.text)
 
241
 
 
242
    def _fork_project(self, project_name):
 
243
        path = 'projects/:%s/fork' % urlutils.quote(str(project_name), '')
 
244
        response = self._api_request('POST', path)
 
245
        if response != 200:
 
246
            raise InvalidHttpResponse(path, response.text)
 
247
        return json.loads(response.data)
 
248
 
 
249
    def _get_logged_in_username(self):
 
250
        return self._current_user['username']
 
251
 
 
252
    def _list_merge_requests(self, owner=None, project=None, state=None):
 
253
        if project is not None:
 
254
            path = 'projects/:%s/merge_requests' % urlutils.quote(str(project_name), '')
 
255
        else:
 
256
            path = 'merge_requests'
 
257
        parameters = {}
 
258
        if state:
 
259
            parameters['state'] = state
 
260
        if owner:
 
261
            parameters['owner_id'] = urlutils.quote(owner, '')
 
262
        response = self._api_request(
 
263
            'GET', path + '?' +
 
264
            ';'.join(['%s=%s' % item for item in parameters.items()]))
 
265
        if response.status == 403:
 
266
            raise errors.PermissionDenied(response.text)
 
267
        if response.status == 200:
 
268
            return json.loads(response.data)
 
269
        raise InvalidHttpResponse(path, response.text)
 
270
 
 
271
    def _create_mergerequest(
 
272
            self, title, source_project_id, target_project_id,
 
273
            source_branch_name, target_branch_name, description,
 
274
            labels=None):
 
275
        path = 'projects/:%s/merge_requests' % source_project_id
 
276
        response = self._api_request(
 
277
            'POST', path, fields={
 
278
                'title': title,
 
279
                'source_branch': source_branch_name,
 
280
                'target_branch': target_branch_name,
 
281
                'target_project_id': target_project_id,
 
282
                'description': description,
 
283
                'labels': labels})
 
284
        if response.status == 403:
 
285
            raise errors.PermissionDenied(response.text)
 
286
        if response.status == 409:
 
287
            raise MergeProposalExists(self.source_branch.user_url)
 
288
        if response.status == 200:
 
289
            raise InvalidHttpResponse(path, response.text)
 
290
        return json.loads(response.data)
 
291
 
 
292
    def get_push_url(self, branch):
 
293
        (host, project_name, branch_name) = parse_gitlab_branch_url(branch)
 
294
        project = self._get_project(project_name)
 
295
        return gitlab_url_to_bzr_url(
 
296
            project['ssh_url_to_repo'], branch_name)
 
297
 
 
298
    def publish_derived(self, local_branch, base_branch, name, project=None,
 
299
                        owner=None, revision_id=None, overwrite=False,
 
300
                        allow_lossy=True):
 
301
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
 
302
        if owner is None:
 
303
            owner = self._get_logged_in_username()
 
304
        if project is None:
 
305
            project = self._get_project(base_project)['path']
 
306
        try:
 
307
            target_project = self._get_project('%s/%s' % (owner, project))
 
308
        except NoSuchProject:
 
309
            target_project = self._fork_project(base_project)
 
310
            # TODO(jelmer): Spin and wait until import_status for new project
 
311
            # is complete.
 
312
        remote_repo_url = git_url_to_bzr_url(target_project['ssh_url_to_repo'])
 
313
        remote_dir = controldir.ControlDir.open(remote_repo_url)
 
314
        try:
 
315
            push_result = remote_dir.push_branch(
 
316
                local_branch, revision_id=revision_id, overwrite=overwrite,
 
317
                name=name)
 
318
        except errors.NoRoundtrippingSupport:
 
319
            if not allow_lossy:
 
320
                raise
 
321
            push_result = remote_dir.push_branch(
 
322
                local_branch, revision_id=revision_id, overwrite=overwrite,
 
323
                name=name, lossy=True)
 
324
        public_url = gitlab_url_to_bzr_url(
 
325
            target_project['http_url_to_repo'], name)
 
326
        return push_result.target_branch, public_url
 
327
 
 
328
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
 
329
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
 
330
        if owner is None:
 
331
            owner = self._get_logged_in_username()
 
332
        if project is None:
 
333
            project = self._get_project(base_project)['path']
 
334
        try:
 
335
            target_project = self._get_project('%s/%s' % (owner, project))
 
336
        except NoSuchProject:
 
337
            raise errors.NotBranchError('%s/%s/%s' % (self.base_url, owner, project))
 
338
        return _mod_branch.Branch.open(gitlab_url_to_bzr_url(
 
339
            target_project['ssh_url_to_repo'], name))
 
340
 
 
341
    def get_proposer(self, source_branch, target_branch):
 
342
        return GitlabMergeProposalBuilder(self, source_branch, target_branch)
 
343
 
 
344
    def iter_proposals(self, source_branch, target_branch, status):
 
345
        (source_host, source_project_name, source_branch_name) = (
 
346
            parse_gitlab_branch_url(source_branch))
 
347
        (target_host, target_project_name, target_branch_name) = (
 
348
            parse_gitlab_branch_url(target_branch))
 
349
        if source_host != target_host:
 
350
            raise DifferentGitLabInstances(source_host, target_host)
 
351
        source_project = self._get_project(source_project_name)
 
352
        target_project = self._get_project(target_project_name)
 
353
        state = mp_status_to_status(status)
 
354
        for mr in self.gl._list_merge_requests(
 
355
                project=target_project['id'], state=state):
 
356
            if (mr['source_project_id'] != source_project['id'] or
 
357
                    mr['source_branch'] != source_branch_name or
 
358
                    mr['target_project_id'] != target_project['id'] or
 
359
                    mr['target_branch'] != target_branch_name):
 
360
                continue
 
361
            yield GitLabMergeProposal(self, mr)
 
362
 
 
363
    def hosts(self, branch):
 
364
        try:
 
365
            (host, project, branch_name) = parse_gitlab_branch_url(branch)
 
366
        except NotGitLabUrl:
 
367
            return False
 
368
        return (self.base_url == ('https://%s' % host))
 
369
 
 
370
    def check(self):
 
371
        response = self._api_request('GET', 'user')
 
372
        if response.status == 200:
 
373
            self._current_user = json.loads(response.data)
 
374
            return
 
375
        if response == 401:
 
376
            if json.loads(response.data) == {"message": "401 Unauthorized"}:
 
377
                raise GitLabLoginMissing()
 
378
            else:
 
379
                raise GitlabLoginError(response.text)
 
380
        raise UnsupportedHoster(url)
 
381
 
 
382
    @classmethod
 
383
    def probe_from_url(cls, url, possible_transports=None):
 
384
        try:
 
385
            (host, project) = parse_gitlab_url(url)
 
386
        except NotGitLabUrl:
 
387
            raise UnsupportedHoster(url)
 
388
        transport = get_transport(
 
389
            'https://%s' % host, possible_transports=possible_transports)
 
390
        return cls(transport)
 
391
 
 
392
    @classmethod
 
393
    def iter_instances(cls):
 
394
        for name, credentials in iter_tokens():
 
395
            if 'url' not in credentials:
 
396
                continue
 
397
            yield cls(
 
398
                get_transport(credentials['url']),
 
399
                private_token=credentials.get('private_token'))
 
400
 
 
401
    def iter_my_proposals(self, status='open'):
 
402
        state = mp_status_to_status(status)
 
403
        for mp in self._list_merge_requests(
 
404
                owner=self._get_logged_in_username(), state=state):
 
405
            yield GitLabMergeProposal(self, mp)
 
406
 
 
407
    def get_proposal_by_url(self, url):
 
408
        try:
 
409
            (host, project, merge_id) = parse_gitlab_merge_request_url(url)
 
410
        except NotGitLabUrl:
 
411
            raise UnsupportedHoster(url)
 
412
        except NotMergeRequestUrl as e:
 
413
            if self.gl.url == ('https://%s' % e.host):
 
414
                raise
 
415
            else:
 
416
                raise UnsupportedHoster(url)
 
417
        if self.gl.url != ('https://%s' % host):
 
418
            raise UnsupportedHoster(url)
 
419
        project = self.gl.projects.get(project)
 
420
        mr = project.mergerequests.get(merge_id)
 
421
        return GitLabMergeProposal(mr)
 
422
 
 
423
 
 
424
class GitlabMergeProposalBuilder(MergeProposalBuilder):
 
425
 
 
426
    def __init__(self, l, source_branch, target_branch):
 
427
        self.gl = gl
 
428
        self.source_branch = source_branch
 
429
        (self.source_host, self.source_project_name, self.source_branch_name) = (
 
430
            parse_gitlab_branch_url(source_branch))
 
431
        self.target_branch = target_branch
 
432
        (self.target_host, self.target_project_name, self.target_branch_name) = (
 
433
            parse_gitlab_branch_url(target_branch))
 
434
        if self.source_host != self.target_host:
 
435
            raise DifferentGitLabInstances(self.source_host, self.target_host)
 
436
 
 
437
    def get_infotext(self):
 
438
        """Determine the initial comment for the merge proposal."""
 
439
        info = []
 
440
        info.append("Gitlab instance: %s\n" % self.target_host)
 
441
        info.append("Source: %s\n" % self.source_branch.user_url)
 
442
        info.append("Target: %s\n" % self.target_branch.user_url)
 
443
        return ''.join(info)
 
444
 
 
445
    def get_initial_body(self):
 
446
        """Get a body for the proposal for the user to modify.
 
447
 
 
448
        :return: a str or None.
 
449
        """
 
450
        return None
 
451
 
 
452
    def create_proposal(self, description, reviewers=None, labels=None,
 
453
                        prerequisite_branch=None, commit_message=None):
 
454
        """Perform the submission."""
 
455
        # https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
 
456
        if prerequisite_branch is not None:
 
457
            raise PrerequisiteBranchUnsupported(self)
 
458
        # Note that commit_message is ignored, since Gitlab doesn't support it.
 
459
        # TODO(jelmer): Support reviewers
 
460
        source_project = self.gl._get_project(self.source_project_name)
 
461
        target_project = self.gl._get_project(self.target_project_name)
 
462
        # TODO(jelmer): Allow setting title explicitly
 
463
        title = description.splitlines()[0]
 
464
        # TODO(jelmer): Allow setting allow_collaboration field
 
465
        # TODO(jelmer): Allow setting milestone field
 
466
        # TODO(jelmer): Allow setting squash field
 
467
        kwargs = {
 
468
            'title': title,
 
469
            'source_project_id': source_project['id'],
 
470
            'target_project_id': target_project['id'],
 
471
            'source_branch': self.source_branch_name,
 
472
            'target_branch': self.target_branch_name,
 
473
            'description': description}
 
474
        if labels:
 
475
            kwargs['labels'] = ','.join(labels)
 
476
        merge_request = self.gl._create_mergerequest(**kwargs)
 
477
        return GitLabMergeProposal(self.gl, merge_request)
 
478
 
 
479
 
 
480
def register_gitlab_instance(shortname, url):
 
481
    """Register a gitlab instance.
 
482
 
 
483
    :param shortname: Short name (e.g. "gitlab")
 
484
    :param url: URL to the gitlab instance
 
485
    """
 
486
    from breezy.bugtracker import (
 
487
        tracker_registry,
 
488
        ProjectIntegerBugTracker,
 
489
        )
 
490
    tracker_registry.register(
 
491
        shortname, ProjectIntegerBugTracker(
 
492
            shortname, url + '/{project}/issues/{id}'))