/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/github.py

  • Committer: Jelmer Vernooij
  • Date: 2020-01-11 17:41:33 UTC
  • mto: This revision was merged to the branch mainline in revision 7440.
  • Revision ID: jelmer@jelmer.uk-20200111174133-ob2p0twwsmvw5ut7
Don't lazy-import errors.

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 GitHub."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
import json
 
22
import os
 
23
 
 
24
from .propose import (
 
25
    Hoster,
 
26
    HosterLoginRequired,
 
27
    MergeProposal,
 
28
    MergeProposalBuilder,
 
29
    MergeProposalExists,
 
30
    NoSuchProject,
 
31
    PrerequisiteBranchUnsupported,
 
32
    ReopenFailed,
 
33
    UnsupportedHoster,
 
34
    )
 
35
 
 
36
from ... import (
 
37
    bedding,
 
38
    branch as _mod_branch,
 
39
    controldir,
 
40
    errors,
 
41
    hooks,
 
42
    urlutils,
 
43
    version_string as breezy_version,
 
44
    )
 
45
from ...config import AuthenticationConfig, GlobalStack
 
46
from ...errors import InvalidHttpResponse, PermissionDenied
 
47
from ...git.urls import git_url_to_bzr_url
 
48
from ...i18n import gettext
 
49
from ...sixish import PY3
 
50
from ...trace import note
 
51
from ...transport import get_transport
 
52
from ...transport.http import default_user_agent
 
53
 
 
54
 
 
55
GITHUB_HOST = 'github.com'
 
56
WEB_GITHUB_URL = 'https://github.com'
 
57
API_GITHUB_URL = 'https://api.github.com'
 
58
DEFAULT_PER_PAGE = 50
 
59
 
 
60
 
 
61
def store_github_token(scheme, host, token):
 
62
    with open(os.path.join(bedding.config_dir(), 'github.conf'), 'w') as f:
 
63
        f.write(token)
 
64
 
 
65
 
 
66
def retrieve_github_token(scheme, host):
 
67
    path = os.path.join(bedding.config_dir(), 'github.conf')
 
68
    if not os.path.exists(path):
 
69
        return None
 
70
    with open(path, 'r') as f:
 
71
        return f.read().strip()
 
72
 
 
73
 
 
74
def determine_title(description):
 
75
    return description.splitlines()[0]
 
76
 
 
77
 
 
78
class ValidationFailed(errors.BzrError):
 
79
 
 
80
    _fmt = "GitHub validation failed: %(error)s"
 
81
 
 
82
    def __init__(self, error):
 
83
        errors.BzrError.__init__(self)
 
84
        self.error = error
 
85
 
 
86
 
 
87
class NotGitHubUrl(errors.BzrError):
 
88
 
 
89
    _fmt = "Not a GitHub URL: %(url)s"
 
90
 
 
91
    def __init__(self, url):
 
92
        errors.BzrError.__init__(self)
 
93
        self.url = url
 
94
 
 
95
 
 
96
class GitHubLoginRequired(HosterLoginRequired):
 
97
 
 
98
    _fmt = "Action requires GitHub login."
 
99
 
 
100
 
 
101
def connect_github():
 
102
    """Connect to GitHub.
 
103
    """
 
104
    user_agent = default_user_agent()
 
105
    auth = AuthenticationConfig()
 
106
 
 
107
    credentials = auth.get_credentials('https', GITHUB_HOST)
 
108
    if credentials is not None:
 
109
        return Github(credentials['user'], credentials['password'],
 
110
                      user_agent=user_agent)
 
111
 
 
112
    # TODO(jelmer): token = auth.get_token('https', GITHUB_HOST)
 
113
    if token is not None:
 
114
        return Github(token, user_agent=user_agent)
 
115
    else:
 
116
        note('Accessing GitHub anonymously. To log in, run \'brz gh-login\'.')
 
117
        return Github(user_agent=user_agent)
 
118
 
 
119
 
 
120
class GitHubMergeProposal(MergeProposal):
 
121
 
 
122
    def __init__(self, gh, pr):
 
123
        self._gh = gh
 
124
        self._pr = pr
 
125
 
 
126
    def __repr__(self):
 
127
        return "<%s at %r>" % (type(self).__name__, self.url)
 
128
 
 
129
    @property
 
130
    def url(self):
 
131
        return self._pr['html_url']
 
132
 
 
133
    def _branch_from_part(self, part):
 
134
        if part['repo'] is None:
 
135
            return None
 
136
        return github_url_to_bzr_url(part['repo']['html_url'], part['ref'])
 
137
 
 
138
    def get_source_branch_url(self):
 
139
        return self._branch_from_part(self._pr['head'])
 
140
 
 
141
    def get_target_branch_url(self):
 
142
        return self._branch_from_part(self._pr['base'])
 
143
 
 
144
    def get_source_project(self):
 
145
        return self._pr['head']['repo']['full_name']
 
146
 
 
147
    def get_target_project(self):
 
148
        return self._pr['base']['repo']['full_name']
 
149
 
 
150
    def get_description(self):
 
151
        return self._pr['body']
 
152
 
 
153
    def get_commit_message(self):
 
154
        return None
 
155
 
 
156
    def set_commit_message(self, message):
 
157
        self._patch({'title': message})
 
158
 
 
159
    def _patch(self, data):
 
160
        response = self._gh._api_request(
 
161
            'PATCH', self._pr['url'], body=json.dumps(data).encode('utf-8'))
 
162
        if response.status == 422:
 
163
            raise ValidationFailed(json.loads(response.text))
 
164
        if response.status != 200:
 
165
            raise InvalidHttpResponse(self._pr['url'], response.text)
 
166
        self._pr = json.loads(response.text)
 
167
 
 
168
    def set_description(self, description):
 
169
        self._patch({
 
170
            'body': description,
 
171
            'title': determine_title(description),
 
172
            })
 
173
 
 
174
    def is_merged(self):
 
175
        return bool(self._pr.get('merged_at'))
 
176
 
 
177
    def is_closed(self):
 
178
        return self._pr['state'] == 'closed' and not bool(self._pr.get('merged_at'))
 
179
 
 
180
    def reopen(self):
 
181
        try:
 
182
            self._patch({'state': 'open'})
 
183
        except ValidationFailed as e:
 
184
            raise ReopenFailed(e.error['errors'][0]['message'])
 
185
 
 
186
    def close(self):
 
187
        self._patch({'state': 'closed'})
 
188
 
 
189
    def can_be_merged(self):
 
190
        return self._pr['mergeable']
 
191
 
 
192
    def merge(self, commit_message=None):
 
193
        # https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button
 
194
        self._pr.merge(commit_message=commit_message)
 
195
 
 
196
    def get_merged_by(self):
 
197
        merged_by = self._pr.get('merged_by')
 
198
        if merged_by is None:
 
199
            return None
 
200
        return merged_by['login']
 
201
 
 
202
    def get_merged_at(self):
 
203
        merged_at = self._pr.get('merged_at')
 
204
        if merged_at is None:
 
205
            return None
 
206
        import iso8601
 
207
        return iso8601.parse_date(merged_at)
 
208
 
 
209
 
 
210
def parse_github_url(url):
 
211
    (scheme, user, password, host, port, path) = urlutils.parse_url(
 
212
        url)
 
213
    if host != GITHUB_HOST:
 
214
        raise NotGitHubUrl(url)
 
215
    (owner, repo_name) = path.strip('/').split('/')
 
216
    if repo_name.endswith('.git'):
 
217
        repo_name = repo_name[:-4]
 
218
    return owner, repo_name
 
219
 
 
220
 
 
221
def parse_github_branch_url(branch):
 
222
    url = urlutils.split_segment_parameters(branch.user_url)[0]
 
223
    owner, repo_name = parse_github_url(url)
 
224
    return owner, repo_name, branch.name
 
225
 
 
226
 
 
227
def github_url_to_bzr_url(url, branch_name):
 
228
    if not PY3:
 
229
        branch_name = branch_name.encode('utf-8')
 
230
    return git_url_to_bzr_url(url, branch_name)
 
231
 
 
232
 
 
233
def strip_optional(url):
 
234
    return url.split('{')[0]
 
235
 
 
236
 
 
237
class GitHub(Hoster):
 
238
 
 
239
    name = 'github'
 
240
 
 
241
    supports_merge_proposal_labels = True
 
242
    supports_merge_proposal_commit_message = False
 
243
 
 
244
    def __repr__(self):
 
245
        return "GitHub()"
 
246
 
 
247
    def _api_request(self, method, path, body=None):
 
248
        headers = {
 
249
            'Content-Type': 'application/json',
 
250
            'Accept': 'application/vnd.github.v3+json'}
 
251
        if self._token:
 
252
            headers['Authorization'] = 'token %s' % self._token
 
253
        response = self.transport.request(
 
254
            method, urlutils.join(self.transport.base, path),
 
255
            headers=headers, body=body, retries=3)
 
256
        if response.status == 401:
 
257
            raise GitHubLoginRequired(self)
 
258
        return response
 
259
 
 
260
    def _get_repo(self, owner, repo):
 
261
        path = 'repos/%s/%s' % (owner, repo)
 
262
        response = self._api_request('GET', path)
 
263
        if response.status == 404:
 
264
            raise NoSuchProject(path)
 
265
        if response.status == 200:
 
266
            return json.loads(response.text)
 
267
        raise InvalidHttpResponse(path, response.text)
 
268
 
 
269
    def _get_repo_pulls(self, path, head=None, state=None):
 
270
        path = path + '?'
 
271
        params = {}
 
272
        if head is not None:
 
273
            params['head'] = head
 
274
        if state is not None:
 
275
            params['state'] = state
 
276
        path += ';'.join(['%s=%s' % (k, urlutils.quote(v))
 
277
                         for k, v in params.items()])
 
278
        response = self._api_request('GET', path)
 
279
        if response.status == 404:
 
280
            raise NoSuchProject(path)
 
281
        if response.status == 200:
 
282
            return json.loads(response.text)
 
283
        raise InvalidHttpResponse(path, response.text)
 
284
 
 
285
    def _create_pull(self, path, title, head, base, body=None, labels=None, assignee=None):
 
286
        data = {
 
287
            'title': title,
 
288
            'head': head,
 
289
            'base': base,
 
290
        }
 
291
        if labels is not None:
 
292
            data['labels'] = labels
 
293
        if assignee is not None:
 
294
            data['assignee'] = assignee
 
295
        if body:
 
296
            data['body'] = body
 
297
 
 
298
        response = self._api_request(
 
299
            'POST', path, body=json.dumps(data).encode('utf-8'))
 
300
        if response.status == 403:
 
301
            raise PermissionDenied(path, response.text)
 
302
        if response.status != 201:
 
303
            raise InvalidHttpResponse(path, 'req is invalid %d %r: %r' % (response.status, data, response.text))
 
304
        return json.loads(response.text)
 
305
 
 
306
    def _get_user_by_email(self, email):
 
307
        path = 'search/users?q=%s+in:email' % email
 
308
        response = self._api_request('GET', path)
 
309
        if response.status != 200:
 
310
            raise InvalidHttpResponse(path, response.text)
 
311
        ret = json.loads(response.text)
 
312
        if ret['total_count'] == 0:
 
313
            raise KeyError('no user with email %s' % email)
 
314
        elif ret['total_count'] > 1:
 
315
            raise ValueError('more than one result for email %s' % email)
 
316
        return ret['items'][0]
 
317
 
 
318
    def _get_user(self, username=None):
 
319
        if username:
 
320
            path = 'users/%s' % username
 
321
        else:
 
322
            path = 'user'
 
323
        response = self._api_request('GET', path)
 
324
        if response.status != 200:
 
325
            raise InvalidHttpResponse(path, response.text)
 
326
        return json.loads(response.text)
 
327
 
 
328
    def _get_organization(self, name):
 
329
        path = 'orgs/%s' % name
 
330
        response = self._api_request('GET', path)
 
331
        if response.status != 200:
 
332
            raise InvalidHttpResponse(path, response.text)
 
333
        return json.loads(response.text)
 
334
 
 
335
    def _list_paged(self, path, parameters=None, per_page=None):
 
336
        if parameters is None:
 
337
            parameters = {}
 
338
        else:
 
339
            parameters = dict(parameters.items())
 
340
        if per_page:
 
341
            parameters['per_page'] = str(per_page)
 
342
        page = 1
 
343
        i = 0
 
344
        while path:
 
345
            parameters['page'] = str(page)
 
346
            response = self._api_request(
 
347
                'GET', path + '?' +
 
348
                ';'.join(['%s=%s' % (k, urlutils.quote(v))
 
349
                          for (k, v) in parameters.items()]))
 
350
            if response.status != 200:
 
351
                raise InvalidHttpResponse(path, response.text)
 
352
            data = json.loads(response.text)
 
353
            for entry in data['items']:
 
354
                i += 1
 
355
                yield entry
 
356
            if i >= data['total_count']:
 
357
                break
 
358
            page += 1
 
359
 
 
360
    def _search_issues(self, query):
 
361
        path = 'search/issues'
 
362
        return self._list_paged(path, {'q': query}, per_page=DEFAULT_PER_PAGE)
 
363
 
 
364
    def _create_fork(self, path, owner=None):
 
365
        if owner and owner != self._current_user['login']:
 
366
            path += '?organization=%s' % owner
 
367
        response = self._api_request('POST', path)
 
368
        if response.status != 202:
 
369
            raise InvalidHttpResponse(path, 'status: %d, %r' % (response.status, response.text))
 
370
        return json.loads(response.text)
 
371
 
 
372
    @property
 
373
    def base_url(self):
 
374
        return WEB_GITHUB_URL
 
375
 
 
376
    def __init__(self, transport):
 
377
        self._token = retrieve_github_token('https', GITHUB_HOST)
 
378
        self.transport = transport
 
379
        self._current_user = self._get_user()
 
380
 
 
381
    def publish_derived(self, local_branch, base_branch, name, project=None,
 
382
                        owner=None, revision_id=None, overwrite=False,
 
383
                        allow_lossy=True):
 
384
        base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
 
385
        base_repo = self._get_repo(base_owner, base_project)
 
386
        if owner is None:
 
387
            owner = self._current_user['login']
 
388
        if project is None:
 
389
            project = base_repo['name']
 
390
        try:
 
391
            remote_repo = self._get_repo(owner, project)
 
392
        except NoSuchProject:
 
393
            base_repo = self._get_repo(base_owner, base_project)
 
394
            remote_repo = self._create_fork(base_repo['forks_url'], owner)
 
395
            note(gettext('Forking new repository %s from %s') %
 
396
                 (remote_repo['html_url'], base_repo['html_url']))
 
397
        else:
 
398
            note(gettext('Reusing existing repository %s') % remote_repo['html_url'])
 
399
        remote_dir = controldir.ControlDir.open(git_url_to_bzr_url(remote_repo['ssh_url']))
 
400
        try:
 
401
            push_result = remote_dir.push_branch(
 
402
                local_branch, revision_id=revision_id, overwrite=overwrite,
 
403
                name=name)
 
404
        except errors.NoRoundtrippingSupport:
 
405
            if not allow_lossy:
 
406
                raise
 
407
            push_result = remote_dir.push_branch(
 
408
                local_branch, revision_id=revision_id,
 
409
                overwrite=overwrite, name=name, lossy=True)
 
410
        return push_result.target_branch, github_url_to_bzr_url(
 
411
            remote_repo['html_url'], name)
 
412
 
 
413
    def get_push_url(self, branch):
 
414
        owner, project, branch_name = parse_github_branch_url(branch)
 
415
        repo = self._get_repo(owner, project)
 
416
        return github_url_to_bzr_url(repo['ssh_url'], branch_name)
 
417
 
 
418
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
 
419
        base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
 
420
        base_repo = self._get_repo(base_owner, base_project)
 
421
        if owner is None:
 
422
            owner = self._current_user['login']
 
423
        if project is None:
 
424
            project = base_repo['name']
 
425
        try:
 
426
            remote_repo = self._get_repo(owner, project)
 
427
            full_url = github_url_to_bzr_url(remote_repo['ssh_url'], name)
 
428
            return _mod_branch.Branch.open(full_url)
 
429
        except NoSuchProject:
 
430
            raise errors.NotBranchError('%s/%s/%s' % (WEB_GITHUB_URL, owner, project))
 
431
 
 
432
    def get_proposer(self, source_branch, target_branch):
 
433
        return GitHubMergeProposalBuilder(self, source_branch, target_branch)
 
434
 
 
435
    def iter_proposals(self, source_branch, target_branch, status='open'):
 
436
        (source_owner, source_repo_name, source_branch_name) = (
 
437
            parse_github_branch_url(source_branch))
 
438
        (target_owner, target_repo_name, target_branch_name) = (
 
439
            parse_github_branch_url(target_branch))
 
440
        target_repo = self._get_repo(target_owner, target_repo_name)
 
441
        state = {
 
442
            'open': 'open',
 
443
            'merged': 'closed',
 
444
            'closed': 'closed',
 
445
            'all': 'all'}
 
446
        pulls = self._get_repo_pulls(
 
447
            strip_optional(target_repo['pulls_url']),
 
448
            head=target_branch_name,
 
449
            state=state[status])
 
450
        for pull in pulls:
 
451
            if (status == 'closed' and pull['merged'] or
 
452
                    status == 'merged' and not pull['merged']):
 
453
                continue
 
454
            if pull['head']['ref'] != source_branch_name:
 
455
                continue
 
456
            if pull['head']['repo'] is None:
 
457
                # Repo has gone the way of the dodo
 
458
                continue
 
459
            if (pull['head']['repo']['owner']['login'] != source_owner or
 
460
                    pull['head']['repo']['name'] != source_repo_name):
 
461
                continue
 
462
            yield GitHubMergeProposal(self, pull)
 
463
 
 
464
    def hosts(self, branch):
 
465
        try:
 
466
            parse_github_branch_url(branch)
 
467
        except NotGitHubUrl:
 
468
            return False
 
469
        else:
 
470
            return True
 
471
 
 
472
    @classmethod
 
473
    def probe_from_url(cls, url, possible_transports=None):
 
474
        try:
 
475
            parse_github_url(url)
 
476
        except NotGitHubUrl:
 
477
            raise UnsupportedHoster(url)
 
478
        transport = get_transport(
 
479
            API_GITHUB_URL, possible_transports=possible_transports)
 
480
        return cls(transport)
 
481
 
 
482
    @classmethod
 
483
    def iter_instances(cls):
 
484
        yield cls(get_transport(API_GITHUB_URL))
 
485
 
 
486
    def iter_my_proposals(self, status='open'):
 
487
        query = ['is:pr']
 
488
        if status == 'open':
 
489
            query.append('is:open')
 
490
        elif status == 'closed':
 
491
            query.append('is:unmerged')
 
492
            # Also use "is:closed" otherwise unmerged open pull requests are
 
493
            # also included.
 
494
            query.append('is:closed')
 
495
        elif status == 'merged':
 
496
            query.append('is:merged')
 
497
        query.append('author:%s' % self._current_user['login'])
 
498
        for issue in self._search_issues(query=' '.join(query)):
 
499
            url = issue['pull_request']['url']
 
500
            response = self._api_request('GET', url)
 
501
            if response.status != 200:
 
502
                raise InvalidHttpResponse(url, response.text)
 
503
            yield GitHubMergeProposal(self, json.loads(response.text))
 
504
 
 
505
    def get_proposal_by_url(self, url):
 
506
        raise UnsupportedHoster(url)
 
507
 
 
508
    def iter_my_forks(self):
 
509
        response = self._api_request('GET', '/user/repos')
 
510
        if response.status != 200:
 
511
            raise InvalidHttpResponse(url, response.text)
 
512
        for project in json.loads(response.text):
 
513
            if not project['fork']:
 
514
                continue
 
515
            yield project['full_name']
 
516
 
 
517
    def delete_project(self, path):
 
518
        path = 'repos/' + path
 
519
        response = self._api_request('DELETE', path)
 
520
        if response.status == 404:
 
521
            raise NoSuchProject(path)
 
522
        if response.status == 204:
 
523
            return
 
524
        if response.status == 200:
 
525
            return json.loads(response.text)
 
526
        raise InvalidHttpResponse(path, response.text)
 
527
 
 
528
 
 
529
class GitHubMergeProposalBuilder(MergeProposalBuilder):
 
530
 
 
531
    def __init__(self, gh, source_branch, target_branch):
 
532
        self.gh = gh
 
533
        self.source_branch = source_branch
 
534
        self.target_branch = target_branch
 
535
        (self.target_owner, self.target_repo_name, self.target_branch_name) = (
 
536
            parse_github_branch_url(self.target_branch))
 
537
        (self.source_owner, self.source_repo_name, self.source_branch_name) = (
 
538
            parse_github_branch_url(self.source_branch))
 
539
 
 
540
    def get_infotext(self):
 
541
        """Determine the initial comment for the merge proposal."""
 
542
        info = []
 
543
        info.append("Merge %s into %s:%s\n" % (
 
544
            self.source_branch_name, self.target_owner,
 
545
            self.target_branch_name))
 
546
        info.append("Source: %s\n" % self.source_branch.user_url)
 
547
        info.append("Target: %s\n" % self.target_branch.user_url)
 
548
        return ''.join(info)
 
549
 
 
550
    def get_initial_body(self):
 
551
        """Get a body for the proposal for the user to modify.
 
552
 
 
553
        :return: a str or None.
 
554
        """
 
555
        return None
 
556
 
 
557
    def create_proposal(self, description, reviewers=None, labels=None,
 
558
                        prerequisite_branch=None, commit_message=None):
 
559
        """Perform the submission."""
 
560
        if prerequisite_branch is not None:
 
561
            raise PrerequisiteBranchUnsupported(self)
 
562
        # Note that commit_message is ignored, since github doesn't support it.
 
563
        # TODO(jelmer): Probe for right repo name
 
564
        if self.target_repo_name.endswith('.git'):
 
565
            self.target_repo_name = self.target_repo_name[:-4]
 
566
        # TODO(jelmer): Allow setting title explicitly?
 
567
        title = determine_title(description)
 
568
        # TODO(jelmer): Set maintainers_can_modify?
 
569
        target_repo = self.gh._get_repo(
 
570
            self.target_owner, self.target_repo_name)
 
571
        assignees = []
 
572
        if reviewers:
 
573
            assignees = []
 
574
            for reviewer in reviewers:
 
575
                if '@' in reviewer:
 
576
                    user = self.gh._get_user_by_email(reviewer)
 
577
                else:
 
578
                    user = self.gh._get_user(reviewer)
 
579
                assignees.append(user['login'])
 
580
        else:
 
581
            assignees = None
 
582
        try:
 
583
            pull_request = self.gh._create_pull(
 
584
                strip_optional(target_repo['pulls_url']),
 
585
                title=title, body=description,
 
586
                head="%s:%s" % (self.source_owner, self.source_branch_name),
 
587
                base=self.target_branch_name,
 
588
                labels=labels, assignee=assignees)
 
589
        except ValidationFailed:
 
590
            raise MergeProposalExists(self.source_branch.user_url)
 
591
        return GitHubMergeProposal(self.gh, pull_request)