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

Function ensureSecretAvailable

pkg/cli/engine_secrets.go:217–274  ·  view source on GitHub ↗

ensureSecretAvailable ensures that a required secret is available. It checks the repository, environment, and prompts the user if needed.

(req SecretRequirement, config EngineSecretConfig)

Source from the content-addressed store, hash-verified

215// ensureSecretAvailable ensures that a required secret is available.
216// It checks the repository, environment, and prompts the user if needed.
217func ensureSecretAvailable(req SecretRequirement, config EngineSecretConfig) error {
218 engineSecretsLog.Printf("Ensuring secret available: %s", req.Name)
219
220 // Check if secret already exists in the repository
221 if setutil.Contains(config.ExistingSecrets, req.Name) {
222 fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(fmt.Sprintf("Using existing %s secret in repository", req.Name)))
223 return nil
224 }
225
226 // Check alternative secret names in repository
227 for _, alt := range req.AlternativeEnvVars {
228 if setutil.Contains(config.ExistingSecrets, alt) {
229 fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(fmt.Sprintf("Using existing %s secret in repository (alternative for %s)", alt, req.Name)))
230 return nil
231 }
232 }
233
234 // Check environment variable
235 envValue := os.Getenv(req.Name)
236 if envValue == "" {
237 // Check alternative environment variables
238 for _, alt := range req.AlternativeEnvVars {
239 envValue = os.Getenv(alt)
240 if envValue != "" {
241 engineSecretsLog.Printf("Found secret in alternative env var: %s", alt)
242 break
243 }
244 }
245 }
246
247 if envValue != "" {
248 // Validate if it's a Copilot token
249 if req.IsEngineSecret && req.EngineName == string(constants.CopilotEngine) {
250 if err := stringutil.ValidateCopilotPAT(envValue); err != nil {
251 fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("%s in environment is not a valid fine-grained PAT: %s", req.Name, stringutil.GetPATTypeDescription(envValue))))
252 fmt.Fprintln(os.Stderr, console.FormatErrorMessage(err.Error()))
253 // Continue to prompt for a new token
254 } else {
255 fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(fmt.Sprintf("Found valid %s in environment", req.Name)))
256 // Upload to repository if we have a repo slug
257 if config.RepoSlug != "" {
258 return uploadSecretToRepo(req.Name, envValue, config.RepoSlug, config.Verbose)
259 }
260 return nil
261 }
262 } else {
263 fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(fmt.Sprintf("Found %s in environment", req.Name)))
264 // Upload to repository if we have a repo slug
265 if config.RepoSlug != "" {
266 return uploadSecretToRepo(req.Name, envValue, config.RepoSlug, config.Verbose)
267 }
268 return nil
269 }
270 }
271
272 // Secret not found, prompt user for it
273 return promptForSecret(req, config)
274}

Callers 1

Calls 11

ContainsFunction · 0.92
FormatSuccessMessageFunction · 0.92
ValidateCopilotPATFunction · 0.92
FormatWarningMessageFunction · 0.92
GetPATTypeDescriptionFunction · 0.92
FormatErrorMessageFunction · 0.92
uploadSecretToRepoFunction · 0.85
promptForSecretFunction · 0.85
GetenvMethod · 0.80
PrintfMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected