checkExistingSecrets fetches which secrets already exist in the repository or its organization
()
| 12 | |
| 13 | // checkExistingSecrets fetches which secrets already exist in the repository or its organization |
| 14 | func (c *AddInteractiveConfig) checkExistingSecrets() error { |
| 15 | addInteractiveLog.Print("Checking existing repository secrets") |
| 16 | |
| 17 | c.existingSecrets = make(map[string]struct{}) |
| 18 | |
| 19 | // Use gh api to list repository secrets |
| 20 | output, err := workflow.RunGH("Checking repository secrets...", "api", fmt.Sprintf("/repos/%s/actions/secrets", c.RepoOverride), "--jq", ".secrets[].name") |
| 21 | if err != nil { |
| 22 | addInteractiveLog.Printf("Could not fetch existing secrets: %v", err) |
| 23 | // Continue without error - we'll just assume no secrets exist |
| 24 | } else { |
| 25 | for _, name := range parseSecretNames(output) { |
| 26 | c.existingSecrets[name] = struct{}{} |
| 27 | addInteractiveLog.Printf("Found existing repository secret: %s", name) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // Also check org-level secrets if the repo belongs to an organization |
| 32 | if org, _, found := strings.Cut(c.RepoOverride, "/"); found && org != "" { |
| 33 | orgOutput, orgErr := workflow.RunGH("Checking organization secrets...", "api", fmt.Sprintf("/orgs/%s/actions/secrets", org), "--jq", ".secrets[].name") |
| 34 | if orgErr != nil { |
| 35 | addInteractiveLog.Printf("Could not fetch org secrets (this is expected for personal repos or if org access is restricted): %v", orgErr) |
| 36 | } else { |
| 37 | for _, name := range parseSecretNames(orgOutput) { |
| 38 | c.existingSecrets[name] = struct{}{} |
| 39 | addInteractiveLog.Printf("Found existing org secret: %s", name) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if c.Verbose && len(c.existingSecrets) > 0 { |
| 45 | fmt.Fprintln(os.Stderr, console.FormatInfoMessageStderr(fmt.Sprintf("Found %d existing secret(s) (repository + organization)", len(c.existingSecrets)))) |
| 46 | } |
| 47 | |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | // addRepositorySecret adds a secret to the repository |
| 52 | func (c *AddInteractiveConfig) addRepositorySecret(name, value string) error { |