lrossett / centos / centpkg

Forked from centos/centpkg a year ago
Clone
Pat Riehecky 85a850
'''
Pat Riehecky 85a850
    Command line behavior for centpkg
Pat Riehecky 85a850
'''
Pat Riehecky 85a850
#
Pat Riehecky 85a850
# Author(s):
Pat Riehecky 85a850
#            Jesse Keating <jkeating@redhat.com>
Pat Riehecky 85a850
#            Pat Riehecky <riehecky@fnal.gov>
Pat Riehecky 85a850
#            Brian Stinson <bstinson@ksu.edu>
Pat Riehecky 85a850
#
Pat Riehecky 85a850
# This program is free software; you can redistribute it and/or modify it
Pat Riehecky 85a850
# under the terms of the GNU General Public License as published by the
Pat Riehecky 85a850
# Free Software Foundation; either version 2 of the License, or (at your
Pat Riehecky 85a850
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
Pat Riehecky 85a850
# the full text of the license.
Pat Riehecky 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
32835c
from pyrpkg import rpkgError
80f38d
from six.moves.urllib_parse import urlparse
Brian Stinson aa8548
a394b1
32835c
_DEFAULT_API_BASE_URL = 'https://gitlab.com'
32835c
32835c
Brian Stinson aa8548
class centpkgClient(cliClient):
32835c
    def __init__(self, config, name='centpkg'):
32835c
        self.DEFAULT_CLI_NAME = name
32835c
Brian Stinson aa8548
        super(centpkgClient, self).__init__(config, name)
Brian Stinson 3edf28
Brian Stinson 3edf28
        self.setup_centos_subparsers()
Brian Stinson 3edf28
Brian Stinson 3edf28
    def setup_centos_subparsers(self):
80f38d
        self.register_do_fork()
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)
32835c
        # uses default dist-git url in case section is not present
32835c
        try:
32835c
            distgit_api_base_url = config_get_safely(self.config, distgit_section, "apibaseurl")
32835c
        except rpkgError:
32835c
            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
80f38d
            https://{1}/-/profile/personal_access_tokens, select the relevant
80f38d
            scope(s) 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
80f38d
            Operation requires username (GITLAB_ID). by default, current logged
80f38d
            username is taken. It could be overridden by reusing an argument:
80f38d
80f38d
                {0} --user GITLAB_ID 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_remote_base_url = self.config.get(
80f38d
            '{0}'.format(self.name),
80f38d
            "gitbaseurl",
80f38d
            vars={'user': self.cmd.user, 'repo': self.cmd.repo_name},
80f38d
        )
80f38d
        distgit_token = config_get_safely(self.config, distgit_section, 'token')
80f38d
80f38d
        ret = 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
9a602e
        fork_url = '{0}/{1}/centos_{2}_{3}'.format(
80f38d
            distgit_api_base_url.rstrip('/'),
80f38d
            self.cmd.user,
9a602e
            self.cmd.ns,
80f38d
            self.cmd.repo_name,
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
80f38d
        ret = do_add_remote(
80f38d
            base_url=distgit_api_base_url,
80f38d
            remote_base_url=distgit_remote_base_url,
80f38d
            username=self.cmd.user,
80f38d
            repo=self.cmd.repo,
80f38d
            repo_name=self.cmd.repo_name,
80f38d
            namespace=self.cmd.ns,
80f38d
        )
80f38d
        if ret:
80f38d
            msg = "Adding as remote '{0}'."
80f38d
        else:
80f38d
            msg = "Remote with name '{0}' already exists."
80f38d
        self.log.info(msg.format(self.cmd.user))