Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This action can be used in other repositories' workflows to clone multiple repos
**Inputs:**
- **`repo-list`** (required): Space-separated list of repositories in `org/repo` format to clone
- **`git-pat`** (optional): Git Personal Access Token for authentication
- **`default-repo-ref`** (optional): Default Git reference to check out
- **`pyproject-path`** (optional): Path to the pyproject.toml file

**Example workflow:**

Expand All @@ -22,7 +24,8 @@ This action can be used in other repositories' workflows to clone multiple repos
with:
repo-list: "org/repo1 org/repo2"
git-pat: ${{ secrets.GIT_PAT }}
default-repo-ref: "main"
default-repo-ref: main
pyproject-path: pyproject.toml
~~~

### Features
Expand Down
10 changes: 8 additions & 2 deletions git-clone/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ inputs:
required: false
default-repo-ref:
description: 'Default Git reference to check out'
default: 'main'
default: ''
required: false
pyproject-path:
description: 'Path to the pyproject.toml file'
default: ''
required: false
runs:
using: "composite"
Expand All @@ -22,5 +26,7 @@ runs:
env:
GIT_PAT: ${{ inputs.git-pat }}
REPO_LIST: ${{ inputs.repo-list }}
DEFAULT_REPO_REF: ${{ inputs.default-repo-ref }}
PYPROJECT_PATH: ${{ inputs.pyproject-path }}
run: |
uv run ${{ github.action_path }}/../scripts/git-clone-ref.py ${REPO_LIST}
uv run ${{ github.action_path }}/../scripts/git-clone-ref.py ${REPO_LIST} --default-repo-ref "${DEFAULT_REPO_REF}" --pyproject-path "${PYPROJECT_PATH}"
43 changes: 32 additions & 11 deletions scripts/git-clone-ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
# ]
# ///

import os
import logging
import os
import shutil
from typing import Annotated

import git
import tomllib
import typer

logger = logging.getLogger(__name__)
Expand All @@ -24,7 +26,8 @@ def git_clone_repo(
repo_ref: str,
multi_options: tuple[str, ...] = ("--depth=1", "--recurse-submodules"),
) -> str:
multi_options += (f"--branch {repo_ref}",)
if repo_ref:
multi_options += (f"--branch {repo_ref}",)
Comment on lines +29 to +30

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows to rely on the default repo branch instead of hardcoding main as default. There's some repos that use different default branches, and others such as cdsapi use master


if os.path.exists(repo_path):
logger.info(f"removing {repo_path!r}")
Expand All @@ -44,7 +47,10 @@ def git_clone_repos(
git_pat: str,
default_repo_ref: str,
) -> None:
for repo_path in paths:
if not paths:
logger.warning("No repository to clone.")

for repo_path in set(paths):
repo_name = os.path.basename(repo_path)
repo_org = os.path.basename(os.path.dirname(repo_path))
git_pat_org = os.getenv(f"GIT_PAT_{repo_org.upper()}", git_pat)
Expand All @@ -68,17 +74,32 @@ def git_clone_repos(

@app.command()
def main(
github_repos: list[str] = typer.Argument(..., help="GitHub repositories to clone"),
git_pat: str = typer.Option(
default="", envvar="GIT_PAT", help="GitHub Personal Access Token"
),
default_repo_ref: str = typer.Option(
default="main", help="Default Git reference to check out"
),
repo_list: Annotated[

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the script variable name for consistency with the action. But should not have impact has it's an argument variable

list[str],
typer.Argument(help="GitHub repositories to clone."),
] = [],
git_pat: Annotated[
str,
typer.Option(envvar="GIT_PAT", help="GitHub Personal Access Token."),
] = "",
default_repo_ref: Annotated[
str,
typer.Option(help="Default Git reference to check out."),
] = "",
pyproject_path: Annotated[
str | None,
typer.Option(help="Path to the pyproject.toml file."),
] = None,
):
if pyproject_path:
with open(pyproject_path, "rb") as fp:
pyproject = tomllib.load(fp)
config = pyproject.get("tool", {}).get("git-clone", {})
repo_list.extend(config.get("repo-list", []))

logging.basicConfig(level=logging.INFO)
git_clone_repos(
github_repos,
repo_list,
"https://{credentials}github.com/{repo_org}/{repo_name}.git",
git_pat,
default_repo_ref,
Expand Down