fix: prevent inconsistent final plan for prevent_self_review in github_repository_environment - #3583
Conversation
|
👋 Hi, and thank you for this contribution! This repo is maintained by GitHub and community members on a best-effort basis. We'll get to this as soon as we can. You can help us prioritize by joining the discussion on open issues and PRs, sharing details on the changes you need, and reviewing other contributions. 🤖 This is an automated message. |
|
Nice, thanks! Could you add a regression acceptance test case as well? |
There was a problem hiding this comment.
Pull request overview
These provider review instructions are being used.
Fixes prevent_self_review state normalization when GitHub omits reviewer protection data.
Changes:
- Defaults
prevent_self_reviewstate tofalse. - Safely handles a nil API value.
|
@deiga Have added the test. Can you kindly review? |
|
@deiga Have resolved your PR review feedback. Can you kindly check once? |
|
@kishaningithub I ran your test without any other code changes and the test doesn't fail. Can you please provide DEBUG level logs of the issue you are trying to resolve here? |
|
@deiga The issue was with the test The original test did not reproduce the issue because it asserted the value immediately after creation. At that point, the schema default had already populated The failing scenario is a refresh/read from imported or legacy state where I replaced the test with a read-path regression test that starts with the attribute absent from state and mocks that API response. The corrected test fails without the production change and passes with it. I also removed |
b0a5831 to
7aaf5ec
Compare
7aaf5ec to
8ec4533
Compare
|
@deiga This is the exact scenario.. I have a state which is like this Result of
|
|
@deiga Given the above context. Which route do you think i must take for the fix? Any suggestions? |
|
@kishaningithub Please provide a testcase that reproduces your error-case and/or DEBUG level logs if the apply run |
8ec4533 to
bd0d53e
Compare
|
I think this PR would/should resolve #3609 as well |
|
@kishaningithub I tried creating a test case which follows your example, but I couldn't get it to fail like that |
|
@deiga Yes I tried it too.. But could not make it fail.. We need to simulate it like this somehow in the test case (which I am trying) Use provider version 6.8.3, Create the github_repository_environment without reviewers and prevent_self_review Upgrade to latest provider version setting the legacy_client provider attribute to false,Then set prevent_self_review setting it to false for this resource and run a terraform plan and apply That's my exact production scenario |
|
@kishaningithub Ah! This is about a migration between provider versions? |
|
Yes @deiga We hit this issue when upgrading the provider version from 6.8.3 to latest version disabling the legacy client |
|
@kishaningithub Have you tried pinpointing which version upgrade causes this? |
|
@deiga Thats the challenge. Putting here the exact state file entries (redacted) causing this issue for reference {
"index_key": "example_environment",
"schema_version": 1,
"attributes": {
"can_admins_bypass": true,
"deployment_branch_policy": [],
"environment": "example_environment",
"id": "example-repo:example_environment",
"prevent_self_review": null,
"repository": "example-repo",
"repository_id": 123456789,
"reviewers": [
{
"teams": null,
"users": null
}
],
"wait_timer": 0
},
"sensitive_attributes": [],
} |
…b_repository_environment
…nd use test helper
bd0d53e to
f88454d
Compare
|
@kishaningithub Thanks! I wonder how that state was even achieved, since the default has been "false" for years. But a plan/apply with 6.8.3 does not have those issue? Can you test if it already happens on 6.9 or 6.10? |
|
[For issue followers] In case you are wondering how to work around this issue without directly editing the statefile here is a possible solution (this is what i did)
curl -L \
-X DELETE \
-H "Authorization: Bearer YOUR_GITHUB_TOKEN" \
https://api.github.com/repos/OWNER/REPO/environments/ENVIRONMENT_NAME
|
Fixes an issue where Terraform reports "Provider produced inconsistent final plan" for the
prevent_self_reviewattribute ongithub_repository_environmentresources that have no reviewers configured.Resolves a missed edge case in #1967
Problem
When an environment has no reviewers, the GitHub API does not return a
required_reviewersprotection rule. The read function only setprevent_self_reviewinside thecase "required_reviewers"branch, so the attribute was never written to state—leaving it asnull. Terraform's plan expectedfalse(the schema default), causing the following mismatch:Error: Provider produced inconsistent final plan .prevent_self_review: was cty.False, but now null.
Root Cause
prevent_self_reviewwas only set in state when arequired_reviewersprotection rule existed in the API response.pr.PreventSelfReview(a*bool) was passed directly tod.Set()without nil-checking, which could also storenullin state.Fix
prevent_self_reviewtofalsebefore iterating protection rules, ensuring it always has a value in state.pr.PreventSelfReviewwith a nil guard, defaulting tofalse.Testing
Environments without reviewers (the failing case) are already exercised by the
create_with_id_separator_in_nameandupdate_to_add_reviewersacceptance tests which create environments with no initial reviewers.Pull request checklist
Does this introduce a breaking change?
Please see our docs on breaking changes to help!