docs: add adr 0004 and rewrite readme to be portable and impersonal

- Add ADR 004 documenting existing repository linking architecture
- Rewrite README with impersonal tone, removing workstation/laptop coupling
- Document existing repository workflow and updated CLI options
This commit is contained in:
kiskaadee committed 2026-10-10 00:17:27 -05:00
1 parent 2d0731ebe7
commit 5c29be4fb7
2 files changed
+130 -64

No files matched your search

+87 -64
View File
@@ -1,136 +1,159 @@
# 🚀 `new-repo` — Automated Repository Provisioning CLI # `new-repo` — Automated Repository Provisioning CLI
`new-repo` is a lightweight, zero-dependency Python 3 command-line utility that automates repository provisioning across a hybrid Git topology: `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**: Self-hosted Gitea instance (`gitea.roadtotech.me`). - **Primary Forge**: A self-hosted Gitea instance for local sovereignty and CI/CD automation.
- **Offsite Backup Forge**: GitHub (`github.com`) via automated push mirroring. - **Backup Forge**: GitHub for offsite redundancy via automated server-side push mirroring.
It eliminates repetitive manual setup by providing sensible defaults (Gitea as primary, automated GitHub push mirroring, `UNLICENSE` default, private visibility, auto-clone locally into `$PWD/<name>`) while supporting Gitea repository templates and optional standalone configurations. It automates the multi-step setup required to create a repository, establish push mirroring, and prepare a local working tree in a single command.
--- ---
## 🎯 Architecture & Design Principles ## 🎯 Key Design Principles
1. **Zero External Dependencies**: Implemented strictly with the Python 3 standard library (`urllib.request`, `argparse`, `json`, `subprocess`, `dataclasses`, `typing`). No virtual environments, compilation steps, or pip packages required. 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. **12-Factor Environment Configuration**: Standard environment variables (`GITEA_TOKEN`, `GITHUB_TOKEN`) serve as the primary contract. The tool runs anywhere—inside or outside Nix, locally, or in CI/CD pipelines—with no code modifications. 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. **Transparent SOPS Fallback**: For daily workstation usage, when environment variables are not exported in the shell, credentials resolve transparently from SOPS-encrypted files (`~/Config/secrets.yaml` or legacy `Homelab` secrets) in process memory. 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. **Strict Typing & Linting**: Built with full static type annotations passing **Pyright** with 0 errors and styled with **Ruff**. 4. **Resilient Failure Recovery**: Adheres to a *retain-and-report* policy. If backup mirror provisioning or push mirror registration encounters an error after the primary repository is created, the primary repository is preserved and diagnostic URLs are printed for manual review.
5. **Strict Static Typing**: Enforces full static type hints validated via **Pyright** and formatted/linted via **Ruff**.
See our Architecture Decision Records in [`docs/adr/`](docs/adr/): Architecture Decision Records (ADRs) are maintained in [`docs/adr/`](docs/adr/):
- [**ADR 001**: Adopt Zero-Dependency Python Over Compiled Rust](docs/adr/0001-adopt-zero-dependency-python.md) - [**ADR 001**: Adopt Zero-Dependency Python Over Compiled Rust](docs/adr/0001-adopt-zero-dependency-python.md)
- [**ADR 002**: Environment Variables as Primary Configuration Interface with Pluggable Secret Resolvers](docs/adr/0002-environment-variables-as-primary-interface-with-sops-fallback.md) - [**ADR 002**: Environment Variables as Primary Configuration Interface with Pluggable Secret Resolvers](docs/adr/0002-environment-variables-as-primary-interface-with-sops-fallback.md)
- [**ADR 003**: Dual-Forge Topology with Automated Push Mirroring and Retain-and-Report Error Policy](docs/adr/0003-dual-forge-topology-with-automated-push-mirroring.md) - [**ADR 003**: Dual-Forge Topology with Automated Push Mirroring and Retain-and-Report Error Policy](docs/adr/0003-dual-forge-topology-with-automated-push-mirroring.md)
- [**ADR 004**: Existing Repository Linking and Auto-Detection](docs/adr/0004-existing-repository-linking-and-auto-detection.md)
--- ---
## 💻 CLI Usage ## 💻 CLI Specification
```text ```text
Usage: new-repo <NAME> [OPTIONS] Usage: new-repo [NAME] [OPTIONS]
Arguments: Arguments:
name Name of the repository to create name Optional repository name (defaults to current directory if omitted or '.')
Options: Options:
-p, --public Make repository public (default: private) -p, --public Make repository public (default: private)
-d, --description Optional repository description -d, --description Optional repository description string
-l, --license License template identifier (default: UNLICENSE) -l, --license License template identifier (default: UNLICENSE, pass "" to disable)
-t, --template Optional Gitea template repository ('[owner/]template-name') -t, --template Optional Gitea template repository ('[owner/]template-name')
-M, --no-mirror Disable automatic offsite push mirroring to GitHub -M, --no-mirror Disable automatic offsite push mirroring to GitHub
--no-clone Skip cloning repository locally after creation --no-clone Skip cloning the repository locally after creation
-h, --help Show help message and exit -h, --help Show help message and exit
``` ```
### Examples ### Examples
```bash ```bash
# 1. Standard creation (Gitea + GitHub push mirror + UNLICENSE + private + auto-clone) # 1. Standard greenfield creation (Private Gitea + GitHub push mirror + UNLICENSE + auto-clone)
new-repo my-microservice new-repo service-api
# 2. Generate from a Gitea template repository # 2. Attach remotes & push an existing local repository
new-repo auth-api --template template-python-api cd ~/Projects/my-existing-app
new-repo
# 3. Local-only Gitea repo (no GitHub mirror) # 3. Provision from an existing Gitea template
new-repo web-client --template template-react-app
# 4. Local-only repository (no GitHub mirror)
new-repo private-notes --no-mirror new-repo private-notes --no-mirror
# 4. Public repository with MIT license # 5. Public open-source repository with MIT license
new-repo open-tool --public --license MIT new-repo utility-lib --public --license MIT
``` ```
--- ---
## 🔐 Credentials Configuration ## 🔐 Credentials & Configuration
### 1. Environment Variables (Universal / CI/CD)
The script resolves credentials from standard environment variables:
### Option A: Standard Environment Variables (Any System / CI)
Export credentials in your environment or place them in a `.env` file:
```bash ```bash
export GITEA_TOKEN="your_gitea_api_token" # Required
export GITHUB_TOKEN="your_github_token" export GITEA_TOKEN="your_gitea_personal_access_token"
export GITEA_URL="https://gitea.roadtotech.me" # optional, default export GITHUB_TOKEN="your_github_personal_access_token"
export GITEA_USER="kiskaadee" # optional, default
export GITHUB_USER="kiskaadee" # optional, default # Optional overrides (defaults shown)
export GITEA_URL="https://gitea.example.com"
export GITEA_USER="git_username"
export GITHUB_USER="github_username"
``` ```
### Option B: SOPS Workstation Encryption (Workstation Default) ### 2. Local `.env` File
On your laptop, store tokens in `~/Config/secrets.yaml`: Alternatively, create a `.env` file in the working directory or project root:
```bash
sops ~/Config/secrets.yaml ```env
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:
```yaml ```yaml
system: system:
gh_repo_token: <github_pat> gh_repo_token: <github_token>
gitea: gitea:
api_token: <gitea_pat> api_token: <gitea_token>
``` ```
--- ---
## 🏛️ System Workflow ## 🏛️ Provisioning Architecture & Workflow
```mermaid ```mermaid
sequenceDiagram sequenceDiagram
autonumber autonumber
actor User as Developer (Laptop) actor User as Developer / CI Runner
participant CLI as new-repo (Python 3) participant CLI as new-repo (Python 3)
participant SP as SecretProvider (Env / SOPS RAM) participant SP as SecretProvider (Env / .env / SOPS)
participant Gitea as Gitea API (Primary) participant Gitea as Gitea API (Primary)
participant GitHub as GitHub API (Backup) participant GitHub as GitHub API (Backup)
participant Git as Local Git participant Git as Local Git
User->>CLI: new-repo <repo-name> [flags] User->>CLI: new-repo <repo-name> [flags]
CLI->>SP: Resolve credentials CLI->>SP: Resolve API credentials
SP-->>CLI: (Gitea token, GitHub token) SP-->>CLI: Credentials (Gitea token, GitHub token)
alt Using Template (-t <template>) alt Template Repo Specified (-t <template>)
CLI->>Gitea: POST /repos/{owner}/{template}/generate CLI->>Gitea: POST /api/v1/repos/{owner}/{template}/generate
Gitea-->>CLI: Repo created from template Gitea-->>CLI: Primary repository generated
else Standard Repo else Standard Repository
CLI->>Gitea: POST /user/repos (with license & init) CLI->>Gitea: POST /api/v1/user/repos (with license & auto-init)
Gitea-->>CLI: Repo created Gitea-->>CLI: Primary repository created
end end
opt Mirroring Enabled (Default) opt Push Mirroring Enabled (Default)
CLI->>GitHub: POST /user/repos (create empty target repo) CLI->>GitHub: POST https://api.github.com/user/repos (empty target)
GitHub-->>CLI: 201 Created GitHub-->>CLI: Mirror target created
CLI->>Gitea: POST /repos/{owner}/{repo}/push_mirrors CLI->>Gitea: POST /api/v1/repos/{owner}/{repo}/push_mirrors
Gitea-->>CLI: Push Mirror Registered (sync on commit) Gitea-->>CLI: Push mirror registered (sync on commit)
end end
opt Auto-clone Enabled (Default) opt Local Clone Enabled (Default)
CLI->>Git: git clone git@gitea.roadtotech.me:user/repo.git CLI->>Git: git clone git@<gitea-host>:<user>/<repo>.git
Git-->>CLI: Working tree ready in $PWD/<repo-name> Git-->>CLI: Working tree cloned to ./<repo-name>
end end
``` ```
--- ---
## 🧪 Verification & Development ## 🧪 Development & Quality Assurance
Run verification commands using the standard Python toolchain:
Run static checks using the configured toolchains:
```bash ```bash
# Type check # Execute unit tests
pyright new_repo.py python3 test_new_repo.py
# Lint & Format checks # Static type checking
ruff check new_repo.py pyright new_repo.py test_new_repo.py
ruff format --check 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
``` ```
@@ -0,0 +1,43 @@
# ADR 004: Existing Repository Linking and Auto-Detection
## Status
Accepted (2026-10-10)
## Context
Initial iterations of `new-repo` assumed a purely greenfield workflow:
1. Create a remote repository on Gitea with `auto_init=True` (generating an initial commit with a README and license).
2. Create an empty backup repository on GitHub.
3. Register Gitea push mirroring.
4. Clone the newly generated repository into `$PWD/<name>`.
However, in daily developer workflows, repositories frequently already exist locally:
- A developer starts prototyping code locally before provisioning remotes.
- An existing standalone repository needs sovereign Gitea hosting and GitHub offsite backup attached.
If executed against an existing local repository:
- Creating the remote on Gitea with `auto_init=True` generates a remote initial commit that diverges from local commits, causing git history conflicts (`fatal: refusing to merge unrelated histories`).
- Skipping the local clone step without adding `origin` or pushing commits leaves the developer with an empty remote and unlinked local working tree.
## Decision
We will add **first-class existing repository support and auto-detection** to `new-repo`:
1. **Target & Name Resolution**:
- If `name` is omitted or passed as `.`, `new-repo` uses the current working directory name and targets `$PWD`.
- If `name` matches the current directory or points to an existing directory containing `.git`, the utility switches into *Existing Repository Mode*.
2. **Empty Remote Creation**:
- When linking an existing repository, Gitea is provisioned with `auto_init=False` and no license generation. Both Gitea and GitHub remotes are initialized completely empty.
3. **Local Remote Wiring & Push**:
- `new-repo` configures `origin` on the local repository pointing to `git@<gitea-host>:<user>/<repo>.git`.
- The tool detects the current active branch (defaulting to `main`) and pushes with upstream tracking (`git push -u origin <branch>`).
- Server-side, Gitea automatically synchronizes all pushed commits to GitHub via the registered push mirror.
## Consequences
### Good
- **Unified Command**: `new-repo` handles both greenfield project generation and remote attachment for existing local code with the same interface.
- **Commit Lineage Preserved**: Eliminates divergent history conflicts by ensuring remote forges remain empty until the local branch is pushed.
- **Developer Ergonomics**: Running `new-repo` inside any existing git project immediately binds it to sovereign dual-forge infrastructure in one step.
### Bad
- The script now requires read/write access to local `.git` configuration and executes local `git remote` and `git push` commands.
### Neutral
- If `origin` already exists, `new-repo` updates the remote URL to the primary Gitea instance.