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

github_branch_protection required_status_checks doesn't remove contexts

Open piotr-bzdyl-vertexinc opened this issue 4 years ago • 8 comments

Terraform Version

$  terraform version 
Terraform v1.1.7
on darwin_amd64
+ provider registry.terraform.io/integrations/github v4.20.1

Affected Resource(s)

github_branch_protection

Terraform Configuration Files

terraform {
  required_version = "~> 1.1.7"
  required_providers {
    github = {
      source  = "integrations/github"
      version = "4.20.1"
    }
  }
}

provider "github" {}

resource "github_repository" "test" {
  name = "github-provider-issue"
}

resource "github_branch_protection" "main" {
  pattern       = "main"
  repository_id = github_repository.test.node_id
  required_status_checks {
    contexts = ["Test"]
  }
}

Expected Behavior

When github_branch_protection.required_status_checks.contexts is changed from a non-empty list to an empty list, all status checks are removed from the branch protection.

Actual Behavior

Terraform detects that it needs to remove a single existing entry from github_branch_protection.required_status_checks.contexts but apply doesn't actually do it and the next plan/apply detect this change again.

Steps to Reproduce

With the HCL above:

  1. terraform apply to create Github repository and branch protection
  2. Change contexts = ["Test"] to contexts = []
  3. terraform apply will show that it will remove "Test" status check and report that the modification is complete. The required status check will still be present in the branch protection in Github repository settings UI.
  4. Further terraform apply will still show that the contexts needs to be updated.

Output from example run:

$ cat main.tf
terraform {
  required_version = "~> 1.1.7"
  required_providers {
    github = {
      source  = "integrations/github"
      version = "4.20.1"
    }
  }
}

provider "github" {}

resource "github_repository" "test" {
  name = "github-provider-issue"
}

resource "github_branch_protection" "main" {
  pattern       = "main"
  repository_id = github_repository.test.node_id
  required_status_checks {
    contexts = ["Test"]
  }
}

$ terraform apply

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # github_branch_protection.main will be created
  + resource "github_branch_protection" "main" {
      + allows_deletions                = false
      + allows_force_pushes             = false
      + enforce_admins                  = false
      + id                              = (known after apply)
      + pattern                         = "main"
      + repository_id                   = (known after apply)
      + require_conversation_resolution = false
      + require_signed_commits          = false
      + required_linear_history         = false

      + required_status_checks {
          + contexts = [
              + "Test",
            ]
        }
    }

  # github_repository.test will be created
  + resource "github_repository" "test" {
      + allow_auto_merge       = false
      + allow_merge_commit     = true
      + allow_rebase_merge     = true
      + allow_squash_merge     = true
      + archived               = false
      + branches               = (known after apply)
      + default_branch         = (known after apply)
      + delete_branch_on_merge = false
      + etag                   = (known after apply)
      + full_name              = (known after apply)
      + git_clone_url          = (known after apply)
      + html_url               = (known after apply)
      + http_clone_url         = (known after apply)
      + id                     = (known after apply)
      + name                   = "github-provider-issue"
      + node_id                = (known after apply)
      + private                = (known after apply)
      + repo_id                = (known after apply)
      + ssh_clone_url          = (known after apply)
      + svn_url                = (known after apply)
      + visibility             = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

github_repository.test: Creating...
github_repository.test: Creation complete after 5s [id=github-provider-issue]
github_branch_protection.main: Creating...
github_branch_protection.main: Creation complete after 3s [id=BPR_kwDOG8cL7s4BdlnB]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

$ cat main.tf
terraform {
  required_version = "~> 1.1.7"
  required_providers {
    github = {
      source  = "integrations/github"
      version = "4.20.1"
    }
  }
}

provider "github" {}

resource "github_repository" "test" {
  name = "github-provider-issue"
}

resource "github_branch_protection" "main" {
  pattern       = "main"
  repository_id = github_repository.test.node_id
  required_status_checks {
    contexts = []
  }
}

$ terraform apply
github_repository.test: Refreshing state... [id=github-provider-issue]
github_branch_protection.main: Refreshing state... [id=BPR_kwDOG8cL7s4BdlnB]

Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the last
"terraform apply":

  # github_branch_protection.main has changed
  ~ resource "github_branch_protection" "main" {
        id                              = "BPR_kwDOG8cL7s4BdlnB"
      + push_restrictions               = []
        # (8 unchanged attributes hidden)

        # (1 unchanged block hidden)
    }


Unless you have made equivalent changes to your configuration, or ignored the relevant
attributes using ignore_changes, the following plan may include actions to undo or
respond to these changes.

─────────────────────────────────────────────────────────────────────────────────────────

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # github_branch_protection.main will be updated in-place
  ~ resource "github_branch_protection" "main" {
        id                              = "BPR_kwDOG8cL7s4BdlnB"
        # (9 unchanged attributes hidden)

      ~ required_status_checks {
          ~ contexts = [
              - "Test",
            ]
            # (1 unchanged attribute hidden)
        }
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

github_branch_protection.main: Modifying... [id=BPR_kwDOG8cL7s4BdlnB]
github_branch_protection.main: Modifications complete after 2s [id=BPR_kwDOG8cL7s4BdlnB]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

$ terraform apply
github_repository.test: Refreshing state... [id=github-provider-issue]
github_branch_protection.main: Refreshing state... [id=BPR_kwDOG8cL7s4BdlnB]

Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the last
"terraform apply":

  # github_branch_protection.main has changed
  ~ resource "github_branch_protection" "main" {
        id                              = "BPR_kwDOG8cL7s4BdlnB"
        # (9 unchanged attributes hidden)

      ~ required_status_checks {
          ~ contexts = [
              + "Test",
            ]
            # (1 unchanged attribute hidden)
        }
    }


Unless you have made equivalent changes to your configuration, or ignored the relevant
attributes using ignore_changes, the following plan may include actions to undo or
respond to these changes.

─────────────────────────────────────────────────────────────────────────────────────────

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # github_branch_protection.main will be updated in-place
  ~ resource "github_branch_protection" "main" {
        id                              = "BPR_kwDOG8cL7s4BdlnB"
        # (9 unchanged attributes hidden)

      ~ required_status_checks {
          ~ contexts = [
              - "Test",
            ]
            # (1 unchanged attribute hidden)
        }
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

github_branch_protection.main: Modifying... [id=BPR_kwDOG8cL7s4BdlnB]
github_branch_protection.main: Modifications complete after 3s [id=BPR_kwDOG8cL7s4BdlnB]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

piotr-bzdyl-vertexinc avatar Mar 04 '22 07:03 piotr-bzdyl-vertexinc

Facing same issue here, changes are detected and planned but not applied

JuanAnsio avatar Mar 29 '22 12:03 JuanAnsio

Having the same problem. It's giving us a headache since we want to enforce unified branch protection rules on all our repositories.

enricojonas avatar Apr 04 '22 07:04 enricojonas

+1 we also ran into this issue just now! Has anyone already looked into fixing this?

tobiasbueschel avatar Apr 19 '22 05:04 tobiasbueschel

I would also like to flag that contexts is depreciated on the GitHub API and it now uses a checks object that contains the context and the app_id integer. Maybe as part of this fix, the provider could be updated to allow using checks as well as currently the source of the status check defaults to the first app used, which is not ideal (and we would like to set it to all sources, which requires -1).

david-bain avatar Apr 27 '22 02:04 david-bain

Same here. Anyone has a fix? Does moving back to github_branch_protection_v3 help?

Leooo avatar May 03 '22 14:05 Leooo

+1 we also ran into this issue. Has any one found any fix or solution around this yet.

avinashsi avatar May 20 '22 08:05 avinashsi

@TheQueenIsDead mentioned this issue in #1415, but that change appears to be specific to github_branch_protection_v3, not github_branch_protection, so I think this will still be an issue after that pull request is merged.

cwthrivent avatar Dec 15 '22 00:12 cwthrivent

Ah, you're quite correct @cwthrivent , my apologies. There are quite a few issues around checks etc, I improperly filtered this one out.

TheQueenIsDead avatar Dec 15 '22 00:12 TheQueenIsDead