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)
| 121 | // |
| 122 | // Finally, if a repo and owner are passed in, it adds the repo to the user secret. |
| 123 | func 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 | |
| 153 | func encryptSecretWithPublicKey(publicKey *github.PublicKey, secretName, secretValue string) (*github.EncryptedSecret, error) { |
| 154 | decodedPublicKey, err := base64.StdEncoding.DecodeString(publicKey.GetKey()) |
no test coverage detected
searching dependent graphs…