Skip to content

Commit 4b4d1fb

Browse files
committed
feat: Add github_enterprise_custom_property data source (#3230)
Add a data source for reading enterprise custom property definitions, complementing the new github_enterprise_custom_property resource.
1 parent 505bbfe commit 4b4d1fb

3 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package github
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGithubEnterpriseCustomProperty() *schema.Resource {
11+
return &schema.Resource{
12+
ReadContext: dataSourceGithubEnterpriseCustomPropertyRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"enterprise_slug": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
Description: "The slug of the enterprise.",
19+
},
20+
"property_name": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
Description: "The name of the custom property.",
24+
},
25+
"value_type": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
Description: "The type of the value for the property.",
29+
},
30+
"required": {
31+
Type: schema.TypeBool,
32+
Computed: true,
33+
Description: "Whether the custom property is required.",
34+
},
35+
"default_value": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
Description: "The default value of the custom property.",
39+
},
40+
"description": {
41+
Type: schema.TypeString,
42+
Computed: true,
43+
Description: "A short description of the custom property.",
44+
},
45+
"allowed_values": {
46+
Type: schema.TypeList,
47+
Computed: true,
48+
Description: "An ordered list of allowed values for the property.",
49+
Elem: &schema.Schema{Type: schema.TypeString},
50+
},
51+
"values_editable_by": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
Description: "Who can edit the values of the property. Can be one of 'org_actors' or 'org_and_repo_actors'.",
55+
},
56+
},
57+
}
58+
}
59+
60+
func dataSourceGithubEnterpriseCustomPropertyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
61+
client := meta.(*Owner).v3client
62+
63+
enterpriseSlug := d.Get("enterprise_slug").(string)
64+
propertyName := d.Get("property_name").(string)
65+
66+
property, _, err := client.Enterprise.GetCustomProperty(ctx, enterpriseSlug, propertyName)
67+
if err != nil {
68+
return diag.Errorf("error reading enterprise custom property %s/%s: %v", enterpriseSlug, propertyName, err)
69+
}
70+
71+
defaultValue, _ := property.DefaultValueString()
72+
73+
d.SetId(buildTwoPartID(enterpriseSlug, propertyName))
74+
_ = d.Set("enterprise_slug", enterpriseSlug)
75+
_ = d.Set("property_name", property.GetPropertyName())
76+
_ = d.Set("value_type", string(property.ValueType))
77+
_ = d.Set("required", property.GetRequired())
78+
_ = d.Set("default_value", defaultValue)
79+
_ = d.Set("description", property.GetDescription())
80+
_ = d.Set("allowed_values", property.AllowedValues)
81+
_ = d.Set("values_editable_by", property.GetValuesEditableBy())
82+
83+
return nil
84+
}

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ func Provider() *schema.Provider {
295295
"github_user_external_identity": dataSourceGithubUserExternalIdentity(),
296296
"github_users": dataSourceGithubUsers(),
297297
"github_enterprise": dataSourceGithubEnterprise(),
298+
"github_enterprise_custom_property": dataSourceGithubEnterpriseCustomProperty(),
298299
"github_repository_environment_deployment_policies": dataSourceGithubRepositoryEnvironmentDeploymentPolicies(),
299300
},
300301
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
layout: "github"
3+
page_title: "GitHub: github_enterprise_custom_property"
4+
description: |-
5+
Get information about a GitHub enterprise custom property definition
6+
---
7+
8+
# github_enterprise_custom_property
9+
10+
Use this data source to retrieve information about a custom property definition for a GitHub enterprise.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "github_enterprise_custom_property" "security_tier" {
16+
enterprise_slug = "my-enterprise"
17+
property_name = "securityTier"
18+
}
19+
```
20+
21+
## Example Usage - Reference in a Repository
22+
23+
```hcl
24+
data "github_enterprise_custom_property" "security_tier" {
25+
enterprise_slug = "my-enterprise"
26+
property_name = "securityTier"
27+
}
28+
29+
resource "github_repository" "example" {
30+
name = "example"
31+
visibility = "private"
32+
33+
custom_properties = {
34+
(data.github_enterprise_custom_property.security_tier.property_name) = "tier1"
35+
}
36+
}
37+
```
38+
39+
## Argument Reference
40+
41+
The following arguments are supported:
42+
43+
* `enterprise_slug` - (Required) The slug of the enterprise.
44+
45+
* `property_name` - (Required) The name of the custom property to retrieve.
46+
47+
## Attributes Reference
48+
49+
* `value_type` - The type of the value for the property. Can be one of `string`, `single_select`, `multi_select`, `true_false`, or `url`.
50+
51+
* `required` - Whether the custom property is required on repositories.
52+
53+
* `description` - A short description of the custom property.
54+
55+
* `default_value` - The default value of the custom property.
56+
57+
* `allowed_values` - An ordered list of allowed values for the property. Only populated when `value_type` is `single_select` or `multi_select`.
58+
59+
* `values_editable_by` - Who can edit the values of the property. Can be one of `org_actors` or `org_and_repo_actors`.

0 commit comments

Comments
 (0)