|
| 1 | +# Configuration Setup Guide |
| 2 | + |
| 3 | +## The Issue You Encountered |
| 4 | + |
| 5 | +``` |
| 6 | +[ERROR] failed to load config | {"error":"failed to retrieve config file: |
| 7 | +failed to get config file: GET https://api.github.com/repos/mongodb/docs-code-examples/contents/config.yaml?ref=main: 404 Not Found []"} |
| 8 | +``` |
| 9 | + |
| 10 | +**Cause:** The app tries to fetch the config file from your **source repository** on GitHub, but it doesn't exist there yet. |
| 11 | + |
| 12 | +## Solutions |
| 13 | + |
| 14 | +### Option 1: Local Config File (For Testing - EASIEST) |
| 15 | + |
| 16 | +The app now supports loading config from a **local file** for testing purposes. |
| 17 | + |
| 18 | +**Steps:** |
| 19 | + |
| 20 | +1. **Config file already created** - `config.yaml` exists in the `examples-copier/` directory |
| 21 | + |
| 22 | +2. **Run the app** - It will automatically use the local file: |
| 23 | + ```bash |
| 24 | + make run-local-quick |
| 25 | + ``` |
| 26 | + |
| 27 | +3. **Test it:** |
| 28 | + ```bash |
| 29 | + # In another terminal |
| 30 | + ./test-webhook -payload test-payloads/example-pr-merged.json |
| 31 | + ``` |
| 32 | + |
| 33 | +**How it works:** |
| 34 | +- App first tries to load `config.yaml` from the current directory |
| 35 | +- If found, uses it (logs: "loaded config from local file") |
| 36 | +- If not found, falls back to fetching from GitHub |
| 37 | + |
| 38 | +### Option 2: Add Config to Source Repository (For Production) |
| 39 | + |
| 40 | +For production use, the config should be in your source repository. |
| 41 | + |
| 42 | +**Steps:** |
| 43 | + |
| 44 | +1. **Find your source repository:** |
| 45 | + ```bash |
| 46 | + # From the error, your source repo is: |
| 47 | + # mongodb/docs-code-examples |
| 48 | + ``` |
| 49 | + |
| 50 | +2. **Copy the config file:** |
| 51 | + ```bash |
| 52 | + # Copy config.yaml to your source repo |
| 53 | + cp examples-copier/config.yaml /path/to/docs-code-examples/config.yaml |
| 54 | + ``` |
| 55 | + |
| 56 | +3. **Customize it** for your needs: |
| 57 | + ```yaml |
| 58 | + source_repo: "mongodb/docs-code-examples" # Your source repo |
| 59 | + source_branch: "main" |
| 60 | + |
| 61 | + copy_rules: |
| 62 | + - name: "Copy examples" |
| 63 | + source_pattern: |
| 64 | + type: "prefix" |
| 65 | + pattern: "examples/" # Adjust to your file structure |
| 66 | + targets: |
| 67 | + - repo: "mongodb/your-target-repo" # Where to copy files |
| 68 | + branch: "main" |
| 69 | + path_transform: "docs/${path}" |
| 70 | + ``` |
| 71 | +
|
| 72 | +4. **Commit and push:** |
| 73 | + ```bash |
| 74 | + cd /path/to/docs-code-examples |
| 75 | + git add config.yaml |
| 76 | + git commit -m "Add examples-copier configuration" |
| 77 | + git push origin main |
| 78 | + ``` |
| 79 | + |
| 80 | +5. **Deploy the app** - It will now fetch config from GitHub |
| 81 | + |
| 82 | +## Configuration File Formats |
| 83 | + |
| 84 | +### YAML Format (Recommended) |
| 85 | + |
| 86 | +**File:** `config.yaml` |
| 87 | + |
| 88 | +```yaml |
| 89 | +source_repo: "mongodb/docs-code-examples" |
| 90 | +source_branch: "main" |
| 91 | + |
| 92 | +copy_rules: |
| 93 | + - name: "Copy Go examples" |
| 94 | + source_pattern: |
| 95 | + type: "regex" |
| 96 | + pattern: "^examples/(?P<lang>go)/(?P<category>[^/]+)/(?P<file>.+)$" |
| 97 | + targets: |
| 98 | + - repo: "mongodb/target-repo" |
| 99 | + branch: "main" |
| 100 | + path_transform: "docs/code-examples/${lang}/${category}/${file}" |
| 101 | + commit_strategy: |
| 102 | + type: "pull_request" |
| 103 | + commit_message: "Update ${category} examples" |
| 104 | + pr_title: "Update ${category} examples" |
| 105 | + auto_merge: false |
| 106 | +``` |
| 107 | +
|
| 108 | +### JSON Format (Legacy) |
| 109 | +
|
| 110 | +**File:** `config.json` |
| 111 | + |
| 112 | +```json |
| 113 | +[ |
| 114 | + { |
| 115 | + "source_directory": "examples", |
| 116 | + "target_repo": "mongodb/target-repo", |
| 117 | + "target_branch": "main", |
| 118 | + "target_directory": "docs/code-examples", |
| 119 | + "recursive_copy": true, |
| 120 | + "copier_commit_strategy": "pr", |
| 121 | + "pr_title": "Update code examples", |
| 122 | + "commit_message": "Sync examples", |
| 123 | + "merge_without_review": false |
| 124 | + } |
| 125 | +] |
| 126 | +``` |
| 127 | + |
| 128 | +## Customizing Your Config |
| 129 | + |
| 130 | +### 1. Set Your Repositories |
| 131 | + |
| 132 | +```yaml |
| 133 | +source_repo: "your-org/your-source-repo" # Where examples come from |
| 134 | +
|
| 135 | +targets: |
| 136 | + - repo: "your-org/your-target-repo" # Where to copy them |
| 137 | +``` |
| 138 | + |
| 139 | +### 2. Define Patterns |
| 140 | + |
| 141 | +**Match all files in a directory:** |
| 142 | +```yaml |
| 143 | +source_pattern: |
| 144 | + type: "prefix" |
| 145 | + pattern: "examples/" |
| 146 | +``` |
| 147 | + |
| 148 | +**Match specific file types:** |
| 149 | +```yaml |
| 150 | +source_pattern: |
| 151 | + type: "regex" |
| 152 | + pattern: "^examples/.*\\.go$" # Only .go files |
| 153 | +``` |
| 154 | + |
| 155 | +**Extract variables from paths:** |
| 156 | +```yaml |
| 157 | +source_pattern: |
| 158 | + type: "regex" |
| 159 | + pattern: "^examples/(?P<lang>[^/]+)/(?P<category>[^/]+)/(?P<file>.+)$" |
| 160 | + # Extracts: lang, category, file |
| 161 | +``` |
| 162 | + |
| 163 | +### 3. Transform Paths |
| 164 | + |
| 165 | +```yaml |
| 166 | +# Keep same structure |
| 167 | +path_transform: "${path}" |
| 168 | +
|
| 169 | +# Add prefix |
| 170 | +path_transform: "docs/${path}" |
| 171 | +
|
| 172 | +# Reorganize with variables |
| 173 | +path_transform: "docs/${lang}/${category}/${file}" |
| 174 | +``` |
| 175 | + |
| 176 | +### 4. Set Commit Strategy |
| 177 | + |
| 178 | +**Direct commit:** |
| 179 | +```yaml |
| 180 | +commit_strategy: |
| 181 | + type: "direct" |
| 182 | + commit_message: "Update examples" |
| 183 | +``` |
| 184 | + |
| 185 | +**Pull request:** |
| 186 | +```yaml |
| 187 | +commit_strategy: |
| 188 | + type: "pull_request" |
| 189 | + commit_message: "Update examples" |
| 190 | + pr_title: "Update ${category} examples" |
| 191 | + pr_body: "Automated update" |
| 192 | + auto_merge: false |
| 193 | +``` |
| 194 | + |
| 195 | +## Validating Your Config |
| 196 | + |
| 197 | +Before using, validate your configuration: |
| 198 | + |
| 199 | +```bash |
| 200 | +# Validate syntax and structure |
| 201 | +./config-validator validate -config config.yaml -v |
| 202 | +
|
| 203 | +# Test pattern matching |
| 204 | +./config-validator test-pattern \ |
| 205 | + -type regex \ |
| 206 | + -pattern "^examples/(?P<lang>[^/]+)/.*$" \ |
| 207 | + -file "examples/go/main.go" |
| 208 | +
|
| 209 | +# Test path transformation |
| 210 | +./config-validator test-transform \ |
| 211 | + -template "docs/${lang}/${file}" \ |
| 212 | + -file "examples/go/main.go" \ |
| 213 | + -pattern "^examples/(?P<lang>[^/]+)/(?P<file>.+)$" |
| 214 | +``` |
| 215 | + |
| 216 | +## Testing Your Config |
| 217 | + |
| 218 | +### 1. Test Locally |
| 219 | + |
| 220 | +```bash |
| 221 | +# Terminal 1: Start app with local config |
| 222 | +make run-local-quick |
| 223 | +
|
| 224 | +# Terminal 2: Send test webhook |
| 225 | +./test-webhook -payload test-payloads/example-pr-merged.json |
| 226 | +``` |
| 227 | + |
| 228 | +### 2. Check Logs |
| 229 | + |
| 230 | +Look for: |
| 231 | +``` |
| 232 | +[INFO] loaded config from local file | {"file":"config.yaml"} |
| 233 | +[INFO] Loaded YAML configuration with 3 copy rules |
| 234 | +[INFO] Pattern matched: examples/go/database/connect.go |
| 235 | +[INFO] → Transformed to: docs/code-examples/go/database/connect.go |
| 236 | +``` |
| 237 | + |
| 238 | +### 3. Verify Metrics |
| 239 | + |
| 240 | +```bash |
| 241 | +curl http://localhost:8080/metrics | jq |
| 242 | +``` |
| 243 | + |
| 244 | +## Environment Variables |
| 245 | + |
| 246 | +```bash |
| 247 | +# Specify config file name (default: config.json) |
| 248 | +CONFIG_FILE=config.yaml |
| 249 | +
|
| 250 | +# For local testing |
| 251 | +COPIER_DISABLE_CLOUD_LOGGING=true |
| 252 | +DRY_RUN=true |
| 253 | +``` |
| 254 | + |
| 255 | +## Troubleshooting |
| 256 | + |
| 257 | +### Error: "config file is empty" |
| 258 | +**Solution:** Make sure config.yaml has content |
| 259 | + |
| 260 | +### Error: "config validation failed" |
| 261 | +**Solution:** Run `./config-validator validate -config config.yaml -v` |
| 262 | + |
| 263 | +### Error: "pattern doesn't match" |
| 264 | +**Solution:** Test pattern with `./config-validator test-pattern` |
| 265 | + |
| 266 | +### Files not being copied |
| 267 | +**Solution:** Check that: |
| 268 | +1. Pattern matches the file paths |
| 269 | +2. Target repo is correct |
| 270 | +3. Commit strategy is set |
| 271 | + |
| 272 | +## Quick Start Checklist |
| 273 | + |
| 274 | +- [ ] Config file created (`config.yaml` or `config.json`) |
| 275 | +- [ ] Repositories set correctly (source and target) |
| 276 | +- [ ] Patterns match your file structure |
| 277 | +- [ ] Path transformations tested |
| 278 | +- [ ] Config validated with `config-validator` |
| 279 | +- [ ] Tested locally with dry-run mode |
| 280 | +- [ ] Ready to deploy! |
| 281 | + |
| 282 | +## Next Steps |
| 283 | + |
| 284 | +1. **Test locally** with the provided config |
| 285 | +2. **Customize** patterns and transformations for your needs |
| 286 | +3. **Validate** with config-validator |
| 287 | +4. **Test** with real PR data |
| 288 | +5. **Deploy** to production |
| 289 | +6. **Add config to source repo** for production use |
| 290 | + |
| 291 | +See [LOCAL-TESTING.md](LOCAL-TESTING.md) for complete testing guide. |
| 292 | + |
0 commit comments