fix: improve github_organization_custom_properties resource#3234
fix: improve github_organization_custom_properties resource#3234mkushakov wants to merge 5 commits into
Conversation
|
👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with |
| Create: resourceGithubCustomPropertiesCreate, | ||
| Read: resourceGithubCustomPropertiesRead, | ||
| Update: resourceGithubCustomPropertiesUpdate, | ||
| Delete: resourceGithubCustomPropertiesDelete, | ||
| Importer: &schema.ResourceImporter{ | ||
| State: resourceGithubCustomPropertiesImport, |
There was a problem hiding this comment.
Please refactor to use the Context functions
| } | ||
| return resourceGithubCustomPropertiesRead(d, meta) | ||
| // Create uses the same upsert API, and already calls Read at the end | ||
| return resourceGithubCustomPropertiesCreate(d, meta) |
There was a problem hiding this comment.
issue: Never call any CRUD function
c8c34c4 to
b4fdf7b
Compare
1267efc to
e40236c
Compare
|
@mkushakov if you're still interested in contributing this change, I think it may be better to create a new resource named correctly (#2936) and then deprecate this resource. Off the top of my head I think the resource should be I've done a full refactor on the |
e40236c to
8122d85
Compare
- Remove broken CustomizeDiff referencing non-existent 'slug' field - Make 'property_name' ForceNew (renaming creates a new property, orphans old) - Make 'value_type' Required and ForceNew (can't create without type, changing type requires recreation) - Add allowed_values validation: required for select types, rejected for others - Add checkOrganization guard to all CRUD functions - Add graceful 404 handling in Read (removes from state instead of erroring) - Improve error messages with context - Fix Update double-read (Create already calls Read at the end) - Add resource Description - Use GetOk for optional fields to avoid sending zero values Resolves integrations#2806
8122d85 to
c19b5c4
Compare
hello @stevehipwell thanks for suggestions, as you asked i have following in my PR:
|
stevehipwell
left a comment
There was a problem hiding this comment.
Thanks for the changes @mkushakov, this looks like a great addition. As well as the inline comments I've got the following high level feedback.
- We need a
github_organization_repository_custom_propertiesdata source to replacegithub_organization_custom_properties - Could you align your changes/patterns to #3476 (this should be "correct" in terms of the contribution guide)
| func dataSourceGithubOrganizationRepositoryCustomPropertyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| if err := checkOrganization(meta); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| client := meta.(*Owner).v3client | ||
| orgName := meta.(*Owner).name | ||
| propertyName := d.Get("property_name").(string) |
There was a problem hiding this comment.
| func dataSourceGithubOrganizationRepositoryCustomPropertyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | |
| if err := checkOrganization(meta); err != nil { | |
| return diag.FromErr(err) | |
| } | |
| client := meta.(*Owner).v3client | |
| orgName := meta.(*Owner).name | |
| propertyName := d.Get("property_name").(string) | |
| func dataSourceGithubOrganizationRepositoryCustomPropertyRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | |
| meta, _ := m.(*Owner) | |
| client := meta.v3client | |
| owner := meta.name | |
| if !meta.IsOrganization { | |
| return return diag.FromErr(fmt.Errorf("repository custom properties are only supported for organizations, %q is a user", owner)) | |
| } | |
| propertyName := d.Get("property_name").(string) |
| if !slices.Contains([]github.PropertyValueType{ | ||
| github.PropertyValueTypeSingleSelect, | ||
| github.PropertyValueTypeMultiSelect, | ||
| }, cp.ValueType) { | ||
| cp.AllowedValues = nil | ||
| } |
There was a problem hiding this comment.
I think a switch statement would work better here.
| func TestAccGithubOrganizationRepositoryCustomPropertyDataSource(t *testing.T) { | ||
| const dataAddr = "data.github_organization_repository_custom_property.test" | ||
|
|
||
| t.Run("reads a property created by the resource", func(t *testing.T) { |
There was a problem hiding this comment.
Can we make this test work correctly in an organization where there are already existing properties?
|
|
||
| t.Run("reads a property created by the resource", func(t *testing.T) { | ||
| config := ` | ||
| resource "github_organization_repository_custom_property" "test" { |
There was a problem hiding this comment.
Could you look at #3476 and copy the pattern for configuring the test dependencies outside of TF code?
| cp.AllowedValues = nil | ||
| } | ||
|
|
||
| if err := setOrganizationRepositoryCustomPropertyState(d, cp); err != nil { |
There was a problem hiding this comment.
I think for now I'd rather see the code duplicated in the method bodies, as if we move the logic we ought to have a single documented pattern for how to do it.
Resolves #2806
Resolves #3191
Refs #2936
Before the change?
github_organization_custom_properties(plural).CustomizeDiffthat referenced a non-existentslugfield, a redundant double-read inUpdate, no 404 handling inRead(drift outside Terraform errored instead of removing from state), andGet/GetOkconfusion on optional fields that masked unset vs. empty.value_typewas optional on the resource even though the API requires it, andproperty_name/value_typewere notForceNeweven though they are immutable server-side, so plans silently corrupted state.allowed_valuesagainstvalue_type, so misconfigurations only surfaced on the API call.After the change?
Following @stevehipwell's review feedback, the in-place bug-fix approach has been replaced with a new, correctly-named resource and data source, leaving the old ones in place but marked deprecated:
github_organization_repository_custom_property(singular) implemented from scratch using the conventions inARCHITECTURE.mdand the pattern established by thegithub_repository_custom_propertyrefactor in fix: Refactor repository custom property #3476:*ContextCRUD withdiag.Diagnosticstflogstructured loggingerrors.AsType[*github.ErrorResponse]for graceful 404 handling onRead/Deletecustomdiff.Allcross-field validation forallowed_values(required forsingle_select/multi_select, rejected for the other types)ForceNewon the immutableproperty_nameandvalue_typecheckOrganizationguard on every CRUD entry pointDescriptionon every schema attributegithub_organization_repository_custom_propertymirroring the resource schema.github_organization_custom_propertiesresource and data source carry aDeprecationMessagepointing at the new singular names. CRUD is left intact so existing state continues to work.ConfigStateChecks/ConfigPlanChecks(statecheck/plancheck) pattern witht.Runsubtests covering create, update, import,ForceNew, cross-field validation, and thevalues_editable_byscenarios.make generatedocsran.Pull request checklist
Does this introduce a breaking change?
The old resource and data source remain functional and only emit a deprecation warning, so existing configurations continue to work without modification.