|
| 1 | +# Organization Automation |
| 2 | + |
| 3 | +This repository practices what it preaches: **democratic infrastructure management through pull requests**. The worlddriven organization is managed entirely through code, with REPOSITORIES.md as the source of truth. |
| 4 | + |
| 5 | +## Purpose |
| 6 | + |
| 7 | +As described in our vision for [Phase 2: Transparent Service Management](README.md#vision-three-phases), infrastructure decisions should be made democratically, just like code. This automation system enables: |
| 8 | + |
| 9 | +- **Democratic control**: Any contributor can propose organization changes via pull requests |
| 10 | +- **Transparency**: All infrastructure changes are visible in git history |
| 11 | +- **Consistency**: Standard configurations are automatically enforced |
| 12 | +- **Accountability**: Changes are tied to commits and contributors |
| 13 | + |
| 14 | +## How It Works |
| 15 | + |
| 16 | +### Source of Truth |
| 17 | + |
| 18 | +**[REPOSITORIES.md](REPOSITORIES.md)** defines all repositories in the worlddriven organization. When this file changes, automation ensures GitHub matches the desired state. |
| 19 | + |
| 20 | +```markdown |
| 21 | +## repository-name |
| 22 | +- Description: Brief description of the repository |
| 23 | +- Topics: topic1, topic2, topic3 |
| 24 | +``` |
| 25 | + |
| 26 | +### Automation Workflow |
| 27 | + |
| 28 | +**1. Pull Request Phase** (Drift Detection) |
| 29 | +- You modify REPOSITORIES.md and create a PR |
| 30 | +- GitHub Actions automatically runs drift detection |
| 31 | +- A comment appears showing exactly what will change: |
| 32 | + - Repositories to be created |
| 33 | + - Descriptions/topics to be updated |
| 34 | + - Repositories to be deleted (with protected repo warnings) |
| 35 | + - Settings to be standardized |
| 36 | + |
| 37 | +**2. Review Phase** (Democratic Decision) |
| 38 | +- Contributors review the proposed changes |
| 39 | +- Discussion happens in the PR |
| 40 | +- Worlddriven's voting system determines merge timing |
| 41 | + |
| 42 | +**3. Merge Phase** (Automatic Sync) |
| 43 | +- PR merges to main |
| 44 | +- Sync workflow executes with APPLY mode |
| 45 | +- Changes are applied to GitHub organization: |
| 46 | + - Creates new repositories |
| 47 | + - Updates existing repositories |
| 48 | + - Deletes repositories not in REPOSITORIES.md |
| 49 | + - Applies standard configurations |
| 50 | + - Sets up branch protection |
| 51 | +- Commit comment shows sync results |
| 52 | +- Issue created if any failures occur |
| 53 | + |
| 54 | +## Standard Configurations |
| 55 | + |
| 56 | +All worlddriven repositories automatically receive these settings to support democratic governance: |
| 57 | + |
| 58 | +### Repository Settings |
| 59 | +- **Merge strategy**: Squash only (1 PR = 1 commit for fair voting) |
| 60 | +- **Auto-merge**: Disabled (worlddriven controls merging) |
| 61 | +- **Branch cleanup**: Delete branches after merge |
| 62 | +- **Update branch**: Disabled |
| 63 | + |
| 64 | +### Branch Protection Ruleset: "Worlddriven Democratic Governance" |
| 65 | +Applied to default branch (usually `main`): |
| 66 | +- **Pull request required**: All changes must go through PR workflow |
| 67 | +- **No force push**: Prevents history rewriting |
| 68 | +- **No branch deletion**: Protects main branch |
| 69 | +- **Allowed merge method**: Squash only |
| 70 | +- **No bypass actors**: True democracy applies to everyone |
| 71 | + |
| 72 | +### Repository Initialization |
| 73 | +New repositories are created with: |
| 74 | +- Public visibility |
| 75 | +- Initial README.md file (created via auto_init) |
| 76 | +- Issues, Projects, and Wiki enabled |
| 77 | +- Standard settings pre-applied |
| 78 | +- Branch protection ruleset active |
| 79 | + |
| 80 | +## Protected Repositories |
| 81 | + |
| 82 | +Three infrastructure repositories are protected from automatic deletion: |
| 83 | + |
| 84 | +- **documentation** - This repository (organization management) |
| 85 | +- **core** - The worlddriven application server |
| 86 | +- **webapp** - The worlddriven web interface |
| 87 | + |
| 88 | +These repositories can still be updated (descriptions, topics, settings) but won't be deleted if removed from REPOSITORIES.md. This prevents accidental deletion of critical infrastructure. |
| 89 | + |
| 90 | +## GitHub Actions Workflows |
| 91 | + |
| 92 | +### 1. Drift Detection (`.github/workflows/drift-detection.yml`) |
| 93 | + |
| 94 | +**Triggers**: PRs that modify REPOSITORIES.md, scripts, or workflows |
| 95 | + |
| 96 | +**Purpose**: Show what will happen before changes merge |
| 97 | + |
| 98 | +**Actions**: |
| 99 | +- Parses REPOSITORIES.md |
| 100 | +- Fetches current GitHub organization state |
| 101 | +- Compares desired vs actual state |
| 102 | +- Runs sync in dry-run mode (no changes made) |
| 103 | +- Comments on PR with drift report and sync preview |
| 104 | +- Updates existing comment instead of creating duplicates |
| 105 | + |
| 106 | +**Example Output**: |
| 107 | +``` |
| 108 | +# Drift Detection Report |
| 109 | +
|
| 110 | +Missing (in REPOSITORIES.md but not GitHub): |
| 111 | +- new-project |
| 112 | +
|
| 113 | +Extra (in GitHub but not REPOSITORIES.md): |
| 114 | +- old-experiment |
| 115 | +
|
| 116 | +Description Drift: |
| 117 | +- existing-repo: "Old description" → "Updated description" |
| 118 | +
|
| 119 | +# Dry-Run Sync Preview |
| 120 | +
|
| 121 | +Would create: |
| 122 | +- new-project (Description: A new worlddriven project) |
| 123 | +
|
| 124 | +Would delete: |
| 125 | +- old-experiment (unprotected repository) |
| 126 | +``` |
| 127 | + |
| 128 | +### 2. Repository Sync (`.github/workflows/sync-repositories.yml`) |
| 129 | + |
| 130 | +**Triggers**: Push to main branch |
| 131 | + |
| 132 | +**Purpose**: Apply infrastructure changes to GitHub |
| 133 | + |
| 134 | +**Permissions**: |
| 135 | +- Runs only on worlddriven organization (not forks) |
| 136 | +- Requires WORLDDRIVEN_GITHUB_TOKEN secret |
| 137 | + |
| 138 | +**Actions**: |
| 139 | +- Executes sync-repositories.js with --apply flag |
| 140 | +- Creates/updates/deletes repositories |
| 141 | +- Applies standard configurations |
| 142 | +- Posts commit comment with results |
| 143 | +- Creates issue if failures occur |
| 144 | +- Adds report to workflow summary |
| 145 | + |
| 146 | +**Example Output**: |
| 147 | +``` |
| 148 | +✅ APPLY Repository Sync Report |
| 149 | +
|
| 150 | +Summary: 3 total actions |
| 151 | +- Create: 1 |
| 152 | +- Update descriptions: 1 |
| 153 | +- Delete: 1 |
| 154 | +
|
| 155 | +✅ Successfully Applied (3) |
| 156 | +- Create new-project |
| 157 | +- Update description for existing-repo |
| 158 | +- Delete old-experiment |
| 159 | +``` |
| 160 | + |
| 161 | +### 3. Test Suite (`.github/workflows/test.yml`) |
| 162 | + |
| 163 | +**Triggers**: All PRs |
| 164 | + |
| 165 | +**Purpose**: Validate automation scripts work correctly |
| 166 | + |
| 167 | +**Actions**: |
| 168 | +- Runs unit tests for parse-repositories.js |
| 169 | +- Validates REPOSITORIES.md syntax |
| 170 | +- Ensures scripts can execute |
| 171 | + |
| 172 | +## Scripts Reference |
| 173 | + |
| 174 | +### `scripts/sync-repositories.js` |
| 175 | + |
| 176 | +Main synchronization engine that applies changes to GitHub. |
| 177 | + |
| 178 | +**Usage**: |
| 179 | +```bash |
| 180 | +# Dry-run (show what would happen) |
| 181 | +WORLDDRIVEN_GITHUB_TOKEN=xxx node scripts/sync-repositories.js |
| 182 | + |
| 183 | +# Apply changes |
| 184 | +WORLDDRIVEN_GITHUB_TOKEN=xxx node scripts/sync-repositories.js --apply |
| 185 | +``` |
| 186 | + |
| 187 | +**Capabilities**: |
| 188 | +- Create repositories with standard settings |
| 189 | +- Update repository descriptions and topics |
| 190 | +- Delete repositories (except protected ones) |
| 191 | +- Apply/enforce standard repository settings |
| 192 | +- Create branch protection rulesets |
| 193 | +- Initialize empty repositories |
| 194 | + |
| 195 | +### `scripts/detect-drift.js` |
| 196 | + |
| 197 | +Compares desired state (REPOSITORIES.md) with actual state (GitHub). |
| 198 | + |
| 199 | +**Detects**: |
| 200 | +- Missing repositories |
| 201 | +- Extra repositories |
| 202 | +- Description differences |
| 203 | +- Topic differences |
| 204 | + |
| 205 | +### `scripts/parse-repositories.js` |
| 206 | + |
| 207 | +Parses REPOSITORIES.md markdown format. |
| 208 | + |
| 209 | +**Features**: |
| 210 | +- Extracts repository definitions |
| 211 | +- Validates required fields (name, description) |
| 212 | +- Skips code blocks and examples |
| 213 | +- Handles optional topics |
| 214 | + |
| 215 | +### `scripts/fetch-github-state.js` |
| 216 | + |
| 217 | +Fetches current GitHub organization repository state via API. |
| 218 | + |
| 219 | +**Returns**: |
| 220 | +- Repository names |
| 221 | +- Descriptions |
| 222 | +- Topics |
| 223 | +- Handles pagination |
| 224 | + |
| 225 | +## Security Model |
| 226 | + |
| 227 | +### Authentication |
| 228 | + |
| 229 | +The sync automation uses a GitHub Personal Access Token stored as `WORLDDRIVEN_GITHUB_TOKEN` in repository secrets. |
| 230 | + |
| 231 | +**Required permissions**: |
| 232 | +- `repo` - Full control of repositories |
| 233 | +- `admin:org` - Manage organization settings |
| 234 | +- `delete_repo` - Delete repositories |
| 235 | + |
| 236 | +### Permissions |
| 237 | + |
| 238 | +- **Forks**: Workflows are restricted to worlddriven organization only |
| 239 | +- **Tests**: Test workflow runs on all repos including forks (no secrets needed) |
| 240 | +- **Secrets**: Organization secrets are not accessible to fork PRs |
| 241 | + |
| 242 | +### Safety Features |
| 243 | + |
| 244 | +- **Protected repositories**: Cannot be auto-deleted |
| 245 | +- **Dry-run preview**: See changes before merge |
| 246 | +- **Atomic operations**: Each action is independent |
| 247 | +- **Failure isolation**: One failure doesn't stop other operations |
| 248 | +- **Audit trail**: All changes visible in git history and workflow logs |
| 249 | + |
| 250 | +## Examples |
| 251 | + |
| 252 | +### Adding a New Repository |
| 253 | + |
| 254 | +1. Create a PR modifying REPOSITORIES.md: |
| 255 | +```markdown |
| 256 | +## my-new-project |
| 257 | +- Description: An experimental worlddriven integration |
| 258 | +- Topics: experiment, integration, worlddriven |
| 259 | +``` |
| 260 | + |
| 261 | +2. Drift detection comments on your PR showing it will create the repository |
| 262 | + |
| 263 | +3. After PR merges, sync workflow: |
| 264 | + - Creates `worlddriven/my-new-project` |
| 265 | + - Sets description and topics |
| 266 | + - Applies standard settings |
| 267 | + - Creates branch protection ruleset |
| 268 | + - Initializes with README.md |
| 269 | + |
| 270 | +### Updating Repository Metadata |
| 271 | + |
| 272 | +1. Edit description in REPOSITORIES.md: |
| 273 | +```markdown |
| 274 | +## my-new-project |
| 275 | +- Description: Production-ready worlddriven integration (updated!) |
| 276 | +- Topics: integration, worlddriven, production |
| 277 | +``` |
| 278 | + |
| 279 | +2. Drift detection shows description and topic changes |
| 280 | + |
| 281 | +3. After merge, sync workflow updates GitHub metadata |
| 282 | + |
| 283 | +### Removing a Repository |
| 284 | + |
| 285 | +1. Remove repository section from REPOSITORIES.md |
| 286 | + |
| 287 | +2. Drift detection warns: |
| 288 | + - If protected: "Would skip - protected repository" |
| 289 | + - If unprotected: "Would delete repository-name" |
| 290 | + |
| 291 | +3. After merge: |
| 292 | + - Protected repos: Skipped with warning in logs |
| 293 | + - Unprotected repos: Deleted from GitHub |
| 294 | + |
| 295 | +**Important**: Repository deletion is permanent. Ensure you have backups. |
| 296 | + |
| 297 | +## Troubleshooting |
| 298 | + |
| 299 | +### Sync Failures |
| 300 | + |
| 301 | +If sync fails, an issue is automatically created with: |
| 302 | +- Error details |
| 303 | +- Which actions failed |
| 304 | +- Commit SHA that triggered the failure |
| 305 | + |
| 306 | +Common failures: |
| 307 | +- **403 errors**: Token permissions issue |
| 308 | +- **409 errors**: Conflicts (e.g., repository already exists) |
| 309 | +- **Rate limits**: Too many API calls (wait and retry) |
| 310 | + |
| 311 | +### Manual Intervention |
| 312 | + |
| 313 | +If automation fails and manual fixes are needed: |
| 314 | + |
| 315 | +1. Fix the issue in GitHub directly |
| 316 | +2. Update REPOSITORIES.md to match actual state |
| 317 | +3. Next sync will detect no drift |
| 318 | + |
| 319 | +### Testing Changes |
| 320 | + |
| 321 | +To test automation changes locally: |
| 322 | + |
| 323 | +```bash |
| 324 | +# Install dependencies |
| 325 | +npm install |
| 326 | + |
| 327 | +# Run tests |
| 328 | +npm test |
| 329 | + |
| 330 | +# Dry-run sync (requires token) |
| 331 | +export WORLDDRIVEN_GITHUB_TOKEN=your_token |
| 332 | +node scripts/sync-repositories.js |
| 333 | +``` |
| 334 | + |
| 335 | +## Contributing |
| 336 | + |
| 337 | +See [CONTRIBUTING.md](CONTRIBUTING.md) for details on: |
| 338 | +- How to propose organization changes |
| 339 | +- Automation script development |
| 340 | +- Testing guidelines |
| 341 | +- Security considerations |
| 342 | + |
| 343 | +## Philosophy |
| 344 | + |
| 345 | +This automation embodies worlddriven's core principle: **those who contribute should govern**. By managing infrastructure through pull requests, we ensure: |
| 346 | + |
| 347 | +- **Transparency**: All decisions visible in git history |
| 348 | +- **Democracy**: Anyone can propose changes |
| 349 | +- **Meritocracy**: Contribution weight determines influence |
| 350 | +- **Accountability**: Changes are attributed and reversible |
| 351 | + |
| 352 | +The automation removes gatekeepers while maintaining safety through standard configurations and protected repositories. |
0 commit comments