Skip to content

Commit 5a27fb7

Browse files
feat: Align environment branch policy support (#2932)
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com> Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
1 parent e6ae94e commit 5a27fb7

9 files changed

+198
-6
lines changed

github/data_source_github_repository_deployment_branch_policies.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
func dataSourceGithubRepositoryDeploymentBranchPolicies() *schema.Resource {
1111
return &schema.Resource{
12+
DeprecationMessage: "This data source is deprecated in favour of the github_repository_environment_deployment_policies data source.",
13+
1214
Read: dataSourceGithubRepositoryDeploymentBranchPoliciesRead,
1315

1416
Schema: map[string]*schema.Schema{
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGithubRepositoryEnvironmentDeploymentPolicies() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"repository": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
ForceNew: true,
19+
Description: "The name of the GitHub repository.",
20+
},
21+
"environment": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
ForceNew: true,
25+
Description: "The name of the environment.",
26+
},
27+
"policies": {
28+
Type: schema.TypeList,
29+
Computed: true,
30+
Elem: &schema.Resource{
31+
Schema: map[string]*schema.Schema{
32+
"type": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"pattern": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
},
41+
},
42+
},
43+
},
44+
}
45+
}
46+
47+
func dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead(d *schema.ResourceData, meta any) error {
48+
client := meta.(*Owner).v3client
49+
owner := meta.(*Owner).name
50+
repoName := d.Get("repository").(string)
51+
environmentName := d.Get("environment_name").(string)
52+
53+
policies, _, err := client.Repositories.ListDeploymentBranchPolicies(context.Background(), owner, repoName, environmentName)
54+
if err != nil {
55+
return err
56+
}
57+
58+
results := make([]map[string]any, 0)
59+
60+
for _, policy := range policies.BranchPolicies {
61+
policyMap := make(map[string]any)
62+
policyMap["type"] = policy.Type
63+
policyMap["pattern"] = policy.GetName()
64+
results = append(results, policyMap)
65+
}
66+
67+
d.SetId(fmt.Sprintf("%s:%s", repoName, environmentName))
68+
return d.Set("policies", results)
69+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 TestAccGithubRepositoryEnvironmentDeploymentPolicies(t *testing.T) {
12+
t.Run("queries environment deployment policies", func(t *testing.T) {
13+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
14+
15+
config := fmt.Sprintf(`
16+
resource "github_repository" "test" {
17+
name = "tf-acc-test-%s"
18+
auto_init = true
19+
}
20+
21+
resource "github_repository_environment" "env" {
22+
repository = github_repository.test.name
23+
environment = "my_env"
24+
deployment_branch_policy {
25+
protected_branches = false
26+
custom_branch_policies = true
27+
}
28+
}
29+
30+
resource "github_repository_environment_deployment_policy" "branch" {
31+
repository = github_repository.test.name
32+
environment = github_repository_environment.env.environment
33+
branch_pattern = "foo"
34+
}
35+
36+
resource "github_repository_environment_deployment_policy" "tag" {
37+
repository = github_repository.test.name
38+
environment = github_repository_environment.env.environment
39+
tag_pattern = "bar"
40+
}
41+
`, randomID)
42+
43+
config2 := config + `
44+
data "github_repository_environment_deployment_policies" "all" {
45+
repository = github_repository.test.name
46+
environment = github_repository_environment.env.environment
47+
}
48+
`
49+
check := resource.ComposeTestCheckFunc(
50+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.#", "2"),
51+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.0.type", "branch"),
52+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.0.name", "foo"),
53+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.1.type", "tag"),
54+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.1.name", "bar"),
55+
)
56+
57+
testCase := func(t *testing.T, mode string) {
58+
resource.Test(t, resource.TestCase{
59+
PreCheck: func() { skipUnlessMode(t, mode) },
60+
Providers: testAccProviders,
61+
Steps: []resource.TestStep{
62+
{
63+
Config: config,
64+
},
65+
{
66+
Config: config2,
67+
Check: check,
68+
},
69+
},
70+
})
71+
}
72+
73+
t.Run("with an anonymous account", func(t *testing.T) {
74+
t.Skip("anonymous account not supported for this operation")
75+
})
76+
77+
t.Run("with an individual account", func(t *testing.T) {
78+
testCase(t, individual)
79+
})
80+
81+
t.Run("with an organization account", func(t *testing.T) {
82+
testCase(t, organization)
83+
})
84+
})
85+
}

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ func Provider() *schema.Provider {
286286
"github_user_external_identity": dataSourceGithubUserExternalIdentity(),
287287
"github_users": dataSourceGithubUsers(),
288288
"github_enterprise": dataSourceGithubEnterprise(),
289+
"github_repository_environment_deployment_policies": dataSourceGithubRepositoryEnvironmentDeploymentPolicies(),
289290
},
290291
}
291292

github/resource_github_repository_deployment_branch_policy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
func resourceGithubRepositoryDeploymentBranchPolicy() *schema.Resource {
1515
return &schema.Resource{
16+
DeprecationMessage: "This resource is deprecated in favour of the github_repository_environment_deployment_policy resource.",
17+
1618
Create: resourceGithubRepositoryDeploymentBranchPolicyCreate,
1719
Read: resourceGithubRepositoryDeploymentBranchPolicyRead,
1820
Update: resourceGithubRepositoryDeploymentBranchPolicyUpdate,

github/resource_github_repository_environment_deployment_policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicy() *schema.Resource {
2727
Type: schema.TypeString,
2828
Required: true,
2929
ForceNew: true,
30-
Description: "The name of the repository. The name is not case sensitive.",
30+
Description: "The name of the GitHub repository.",
3131
},
3232
"environment": {
3333
Type: schema.TypeString,

website/docs/d/repository_deployment_branch_policies.html.markdown

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ description: |-
77

88
# github_repository_deployment_branch_policies
99

10+
~> **Note:** This data source is deprecated, please use the `github_repository_environment_deployment_policies` data source instead.
11+
1012
Use this data source to retrieve deployment branch policies for a repository / environment.
1113

1214
## Example Usage
@@ -27,5 +29,5 @@ data "github_repository_deployment_branch_policies" "example" {
2729
## Attributes Reference
2830

2931
* `deployment_branch_policies` - The list of this repository / environment deployment policies. Each element of `deployment_branch_policies` has the following attributes:
30-
* `id` - Id of the policy.
31-
* `name` - The name pattern that branches must match in order to deploy to the environment.
32+
* `id` - Id of the policy.
33+
* `name` - The name pattern that branches must match in order to deploy to the environment.
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_repository_environment_deployment_policies"
4+
description: |-
5+
Get the list of environment deployment policies for a given repository environment.
6+
---
7+
8+
# github_repository_environment_deployment_policies
9+
10+
Use this data source to retrieve deployment branch policies for a repository environment.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "github_repository_environment_deployment_policies" "example" {
16+
repository = "example-repository"
17+
environment = "env-name"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
* `repository` - (Required) Name of the repository to retrieve the deployment branch policies from.
24+
25+
* `environment` - (Required) Name of the environment to retrieve the deployment branch policies from.
26+
27+
## Attributes Reference
28+
29+
* `policies` - The list of deployment policies for the repository environment. Each element of `policies` has the following attributes:
30+
* `type` - Type of the policy; this could be `branch` or `tag`.
31+
* `pattern` - The pattern that branch or tag names must match in order to deploy to the environment.

website/docs/r/repository_deployment_branch_policy.html.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ description: |-
77

88
# github_repository_deployment_branch_policy
99

10-
This resource allows you to create and manage deployment branch policies.
10+
~> **Note:** This resource is deprecated, please use the `github_repository_environment_deployment_policy` resource instead.
1111

12+
This resource allows you to create and manage deployment branch policies.
1213

1314
## Example Usage
1415

@@ -31,7 +32,6 @@ resource "github_repository_deployment_branch_policy" "foo" {
3132
}
3233
```
3334

34-
3535
## Argument Reference
3636

3737
The following arguments are supported:
@@ -50,6 +50,6 @@ The following additional attributes are exported:
5050

5151
## Import
5252

53-
```
53+
```text
5454
$ terraform import github_repository_deployment_branch_policy.foo repo:env:id
5555
```

0 commit comments

Comments
 (0)