Skip to content

Conversation

@netflash
Copy link

Fix HTTP 422 error in github_organization_settings resource

Fixes #2305

Problem Description

The github_organization_settings resource 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:

  1. Unnecessary API Call: The code was making an Organizations.Edit(ctx, org, nil) call with nil parameters, which could trigger API validation errors.

  2. 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.

  3. Boolean False Handling: The code used d.GetOk() for boolean fields, which returns false for boolean false values, causing these fields to be omitted from the API payload even when explicitly set to false.

Solution

Core Fixes

  • Replaced unnecessary API call: Changed Organizations.Edit(ctx, org, nil) to Organizations.Get() to safely retrieve organization plan information
  • Created conditional payload builder: Implemented buildOrganizationSettings() helper function that only includes explicitly configured fields
  • Fixed boolean handling: Used d.GetOkExists() for boolean fields to properly detect when false values are explicitly configured
  • Optimized API payloads: Only send fields that are actually configured, avoiding empty string issues

Enterprise Support

  • Enterprise detection: Properly detect enterprise organizations using plan name
  • Conditional enterprise fields: Only include enterprise-specific fields like members_can_create_internal_repositories for enterprise organizations
  • EMU compatibility: Full support for Enterprise Managed User organizations

Changes Made

Files Modified

  • github/resource_github_organization_settings.go - Core implementation fixes
  • github/resource_github_organization_settings_test.go - Comprehensive test coverage

Key Implementation Details

  1. buildOrganizationSettings() Function:

    • Conditionally builds github.Organization struct
    • Only includes fields that are explicitly configured
    • Handles enterprise-specific fields appropriately
    • Uses d.GetOkExists() for proper boolean false detection
  2. API Call Optimization:

    • Replaced problematic Edit(nil) call with safe Get() call
    • Ensures organization plan information is retrieved without side effects
  3. Enterprise Field Handling:

    • Detects enterprise organizations via orgInfo.GetPlan().GetName() == "enterprise"
    • Conditionally includes members_can_create_internal_repositories field

Testing

Following the project's contributing guidelines, comprehensive testing has been performed:

Manual Testing

  • EMU Organization: Successfully tested with Enterprise Managed User organization
  • Parameter Coverage: Tested all parameter types individually and in combinations
  • Boolean Values: Verified both true and false values are correctly handled
  • Complex Configurations: Tested multi-parameter configurations with various field types
  • Local Provider Build: Used local provider build with dev_overrides for testing

Automated Testing

Added comprehensive test coverage including:

  • String Fields (8 parameters): billing_email, company, email, twitter_username, location, name, description, blog
  • Security Boolean Fields (6 parameters): All security-related boolean settings
  • Repository Creation Fields (5 parameters): Repository and page creation permissions
  • Other Boolean Fields (3 parameters): Additional boolean settings
  • Enum Fields (1 parameter): default_repository_permission
  • Boolean False Handling: Specific tests for false value handling
  • Complex Configurations: Multi-parameter combination tests
  • Enterprise Functionality: Enterprise-specific field testing

Test Results

  • ✅ All existing tests continue to pass
  • ✅ All new comprehensive tests pass
  • ✅ No linting errors
  • ✅ Manual verification with EMU organization successful
  • ✅ Local provider build tested with dev_overrides configuration
  • ✅ Debug logging used for API payload verification

Impact

Positive Changes

  • Resolves HTTP 422 errors for all organization types (regular, enterprise, EMU)
  • Enables proper EMU management - Users can now manage Enterprise Managed User organization settings
  • Improves API efficiency - Only sends configured fields, reducing payload size
  • Maintains backward compatibility - No breaking changes to existing functionality
  • Better error handling - More robust API interaction patterns

User Benefits

  • Reliable resource management - No more random 422 failures
  • Enterprise support - Full support for enterprise and EMU organizations
  • Boolean false support - Can now properly set boolean fields to false
  • Improved performance - Smaller, more efficient API payloads

Verification

Before Fix

[ERROR] provider.terraform-provider-github_v6.2.2.exe: Response contains error diagnostic: 
PATCH https://api.github.com/orgs/ORGANIZATION: 422 []

After Fix

  • ✅ Successful resource creation and updates
  • ✅ Proper boolean false value handling
  • ✅ Enterprise field support
  • ✅ Complex configuration support

Breaking 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_settings resource with EMU organizations.

@netflash netflash force-pushed the fix-github-org-settings-422-error branch from 30bcb17 to 2b1e4a3 Compare October 23, 2025 15:21
Fixes integrations#2305

This commit resolves the HTTP 422 error that was occurring when using the
github_organization_settings resource, particularly with Enterprise Managed User (EMU)
organizations.

## Root Cause
The issue was caused by:
1. Unnecessary Organizations.Edit() call with nil parameters
2. Unconditional sending of all fields including empty strings for optional fields
3. Incorrect handling of boolean false values using d.GetOk() instead of d.GetOkExists()

## Changes Made

### Core Fixes
- Replace Organizations.Edit(ctx, org, nil) with Organizations.Get() to safely retrieve organization plan
- Create buildOrganizationSettings helper function that conditionally builds settings struct
- Use d.GetOkExists() for boolean fields to properly handle false values
- Only send explicitly configured fields to avoid API rejections

### Enterprise Support
- Properly handle Enterprise-specific fields like members_can_create_internal_repositories
- Detect enterprise organizations using plan name and conditionally include enterprise fields

### Test Coverage
- Add comprehensive test cases for all parameter types:
  * String fields (8 parameters)
  * Security boolean fields (6 parameters)
  * Repository creation fields (5 parameters)
  * Other boolean fields (3 parameters)
  * Enum fields (1 parameter)
- Add specific tests for boolean false value handling
- Add tests for complex multi-parameter configurations
- Add tests for enterprise-specific functionality

## Testing
- Manually tested with EMU organization
- Verified all parameter types work correctly
- Confirmed boolean false values are properly sent to API
- Tested complex configurations with multiple field types
- All existing and new tests pass

## Impact
- Resolves HTTP 422 errors for all organization types
- Enables proper management of EMU organization settings
- Maintains backward compatibility
- Improves API payload efficiency by only sending configured fields
@netflash netflash force-pushed the fix-github-org-settings-422-error branch from 2b1e4a3 to af0d7c8 Compare October 25, 2025 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: github_organization_settings resource fails with HTTP 422

1 participant