integrations / terraform-provider-github

Terraform GitHub provider
https://www.terraform.io/docs/providers/github/
MIT License
906 stars 746 forks source link

Cannot enable pages on an existing repository #777

Open sponte opened 3 years ago

sponte commented 3 years ago

Terraform version

v0.14.8

Affected Resource(s)

Terraform Configuration Files

resource "github_repository" "repo" {
  name        = var.repo_name
  description = var.repo_description
  visibility  = "internal"

  allow_merge_commit = false
  allow_rebase_merge = false
  allow_squash_merge = true
  archived           = var.archived
  auto_init          = true
  // github_branch_default   = "main" can only be set after creatation!!
  delete_branch_on_merge = true
  has_downloads          = false
  has_issues             = false
  has_projects           = false
  has_wiki               = false
  is_template            = var.is_template_repo
  vulnerability_alerts   = var.vulnerability_alerts

  dynamic "pages" {
    # Since dynamic blocks don't support count parameter,
    # we're using for_each with ternary operator returning
    # an array with a single item (empty string), or an empty
    # array
    for_each = var.enable_github_pages ? [""] : []

    content {
      source {
        branch = "gh-pages"
        path   = "/"
      }
    }
  }

  dynamic "template" {
    for_each = var.from_template_repo != null ? [1] : []
    content {
      owner      = "bank-of-england-technology"
      repository = var.from_template_repo
    }
  }

  lifecycle {
    ignore_changes = [auto_init]
  }
}

Debug Output


Error: PUT https://api.github.com/repos/<org>/<repo>/pages: 404 Not Found []

  on modules/repos/repos.tf line 35, in resource "github_repository" "repo":
  35: resource "github_repository" "repo" {

Expected Behavior

Provider should figure out that pages is not enabled in the repo, and use POST request instead of PUT (https://github.com/integrations/terraform-provider-github/blob/master/github/resource_github_repository.go#L520)

Actual Behavior

When running on already provisioned repository, Terraform will call updated method, and inside of of update, code checks for changes in the pages configuration, and checks if the resources is not new. If check passes, it tries to update pages configuration. However, the change in the pages config was caused by adding pages block in the first place, therefore the action should be POST, not PUT.

Excerpt from resourceGithubRepositoryUpdate function in https://github.com/integrations/terraform-provider-github/blob/master/github/resource_github_repository.go#L520

    if d.HasChange("pages") && !d.IsNewResource() {
        opts := expandPagesUpdate(d.Get("pages").([]interface{}))
        if opts != nil {
            _, err := client.Repositories.UpdatePages(ctx, owner, repoName, opts)
            if err != nil {
                return err
            }
        } else {
            _, err := client.Repositories.DisablePages(ctx, owner, repoName)
            if err != nil {
                return err
            }
        }
    }

Steps to Reproduce

  1. Create a github_repository without pages block and apply
  2. Add pages block and apply again
fcjack commented 3 years ago

I have the same problem

martinm82 commented 2 years ago

Any chance this issue get's fixed?

venkkube commented 2 years ago

Any updates on this issue ? It's been open for a long time, we need this feature on our repos.

github-actions[bot] commented 1 year ago

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

usmonster commented 1 year ago

Hello, this remains an issue and so should remain open/active. Can someone with rights please pin it? Thanks!

highb commented 1 year ago

@kfcampbell Any information on when a fix for this might be prioritized? I'm regularly getting users at my org bumping their heads on this and it makes this provider pretty frustrating to use for all parties involved. 😅

kfcampbell commented 1 year ago

@highb unfortunately GitHub's SDK team does not have the capacity to do our own feature work on the provider right now. PRs are appreciated though!