Be able to set branch from option in LocalProjectParameter
-import os
-
import click
-import requests
from packit.local_project import LocalProject
@@ -13,18 +10,31 @@ class LocalProjectParameter(click.ParamType):
name = "repo"
- def convert(self, value, param, ctx):
- try:
- if os.path.isdir(value):
- return LocalProject(working_dir=value)
+ def __init__(self, branch_param_name: str = None) -> None:
+ """
+ :param branch_param_name: name of the cli function parameter (not the option name)
+ """
+ super().__init__()
+ self.branch_param_name = branch_param_name
+ def convert(self, value, param, ctx):
try:
- res = requests.get(value)
- if res.ok:
- return LocalProject(git_url=value)
- self.fail("Cannot connect to specified url.", param, ctx)
- except requests.exceptions.BaseHTTPError as ex:
- self.fail("Cannot connect to specified url.", param, ctx)
-
- except ValueError as ex:
+ branch_name = None
+ if self.branch_param_name:
+ if self.branch_param_name in ctx.params:
+ branch_name = ctx.params[self.branch_param_name]
+ else: # use the default
+ for param in ctx.command.params:
+ if param.name == self.branch_param_name:
+ branch_name = param.default
+
+ local_project = LocalProject(path_or_url=value, ref=branch_name)
+ if not local_project.working_dir and not local_project.git_url:
+ self.fail(
+ "Parameter is not an existing directory nor correct git url.",
+ param,
+ ctx,
+ )
+ return local_project
+ except Exception as ex:
self.fail(ex, param, ctx)
