Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions github/data_source_github_actions_environment_public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGithubActionsEnvironmentPublicKey() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubActionsEnvironmentPublicKeyRead,
ReadContext: dataSourceGithubActionsEnvironmentPublicKeyRead,

Schema: map[string]*schema.Schema{
"repository": {
Expand All @@ -32,33 +33,29 @@ func dataSourceGithubActionsEnvironmentPublicKey() *schema.Resource {
}
}

func dataSourceGithubActionsEnvironmentPublicKeyRead(d *schema.ResourceData, meta any) error {
ctx := context.Background()
func dataSourceGithubActionsEnvironmentPublicKeyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repository := d.Get("repository").(string)

envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)

repo, _, err := client.Repositories.Get(ctx, owner, repository)
if err != nil {
return err
return diag.FromErr(err)
}

publicKey, _, err := client.Actions.GetEnvPublicKey(ctx, int(repo.GetID()), escapedEnvName)
publicKey, _, err := client.Actions.GetEnvPublicKey(ctx, int(repo.GetID()), url.PathEscape(envName))
if err != nil {
return err
return diag.FromErr(err)
}

d.SetId(publicKey.GetKeyID())
err = d.Set("key_id", publicKey.GetKeyID())
if err != nil {
return err
if err := d.Set("key_id", publicKey.GetKeyID()); err != nil {
return diag.FromErr(err)
}
err = d.Set("key", publicKey.GetKey())
if err != nil {
return err
if err := d.Set("key", publicKey.GetKey()); err != nil {
return diag.FromErr(err)
}

return nil
Expand Down
31 changes: 18 additions & 13 deletions github/data_source_github_actions_environment_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package github

import (
"context"
"fmt"
"net/url"

"github.com/google/go-github/v81/github"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGithubActionsEnvironmentSecrets() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubActionsEnvironmentSecretsRead,
ReadContext: dataSourceGithubActionsEnvironmentSecretsRead,

Schema: map[string]*schema.Schema{
"full_name": {
Expand Down Expand Up @@ -55,20 +55,18 @@ func dataSourceGithubActionsEnvironmentSecrets() *schema.Resource {
}
}

func dataSourceGithubActionsEnvironmentSecretsRead(d *schema.ResourceData, meta any) error {
ctx := context.Background()
func dataSourceGithubActionsEnvironmentSecretsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
var repoName string

envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)

if fullName, ok := d.GetOk("full_name"); ok {
var err error
owner, repoName, err = splitRepoFullName(fullName.(string))
if err != nil {
return err
return diag.FromErr(err)
}
}

Expand All @@ -77,23 +75,23 @@ func dataSourceGithubActionsEnvironmentSecretsRead(d *schema.ResourceData, meta
}

if repoName == "" {
return fmt.Errorf("one of %q or %q has to be provided", "full_name", "name")
return diag.Errorf("one of %q or %q has to be provided", "full_name", "name")
}

repo, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
return err
return diag.FromErr(err)
}

options := github.ListOptions{
PerPage: 100,
PerPage: maxPerPage,
}

var all_secrets []map[string]string
for {
secrets, resp, err := client.Actions.ListEnvSecrets(ctx, int(repo.GetID()), escapedEnvName, &options)
secrets, resp, err := client.Actions.ListEnvSecrets(ctx, int(repo.GetID()), url.PathEscape(envName), &options)
if err != nil {
return err
return diag.FromErr(err)
}
for _, secret := range secrets.Secrets {
new_secret := map[string]string{
Expand All @@ -109,8 +107,15 @@ func dataSourceGithubActionsEnvironmentSecretsRead(d *schema.ResourceData, meta
options.Page = resp.NextPage
}

d.SetId(buildTwoPartID(repoName, envName))
_ = d.Set("secrets", all_secrets)
if id, err := buildID(repoName, escapeIDPart(envName)); err != nil {
return diag.FromErr(err)
} else {
d.SetId(id)
}

if err := d.Set("secrets", all_secrets); err != nil {
return diag.FromErr(err)
}

return nil
}
13 changes: 6 additions & 7 deletions github/data_source_github_actions_environment_secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ func TestAccGithubActionsEnvironmentSecretsDataSource(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
auto_init = true
name = "%s"
}

resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "environment / test"
repository = github_repository.test.name
environment = "environment / test"
}

resource "github_actions_environment_secret" "test" {
secret_name = "secret_1"
environment = github_repository_environment.test.environment
repository = github_repository.test.name
repository = github_repository.test.name
environment = github_repository_environment.test.environment
secret_name = "secret_1"
plaintext_value = "foo"
}
`, repoName)
Expand Down
29 changes: 17 additions & 12 deletions github/data_source_github_actions_environment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package github

import (
"context"
"fmt"
"net/url"

"github.com/google/go-github/v81/github"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGithubActionsEnvironmentVariables() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubActionsEnvironmentVariablesRead,
ReadContext: dataSourceGithubActionsEnvironmentVariablesRead,

Schema: map[string]*schema.Schema{
"full_name": {
Expand Down Expand Up @@ -59,20 +59,18 @@ func dataSourceGithubActionsEnvironmentVariables() *schema.Resource {
}
}

func dataSourceGithubActionsEnvironmentVariablesRead(d *schema.ResourceData, meta any) error {
ctx := context.Background()
func dataSourceGithubActionsEnvironmentVariablesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
var repoName string

envName := d.Get("environment").(string)
escapedEnvName := url.PathEscape(envName)

if fullName, ok := d.GetOk("full_name"); ok {
var err error
owner, repoName, err = splitRepoFullName(fullName.(string))
if err != nil {
return err
return diag.FromErr(err)
}
}

Expand All @@ -81,18 +79,18 @@ func dataSourceGithubActionsEnvironmentVariablesRead(d *schema.ResourceData, met
}

if repoName == "" {
return fmt.Errorf("one of %q or %q has to be provided", "full_name", "name")
return diag.Errorf("one of %q or %q has to be provided", "full_name", "name")
}

options := github.ListOptions{
PerPage: 100,
PerPage: maxPerPage,
}

var all_variables []map[string]string
for {
variables, resp, err := client.Actions.ListEnvVariables(ctx, owner, repoName, escapedEnvName, &options)
variables, resp, err := client.Actions.ListEnvVariables(ctx, owner, repoName, url.PathEscape(envName), &options)
if err != nil {
return err
return diag.FromErr(err)
}
for _, variable := range variables.Variables {
new_variable := map[string]string{
Expand All @@ -109,8 +107,15 @@ func dataSourceGithubActionsEnvironmentVariablesRead(d *schema.ResourceData, met
options.Page = resp.NextPage
}

d.SetId(buildTwoPartID(repoName, envName))
_ = d.Set("variables", all_variables)
if id, err := buildID(repoName, escapeIDPart(envName)); err != nil {
return diag.FromErr(err)
} else {
d.SetId(id)
}

if err := d.Set("variables", all_variables); err != nil {
return diag.FromErr(err)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package github

import (
"context"
"fmt"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -49,9 +49,9 @@ func dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead(ctx context.Con
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
environmentName := d.Get("environment").(string)
envName := d.Get("environment").(string)

policies, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, owner, repoName, environmentName)
policies, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, owner, repoName, url.PathEscape(envName))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -65,9 +65,13 @@ func dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead(ctx context.Con
results = append(results, policyMap)
}

d.SetId(fmt.Sprintf("%s:%s", repoName, environmentName))
err = d.Set("policies", results)
if err != nil {
if id, err := buildID(repoName, escapeIDPart(envName)); err != nil {
return diag.FromErr(err)
} else {
d.SetId(id)
}

if err = d.Set("policies", results); err != nil {
return diag.FromErr(err)
}

Expand Down
14 changes: 7 additions & 7 deletions github/data_source_github_repository_environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"

"github.com/google/go-github/v81/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGithubRepositoryEnvironments() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoryEnvironmentsRead,
ReadContext: dataSourceGithubRepositoryEnvironmentsRead,

Schema: map[string]*schema.Schema{
"repository": {
Expand All @@ -36,7 +37,7 @@ func dataSourceGithubRepositoryEnvironments() *schema.Resource {
}
}

func dataSourceGithubRepositoryEnvironmentsRead(d *schema.ResourceData, meta any) error {
func dataSourceGithubRepositoryEnvironmentsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
Expand All @@ -45,9 +46,9 @@ func dataSourceGithubRepositoryEnvironmentsRead(d *schema.ResourceData, meta any

for {
listOptions := &github.EnvironmentListOptions{}
environments, resp, err := client.Repositories.ListEnvironments(context.Background(), orgName, repoName, listOptions)
environments, resp, err := client.Repositories.ListEnvironments(ctx, orgName, repoName, listOptions)
if err != nil {
return err
return diag.FromErr(err)
}

results = append(results, flattenEnvironments(environments)...)
Expand All @@ -60,9 +61,8 @@ func dataSourceGithubRepositoryEnvironmentsRead(d *schema.ResourceData, meta any
}

d.SetId(repoName)
err := d.Set("environments", results)
if err != nil {
return err
if err := d.Set("environments", results); err != nil {
return diag.FromErr(err)
}

return nil
Expand Down
Loading