Skip to content

Commit

Permalink
cli init repo auto provision fix (#2012)
Browse files Browse the repository at this point in the history
  • Loading branch information
sblair-metrostar authored Sep 19, 2023
1 parent 57be4dc commit d58ebb1
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 45 deletions.
9 changes: 4 additions & 5 deletions src/_nebari/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from _nebari.stages.terraform_state import TerraformStateEnum
from _nebari.utils import get_latest_kubernetes_version, random_secure_string
from _nebari.version import __version__
from nebari.schema import ProviderEnum
from nebari.schema import ProviderEnum, github_url_regex

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -194,9 +194,8 @@ def render_config(
print(str(e))

if repository_auto_provision:
GITHUB_REGEX = "(https://)?github.com/([^/]+)/([^/]+)/?"
if re.search(GITHUB_REGEX, repository):
match = re.search(GITHUB_REGEX, repository)
match = re.search(github_url_regex, repository)
if match:
git_repository = github_auto_provision(
config_model, match.group(2), match.group(3)
)
Expand Down Expand Up @@ -230,7 +229,7 @@ def github_auto_provision(config: pydantic.BaseModel, owner: str, repo: str):
f"Unable to create GitHub repo https://github.com/{owner}/{repo} - error message from GitHub is: {he}"
)
else:
logger.warn(f"GitHub repo https://github.com/{owner}/{repo} already exists")
logger.warning(f"GitHub repo https://github.com/{owner}/{repo} already exists")

try:
# Secrets
Expand Down
9 changes: 6 additions & 3 deletions src/_nebari/provider/cicd/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
def github_request(url, method="GET", json=None, authenticate=True):
auth = None
if authenticate:
missing = []
for name in ("GITHUB_USERNAME", "GITHUB_TOKEN"):
if os.environ.get(name) is None:
raise ValueError(
f"Environment variable={name} is required for GitHub automation"
)
missing.append(name)
if len(missing) > 0:
raise ValueError(
f"Environment variable(s) required for GitHub automation - {', '.join(missing)}"
)
auth = requests.auth.HTTPBasicAuth(
os.environ["GITHUB_USERNAME"], os.environ["GITHUB_TOKEN"]
)
Expand Down
11 changes: 8 additions & 3 deletions src/_nebari/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class InitInputs(schema.Base):
namespace: typing.Optional[schema.namespace_pydantic] = "dev"
auth_provider: AuthenticationEnum = AuthenticationEnum.password
auth_auto_provision: bool = False
repository: typing.Union[str, None] = None
repository: typing.Optional[schema.github_url_pydantic] = None
repository_auto_provision: bool = False
ci_provider: CiEnum = CiEnum.none
terraform_state: TerraformStateEnum = TerraformStateEnum.remote
Expand Down Expand Up @@ -512,12 +512,17 @@ def init(
auth_auto_provision: bool = typer.Option(
False,
),
repository: GitRepoEnum = typer.Option(
repository: str = typer.Option(
None,
help=f"options: {enum_to_list(GitRepoEnum)}",
help="Github repository URL to be initialized with --repository-auto-provision",
callback=typer_validate_regex(
schema.github_url_regex,
"Must be a fully qualified GitHub repository URL.",
),
),
repository_auto_provision: bool = typer.Option(
False,
help="Initialize the GitHub repository provided by --repository (GitHub credentials required)",
),
ci_provider: CiEnum = typer.Option(
CiEnum.none,
Expand Down
3 changes: 3 additions & 0 deletions src/nebari/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
email_regex = "^[^ @]+@[^ @]+\\.[^ @]+$"
email_pydantic = pydantic.constr(regex=email_regex)

github_url_regex = "^(https://)?github.com/([^/]+)/([^/]+)/?$"
github_url_pydantic = pydantic.constr(regex=github_url_regex)


class Base(pydantic.BaseModel):
...
Expand Down
62 changes: 28 additions & 34 deletions tests/tests_unit/test_cli_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,36 @@ def generate_test_data_test_cli_init_happy_path():
for domain_name in [f"{project_name}.example.com"]:
for namespace in ["test-ns"]:
for auth_provider in ["password", "Auth0", "GitHub"]:
for repository in ["github.com", "gitlab.com"]:
for ci_provider in [
"none",
"github-actions",
"gitlab-ci",
for ci_provider in [
"none",
"github-actions",
"gitlab-ci",
]:
for terraform_state in [
"local",
"remote",
"existing",
]:
for terraform_state in [
"local",
"remote",
"existing",
]:
for email in ["noreply@example.com"]:
for (
kubernetes_version
) in get_kubernetes_versions(provider) + [
"latest"
]:
test_data.append(
(
provider,
region,
project_name,
domain_name,
namespace,
auth_provider,
repository,
ci_provider,
terraform_state,
email,
kubernetes_version,
)
for email in ["noreply@example.com"]:
for (
kubernetes_version
) in get_kubernetes_versions(provider) + [
"latest"
]:
test_data.append(
(
provider,
region,
project_name,
domain_name,
namespace,
auth_provider,
ci_provider,
terraform_state,
email,
kubernetes_version,
)
)

keys = [
"provider",
Expand All @@ -114,7 +112,6 @@ def generate_test_data_test_cli_init_happy_path():
"domain_name",
"namespace",
"auth_provider",
"repository",
"ci_provider",
"terraform_state",
"email",
Expand All @@ -130,7 +127,6 @@ def test_cli_init_happy_path(
domain_name: str,
namespace: str,
auth_provider: str,
repository: str,
ci_provider: str,
terraform_state: str,
email: str,
Expand All @@ -152,8 +148,6 @@ def test_cli_init_happy_path(
namespace,
"--auth-provider",
auth_provider,
"--repository",
repository, # TODO: doesn't show up in the output anywhere, how do I verify this?
"--ci-provider",
ci_provider,
"--terraform-state",
Expand Down
Loading

0 comments on commit d58ebb1

Please sign in to comment.