Skip to content

Commit 00c78b6

Browse files
nobbsnickfloyd
andauthored
feat: implement missing data source github_actions_environment_public_key (#2500)
* feat: add data source for GitHub Actions environment public key * feat: add data source for GitHub Actions environment public key * refactor: simplify repository handling in GitHub Actions environment public key data source * fix: correct environment attribute in GitHub Actions environment public key test * docs: add actions_environment_public_key documentation --------- Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
1 parent 4af3fd3 commit 00c78b6

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"net/url"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGithubActionsEnvironmentPublicKey() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceGithubActionsEnvironmentPublicKeyRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"repository": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
"environment": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
},
23+
"key_id": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
"key": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
},
32+
}
33+
}
34+
35+
func dataSourceGithubActionsEnvironmentPublicKeyRead(d *schema.ResourceData, meta interface{}) error {
36+
client := meta.(*Owner).v3client
37+
owner := meta.(*Owner).name
38+
repository := d.Get("repository").(string)
39+
40+
envName := d.Get("environment").(string)
41+
escapedEnvName := url.PathEscape(envName)
42+
43+
repo, _, err := client.Repositories.Get(context.TODO(), owner, repository)
44+
if err != nil {
45+
return err
46+
}
47+
48+
publicKey, _, err := client.Actions.GetEnvPublicKey(context.TODO(), int(repo.GetID()), escapedEnvName)
49+
if err != nil {
50+
return err
51+
}
52+
53+
d.SetId(publicKey.GetKeyID())
54+
err = d.Set("key_id", publicKey.GetKeyID())
55+
if err != nil {
56+
return err
57+
}
58+
err = d.Set("key", publicKey.GetKey())
59+
if err != nil {
60+
return err
61+
}
62+
63+
return nil
64+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccGithubActionsEnvironmentPublicKeyDataSource(t *testing.T) {
12+
13+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
14+
15+
t.Run("queries a repository environment public key without error", func(t *testing.T) {
16+
17+
config := fmt.Sprintf(`
18+
resource "github_repository" "test" {
19+
name = "tf-acc-test-%[1]s"
20+
auto_init = true
21+
}
22+
23+
resource "github_repository_environment" "test" {
24+
repository = github_repository.test.name
25+
environment = "tf-acc-test-%[1]s"
26+
}
27+
28+
data "github_actions_environment_public_key" "test" {
29+
repository = github_repository.test.name
30+
environment = github_repository_environment.test.environment
31+
}`, randomID)
32+
33+
check := resource.ComposeTestCheckFunc(
34+
resource.TestCheckResourceAttrSet(
35+
"data.github_actions_environment_public_key.test", "key",
36+
),
37+
)
38+
39+
testCase := func(t *testing.T, mode string) {
40+
resource.Test(t, resource.TestCase{
41+
PreCheck: func() { skipUnlessMode(t, mode) },
42+
Providers: testAccProviders,
43+
Steps: []resource.TestStep{
44+
{
45+
Config: config,
46+
Check: check,
47+
},
48+
},
49+
})
50+
}
51+
52+
t.Run("with an anonymous account", func(t *testing.T) {
53+
t.Skip("anonymous account not supported for this operation")
54+
})
55+
56+
t.Run("with an individual account", func(t *testing.T) {
57+
testCase(t, individual)
58+
})
59+
60+
t.Run("with an organization account", func(t *testing.T) {
61+
testCase(t, organization)
62+
})
63+
64+
})
65+
}

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ func Provider() *schema.Provider {
206206
},
207207

208208
DataSourcesMap: map[string]*schema.Resource{
209+
"github_actions_environment_public_key": dataSourceGithubActionsEnvironmentPublicKey(),
209210
"github_actions_environment_secrets": dataSourceGithubActionsEnvironmentSecrets(),
210211
"github_actions_environment_variables": dataSourceGithubActionsEnvironmentVariables(),
211212
"github_actions_organization_oidc_subject_claim_customization_template": dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate(),
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
layout: "github"
3+
page_title: "GitHub: github_actions_environment_public_key"
4+
description: |-
5+
Get information on a GitHub Actions Environment Public Key.
6+
---
7+
8+
# github_actions_environment_public_key
9+
10+
Use this data source to retrieve information about a GitHub Actions public key of a specific environment. This data source is required to be used with other GitHub secrets interactions.
11+
Note that the provider `token` must have admin rights to a repository to retrieve the action public keys of it's environments.
12+
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "github_actions_environment_public_key" "example" {
18+
repository = "example_repo"
19+
environment = "example_environment"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
* `repository` - (Required) Name of the repository to get public key from.
26+
* `environment` - (Required) Name of the environment to get public key from.
27+
28+
## Attributes Reference
29+
30+
* `key_id` - ID of the key that has been retrieved.
31+
* `key` - Actual key retrieved.

website/github.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<li>
1414
<a href="#">Data Sources</a>
1515
<ul class="nav nav-visible">
16+
<li>
17+
<a href="/docs/providers/github/d/actions_environment_public_key.html">actions_environment_public_key</a>
18+
</li>
1619
<li>
1720
<a href="/docs/providers/github/d/actions_environment_secrets.html">actions_environment_secrets</a>
1821
</li>

0 commit comments

Comments
 (0)