terraform-provider-github icon indicating copy to clipboard operation
terraform-provider-github copied to clipboard

[MAINT] Migrate all resources and data sources to Context-aware CRUD functions

Open deiga opened this issue 1 month ago • 4 comments

Summary

This issue tracks the migration of all resources and data sources from legacy CRUD functions (Create, Read, Update, Delete) to Context-aware CRUD functions (CreateContext, ReadContext, UpdateContext, DeleteContext).

Why This Migration Is Needed

Per the Terraform Plugin SDK v2 Upgrade Guide:

  1. Deprecation: The legacy CRUD function types (CreateFunc, ReadFunc, UpdateFunc, DeleteFunc) are deprecated in favor of their context-aware counterparts
  2. Reliable Timeouts & Cancellation: Context-aware functions plumb context.Context throughout the SDK, enabling better request lifecycle management
  3. Improved Error Handling: Migration to diag.Diagnostics allows returning multiple errors and warnings while associating them with specific fields

Migration Pattern

Before (Legacy)

func resourceGithubExample() *schema.Resource {
    return &schema.Resource{
        Create: resourceGithubExampleCreate,
        Read:   resourceGithubExampleRead,
        Update: resourceGithubExampleUpdate,
        Delete: resourceGithubExampleDelete,
        // ...
    }
}

func resourceGithubExampleCreate(d *schema.ResourceData, meta interface{}) error {
    // ...
    return nil
}

After (Context-Aware)

func resourceGithubExample() *schema.Resource {
    return &schema.Resource{
        CreateContext: resourceGithubExampleCreate,
        ReadContext:   resourceGithubExampleRead,
        UpdateContext: resourceGithubExampleUpdate,
        DeleteContext: resourceGithubExampleDelete,
        // ...
    }
}

func resourceGithubExampleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
    // Use ctx in API calls for proper cancellation support
    // ...
    return nil
}

Current Status

  • Total files requiring migration: 148
  • Already migrated: 1 (data_source_github_repository_file.go)
  • Partially migrated (StateContext in Importer only): 43

Migration Checklist

Resources (77 files)

Actions

  • [ ] resource_github_actions_environment_secret.go
  • [ ] resource_github_actions_environment_variable.go
  • [ ] resource_github_actions_hosted_runner.go
  • [ ] resource_github_actions_organization_oidc_subject_claim_customization_template.go
  • [ ] resource_github_actions_organization_permissions.go
  • [ ] resource_github_actions_organization_secret.go
  • [ ] resource_github_actions_organization_secret_repositories.go
  • [ ] resource_github_actions_organization_secret_repository.go
  • [ ] resource_github_actions_organization_variable.go
  • [ ] resource_github_actions_repository_access_level.go
  • [ ] resource_github_actions_repository_oidc_subject_claim_customization_template.go
  • [ ] resource_github_actions_repository_permissions.go
  • [ ] resource_github_actions_runner_group.go
  • [ ] resource_github_actions_secret.go
  • [ ] resource_github_actions_variable.go

App Installation

  • [ ] resource_github_app_installation_repositories.go
  • [ ] resource_github_app_installation_repository.go

Branch

  • [ ] resource_github_branch.go
  • [ ] resource_github_branch_default.go
  • [ ] resource_github_branch_protection.go
  • [ ] resource_github_branch_protection_v3.go

Codespaces

  • [ ] resource_github_codespaces_organization_secret.go
  • [ ] resource_github_codespaces_organization_secret_repositories.go
  • [ ] resource_github_codespaces_secret.go
  • [ ] resource_github_codespaces_user_secret.go

Dependabot

  • [ ] resource_github_dependabot_organization_secret.go
  • [ ] resource_github_dependabot_organization_secret_repositories.go
  • [ ] resource_github_dependabot_secret.go

Enterprise

  • [ ] resource_github_enterprise_actions_permissions.go
  • [ ] resource_github_enterprise_actions_runner_group.go
  • [ ] resource_github_enterprise_actions_workflow_permissions.go
  • [ ] resource_github_enterprise_organization.go
  • [ ] resource_github_enterprise_security_analysis_settings.go

EMU

  • [ ] resource_github_emu_group_mapping.go

Issues

  • [ ] resource_github_issue.go
  • [ ] resource_github_issue_label.go
  • [ ] resource_github_issue_labels.go

Membership

  • [ ] resource_github_membership.go

Organization

  • [ ] resource_github_organization_custom_properties.go
  • [ ] resource_github_organization_custom_role.go
  • [ ] resource_github_organization_repository_role.go
  • [ ] resource_github_organization_role.go
  • [ ] resource_github_organization_role_team.go
  • [ ] resource_github_organization_role_team_assignment.go
  • [ ] resource_github_organization_role_user.go
  • [ ] resource_github_organization_ruleset.go
  • [ ] resource_github_organization_security_manager.go
  • [ ] resource_github_organization_settings.go
  • [ ] resource_github_organization_webhook.go
  • [ ] resource_organization_block.go

Release

  • [ ] resource_github_release.go

Repository

  • [ ] resource_github_repository.go
  • [ ] resource_github_repository_autolink_reference.go
  • [ ] resource_github_repository_automated_security_fixes.go
  • [ ] resource_github_repository_collaborator.go
  • [ ] resource_github_repository_collaborators.go
  • [ ] resource_github_repository_custom_property.go
  • [ ] resource_github_repository_deploy_key.go
  • [ ] resource_github_repository_deployment_branch_policy.go
  • [ ] resource_github_repository_environment.go
  • [ ] resource_github_repository_environment_deployment_policy.go
  • [ ] resource_github_repository_file.go
  • [ ] resource_github_repository_milestone.go
  • [ ] resource_github_repository_pull_request.go
  • [ ] resource_github_repository_ruleset.go
  • [ ] resource_github_repository_topics.go
  • [ ] resource_github_repository_webhook.go

Team

  • [ ] resource_github_team.go
  • [ ] resource_github_team_members.go
  • [ ] resource_github_team_membership.go
  • [ ] resource_github_team_repository.go
  • [ ] resource_github_team_settings.go
  • [ ] resource_github_team_sync_group_mapping.go

User

  • [ ] resource_github_user_gpg_key.go
  • [ ] resource_github_user_invitation_accepter.go
  • [ ] resource_github_user_ssh_key.go

Workflow

  • [ ] resource_github_workflow_repository_permissions.go

Data Sources (71 files)

Actions

  • [ ] data_source_github_actions_environment_public_key.go
  • [ ] data_source_github_actions_environment_secrets.go
  • [ ] data_source_github_actions_environment_variables.go
  • [ ] data_source_github_actions_organization_oidc_subject_claim_customization_template.go
  • [ ] data_source_github_actions_organization_public_key.go
  • [ ] data_source_github_actions_organization_registration_token.go
  • [ ] data_source_github_actions_organization_secrets.go
  • [ ] data_source_github_actions_organization_variables.go
  • [ ] data_source_github_actions_public_key.go
  • [ ] data_source_github_actions_registration_token.go
  • [ ] data_source_github_actions_repository_oidc_subject_claim_customization_template.go
  • [ ] data_source_github_actions_secrets.go
  • [ ] data_source_github_actions_variables.go

App

  • [ ] data_source_github_app.go
  • [ ] data_source_github_app_token.go

Branch

  • [ ] data_source_github_branch.go
  • [ ] data_source_github_branch_protection_rules.go

Codespaces

  • [ ] data_source_github_codespaces_organization_public_key.go
  • [ ] data_source_github_codespaces_organization_secrets.go
  • [ ] data_source_github_codespaces_public_key.go
  • [ ] data_source_github_codespaces_secrets.go
  • [ ] data_source_github_codespaces_user_public_key.go
  • [ ] data_source_github_codespaces_user_secrets.go

Collaborators

  • [ ] data_source_github_collaborators.go

Dependabot

  • [ ] data_source_github_dependabot_organization_public_key.go
  • [ ] data_source_github_dependabot_organization_secrets.go
  • [ ] data_source_github_dependabot_public_key.go
  • [ ] data_source_github_dependabot_secrets.go

Enterprise

  • [ ] data_source_github_enterprise.go

External Groups

  • [ ] data_source_github_external_groups.go

IP Ranges

  • [ ] data_source_github_ip_ranges.go

Issues

  • [ ] data_source_github_issue_labels.go

Membership

  • [ ] data_source_github_membership.go

Organization

  • [ ] data_source_github_organization.go
  • [ ] data_source_github_organization_custom_properties.go
  • [ ] data_source_github_organization_custom_role.go
  • [ ] data_source_github_organization_external_identities.go
  • [ ] data_source_github_organization_ip_allow_list.go
  • [ ] data_source_github_organization_repository_role.go
  • [ ] data_source_github_organization_repository_roles.go
  • [ ] data_source_github_organization_role.go
  • [ ] data_source_github_organization_role_teams.go
  • [ ] data_source_github_organization_role_users.go
  • [ ] data_source_github_organization_roles.go
  • [ ] data_source_github_organization_security_managers.go
  • [ ] data_source_github_organization_team_sync_groups.go
  • [ ] data_source_github_organization_teams.go
  • [ ] data_source_github_organization_webhooks.go

Ref

  • [ ] data_source_github_ref.go

Release

  • [ ] data_source_github_release.go

Repository

  • [ ] data_source_github_repositories.go
  • [ ] data_source_github_repository.go
  • [ ] data_source_github_repository_autolink_references.go
  • [ ] data_source_github_repository_branches.go
  • [ ] data_source_github_repository_custom_properties.go
  • [ ] data_source_github_repository_deploy_keys.go
  • [ ] data_source_github_repository_deployment_branch_policies.go
  • [ ] data_source_github_repository_environment_deployment_policies.go
  • [ ] data_source_github_repository_environments.go
  • [x] data_source_github_repository_file.go (Already migrated)
  • [ ] data_source_github_repository_milestone.go
  • [ ] data_source_github_repository_pull_request.go
  • [ ] data_source_github_repository_pull_requests.go
  • [ ] data_source_github_repository_teams.go
  • [ ] data_source_github_repository_webhooks.go

REST API

  • [ ] data_source_github_rest_api.go

SSH Keys

  • [ ] data_source_github_ssh_keys.go

Team

  • [ ] data_source_github_team.go

Tree

  • [ ] data_source_github_tree.go

User

  • [ ] data_source_github_user.go
  • [ ] data_source_github_user_external_identity.go
  • [ ] data_source_github_users.go

Suggested Approach

  1. Batch by domain: Group related resources/data sources and migrate them together (e.g., all Actions resources, all Repository resources)
  2. Reference the existing migration: Use data_source_github_repository_file.go as a reference implementation
  3. Update function signatures: Change CRUD function signatures to include ctx context.Context and return diag.Diagnostics
  4. Pass context to API calls: Update go-github client calls to use the context parameter
  5. Test thoroughly: Ensure acceptance tests pass after each batch migration

References

deiga avatar Dec 10 '25 17:12 deiga

👋 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 Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

github-actions[bot] avatar Dec 10 '25 17:12 github-actions[bot]

We will want to add provider scoped timeouts even if we don't expose this as a setting initially. This timeout will be important for rate limit handling, as if the delay is longer than the timeout there is no point waiting so we might as well exit early.

stevehipwell avatar Dec 10 '25 19:12 stevehipwell

We will want to add provider scoped timeouts even if we don't expose this as a setting initially. This timeout will be important for rate limit handling, as if the delay is longer than the timeout there is no point waiting so we might as well exit early.

@stevehipwell Excellent point. But I wonder if that should be a separate "Epic"?

deiga avatar Dec 10 '25 21:12 deiga

I was thinking of setting a const and requiring the timeouts to be set to that const before we check off the resource. Then it'd be much simpler to refactor it later. This is all just off the top of my head and needs a bit more investigation.

stevehipwell avatar Dec 10 '25 21:12 stevehipwell