Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempted fix for #1657 #1774

Merged

Conversation

yaakov-h
Copy link
Contributor

@yaakov-h yaakov-h commented Jul 6, 2023

Resolves #1657


Behavior

Given the following resource:

resource "github_branch_protection_v3" "myrepo-master" {
  repository     = "MyRepo"
  branch         = "master"

  # irrelevant fields omitted

  required_status_checks {
    checks = [
      # format: "<name>:<GitHub App ID>"
      "MyStatusCheck:123456"
    ]
  }
}

Before the change?

  • Generating the plan would always show too many checks
  • If a GitHub App ID was specified it would not be seen as correct:
  ~ resource "github_branch_protection_v3" "myrepo-master" {
        id                              = "MyRepo:master"
        # (6 unchanged attributes hidden)

      ~ required_status_checks {
          ~ checks         = [
              - "MyStatusCheck",
              - "MyStatusCheck:824642865624",
              + "MyStatusCheck:123456",
            ]
          ~ contexts       = [
              - "MyStatusCheck",
            ]
            # (2 unchanged attributes hidden)
        }
    }

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

After the change?

  • Status checks with GitHub App IDs are seen as valid and do not get recreated.
  • There are no extra "checks" elements
  • There are extra "contexts" elements, but this is due to server behaviour - see below.
  ~ resource "github_branch_protection_v3" "myrepo-master" {
        id                              = "MyRepo:master"
        # (6 unchanged attributes hidden)

      ~ required_status_checks {
          ~ contexts       = [
              - "MyStatusCheck",
            ]
            # (3 unchanged attributes hidden)
        }
    }

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

The GitHub API returns the contexts value, so users will either need to include this deprecated field in their HCL or set ignore_changes for required_status_checks.contexts, and then ignore the deprecation warning that the field they are ignoring is deprecated.

Other information

I'm not very experienced in Go so this can probably be done better.

I've also ignored the AppID entirely if it is omitted from JSON or is a JSON null, which can happen if a user changes the branch protection policy from requiring a specific app to allowing any GitHub app.

This will probably need some unit tests added too, though #1415 didn't have any when it added checks in the first place.


Additional info

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Added the appropriate label for the given change

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes (Please add the Type: Breaking change label)
  • No

If Yes, what's the impact:

  • N/A

Pull request type

Please add the corresponding label for change this PR introduces:

  • Bugfix: Type: Bug
  • Feature/model/API additions: Type: Feature
  • Updates to docs or samples: Type: Documentation
  • Dependencies/code cleanup: Type: Maintenance

- use app_id rather than memory address
- don't mix contexts and checks
@kfcampbell
Copy link
Member

@yaakov-h can you describe how you've tested this manually so I can replicate it? Thank you for contributing!

@domsleee
Copy link

domsleee commented Jul 7, 2023

I believe this is a valid repro: #1657 (comment)

The current behaviour is that this line is printing a pointer to the app ID as an integer:
> checks = append(contexts, fmt.Sprintf("%s:%d", chk.Context, chk.AppID))

From the end-user perspective, the App ID is "changing all the time" described in the issue. Because we are printing the pointer and not the actual value. The suggested fix here looks reasonable to me 👍

@yaakov-h
Copy link
Contributor Author

yaakov-h commented Jul 7, 2023

@kfcampbell I used the existing config I was working on (the resource I posted at the top of the PR) and used Terraform dev_overrides to use a custom build with these changes. Running terraform plan produced the output shown in "After the change".

@pgressa
Copy link

pgressa commented Jul 11, 2023

@kfcampbell is it possible to validate this one and release new version of the provider? We're migrating to v5 provider given the hundreds of the repositories we have it makes the plan pretty unusable

@kfcampbell kfcampbell merged commit 8ad3299 into integrations:main Jul 13, 2023
3 checks passed
@yaakov-h yaakov-h deleted the fix-1657-branch-protection-appid branch July 14, 2023 03:30
doonga pushed a commit to doonga/greyrock-ops that referenced this pull request Jul 15, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github](https://registry.terraform.io/providers/integrations/github)
([source](https://togithub.com/integrations/terraform-provider-github))
| required_provider | minor | `5.30.1` -> `5.31.0` |

---

### Release Notes

<details>
<summary>integrations/terraform-provider-github (github)</summary>

###
[`v5.31.0`](https://togithub.com/integrations/terraform-provider-github/releases/tag/v5.31.0)

[Compare
Source](https://togithub.com/integrations/terraform-provider-github/compare/v5.30.1...v5.31.0)

#### What's Changed

- build(deps): bump golang.org/x/oauth2 from 0.9.0 to 0.10.0 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[integrations/terraform-provider-github#1785
- 💅 Typo in docs by [@&#8203;nmacri](https://togithub.com/nmacri) in
[integrations/terraform-provider-github#1789
- Attempted fix for
[#&#8203;1657](https://togithub.com/integrations/terraform-provider-github/issues/1657)
by [@&#8203;yaakov-h](https://togithub.com/yaakov-h) in
[integrations/terraform-provider-github#1774
- Do not change allow_update_branch/has_downloads on archived repos by
[@&#8203;kristian-lesko](https://togithub.com/kristian-lesko) in
[integrations/terraform-provider-github#1795
- feat: add ability to downgrade membership when `github_membership` is
destroyed by [@&#8203;jsifuentes](https://togithub.com/jsifuentes) in
[integrations/terraform-provider-github#1783

#### New Contributors

- [@&#8203;nmacri](https://togithub.com/nmacri) made their first
contribution in
[integrations/terraform-provider-github#1789
- [@&#8203;yaakov-h](https://togithub.com/yaakov-h) made their first
contribution in
[integrations/terraform-provider-github#1774

**Full Changelog**:
integrations/terraform-provider-github@v5.30.1...v5.31.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi44LjUiLCJ1cGRhdGVkSW5WZXIiOiIzNi44LjUiLCJ0YXJnZXRCcmFuY2giOiJtYWluIn0=-->

Co-authored-by: greyrock-bot <112729691+greyrock-bot[bot]@users.noreply.github.com>
jsifuentes pushed a commit to jsifuentes/terraform-provider-github that referenced this pull request Jul 17, 2023
- use app_id rather than memory address
- don't mix contexts and checks

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
felixlut pushed a commit to felixlut/terraform-provider-github that referenced this pull request Jul 22, 2023
- use app_id rather than memory address
- don't mix contexts and checks

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
@entropitor
Copy link

entropitor commented Aug 16, 2023

The GitHub API returns the contexts value, so users will either need to include this deprecated field in their HCL or set ignore_changes for required_status_checks.contexts, and then ignore the deprecation warning that the field they are ignoring is deprecated.

@yaakov-h The problem is that if you set both contexts and checks it's trying to apply both and that fails as you can only specify one. Should we instead only look at checks if both are set while applying? The same is true for ignore_changes, even when setting contexts to [] given the server values is fetched upon refresh...

@yaakov-h
Copy link
Contributor Author

@entropitor I think if you set both then that would be consumer error, but tbh I'd be in favour of removing contexts entirely as mentioned in #1701 as that also gets rid of the problem of deprecation warnings.

avidspartan1 pushed a commit to avidspartan1/terraform-provider-github that referenced this pull request Feb 5, 2024
- use app_id rather than memory address
- don't mix contexts and checks

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
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_branch_protection_v3.required-status-checks.checks - app_id changes all the time
5 participants