docs: add adrs 0001-0003 and update project documentation
- Add ADR 001 documenting shift from Rust to zero-dependency Python - Add ADR 002 establishing 12-factor env vars with SOPS fallback - Add ADR 003 detailing dual-forge topology and retain-and-report policy - Update README with CLI usage, credentials options, and architecture diagram
This commit is contained in:
1 parent
717e326a2b
commit
9d07c3ecd4
4 files changed
+259
-1
No files matched your search
@@ -1 +1,136 @@
|
|||||||
# new-repo
|
# 🚀 `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:
|
||||||
|
- **Primary Forge**: Self-hosted Gitea instance (`gitea.roadtotech.me`).
|
||||||
|
- **Offsite Backup Forge**: GitHub (`github.com`) via automated 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.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎯 Architecture & 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.
|
||||||
|
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.
|
||||||
|
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.
|
||||||
|
4. **Strict Typing & Linting**: Built with full static type annotations passing **Pyright** with 0 errors and styled with **Ruff**.
|
||||||
|
|
||||||
|
See our Architecture Decision Records in [`docs/adr/`](docs/adr/):
|
||||||
|
- [**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 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)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 💻 CLI Usage
|
||||||
|
|
||||||
|
```text
|
||||||
|
Usage: new-repo <NAME> [OPTIONS]
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
name Name of the repository to create
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-p, --public Make repository public (default: private)
|
||||||
|
-d, --description Optional repository description
|
||||||
|
-l, --license License template identifier (default: UNLICENSE)
|
||||||
|
-t, --template Optional Gitea template repository ('[owner/]template-name')
|
||||||
|
-M, --no-mirror Disable automatic offsite push mirroring to GitHub
|
||||||
|
--no-clone Skip cloning repository locally after creation
|
||||||
|
-h, --help Show help message and exit
|
||||||
|
```
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. Standard creation (Gitea + GitHub push mirror + UNLICENSE + private + auto-clone)
|
||||||
|
new-repo my-microservice
|
||||||
|
|
||||||
|
# 2. Generate from a Gitea template repository
|
||||||
|
new-repo auth-api --template template-python-api
|
||||||
|
|
||||||
|
# 3. Local-only Gitea repo (no GitHub mirror)
|
||||||
|
new-repo private-notes --no-mirror
|
||||||
|
|
||||||
|
# 4. Public repository with MIT license
|
||||||
|
new-repo open-tool --public --license MIT
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔐 Credentials Configuration
|
||||||
|
|
||||||
|
### Option A: Standard Environment Variables (Any System / CI)
|
||||||
|
Export credentials in your environment or place them in a `.env` file:
|
||||||
|
```bash
|
||||||
|
export GITEA_TOKEN="your_gitea_api_token"
|
||||||
|
export GITHUB_TOKEN="your_github_token"
|
||||||
|
export GITEA_URL="https://gitea.roadtotech.me" # optional, default
|
||||||
|
export GITEA_USER="kiskaadee" # optional, default
|
||||||
|
export GITHUB_USER="kiskaadee" # optional, default
|
||||||
|
```
|
||||||
|
|
||||||
|
### Option B: SOPS Workstation Encryption (Workstation Default)
|
||||||
|
On your laptop, store tokens in `~/Config/secrets.yaml`:
|
||||||
|
```bash
|
||||||
|
sops ~/Config/secrets.yaml
|
||||||
|
```
|
||||||
|
```yaml
|
||||||
|
system:
|
||||||
|
gh_repo_token: <github_pat>
|
||||||
|
gitea:
|
||||||
|
api_token: <gitea_pat>
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🏛️ System Workflow
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
sequenceDiagram
|
||||||
|
autonumber
|
||||||
|
actor User as Developer (Laptop)
|
||||||
|
participant CLI as new-repo (Python 3)
|
||||||
|
participant SP as SecretProvider (Env / SOPS RAM)
|
||||||
|
participant Gitea as Gitea API (Primary)
|
||||||
|
participant GitHub as GitHub API (Backup)
|
||||||
|
participant Git as Local Git
|
||||||
|
|
||||||
|
User->>CLI: new-repo <repo-name> [flags]
|
||||||
|
CLI->>SP: Resolve credentials
|
||||||
|
SP-->>CLI: (Gitea token, GitHub token)
|
||||||
|
|
||||||
|
alt Using Template (-t <template>)
|
||||||
|
CLI->>Gitea: POST /repos/{owner}/{template}/generate
|
||||||
|
Gitea-->>CLI: Repo created from template
|
||||||
|
else Standard Repo
|
||||||
|
CLI->>Gitea: POST /user/repos (with license & init)
|
||||||
|
Gitea-->>CLI: Repo created
|
||||||
|
end
|
||||||
|
|
||||||
|
opt Mirroring Enabled (Default)
|
||||||
|
CLI->>GitHub: POST /user/repos (create empty target repo)
|
||||||
|
GitHub-->>CLI: 201 Created
|
||||||
|
CLI->>Gitea: POST /repos/{owner}/{repo}/push_mirrors
|
||||||
|
Gitea-->>CLI: Push Mirror Registered (sync on commit)
|
||||||
|
end
|
||||||
|
|
||||||
|
opt Auto-clone Enabled (Default)
|
||||||
|
CLI->>Git: git clone git@gitea.roadtotech.me:user/repo.git
|
||||||
|
Git-->>CLI: Working tree ready in $PWD/<repo-name>
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🧪 Verification & Development
|
||||||
|
|
||||||
|
Run static checks using the configured toolchains:
|
||||||
|
```bash
|
||||||
|
# Type check
|
||||||
|
pyright new_repo.py
|
||||||
|
|
||||||
|
# Lint & Format checks
|
||||||
|
ruff check new_repo.py
|
||||||
|
ruff format --check new_repo.py
|
||||||
|
```
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
# ADR 001: Adopt Zero-Dependency Python Over Compiled Rust
|
||||||
|
|
||||||
|
## Status
|
||||||
|
Accepted (2026-10-09)
|
||||||
|
|
||||||
|
## Context
|
||||||
|
The initial design for `new-repo` proposed a compiled native Rust binary utilizing `clap`, `reqwest`, `serde`, and `secrecy`. The motivation was strong compile-time type safety and static single-binary distribution.
|
||||||
|
|
||||||
|
However, during practical evaluation, several forces made Rust ill-suited for this specific tool:
|
||||||
|
1. **Compilation Latency & Iteration Friction**: As an interactive CLI used occasionally to provision repositories, waiting for `cargo build` and maintaining dependency trees creates high developer friction when tweaking CLI options or REST payloads.
|
||||||
|
2. **Boilerplate vs. Scope**: The tool fundamentally orchestrates three HTTP REST calls (Gitea API, GitHub API, push mirror registration) and executes a local `git clone`. Rust's asynchronous/synchronous network ceremony and dependency footprint are disproportionate to this scope.
|
||||||
|
3. **Workstation Integration**: Modifying a shell or provisioning script should allow immediate execution without an intermediate compile step.
|
||||||
|
|
||||||
|
We evaluated three runtime alternatives:
|
||||||
|
- **Bash with curl and jq**: Lowest barrier, but fragile when constructing nested JSON payloads, parsing multi-step API errors, and managing tokens securely.
|
||||||
|
- **Python with external libraries (httpx/requests)**: Clean syntax, but introduces virtual environment or `uv` packaging requirements.
|
||||||
|
- **Python Standard Library only**: Native JSON parsing, structured error handling via `urllib.error`, built-in argument parsing via `argparse`, and process memory safety via `subprocess`.
|
||||||
|
|
||||||
|
## Decision
|
||||||
|
We will implement `new-repo` in **Python 3 using only the standard library** (`urllib.request`, `argparse`, `json`, `subprocess`, `dataclasses`, `typing`).
|
||||||
|
To maintain the rigor initially sought in Rust:
|
||||||
|
- The codebase will enforce strict static type hints validated via **Pyright**.
|
||||||
|
- Code quality, modern Python idiom adherence, and formatting will be continuously enforced via **Ruff**.
|
||||||
|
|
||||||
|
## Consequences
|
||||||
|
### Good
|
||||||
|
- **Zero Build Step**: Edits are immediately executable without compilation or binary relinking.
|
||||||
|
- **Zero External Dependencies**: Works out of the box on any system with Python 3.10+ without virtual environments, `pip install`, or wheel caching.
|
||||||
|
- **Clean JSON Handling**: Native Python dictionaries serialize cleanly to JSON, avoiding escaping and quoting bugs inherent in shell scripts.
|
||||||
|
- **High Type Safety**: Strict Pyright type checking provides confidence similar to compiled languages without the build overhead.
|
||||||
|
|
||||||
|
### Bad
|
||||||
|
- **No Standalone Static Binary**: Requires an installed Python 3 interpreter on the host machine.
|
||||||
|
- **Slightly More Verbose HTTP Calls**: `urllib.request` requires more explicit request/header construction than third-party libraries like `httpx` or `requests`.
|
||||||
|
|
||||||
|
### Neutral
|
||||||
|
- Formatting and linting must be maintained using `ruff format` and `ruff check`.
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
# ADR 002: Environment Variables as Primary Configuration Interface with Pluggable Secret Resolvers
|
||||||
|
|
||||||
|
## Status
|
||||||
|
Accepted (2026-10-09)
|
||||||
|
|
||||||
|
## Context
|
||||||
|
When automating infrastructure and repository creation across multiple git forges, credentials (API tokens) are required for both Gitea and GitHub.
|
||||||
|
|
||||||
|
In the author's primary workstation environment, secrets are managed securely using Mozilla SOPS and Age encryption keys, ensuring tokens never touch disk in plaintext.
|
||||||
|
|
||||||
|
However, hard-coding a strict dependency on SOPS, specific filesystem paths (like `~/Config/secrets.yaml`), or NixOS packaging creates tight environmental coupling:
|
||||||
|
1. If a collaborator, CI pipeline, or developer outside Nix wants to run `new-repo`, they would be blocked by missing SOPS keys and hardcoded local paths.
|
||||||
|
2. If the author runs the script in temporary containers, GitHub Actions, or alternative workstations, it would fail unless SOPS was configured.
|
||||||
|
|
||||||
|
We need a design that satisfies two seemingly conflicting requirements:
|
||||||
|
- **Maximum Portability**: The tool must run anywhere with standard, universal configuration conventions.
|
||||||
|
- **Zero Friction on Host**: The author should not have to manually export sensitive tokens in every shell session on their primary workstation.
|
||||||
|
|
||||||
|
## Decision
|
||||||
|
We will establish **12-Factor Environment Variables as the primary configuration contract** for `new-repo`:
|
||||||
|
- `GITEA_TOKEN`: Personal access token for Gitea REST API.
|
||||||
|
- `GITHUB_TOKEN`: Personal access token for GitHub REST API.
|
||||||
|
- `GITEA_URL`: Base URL of the Gitea instance (defaults to `https://gitea.roadtotech.me`).
|
||||||
|
- `GITEA_USER`: Gitea account owner (defaults to `kiskaadee`).
|
||||||
|
- `GITHUB_USER`: GitHub account owner (defaults to `kiskaadee`).
|
||||||
|
|
||||||
|
To provide zero-friction ergonomics on the author's workstation, the `SecretProvider` will implement a multi-tier fallback resolution hierarchy:
|
||||||
|
1. **Tier 1 (Explicit Environment)**: Use environment variables if set in the current process.
|
||||||
|
2. **Tier 2 (Local `.env` File)**: Check for a local `.env` file if present.
|
||||||
|
3. **Tier 3 (SOPS Decoupled Workstation Secrets)**: If environment variables are absent, invoke `sops -d --extract` against `~/Config/secrets.yaml`.
|
||||||
|
4. **Tier 4 (Local Client Configurations)**:
|
||||||
|
- For Gitea: Check `~/.config/tea/config.yml` (official Gitea CLI login).
|
||||||
|
- For GitHub: Fall back to legacy `~/Homelab/Core/nixos/secrets.yaml` if workstation secrets have not yet migrated.
|
||||||
|
|
||||||
|
## Consequences
|
||||||
|
### Good
|
||||||
|
- **Universal Portability**: Anyone outside NixOS or without SOPS can execute the script immediately simply by running `export GITEA_TOKEN=... GITHUB_TOKEN=...; new-repo my-app`.
|
||||||
|
- **Zero Host Friction**: On the author's laptop, the script resolves tokens silently and safely from SOPS without manual shell exports.
|
||||||
|
- **Process Memory Safety**: Regardless of whether tokens come from environment variables or SOPS, they are handled strictly in memory and injected via HTTPS request headers, never exposed to `/proc/$PID/cmdline` or subprocess arguments.
|
||||||
|
- **CI/CD Compatibility**: In automated pipelines, standard secret injection (`GITEA_TOKEN`, `GITHUB_TOKEN`) works without modification.
|
||||||
|
|
||||||
|
### Bad
|
||||||
|
- The script includes fallback extraction logic (~40 lines) for SOPS and tea YAML files, which is unused when pure environment variables are provided.
|
||||||
|
|
||||||
|
### Neutral
|
||||||
|
- Standardized environment variable naming conventions (`GITEA_TOKEN`, `GITHUB_TOKEN`) must be clearly documented in the project README.
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
# ADR 003: Dual-Forge Topology with Automated Push Mirroring and Retain-and-Report Error Policy
|
||||||
|
|
||||||
|
## Status
|
||||||
|
Accepted (2026-10-09)
|
||||||
|
|
||||||
|
## Context
|
||||||
|
In a sovereign, self-hosted developer environment:
|
||||||
|
1. **Local Autonomy**: Developers need primary repositories hosted on a private forge (`gitea.roadtotech.me`) to eliminate dependencies on third-party cloud availability, allow high-bandwidth local pushes, and enable local CI/CD pipelines.
|
||||||
|
2. **Cloud Redundancy**: Offsite disaster recovery is required. If local storage fails or the self-hosted server experiences downtime, code should be preserved on a redundant offsite cloud provider (GitHub).
|
||||||
|
3. **Operational Overhead**: Manually maintaining dual remotes locally (`git push origin && git push github`) creates cognitive load, is prone to developer forgetfulness, and breaks single-remote automation.
|
||||||
|
4. **Failure Modes**: When automating a multi-stage remote provisioning pipeline across two external network services, network partitions, authentication errors, or rate limits can cause a failure midway through the sequence.
|
||||||
|
|
||||||
|
## Decision
|
||||||
|
1. **Asymmetric Push Mirror Architecture**:
|
||||||
|
- Gitea is the authoritative primary forge for all new repositories.
|
||||||
|
- GitHub is an automated backup mirror target.
|
||||||
|
- When provisioning, `new-repo` creates the repository on Gitea (with initial license and commit) and creates a completely empty target repository on GitHub (`auto_init=False`).
|
||||||
|
- `new-repo` configures Gitea's internal push mirror daemon (`POST /api/v1/repos/{owner}/{repo}/push_mirrors`) with `sync_on_commit=True`.
|
||||||
|
- The developer interacts exclusively with Gitea (`origin`). Gitea automatically synchronizes all branches, tags, and commits to GitHub in the background.
|
||||||
|
|
||||||
|
2. **Auto-Clone by Default**:
|
||||||
|
- Following remote provisioning, `new-repo` clones the repository locally into `$PWD/<name>` by default, enabling the developer to immediately begin work. A `--no-clone` flag is provided to bypass local filesystem operations.
|
||||||
|
|
||||||
|
3. **Retain & Report Error Recovery Policy**:
|
||||||
|
- If GitHub repository creation or push mirror registration fails after Gitea repository creation has succeeded, the tool will **not** roll back or delete the Gitea repository.
|
||||||
|
- Deleting remote repositories autonomously on partial failure poses catastrophic data-loss risks if the failure was transient.
|
||||||
|
- Instead, the tool reports actionable diagnostic warnings, prints the URL of the surviving Gitea repository, and exits cleanly.
|
||||||
|
|
||||||
|
## Consequences
|
||||||
|
### Good
|
||||||
|
- **Single Remote Simplicity**: The developer pushes to `origin` (Gitea) as normal; cloud backups happen asynchronously and server-side.
|
||||||
|
- **Disaster Proof**: Cloud backup is guaranteed without relying on local git hooks or manual multi-remote pushes.
|
||||||
|
- **Safe Recovery**: Transient network or GitHub quota errors do not blow away newly created repositories.
|
||||||
|
- **Immediate Ergonomics**: Running `new-repo my-app` leaves the user inside their cloned directory ready to code.
|
||||||
|
|
||||||
|
### Bad
|
||||||
|
- GitHub Personal Access Token must be stored inside Gitea's push mirror configuration so Gitea's worker can authenticate against GitHub.
|
||||||
|
|
||||||
|
### Neutral
|
||||||
|
- If a push mirror is disrupted, sync status must be inspected via Gitea's repository settings web interface.
|
||||||
Reference in new issue
Block a user