lrossett / centos / centpkg

Forked from centos/centpkg 4 years ago
Clone
85a850
'''
85a850
    Command line behavior for centpkg
85a850
'''
85a850
#
85a850
# Author(s):
85a850
#            Jesse Keating <jkeating@redhat.com>
85a850
#            Pat Riehecky <riehecky@fnal.gov>
85a850
#            Brian Stinson <bstinson@ksu.edu>
85a850
#
85a850
# This program is free software; you can redistribute it and/or modify it
85a850
# under the terms of the GNU General Public License as published by the
85a850
# Free Software Foundation; either version 2 of the License, or (at your
85a850
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
85a850
# the full text of the license.
85a850
a394b1
from __future__ import print_function
80f38d
80f38d
import argparse
80f38d
import textwrap
80f38d
80f38d
from centpkg.utils import config_get_safely, do_add_remote, do_fork
Brian Stinson aa8548
from pyrpkg.cli import cliClient
794ab0
from pyrpkg import rpkgError
80f38d
from six.moves.urllib_parse import urlparse
Brian Stinson aa8548
a394b1
794ab0
_DEFAULT_API_BASE_URL = 'https://gitlab.com'
794ab0
794ab0
Brian Stinson aa8548
class centpkgClient(cliClient):
794ab0
    def __init__(self, config, name='centpkg'):
794ab0
        self.DEFAULT_CLI_NAME = name
794ab0
Brian Stinson aa8548
        super(centpkgClient, self).__init__(config, name)
Brian Stinson 3edf28
Brian Stinson 3edf28
        self.setup_centos_subparsers()
Brian Stinson 3edf28
f5ba26
    def load_cmd(self):
f5ba26
        super(centpkgClient, self).load_cmd()
f5ba26
        cfg = self.config[self.get_name()]
f5ba26
        self._cmd.update(cfg)
f5ba26
Brian Stinson 3edf28
    def setup_centos_subparsers(self):
80f38d
        self.register_do_fork()
615d8f
        self.register_request_gated_side_tag()
Brian Stinson 3edf28
80f38d
    def register_do_fork(self):
80f38d
        help_msg = 'Create a new fork of the current repository'
80f38d
        distgit_section = '{0}.distgit'.format(self.name)
794ab0
        # uses default dist-git url in case section is not present
794ab0
        try:
794ab0
            distgit_api_base_url = config_get_safely(self.config, distgit_section, "apibaseurl")
794ab0
        except rpkgError:
794ab0
            distgit_api_base_url = _DEFAULT_API_BASE_URL
80f38d
        description = textwrap.dedent('''
80f38d
            Create a new fork of the current repository
80f38d
80f38d
            Before the operation, you need to generate an API token at
5b7b46
            https://{1}/-/profile/personal_access_tokens, select the "api"
5b7b46
            scope and save it in your local user configuration located
80f38d
            at ~/.config/rpkg/{0}.conf. For example:
80f38d
80f38d
                [{0}.distgit]
80f38d
                token = <api_key_here>
80f38d
80f38d
            Below is a basic example of the command to fork a current repository:
80f38d
80f38d
                {0} fork
80f38d
        '''.format(self.name, urlparse(distgit_api_base_url).netloc))
80f38d
80f38d
        fork_parser = self.subparsers.add_parser(
80f38d
            'fork',
80f38d
            formatter_class=argparse.RawDescriptionHelpFormatter,
80f38d
            help=help_msg,
80f38d
            description=description)
80f38d
        fork_parser.set_defaults(command=self.do_distgit_fork)
80f38d
80f38d
    def do_distgit_fork(self):
80f38d
        """create fork of the distgit repository
80f38d
        That includes creating fork itself using API call and then adding
80f38d
        remote tracked repository
80f38d
        """
80f38d
        distgit_section = '{0}.distgit'.format(self.name)
80f38d
        distgit_api_base_url = config_get_safely(self.config, distgit_section, "apibaseurl")
80f38d
        distgit_token = config_get_safely(self.config, distgit_section, 'token')
80f38d
5b7b46
        ret, repo_path = do_fork(
80f38d
            logger=self.log,
80f38d
            base_url=distgit_api_base_url,
80f38d
            token=distgit_token,
80f38d
            repo_name=self.cmd.repo_name,
80f38d
            namespace=self.cmd.ns,
80f38d
            cli_name=self.name,
80f38d
        )
80f38d
80f38d
        # assemble url of the repo in web browser
5b7b46
        fork_url = '{0}/{1}'.format(
80f38d
            distgit_api_base_url.rstrip('/'),
5b7b46
            repo_path,
80f38d
        )
80f38d
80f38d
        if ret:
80f38d
            msg = "Fork of the repository has been created: '{0}'"
80f38d
        else:
80f38d
            msg = "Repo '{0}' already exists."
80f38d
        self.log.info(msg.format(fork_url))
80f38d
5b7b46
        distgit_remote_base_url = self.config.get(
5b7b46
            '{0}'.format(self.name),
5b7b46
            "gitbaseurl",
5b7b46
            vars={'repo': '{0}/{1}'.format(self.cmd.ns, self.cmd.repo_name)},
5b7b46
        )
5b7b46
        remote_name = repo_path.split('/')[0]
5b7b46
80f38d
        ret = do_add_remote(
80f38d
            base_url=distgit_api_base_url,
80f38d
            remote_base_url=distgit_remote_base_url,
80f38d
            repo=self.cmd.repo,
5b7b46
            repo_path=repo_path,
5b7b46
            remote_name=remote_name,
80f38d
        )
80f38d
        if ret:
80f38d
            msg = "Adding as remote '{0}'."
80f38d
        else:
80f38d
            msg = "Remote with name '{0}' already exists."
5b7b46
        self.log.info(msg.format(remote_name))
615d8f
615d8f
    def register_request_gated_side_tag(self):
615d8f
        """Register command line parser for subcommand request-gated-side-tag"""
615d8f
        parser = self.subparsers.add_parser(
615d8f
            "request-gated-side-tag",
615d8f
            help="Create a new dynamic gated side tag",
615d8f
        )
615d8f
        parser.add_argument("--base-tag", help="name of base tag")
615d8f
        parser.set_defaults(command=self.request_gated_side_tag)
615d8f
615d8f
    def request_gated_side_tag(self):
615d8f
        self.args.suffix = "stack-gate"
615d8f
        super(centpkgClient, self).request_side_tag()