Skip to content

Commit

Permalink
Split git (github) into git (github public) and git (github private).…
Browse files Browse the repository at this point in the history
… Fixed tests to match. Set tests to use git (local) and none by default
  • Loading branch information
GatlenCulp committed Dec 28, 2024
1 parent d83e6b7 commit 6fbd4ed
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
17 changes: 12 additions & 5 deletions ccds-help.json
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,22 @@
{
"choice": "git (local)",
"help": {
"description": "Initialize a git repository locally.",
"more_information": ""
"description": "Initialize project as a local git repository.",
"more_information": "[Git CLI](https://git-scm.com/downloads) Required"
}
},
{
"choice": "git (github)",
"choice": "git (github private)",
"help": {
"description": "Initialize a git repository locally and upload to GitHub",
"more_information": ""
"description": "Initialize project and upload to GitHub as a **private** repo.",
"more_information": "[Git CLI](https://git-scm.com/downloads) + [GitHub CLI](https://cli.github.com/) & [Auth](https://cli.github.com/manual/gh_auth_login) Required"
}
},
{
"choice": "git (github public)",
"help": {
"description": "Initialize project and upload to GitHub as a **public** repo.",
"more_information": "[Git CLI](https://git-scm.com/downloads) + [GitHub CLI](https://cli.github.com/) & [Auth](https://cli.github.com/manual/gh_auth_login) Required"
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion ccds.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"version_control": [
"none",
"git (local)",
"git (github)"
"git (github private)",
"git (github public)"
]
}
29 changes: 12 additions & 17 deletions ccds/hook_utils/configure_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,29 @@ def _check_git_cli_installed() -> bool:
def configure_github_repo(
directory: str | Path,
repo_name: str,
visibility: Literal["private", "public"] = "private"
) -> bool:
"""
Configure a Git repository locally and optionally on GitHub with specified branch protections.
Args:
directory: Directory where the repository will be created or updated
repo_name: Name of the repository
visibility: Whether to upload to github as a public or private repo
Returns:
bool: True if configuration was successful, False otherwise
"""
try:
if not _check_gh_cli_installed_authenticated():
raise RuntimeError(
"gh CLI is required but not installed or not authenticated. "
"Try installing and running `gh auth login`."
)

subprocess.run("gh --version", shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError:
raise RuntimeError("GitHub CLI is not installed. Please install and try again.")
try:
subprocess.run("gh auth status", shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError:
raise RuntimeError("GitHub CLI not authenticated. Please run `gh auth login` and try again.")

try:
# GitHub operations
github_username = _gh("api user -q .login", capture_output=True, text=True).stdout.strip()

Expand All @@ -92,7 +97,7 @@ def configure_github_repo(
if not init_local_git_repo(directory):
return False
_gh(
f"repo create {repo_name} --private --source=. --remote=origin --push"
f"repo create {repo_name} --{visibility} --source=. --remote=origin --push"
)
else:
remote_url = _get_gh_remote_url(github_username, repo_name)
Expand Down Expand Up @@ -120,16 +125,6 @@ def _gh(command: str, **kwargs) -> subprocess.CompletedProcess:
"""Run a GitHub CLI command and return the result."""
return subprocess.run(f"gh {command}", shell=True, check=True, **kwargs)


def _check_gh_cli_installed_authenticated() -> bool:
"""Check if gh CLI is installed and authenticated."""
try:
subprocess.run("gh --version", shell=True, check=True, capture_output=True)
subprocess.run("gh auth status", shell=True, check=True, capture_output=True)
return True
except subprocess.CalledProcessError:
return False

def _get_gh_remote_url(github_username: str, repo_name: str) -> Literal["https", "ssh"]:
"""Returns whether the github protocol is https or ssh from user's config"""
try:
Expand Down
16 changes: 13 additions & 3 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,22 @@
generated_path.write_text("")
# {% endif %}

#
# VERSION CONTROL
#

# {% if cookiecutter.version_control == "git (local)" %}
init_local_git_repo(directory=Path.cwd(), _make_initial_commit=True)
# {% elif cookiecutter.version_control == "git (github)" %}
init_local_git_repo(directory=Path.cwd())
# {% elif cookiecutter.version_control == "git (github private)" %}
configure_github_repo(
directory=Path.cwd(),
repo_name="{{ cookiecutter.repo_name }}",
visibility="private"
)
# {% elif cookiecutter.version_control == "git (github public)" %}
configure_github_repo(
directory=Path.cwd(),
repo_name="{{ cookiecutter.repo_name }}",
protection_type="main_and_dev",
visibility="public"
)
# {% endif %}
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def config_generator(fast=False):
],
[("dependency_file", opt) for opt in cookiecutter_json["dependency_file"]],
[("pydata_packages", opt) for opt in cookiecutter_json["pydata_packages"]],
[("version_control", opt) for opt in ("none", "git (local)")]
# TODO: Tests for "version_control": "git (github)"
)

def _is_valid(config):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def verify_folders(root, config):
if config["docs"] == "mkdocs":
expected_dirs.add("docs/docs")

if config["version_control"] in ("git (local)", "git (github)"):
if config["version_control"] in ("git (local)", "git (github public)", "git (github private)"):
# Expected after `git init`
expected_dirs.update(
{
Expand Down

0 comments on commit 6fbd4ed

Please sign in to comment.