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
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
})
})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}

Expand Down
2 changes: 2 additions & 0 deletions github/resource_github_repository_deployment_branch_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -31,7 +32,6 @@ resource "github_repository_deployment_branch_policy" "foo" {
}
```


## Argument Reference

The following arguments are supported:
Expand All @@ -50,6 +50,6 @@ The following additional attributes are exported:

## Import

```
```text
$ terraform import github_repository_deployment_branch_policy.foo repo:env:id
```