diff --git a/github/data_source_github_repository_deployment_branch_policies.go b/github/data_source_github_repository_deployment_branch_policies.go index cb0f3f7b2a..5091257efb 100644 --- a/github/data_source_github_repository_deployment_branch_policies.go +++ b/github/data_source_github_repository_deployment_branch_policies.go @@ -9,6 +9,8 @@ import ( func dataSourceGithubRepositoryDeploymentBranchPolicies() *schema.Resource { return &schema.Resource{ + DeprecationMessage: "This data source is deprecated in favour of the github_repository_environment_deployment_policies data source.", + Read: dataSourceGithubRepositoryDeploymentBranchPoliciesRead, Schema: map[string]*schema.Schema{ diff --git a/github/data_source_github_repository_environment_deployment_policies.go b/github/data_source_github_repository_environment_deployment_policies.go new file mode 100644 index 0000000000..a80e0632f6 --- /dev/null +++ b/github/data_source_github_repository_environment_deployment_policies.go @@ -0,0 +1,69 @@ +package github + +import ( + "context" + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGithubRepositoryEnvironmentDeploymentPolicies() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead, + + Schema: map[string]*schema.Schema{ + "repository": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The name of the GitHub repository.", + }, + "environment": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The name of the environment.", + }, + "policies": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "type": { + Type: schema.TypeString, + Computed: true, + }, + "pattern": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead(d *schema.ResourceData, meta any) error { + client := meta.(*Owner).v3client + owner := meta.(*Owner).name + repoName := d.Get("repository").(string) + environmentName := d.Get("environment_name").(string) + + policies, _, err := client.Repositories.ListDeploymentBranchPolicies(context.Background(), owner, repoName, environmentName) + if err != nil { + return err + } + + results := make([]map[string]any, 0) + + for _, policy := range policies.BranchPolicies { + policyMap := make(map[string]any) + policyMap["type"] = policy.Type + policyMap["pattern"] = policy.GetName() + results = append(results, policyMap) + } + + d.SetId(fmt.Sprintf("%s:%s", repoName, environmentName)) + return d.Set("policies", results) +} diff --git a/github/data_source_github_repository_environment_deployment_policies_test.go b/github/data_source_github_repository_environment_deployment_policies_test.go new file mode 100644 index 0000000000..94fda8cd35 --- /dev/null +++ b/github/data_source_github_repository_environment_deployment_policies_test.go @@ -0,0 +1,85 @@ +package github + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccGithubRepositoryEnvironmentDeploymentPolicies(t *testing.T) { + t.Run("queries environment deployment policies", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } + + resource "github_repository_environment" "env" { + repository = github_repository.test.name + environment = "my_env" + deployment_branch_policy { + protected_branches = false + custom_branch_policies = true + } + } + + resource "github_repository_environment_deployment_policy" "branch" { + repository = github_repository.test.name + environment = github_repository_environment.env.environment + branch_pattern = "foo" + } + + resource "github_repository_environment_deployment_policy" "tag" { + repository = github_repository.test.name + environment = github_repository_environment.env.environment + tag_pattern = "bar" + } + `, randomID) + + config2 := config + ` + data "github_repository_environment_deployment_policies" "all" { + repository = github_repository.test.name + environment = github_repository_environment.env.environment + } + ` + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.#", "2"), + resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.0.type", "branch"), + resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.0.name", "foo"), + resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.1.type", "tag"), + resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.1.name", "bar"), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + }, + { + Config: config2, + Check: check, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + }) +} diff --git a/github/provider.go b/github/provider.go index fac0fafa83..0bb039d814 100644 --- a/github/provider.go +++ b/github/provider.go @@ -286,6 +286,7 @@ func Provider() *schema.Provider { "github_user_external_identity": dataSourceGithubUserExternalIdentity(), "github_users": dataSourceGithubUsers(), "github_enterprise": dataSourceGithubEnterprise(), + "github_repository_environment_deployment_policies": dataSourceGithubRepositoryEnvironmentDeploymentPolicies(), }, } diff --git a/github/resource_github_repository_deployment_branch_policy.go b/github/resource_github_repository_deployment_branch_policy.go index 82d0c5c07c..a62436260e 100644 --- a/github/resource_github_repository_deployment_branch_policy.go +++ b/github/resource_github_repository_deployment_branch_policy.go @@ -13,6 +13,8 @@ import ( func resourceGithubRepositoryDeploymentBranchPolicy() *schema.Resource { return &schema.Resource{ + DeprecationMessage: "This resource is deprecated in favour of the github_repository_environment_deployment_policy resource.", + Create: resourceGithubRepositoryDeploymentBranchPolicyCreate, Read: resourceGithubRepositoryDeploymentBranchPolicyRead, Update: resourceGithubRepositoryDeploymentBranchPolicyUpdate, diff --git a/github/resource_github_repository_environment_deployment_policy.go b/github/resource_github_repository_environment_deployment_policy.go index cff33e46be..2b573fce29 100644 --- a/github/resource_github_repository_environment_deployment_policy.go +++ b/github/resource_github_repository_environment_deployment_policy.go @@ -27,7 +27,7 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicy() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - Description: "The name of the repository. The name is not case sensitive.", + Description: "The name of the GitHub repository.", }, "environment": { Type: schema.TypeString, diff --git a/website/docs/d/repository_deployment_branch_policies.html.markdown b/website/docs/d/repository_deployment_branch_policies.html.markdown index 13f6f8d55f..d0ec6debec 100644 --- a/website/docs/d/repository_deployment_branch_policies.html.markdown +++ b/website/docs/d/repository_deployment_branch_policies.html.markdown @@ -7,6 +7,8 @@ description: |- # github_repository_deployment_branch_policies +~> **Note:** This data source is deprecated, please use the `github_repository_environment_deployment_policies` data source instead. + Use this data source to retrieve deployment branch policies for a repository / environment. ## Example Usage @@ -27,5 +29,5 @@ data "github_repository_deployment_branch_policies" "example" { ## Attributes Reference * `deployment_branch_policies` - The list of this repository / environment deployment policies. Each element of `deployment_branch_policies` has the following attributes: - * `id` - Id of the policy. - * `name` - The name pattern that branches must match in order to deploy to the environment. + * `id` - Id of the policy. + * `name` - The name pattern that branches must match in order to deploy to the environment. diff --git a/website/docs/d/repository_environment_deployment_policies.html.markdown b/website/docs/d/repository_environment_deployment_policies.html.markdown new file mode 100644 index 0000000000..93efcc912a --- /dev/null +++ b/website/docs/d/repository_environment_deployment_policies.html.markdown @@ -0,0 +1,31 @@ +--- +layout: "github" +page_title: "GitHub: github_repository_environment_deployment_policies" +description: |- + Get the list of environment deployment policies for a given repository environment. +--- + +# github_repository_environment_deployment_policies + +Use this data source to retrieve deployment branch policies for a repository environment. + +## Example Usage + +```hcl +data "github_repository_environment_deployment_policies" "example" { + repository = "example-repository" + environment = "env-name" +} +``` + +## Argument Reference + +* `repository` - (Required) Name of the repository to retrieve the deployment branch policies from. + +* `environment` - (Required) Name of the environment to retrieve the deployment branch policies from. + +## Attributes Reference + +* `policies` - The list of deployment policies for the repository environment. Each element of `policies` has the following attributes: + * `type` - Type of the policy; this could be `branch` or `tag`. + * `pattern` - The pattern that branch or tag names must match in order to deploy to the environment. diff --git a/website/docs/r/repository_deployment_branch_policy.html.markdown b/website/docs/r/repository_deployment_branch_policy.html.markdown index c1d1f8fa88..bda3cbd02b 100644 --- a/website/docs/r/repository_deployment_branch_policy.html.markdown +++ b/website/docs/r/repository_deployment_branch_policy.html.markdown @@ -7,8 +7,9 @@ description: |- # github_repository_deployment_branch_policy -This resource allows you to create and manage deployment branch policies. +~> **Note:** This resource is deprecated, please use the `github_repository_environment_deployment_policy` resource instead. +This resource allows you to create and manage deployment branch policies. ## Example Usage @@ -31,7 +32,6 @@ resource "github_repository_deployment_branch_policy" "foo" { } ``` - ## Argument Reference The following arguments are supported: @@ -50,6 +50,6 @@ The following additional attributes are exported: ## Import -``` +```text $ terraform import github_repository_deployment_branch_policy.foo repo:env:id ```