Skip to content

[BUG]: 422 error: Race condition updating visibility to internal after template repo creation (no retry/backoff on PATCH) #3302

@milagrofrost

Description

@milagrofrost

Expected Behavior

When creating a repository from a template with visibility = "internal", the operation should complete successfully without spurious 422 errors, and the desired visibility should be set reliably by Terraform. The provider should handle any normal GitHub-side settling delays internally (via retry/backoff) so users do not need to tune write_delay_ms or split operations across multiple applies.

Actual Behavior

Creating a repo from a template with visibility = "internal" frequently fails with:

Error: PATCH https://api.github.com/repos/[org]/[repo]: 422 Failed to update visibility. A previous repository operation is still in progress. []

This is because the provider uses the template generate endpoint (which only supports 'private', not 'internal'), and then immediately issues a PATCH to set 'internal' visibility without any retry/backoff when GitHub is not ready. Users need to add large write_delay_ms to get reliable applies, but is not feasible for this one issue.

Terraform Version

Terraform v1.9.5

  • provider registry.terraform.io/integrations/github v6.11.1 (and v6.11.0)

Reproducible on current releases.

Affected Resource(s)

  • github_repository (template create + visibility = "internal")

Terraform Configuration Files

resource "github_repository" "team_repos" {
  name        = "my-repo"
  template {
    owner      = "my-org"
    repository = "template-repo"
  }
  visibility     = "internal"
  auto_init      = true
  # additional fields as needed
}

Steps to Reproduce

  1. Create a template repo (must be "internal" or "private")
  2. Use github_repository resource with that template and visibility = "internal"
  3. terraform apply

Result:
Randomly (often), the operation fails with 422 error above if provider attempts to PATCH visibility too quickly after repo creation.

Debug Output

Terraform apply output:

Error: PATCH https://api.github.com/repos/my-org/my-repo: 422 Failed to update visibility. A previous repository operation is still in progress. []

This repeats unless write_delay_ms is set to large values (2-10 seconds or more).

The provider code path is resourceGithubRepositoryCreate() followed by resourceGithubRepositoryUpdate() which issues the PATCH for visibility = "internal" with no retry.

Proposed Fix:
- In resourceGithubRepositoryUpdate(), add retry/backoff for PATCH /repos/:org/:repo when 422 'previous repository operation' is returned for visibility changes after template creation.
- See also PR #3123 (fixed template create, but not the PATCH retry).

Panic Output

N/A

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: TriageThis is being looked at and prioritizedType: BugSomething isn't working as documented

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions