Fix HTTP 422 error in github_organization_settings resource #2807
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix HTTP 422 error in github_organization_settings resource
Fixes #2305
Problem Description
The
github_organization_settingsresource was failing with HTTP 422 errors, particularly when used with Enterprise Managed User (EMU) organizations. Users reported that the resource "invariably fails with HTTP 422" regardless of configuration attempts.Root Cause Analysis
The issue was caused by three main problems:
Unnecessary API Call: The code was making an
Organizations.Edit(ctx, org, nil)call with nil parameters, which could trigger API validation errors.Empty String Pollution: The resource was unconditionally sending all fields to the GitHub API, including empty strings for optional fields that weren't configured, causing API rejections.
Boolean False Handling: The code used
d.GetOk()for boolean fields, which returnsfalsefor booleanfalsevalues, causing these fields to be omitted from the API payload even when explicitly set tofalse.Solution
Core Fixes
Organizations.Edit(ctx, org, nil)toOrganizations.Get()to safely retrieve organization plan informationbuildOrganizationSettings()helper function that only includes explicitly configured fieldsd.GetOkExists()for boolean fields to properly detect whenfalsevalues are explicitly configuredEnterprise Support
members_can_create_internal_repositoriesfor enterprise organizationsChanges Made
Files Modified
github/resource_github_organization_settings.go- Core implementation fixesgithub/resource_github_organization_settings_test.go- Comprehensive test coverageKey Implementation Details
buildOrganizationSettings()Function:github.Organizationstructd.GetOkExists()for proper boolean false detectionAPI Call Optimization:
Edit(nil)call with safeGet()callEnterprise Field Handling:
orgInfo.GetPlan().GetName() == "enterprise"members_can_create_internal_repositoriesfieldTesting
Following the project's contributing guidelines, comprehensive testing has been performed:
Manual Testing
trueandfalsevalues are correctly handleddev_overridesfor testingAutomated Testing
Added comprehensive test coverage including:
billing_email,company,email,twitter_username,location,name,description,blogdefault_repository_permissionfalsevalue handlingTest Results
dev_overridesconfigurationImpact
Positive Changes
User Benefits
falseVerification
Before Fix
After Fix
falsevalue handlingBreaking Changes
None - This fix maintains full backward compatibility.
Additional Notes
Related Issues
This PR resolves the core issue described in #2305 where users reported HTTP 422 errors when using the
github_organization_settingsresource with EMU organizations.