MCPcopy Index your code
hub / github.com/google/go-github / addUserSecret

Function addUserSecret

example/codespaces/newusersecretwithxcrypto/main.go:123–151  ·  view source on GitHub ↗

addUserSecret will add a secret to a GitHub user for use in GitHub Codespaces. The secretName and secretValue determine the name of the secret added and its corresponding value. The actual transmission of the secret value to GitHub using the API requires that the secret value is encrypted using th

(ctx context.Context, client *github.Client, secretName, secretValue, owner, repo string)

Source from the content-addressed store, hash-verified

121//
122// Finally, if a repo and owner are passed in, it adds the repo to the user secret.
123func addUserSecret(ctx context.Context, client *github.Client, secretName, secretValue, owner, repo string) error {
124 publicKey, _, err := client.Codespaces.GetUserPublicKey(ctx)
125 if err != nil {
126 return err
127 }
128
129 encryptedSecret, err := encryptSecretWithPublicKey(publicKey, secretName, secretValue)
130 if err != nil {
131 return err
132 }
133
134 if _, err := client.Codespaces.CreateOrUpdateUserSecret(ctx, encryptedSecret); err != nil {
135 return fmt.Errorf("client.Codespaces.CreateOrUpdateUserSecret returned error: %v", err)
136 }
137
138 if owner != "" && repo != "" {
139 r, _, err := client.Repositories.Get(ctx, owner, repo)
140 if err != nil {
141 return fmt.Errorf("client.Repositories.Get returned error: %v", err)
142 }
143 _, err = client.Codespaces.AddSelectedRepoToUserSecret(ctx, encryptedSecret.Name, r)
144 if err != nil {
145 return fmt.Errorf("client.Codespaces.AddSelectedRepoToUserSecret returned error: %v", err)
146 }
147 fmt.Printf("Added secret %q to %v/%v\n", secretName, owner, repo)
148 }
149
150 return nil
151}
152
153func encryptSecretWithPublicKey(publicKey *github.PublicKey, secretName, secretValue string) (*github.EncryptedSecret, error) {
154 decodedPublicKey, err := base64.StdEncoding.DecodeString(publicKey.GetKey())

Callers 1

mainFunction · 0.85

Calls 5

GetUserPublicKeyMethod · 0.80
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…