kiskaadee 81f155105b feat(new-repo): add --empty flag for bare GitHub repo creation
- Add --empty flag (auto_init=False) so remote-only migrations avoid root commit conflicts
- Reject combining --empty with --template
- Add test_empty_argument unit test and document --empty in README.md
2026-11-26 09:52:51 -05:00
2026-09-08 14:02:29 -05:00

new-repo — Automated Repository Provisioning CLI

new-repo is a portable, zero-dependency Python 3 command-line utility designed to automate repository provisioning across a dual-forge Git topology:

  • Primary Forge: GitHub for cloud availability, issue/project planning, and CI/CD automation.
  • Backup Forge: Self-hosted Gitea for homelab redundancy via automated server-side pull mirroring.

It automates the multi-step setup required to create a repository on GitHub, prepare a local working tree, and attach a Gitea pull mirror in a single command.


🎯 Key Design Principles

  1. Zero External Dependencies: Built strictly with Python 3 standard library modules (urllib.request, argparse, json, subprocess, dataclasses, typing). Requires no virtual environment, compilation step, or third-party package installation.
  2. Universal Portability (12-Factor): Standard environment variables (GITEA_TOKEN, GITHUB_TOKEN) and optional .env files serve as the primary configuration contract. The utility runs out-of-the-box on any POSIX system, container, or CI/CD runner.
  3. In-Memory Credential Safety: Tokens are injected exclusively via native HTTPS authorization headers. Secrets are never passed as command-line arguments to subprocesses, preventing exposure in system process tables (ps, /proc/$PID/cmdline) or persistent disk logs.
  4. Resilient Failure Recovery: Adheres to a retain-and-report policy. If homelab Gitea pull-mirror registration encounters an error after the primary GitHub repository is created, the GitHub repository is preserved and diagnostic URLs are printed for manual review or --gitea-mirror retry.
  5. Strict Static Typing: Enforces full static type hints validated via Pyright and formatted/linted via Ruff.

Architecture Decision Records (ADRs) are maintained in docs/adr/:


💻 CLI Specification

Usage: new-repo [NAME] [OPTIONS]

Arguments:
  name                  Optional repository name ('[org/]repo', defaults to current directory if omitted or '.')

Options:
  -o, --org ORG         Target GitHub organization to own the repository (defaults to personal GITHUB_USER)
  --gitea-org ORG       Optional Gitea organization for the homelab pull mirror (auto-created if missing; defaults to GITEA_USER)
  --gitea-mirror [URL]  Attach a Gitea pull mirror to an existing GitHub repository (URL or auto-detect from local origin)
  -p, --private         Make repository private (default: public)
  -d, --description     Optional repository description string
  -l, --license         GitHub license template identifier (default: unlicense, pass "" to disable)
  -t, --template        Optional GitHub template repository ('[owner/]template-name')
  -M, --no-mirror       Disable automatic homelab pull mirroring to Gitea
  --no-clone            Skip cloning the repository locally after creation
  --empty               Create an empty GitHub repository with auto_init=False (no initial commit)
  --as PERSONA          Persona / login identity to provision as (e.g. anneux)
  -h, --help            Show help message and exit

Examples

# 1. Standard greenfield creation (Public GitHub + Gitea pull mirror + unlicense + auto-clone)
new-repo service-api

# 2. Provision as a specific agent persona (binds GitHub/Gitea credentials and SSH keys to persona)
new-repo frontend-app --as anneux

# 3. Provision under a GitHub organization (via shorthand or -o/--org flag)
new-repo my-org/core
new-repo core -o my-org

# 4. Attach remotes & push an existing local repository (into personal account or organization)
cd ~/Projects/my-existing-app
new-repo -o my-org

# 5. Provision from an existing GitHub template
new-repo web-client --template template-react-app

# 6. GitHub-only repository (no Gitea pull mirror)
new-repo cloud-only --no-mirror

# 7. Private repository with MIT license
new-repo utility-lib --private --license mit

# 8. Attach a Gitea pull mirror to an existing GitHub repository (explicit URL or local origin)
new-repo --gitea-mirror https://github.com/my-org/existing-service
cd ~/Projects/existing-service && new-repo --gitea-mirror

🔐 Credentials & Configuration

1. Environment Variables (Universal / CI/CD)

The script resolves credentials from standard environment variables:

# Required
export GITEA_TOKEN="your_gitea_personal_access_token"
export GITHUB_TOKEN="your_github_personal_access_token"

# Optional overrides (defaults shown)
export GITEA_URL="https://gitea.example.com"
export GITEA_USER="git_username"
export GITHUB_USER="github_username"

2. Local .env File

Alternatively, create a .env file in the working directory or project root:

GITEA_TOKEN=your_gitea_token
GITHUB_TOKEN=your_github_token
GITEA_URL=https://gitea.example.com
GITEA_USER=git_username
GITHUB_USER=github_username

3. Optional Workstation SOPS Integration

When environment variables are unset, the built-in SecretProvider optionally checks for SOPS-encrypted configuration files (e.g. ~/Config/secrets.yaml), extracting values directly into process memory without persisting plaintext tokens to disk:

system:
  gh_repo_token: <github_token>
gitea:
  api_token: <gitea_token>

🏛️ Provisioning Architecture & Workflow

sequenceDiagram
    autonumber
    actor User as Developer / CI Runner
    participant CLI as new-repo (Python 3)
    participant SP as SecretProvider (Env / .env / SOPS)
    participant GitHub as GitHub API (Primary)
    participant Git as Local Git
    participant Gitea as Gitea API (Pull Mirror)

    User->>CLI: new-repo <repo-name> [flags]
    CLI->>SP: Resolve API credentials
    SP-->>CLI: Credentials (GitHub token, Gitea token)

    alt Existing Local Git Repo
        CLI->>GitHub: POST /user/repos or /orgs/{org}/repos (auto_init=False)
        GitHub-->>CLI: Empty primary repository created
        CLI->>Git: git remote add/set-url origin git@github.com:<owner>/<repo>.git && git push -u origin <branch>
    else Template Repo Specified (-t <template>)
        CLI->>GitHub: POST /repos/{owner}/{template}/generate
        GitHub-->>CLI: Primary repository generated
    else Standard Greenfield Repository
        CLI->>GitHub: POST /user/repos or /orgs/{org}/repos (auto_init=True, license_template)
        GitHub-->>CLI: Primary repository created
    end

    opt Local Clone Enabled (Greenfield Default)
        CLI->>Git: git clone git@github.com:<owner>/<repo>.git
        Git-->>CLI: Working tree cloned to ./<repo-name>
    end

    opt Homelab Pull Mirror Enabled (Default)
        CLI->>Gitea: POST /api/v1/repos/migrate (mirror=True, service=github, auth_token=gh_token)
        Gitea->>GitHub: Server-to-server pull mirror sync
        Gitea-->>CLI: Pull mirror registered
    end

Attach Pull Mirror Workflow (--gitea-mirror)

sequenceDiagram
    autonumber
    actor User as Developer
    participant CLI as new-repo (--gitea-mirror)
    participant Gitea as Gitea API (Pull Mirror)
    participant GitHub as GitHub (Primary)

    User->>CLI: new-repo --gitea-mirror [github-target]
    CLI->>Gitea: GET /api/v1/repos/{owner}/{repo} (Conflict Check)
    alt Repo Exists on Gitea
        Gitea-->>CLI: 200 OK (Abort to avoid overwriting)
    else Repo Does Not Exist
        CLI->>Gitea: POST /api/v1/repos/migrate (mirror=True, auth_token=gh_token)
        Gitea->>GitHub: Server-to-server pull mirror sync
        Gitea-->>CLI: Pull mirror created
    end

🧪 Development & Quality Assurance

Run verification commands using the standard Python toolchain:

# Execute unit tests
python3 test_new_repo.py

# Static type checking
pyright new_repo.py test_new_repo.py

# Linting and formatting
ruff check new_repo.py test_new_repo.py
ruff format --check new_repo.py test_new_repo.py
S
Description
No description provided
Readme
249 KiB
0 Stars 1 Watchers 0 Forks
Languages
Python 100%