From a4f286652aeed582eb3f95225931185cc492062f Mon Sep 17 00:00:00 2001 From: Lenka Segura Date: May 03 2019 09:16:04 +0000 Subject: Add repo_from option to create_pr function Add soft link to libpagure to requirements, to be able to edit libpagure and have the changes available. --- diff --git a/cranc/cranc.py b/cranc/cranc.py index 5dd5353..9fa5437 100644 --- a/cranc/cranc.py +++ b/cranc/cranc.py @@ -21,6 +21,7 @@ import os import click import pprint +from cranc import utils from libpagure import libpagure from libpagure import exceptions @@ -33,6 +34,7 @@ _log = logging.getLogger(__name__) def cranc(): pass + # Creating subgroup 'get' @click.group() def get(): @@ -57,6 +59,7 @@ def pr_list(status, assignee, author): _log.exception("Failed to connect to the server") pprint.pprint(prs) + # Adding pr_list into the group 'get' get.add_command(pr_list) @@ -70,20 +73,33 @@ get.add_command(pr_list) @click.option("--priority") @click.option("--no_stones") @click.option("--since") -#@click.option("--order") -def issue_list(status, tags, assignee, author, milestones, priority, no_stones, - since): +# @click.option("--order") +def issue_list(status, tags, assignee, author, milestones, priority, no_stones, since): """Prints list of issues""" PAGURE = libpagure.Pagure(pagure_token=api_token) try: - issues = PAGURE.list_issues(status=status, tags=tags, assignee=assignee, - author=author, milestones=milestones, priority=priority, - no_stones=no_stones, since=since) + issues = PAGURE.list_issues( + status=status, + tags=tags, + assignee=assignee, + author=author, + milestones=milestones, + priority=priority, + no_stones=no_stones, + since=since, + ) except exceptions.APIError: pagure_noauth = libpagure.Pagure(pagure_repository=project) - issues = pagure_noauth.list_issues(status=status, tags=tags, assignee=assignee, - author=author, milestones=milestones, priority=priority, - no_stones=no_stones, since=since) + issues = pagure_noauth.list_issues( + status=status, + tags=tags, + assignee=assignee, + author=author, + milestones=milestones, + priority=priority, + no_stones=no_stones, + since=since, + ) pprint.pprint(issues) @@ -99,10 +115,11 @@ def merge(): @click.command(name="pr") @click.option("--request_id") -@click.option("--repo") def merge_pr(request_id): """This command merges a pull request""" - PAGURE = libpagure.Pagure(pagure_token=api_token, pagure_repository=repo) + PAGURE = libpagure.Pagure( + pagure_token=api_token, pagure_repository=project, instance_url=instance_url + ) try: request = PAGURE.merge_request(request_id=request_id) pprint.pprint(request) @@ -110,6 +127,7 @@ def merge_pr(request_id): except Exception: _log.exception("Failed to connect to the server") + # Adding merge_pr into the group 'merge' merge.add_command(merge_pr) @@ -120,22 +138,32 @@ def create(): @click.command(name="pr") -@click.option("--repo") +@click.option("--repo_to") @click.option("--title") @click.option("--branch_to") @click.option("--branch_from") -def create_pr(repo, title, branch_to, branch_from): +@click.option("--repo_from") +def create_pr(repo_to, title, branch_to, branch_from, repo_from): """this command creates a new pull request""" - PAGURE = libpagure.Pagure(pagure_token=api_token, pagure_repository=repo, - fork_username='lenkaseg') + repo_to = utils.get_dict_from_str(repo_to) + PAGURE = libpagure.Pagure( + pagure_token=api_token, + repo_to=repo_to["repo"], + fork_username=repo_to["username"], + namespace=repo_to["namespace"], + repo_from=utils.get_dict_from_str(repo_from), + instance_url=instance_url, + ) PAGURE.log_debug(True) try: - request = PAGURE.create_pull_request(title=title, branch_to=branch_to, - branch_from=branch_from) + request = PAGURE.create_pull_request( + title=title, branch_to=branch_to, branch_from=branch_from + ) pprint.pprint(request) except Exception: _log.exception("Failed to connect to the server") + # Adding create_pr into the group 'create' create.add_command(create_pr) diff --git a/cranc/utils.py b/cranc/utils.py new file mode 100644 index 0000000..70ab5d3 --- /dev/null +++ b/cranc/utils.py @@ -0,0 +1,28 @@ +# Some utilities + +def get_dict_from_str(string): + """ + Converts repo_from string into dictionary. + Expected strings: + 'repo' + 'namespace/repo' + 'fork/username/repo' + 'fork/username/namespace/repo' + """ + + if string: + words = string.split('/') + if len(words) == 4: + fork, username, namespace, repo = words + return {"username": username, "namespace": namespace, "repo": repo} + elif len(words) == 3: + fork, username, repo = words + return {"username": username, "namespace": None, "repo": repo} + elif len(words) == 2: + namespace, repo = words + return {"username": None, "namespace": namespace, "repo": repo} + elif len(words) == 1: + repo, = words + return {"username": None, "namespace": None, "repo": repo} + else: + raise Exception("Wrong formatted repo path") diff --git a/requirements.txt b/requirements.txt index b316fec..2075856 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,7 @@ ############################################## click -libpagure +-e git+https://pagure.io/libpagure.git#egg=libpagure requests ##############################################