MCPcopy Create free account
hub / github.com/github/gh-aw / ensureGHESRepoConfig

Function ensureGHESRepoConfig

pkg/cli/init.go:376–440  ·  view source on GitHub ↗

ensureGHESRepoConfig writes or updates .github/workflows/aw.json to set "ghes": true when running on a GHES deployment. The function is a no-op if GHES is not detected or if "ghes": true is already present. Returns (updated bool, err).

(verbose bool)

Source from the content-addressed store, hash-verified

374// if GHES is not detected or if "ghes": true is already present.
375// Returns (updated bool, err).
376func ensureGHESRepoConfig(verbose bool) (bool, error) {
377 ghesHost := detectGHESDeployment()
378 if ghesHost == "" {
379 initLog.Print("No GHES deployment detected, skipping aw.json ghes configuration")
380 return false, nil
381 }
382
383 initLog.Printf("GHES deployment detected (%s): configuring aw.json ghes: true", ghesHost)
384
385 gitRoot, err := gitutil.FindGitRoot()
386 if err != nil {
387 return false, fmt.Errorf("failed to find git root: %w", err)
388 }
389
390 configPath := filepath.Join(gitRoot, workflow.RepoConfigFileName)
391
392 // Read existing content or start with an empty document.
393 var doc map[string]any
394 if data, readErr := os.ReadFile(configPath); readErr == nil {
395 if jsonErr := json.Unmarshal(data, &doc); jsonErr != nil {
396 return false, fmt.Errorf("failed to parse %s: %w", workflow.RepoConfigFileName, jsonErr)
397 }
398 } else if !errors.Is(readErr, os.ErrNotExist) {
399 return false, fmt.Errorf("failed to read %s: %w", workflow.RepoConfigFileName, readErr)
400 }
401
402 if doc == nil {
403 doc = make(map[string]any)
404 }
405
406 // Nothing to do if ghes is already true.
407 if existing, ok := doc["ghes"].(bool); ok && existing {
408 initLog.Print("aw.json already has ghes: true, nothing to update")
409 return false, nil
410 }
411
412 doc["ghes"] = true
413
414 data, err := json.MarshalIndent(doc, "", " ")
415 if err != nil {
416 return false, fmt.Errorf("failed to serialise %s: %w", workflow.RepoConfigFileName, err)
417 }
418 data = append(data, '\n')
419
420 if mkdirErr := fileutil.EnsureParentDir(configPath, constants.DirPermPublic); mkdirErr != nil {
421 return false, fmt.Errorf("failed to create directory for %s: %w", workflow.RepoConfigFileName, mkdirErr)
422 }
423
424 if writeErr := os.WriteFile(configPath, data, constants.FilePermPublic); writeErr != nil {
425 return false, fmt.Errorf("failed to write %s: %w", workflow.RepoConfigFileName, writeErr)
426 }
427
428 initLog.Printf("Wrote ghes: true to %s", configPath)
429
430 if verbose {
431 fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(
432 fmt.Sprintf("Configured %s with ghes: true (GHES deployment detected: %s)", workflow.RepoConfigFileName, ghesHost),
433 ))

Calls 8

FindGitRootFunction · 0.92
EnsureParentDirFunction · 0.92
FormatSuccessMessageFunction · 0.92
FormatInfoMessageFunction · 0.92
detectGHESDeploymentFunction · 0.85
PrintMethod · 0.80
PrintfMethod · 0.45
ErrorfMethod · 0.45