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
32 changes: 32 additions & 0 deletions .github/branch-protection.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Declarative GitHub branch protection policies
- branch: "main"
enforce_admins: true
require_linear_history: true
allow_force_pushes: false
allow_deletions: false
required_conversation_resolution: true
required_status_checks:
strict: true
contexts:
- "ci"
required_pull_request_reviews:
required_approving_review_count: 1
dismiss_stale_reviews: true
require_code_owner_reviews: true
require_last_push_approval: true

- branch: "release/*"
enforce_admins: true
require_linear_history: true
allow_force_pushes: false
allow_deletions: false
required_conversation_resolution: true
required_status_checks:
strict: true
contexts:
- "ci"
required_pull_request_reviews:
required_approving_review_count: 1
dismiss_stale_reviews: true
require_code_owner_reviews: true
require_last_push_approval: true
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ The comprehensive Value vs. Effort Prioritization Matrix, phased milestone deliv
| | `devops gh pages COMMAND [ARGS]...` | GitHub Views, Projects, Issues, Pages, Milestones, and Labels automation. |
| | `devops gh issues COMMAND [ARGS]...` | GitHub Views, Projects, Issues, Pages, Milestones, and Labels automation. |
| | `devops gh runs COMMAND [ARGS]...` | GitHub Views, Projects, Issues, Pages, Milestones, and Labels automation. |
| | `devops gh branch-protection COMMAND [ARGS]...` | GitHub Views, Projects, Issues, Pages, Milestones, and Labels automation. |
| | `devops gh secrets COMMAND [ARGS]...` | GitHub Views, Projects, Issues, Pages, Milestones, and Labels automation. |
| | `devops gh pr COMMAND [ARGS]...` | GitHub Views, Projects, Issues, Pages, Milestones, and Labels automation. |
| **tf** | `devops tf init [OPTIONS] <directory>` | Initialize an OpenTofu working directory. |
| | `devops tf plan [OPTIONS] <directory>` | Generate and show an OpenTofu execution plan. |
Expand Down
78 changes: 78 additions & 0 deletions docs/CLI_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4988,6 +4988,84 @@ devops gh runs view [OPTIONS] <run_id>
| `--job`, `-j` | `string` | - | Filter workflow run logs to a specific job ID. |
| `--repo`, `-R` | `string` | - | Target repository |

### `devops gh branch-protection`

```bash
devops gh branch-protection COMMAND [ARGS]...
```

#### `devops gh branch-protection audit`

**Audit repository branch protection rulesets against declarative policy specification.**

```bash
devops gh branch-protection audit [OPTIONS]
```

**Options:**

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--repo`, `-R` | `string` | - | Target repository |
| `--branch`, `-b` | `string` | - | Target specific branch for protection audit or synchronization. |
| `--policy-file`, `-f` | `path` | `.github/branch-protection.yml` | Path to declarative branch protection policy YAML file. |
| `--json` | `boolean` | - | Output findings or metrics as JSON. |

#### `devops gh branch-protection sync`

**Synchronize repository branch protection rulesets against declarative policy specification.**

```bash
devops gh branch-protection sync [OPTIONS]
```

**Options:**

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--repo`, `-R` | `string` | - | Target repository |
| `--branch`, `-b` | `string` | - | Target specific branch for protection audit or synchronization. |
| `--policy-file`, `-f` | `path` | `.github/branch-protection.yml` | Path to declarative branch protection policy YAML file. |
| `--dry-run` | `boolean` | - | Preview branch protection synchronization without applying mutations |

### `devops gh secrets`

```bash
devops gh secrets COMMAND [ARGS]...
```

#### `devops gh secrets sync`

**Synchronize repository secrets from OS Keyring or HashiCorp Vault with libsodium sealing.**

```bash
devops gh secrets sync [OPTIONS]
```

**Options:**

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--secret-names`, `-n` | `string` | - | Comma-separated list of secret names to synchronize. |
| `--repo`, `-R` | `string` | - | Target repository |
| `--source`, `-s` | `string` | `keyring` | Source store for secrets to synchronize (keyring or vault). |
| `--vault-path` | `string` | `secret/devops` | Vault KV-v2 secret path when source is vault (default: secret/devops). |
| `--dry-run` | `boolean` | - | Preview secret synchronization without mutations |

#### `devops gh secrets list`

**List Actions secrets configured in the repository (names only, values are hidden).**

```bash
devops gh secrets list [OPTIONS]
```

**Options:**

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--repo`, `-R` | `string` | - | Target repository |

### `devops gh pr`

```bash
Expand Down
68 changes: 68 additions & 0 deletions docs/agent/tasks/task-114-branch-protection-secrets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Task 114: Declarative Branch Protection Auditor & Repository Secret Synchronization

**Issue**: [#114](https://github.com/dan-petty/devops-cli/issues/114)
**PR**: [#197](https://github.com/dan-petty/devops-cli/pull/197)
**Status**: In Review
Comment on lines +4 to +5
**Milestone**: `v0.2.17`
**Priority**: `priority/p2-medium`
**Scope**: `scope/github`

---

## 1. Description & Architectural Objectives

Implement declarative branch protection policy enforcement and libsodium-encrypted repository secret synchronization:
1. **Declarative Branch Protection Policies (`devops gh branch-protection audit|sync`)**:
- Define declarative branch protection specifications in YAML (`.github/branch-protection.yml`).
- Audit existing branch protection rulesets against desired policies, reporting compliance and drift with Rich formatters.
- Synchronize branch protection rulesets via GitHub REST API, supporting `--dry-run` and per-branch targeting.
2. **Libsodium-Sealed Repository Secrets (`devops gh secrets sync`)**:
- Retrieve secrets from OS Keyring or HashiCorp Vault.
- Encrypt secret values using libsodium sealed-box public key encryption (`PyNaCl`).
- Synchronize encrypted secrets to GitHub Actions repository secrets via REST API (`PUT /repos/{owner}/{repo}/actions/secrets/{secret_name}`).
- Support `--dry-run`, ensuring zero leakage of secret values in logs, outputs, or error details.
3. **Engineering Standards & Architectural Invariants**:
- Strict cyclomatic complexity $\le 10$ and maximum nesting depth $\le 5$ project-wide.
- Comprehensive unit and integration test coverage $\ge 90\%$.
- Bounded string truncation ($\le 256$ chars) on all error details.

---

## 2. Planned Changes

1. **`.github/branch-protection.yml`**:
- Declarative branch protection policy specification for `main` and `release/*`.
2. **`src/devops_cli/github/branch_protection.py`**:
- Models: `BranchProtectionPolicy`, `StatusChecksPolicy`, `ReviewsPolicy`, `BranchProtectionAuditResult`, `BranchProtectionSyncResult`.
- Loader: `load_branch_protection_policies(path: Path) -> list[BranchProtectionPolicy]`.
- Engine: `audit_branch_protection(...)` and `sync_branch_protection(...)`.
3. **`src/devops_cli/github/secrets.py`**:
- Models: `GitHubPublicKey`, `SecretSyncItem`, `SecretSyncResult`.
- Encryption: `encrypt_secret(public_key_b64: str, secret_value: str) -> str` using `nacl.public.SealedBox`.
- Fetchers: `get_repo_public_key(...)`, `resolve_secret_value(name: str, source: str, ...) -> str | None`.
- Engine: `sync_repository_secrets(...)`.
4. **`src/devops_cli/github/__init__.py`**:
- Export new symbols and models.
5. **`src/devops_cli/commands/gh.py`**:
- Add `branch-protection` subcommand group (`audit`, `sync`).
- Add `secrets` subcommand group (`sync`).
6. **`src/devops_cli/lang/en/help.py`**:
- Add command help strings to `GHCommandHelp`.
7. **`tests/test_github_branch_protection.py` & `tests/test_github_secrets.py`**:
- Comprehensive test suites with $\ge 90\%$ code coverage.

---

## 3. Progress Tracking

- [x] Ground issue in GitHub tracking (#114) with `status/in-progress`.
- [x] Author task tracking file `docs/agent/tasks/task-114-branch-protection-secrets.md`.
- [x] Checkout dedicated topic branch `feat/114-branch-protection-secrets`.
- [x] Author declarative policy template `.github/branch-protection.yml`.
- [x] Implement `src/devops_cli/github/branch_protection.py`.
- [x] Implement `src/devops_cli/github/secrets.py`.
- [x] Register CLI subcommands in `src/devops_cli/commands/gh.py` and update `help.py`.
- [x] Author unit tests in `tests/test_github_branch_protection.py` and `tests/test_github_secrets.py`.
- [x] Run quality gates (`devops scan complexity`, `ruff`, `mypy`, `pytest`, `devops docs generate --check`).
- [x] Commit changes, push branch, and open Draft Pull Request to `release/v0.2.17` ([#197](https://github.com/dan-petty/devops-cli/pull/197)).
- [ ] Monitor CI checks, transition PR to ready, address review threads, and squash-merge into `release/v0.2.17`.
82 changes: 82 additions & 0 deletions docs/commands/gh.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,88 @@ devops gh runs view [OPTIONS] <run_id>

---

## `devops gh branch-protection`

```bash
devops gh branch-protection COMMAND [ARGS]...
```

### `devops gh branch-protection audit`

**Audit repository branch protection rulesets against declarative policy specification.**

```bash
devops gh branch-protection audit [OPTIONS]
```

**Options:**

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--repo`, `-R` | `string` | - | Target repository |
| `--branch`, `-b` | `string` | - | Target specific branch for protection audit or synchronization. |
| `--policy-file`, `-f` | `path` | `.github/branch-protection.yml` | Path to declarative branch protection policy YAML file. |
| `--json` | `boolean` | - | Output findings or metrics as JSON. |

### `devops gh branch-protection sync`

**Synchronize repository branch protection rulesets against declarative policy specification.**

```bash
devops gh branch-protection sync [OPTIONS]
```

**Options:**

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--repo`, `-R` | `string` | - | Target repository |
| `--branch`, `-b` | `string` | - | Target specific branch for protection audit or synchronization. |
| `--policy-file`, `-f` | `path` | `.github/branch-protection.yml` | Path to declarative branch protection policy YAML file. |
| `--dry-run` | `boolean` | - | Preview branch protection synchronization without applying mutations |

---

## `devops gh secrets`

```bash
devops gh secrets COMMAND [ARGS]...
```

### `devops gh secrets sync`

**Synchronize repository secrets from OS Keyring or HashiCorp Vault with libsodium sealing.**

```bash
devops gh secrets sync [OPTIONS]
```

**Options:**

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--secret-names`, `-n` | `string` | - | Comma-separated list of secret names to synchronize. |
| `--repo`, `-R` | `string` | - | Target repository |
| `--source`, `-s` | `string` | `keyring` | Source store for secrets to synchronize (keyring or vault). |
| `--vault-path` | `string` | `secret/devops` | Vault KV-v2 secret path when source is vault (default: secret/devops). |
| `--dry-run` | `boolean` | - | Preview secret synchronization without mutations |

### `devops gh secrets list`

**List Actions secrets configured in the repository (names only, values are hidden).**

```bash
devops gh secrets list [OPTIONS]
```

**Options:**

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--repo`, `-R` | `string` | - | Target repository |

---

## `devops gh pr`

```bash
Expand Down
Loading
Loading