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

  • Committer: Jelmer Vernooij
  • Date: 2020-05-24 00:39:50 UTC
  • mto: This revision was merged to the branch mainline in revision 7504.
  • Revision ID: jelmer@jelmer.uk-20200524003950-bbc545r76vc5yajg
Add github action.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
    return urlutils.basename(branch.user_url)
47
47
 
48
48
 
49
 
def _check_already_merged(branch, target):
50
 
    # TODO(jelmer): Check entire ancestry rather than just last revision?
51
 
    if branch.last_revision() == target.last_revision():
52
 
        raise errors.CommandError(gettext(
53
 
            'All local changes are already present in target.'))
54
 
 
55
 
 
56
49
class cmd_publish_derived(Command):
57
50
    __doc__ = """Publish a derived branch.
58
51
 
86
79
            submit_branch = local_branch.get_parent()
87
80
            note(gettext('Using parent branch %s') % submit_branch)
88
81
        submit_branch = _mod_branch.Branch.open(submit_branch)
89
 
        _check_already_merged(local_branch, submit_branch)
90
82
        if name is None:
91
83
            name = branch_name(local_branch)
92
84
        hoster = _mod_propose.get_hoster(submit_branch)
159
151
               help='Allow fallback to lossy push, if necessary.'),
160
152
        Option('allow-collaboration',
161
153
               help='Allow collaboration from target branch maintainer(s)'),
162
 
        Option('allow-empty',
163
 
               help='Do not prevent empty merge proposals.'),
164
154
        ]
165
155
    takes_args = ['submit_branch?']
166
156
 
169
159
    def run(self, submit_branch=None, directory='.', hoster=None,
170
160
            reviewers=None, name=None, no_allow_lossy=False, description=None,
171
161
            labels=None, prerequisite=None, commit_message=None, wip=False,
172
 
            allow_collaboration=False, allow_empty=False):
 
162
            allow_collaboration=False):
173
163
        tree, branch, relpath = (
174
164
            controldir.ControlDir.open_containing_tree_or_branch(directory))
175
165
        if submit_branch is None:
177
167
        if submit_branch is None:
178
168
            submit_branch = branch.get_parent()
179
169
        if submit_branch is None:
180
 
            raise errors.CommandError(
 
170
            raise errors.BzrCommandError(
181
171
                gettext("No target location specified or remembered"))
182
 
        target = _mod_branch.Branch.open(submit_branch)
183
 
        if not allow_empty:
184
 
            _check_already_merged(branch, target)
 
172
        else:
 
173
            target = _mod_branch.Branch.open(submit_branch)
185
174
        if hoster is None:
186
175
            hoster = _mod_propose.get_hoster(target)
187
176
        else:
237
226
        if submit_branch is None:
238
227
            submit_branch = branch.get_parent()
239
228
        if submit_branch is None:
240
 
            raise errors.CommandError(
 
229
            raise errors.BzrCommandError(
241
230
                gettext("No target location specified or remembered"))
242
231
        else:
243
232
            target = _mod_branch.Branch.open(submit_branch)
246
235
            self.outf.write(gettext('Merge proposal: %s\n') % mp.url)
247
236
 
248
237
 
 
238
class cmd_github_login(Command):
 
239
    __doc__ = """Log into GitHub.
 
240
 
 
241
    When communicating with GitHub, some commands need to authenticate to
 
242
    GitHub.
 
243
    """
 
244
 
 
245
    takes_args = ['username?']
 
246
 
 
247
    def run(self, username=None):
 
248
        from github import Github, GithubException
 
249
        from breezy.config import AuthenticationConfig
 
250
        authconfig = AuthenticationConfig()
 
251
        if username is None:
 
252
            username = authconfig.get_user(
 
253
                'https', 'github.com', prompt=u'GitHub username', ask=True)
 
254
        password = authconfig.get_password('https', 'github.com', username)
 
255
        client = Github(username, password)
 
256
        user = client.get_user()
 
257
        try:
 
258
            authorization = user.create_authorization(
 
259
                scopes=['user', 'repo', 'delete_repo'], note='Breezy',
 
260
                note_url='https://github.com/breezy-team/breezy')
 
261
        except GithubException as e:
 
262
            errs = e.data.get('errors', [])
 
263
            if errs:
 
264
                err_code = errs[0].get('code')
 
265
                if err_code == u'already_exists':
 
266
                    raise errors.BzrCommandError('token already exists')
 
267
            raise errors.BzrCommandError(e.data['message'])
 
268
        # TODO(jelmer): This should really use something in
 
269
        # AuthenticationConfig
 
270
        from .github import store_github_token
 
271
        store_github_token(scheme='https', host='github.com',
 
272
                           token=authorization.token)
 
273
 
 
274
 
 
275
class cmd_gitlab_login(Command):
 
276
    __doc__ = """Log into a GitLab instance.
 
277
 
 
278
    This command takes a GitLab instance URL (e.g. https://gitlab.com)
 
279
    as well as an optional private token. Private tokens can be created via the
 
280
    web UI.
 
281
 
 
282
    :Examples:
 
283
 
 
284
      Log into GNOME's GitLab (prompts for a token):
 
285
 
 
286
         brz gitlab-login https://gitlab.gnome.org/
 
287
 
 
288
      Log into Debian's salsa, using a token created earlier:
 
289
 
 
290
         brz gitlab-login https://salsa.debian.org if4Theis6Eich7aef0zo
 
291
    """
 
292
 
 
293
    takes_args = ['url', 'private_token?']
 
294
 
 
295
    takes_options = [
 
296
        Option('name', help='Name for GitLab site in configuration.',
 
297
               type=str),
 
298
        Option('no-check',
 
299
               "Don't check that the token is valid."),
 
300
        ]
 
301
 
 
302
    def run(self, url, private_token=None, name=None, no_check=False):
 
303
        from breezy import ui
 
304
        from .gitlabs import store_gitlab_token
 
305
        if name is None:
 
306
            try:
 
307
                name = urlutils.parse_url(url)[3].split('.')[-2]
 
308
            except (ValueError, IndexError):
 
309
                raise errors.BzrCommandError(
 
310
                    'please specify a site name with --name')
 
311
        if private_token is None:
 
312
            note("Please visit %s to obtain a private token.",
 
313
                 urlutils.join(url, "profile/personal_access_tokens"))
 
314
            private_token = ui.ui_factory.get_password(u'Private token')
 
315
        if not no_check:
 
316
            from breezy.transport import get_transport
 
317
            from .gitlabs import GitLab
 
318
            GitLab(get_transport(url), private_token=private_token)
 
319
        store_gitlab_token(name=name, url=url, private_token=private_token)
 
320
 
 
321
 
249
322
class cmd_my_merge_proposals(Command):
250
323
    __doc__ = """List all merge proposals owned by the logged-in user.
251
324
 
267
340
            closed='Closed merge proposals')]
268
341
 
269
342
    def run(self, status='open', verbose=False):
270
 
        for instance in _mod_propose.iter_hoster_instances():
271
 
            for mp in instance.iter_my_proposals(status=status):
272
 
                self.outf.write('%s\n' % mp.url)
273
 
                if verbose:
274
 
                    self.outf.write(
275
 
                        '(Merging %s into %s)\n' %
276
 
                        (mp.get_source_branch_url(),
277
 
                         mp.get_target_branch_url()))
278
 
                    description = mp.get_description()
279
 
                    if description:
280
 
                        self.outf.writelines(
281
 
                            ['\t%s\n' % l
282
 
                             for l in description.splitlines()])
283
 
                    self.outf.write('\n')
 
343
        for name, hoster_cls in _mod_propose.hosters.items():
 
344
            for instance in hoster_cls.iter_instances():
 
345
                for mp in instance.iter_my_proposals(status=status):
 
346
                    self.outf.write('%s\n' % mp.url)
 
347
                    if verbose:
 
348
                        self.outf.write(
 
349
                            '(Merging %s into %s)\n' %
 
350
                            (mp.get_source_branch_url(),
 
351
                             mp.get_target_branch_url()))
 
352
                        description = mp.get_description()
 
353
                        if description:
 
354
                            self.outf.writelines(
 
355
                                ['\t%s\n' % l
 
356
                                 for l in description.splitlines()])
 
357
                        self.outf.write('\n')
284
358
 
285
359
 
286
360
class cmd_land_merge_proposal(Command):
293
367
    def run(self, url, message=None):
294
368
        proposal = _mod_propose.get_proposal_by_url(url)
295
369
        proposal.merge(commit_message=message)
296
 
 
297
 
 
298
 
class cmd_hosters(Command):
299
 
    __doc__ = """List all known hosting sites and user details."""
300
 
 
301
 
    hidden = True
302
 
 
303
 
    def run(self):
304
 
        for instance in _mod_propose.iter_hoster_instances():
305
 
            current_user = instance.get_current_user()
306
 
            self.outf.write(
307
 
                gettext('%s (%s) - user: %s (%s)\n') % (
308
 
                    instance.name, instance.base_url,
309
 
                    current_user, instance.get_user_url(current_user)))