addRepoSecret will add a secret to a GitHub repo 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, owner, repo, secretName, secretValue string)
| 126 | // Finally, the github.EncryptedSecret is passed into the GitHub client.Codespaces.CreateOrUpdateRepoSecret method to |
| 127 | // populate the secret in the GitHub repo. |
| 128 | func addRepoSecret(ctx context.Context, client *github.Client, owner, repo, secretName, secretValue string) error { |
| 129 | publicKey, _, err := client.Codespaces.GetRepoPublicKey(ctx, owner, repo) |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | |
| 134 | encryptedSecret, err := encryptSecretWithPublicKey(publicKey, secretName, secretValue) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | |
| 139 | if _, err := client.Codespaces.CreateOrUpdateRepoSecret(ctx, owner, repo, encryptedSecret); err != nil { |
| 140 | return fmt.Errorf("client.Codespaces.CreateOrUpdateRepoSecret returned error: %v", err) |
| 141 | } |
| 142 | |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | func encryptSecretWithPublicKey(publicKey *github.PublicKey, secretName, secretValue string) (*github.EncryptedSecret, error) { |
| 147 | decodedPublicKey, err := base64.StdEncoding.DecodeString(publicKey.GetKey()) |
no test coverage detected
searching dependent graphs…