From 907b4ff6b5e357d81ba94e6c4a5b7efa753ed77e Mon Sep 17 00:00:00 2001 From: Dan Kolbman Date: Wed, 1 Jul 2020 14:40:10 -0400 Subject: [PATCH] :sparkles: Set default repo from git --- d3b_release_maker/cli.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/d3b_release_maker/cli.py b/d3b_release_maker/cli.py index b79add5..1a34def 100644 --- a/d3b_release_maker/cli.py +++ b/d3b_release_maker/cli.py @@ -1,10 +1,29 @@ #!/usr/bin/env python import click +import re +import subprocess -from d3b_release_maker.release_maker import ( - make_release, - new_notes, -) +from d3b_release_maker.release_maker import make_release, new_notes + + +def get_repository(): + """ + Try to retrieve the github repository by extracting it from the current git + repository's 'origin' url. + """ + try: + result = subprocess.check_output( + ["git", "remote", "get-url", "origin"], stderr=subprocess.DEVNULL + ) + except subprocess.CalledProcessError: + # If the git command fails, bail early + return None + + result = result.decode().strip() + match = re.match(r".*:([\w\d0-9-]+\/[\w\d-]+)", result) + if match: + return match.group(1) + return None @click.group(context_settings={"help_option_names": ["-h", "--help"]}) @@ -26,6 +45,7 @@ def options(function): "--repo", prompt="The github repository (e.g. my-organization/my-project-name)", help="The github organization/repository to make a release for", + default=get_repository, )(function) return function