Repository navigation
fix: remove vulnerability alerts from state for archived/deleted repos - #3553
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. |
a5959ba to
32b0152
Compare
There was a problem hiding this comment.
Pull request overview
These provider review instructions are being used.
Updates vulnerability-alert refresh behavior so archived or deleted repositories are removed from Terraform state rather than blocking refresh.
Changes:
- Gracefully clears state for unavailable alerts and archived/deleted repositories.
- Updates acceptance coverage for archived repositories.
- Documents archived-repository behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
github/resource_github_repository_vulnerability_alerts.go |
Updates read/error handling. |
github/resource_github_repository_vulnerability_alerts_test.go |
Tests archived-repository refresh. |
templates/resources/repository_vulnerability_alerts.md.tmpl |
Adds behavior notes. |
docs/resources/repository_vulnerability_alerts.md |
Regenerates documentation. |
Address Copilot review on integrations#3553: - Repository lookup now clears state only when it returns 404; permission, rate-limit, network, and 5xx errors are propagated instead of silently dropping valid state and forcing a recreate. - Guard resp against nil before reading StatusCode in the debug log, matching the nil guards added elsewhere in Read. - Add a deleted-repository acceptance step (out-of-band delete + refresh + non-empty plan) covering the deleted-repo path.
deiga
left a comment
There was a problem hiding this comment.
Minor change request, otherwise this is looking quite good!
The Read function for github_repository_vulnerability_alerts returned a
hard error when the underlying repository was archived or no longer
existed, wedging every subsequent plan/apply with:
repository <name> is archived, please remove the resource from your
configuration
Per the provider's own Read convention (a 404 means "remove from state"),
Read now calls d.SetId("") and returns nil in these cases instead of
erroring, so Terraform drops the resource gracefully. Also guards against
a nil *http.Response before dereferencing StatusCode, and fixes the
existing 404 branch that set the ID to "" but then returned an error
(which the SDK discards, so the removal never took effect).
Resolves integrations#3369
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Address Copilot review on integrations#3553: - Repository lookup now clears state only when it returns 404; permission, rate-limit, network, and 5xx errors are propagated instead of silently dropping valid state and forcing a recreate. - Guard resp against nil before reading StatusCode in the debug log, matching the nil guards added elsewhere in Read. - Add a deleted-repository acceptance step (out-of-band delete + refresh + non-empty plan) covering the deleted-repo path.
Co-authored-by: Timo Sand <[email protected]>
Co-authored-by: Timo Sand <[email protected]>
Read now follows ARCHITECTURE.md#error-handling: bubble errors via diag.FromErr and detect the deleted-repository case with errors.AsType[*github.ErrorResponse]. Drops the unreachable 404 branch on GetVulnerabilityAlerts (parseBoolResponse maps 404 to enabled=false, nil) and the response_status debug scaffolding. Tests: assert the recreate with plan checks (PostApplyPostRefresh for the archived case, RefreshPlanChecks.PostRefresh for the deleted case) and provision the repo out of band with mustCreateTestRepository. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
d882da7 to
9dfe90a
Compare
robert-crandall
left a comment
There was a problem hiding this comment.
Thanks for addressing the archived and deleted repository cases so thoughtfully! The state-removal behavior and added coverage look good to me.
GitHub omits merge-related settings from the repository object unless the caller has push or admin access: "To view merge-related settings, you must have the contents:read and contents:write permissions." Read through a Get* accessor, an omitted field yields the zero value, which is indistinguishable from the field genuinely being false or empty. Writing that to state produces a diff on every plan that no apply can resolve, because the value an apply would write back is not readable either. Setting only what the response actually contained leaves prior state in place instead. integrations#3144 took this approach for vulnerability_alerts and security_and_analysis, keying off the presence of security_and_analysis in the response and deprecating ignore_vulnerability_alerts_during_read as "ignored as the provider now handles lack of permissions automatically". setIfPresent extends the same treatment to the merge-related fields, and sits in util.go next to resourceKeysGetOk because the bug class is not repository-specific. Skipping a field silently would hide the trade-off, so the read logs one tflog.Warn per repository naming the fields it could not read. integrations#3553 uses the same signal for archived and deleted repositories. A user-facing warning diagnostic would fire on every plan for a token that is permanently read-only, which is noise rather than information. The archived special-case is deliberately left in place. It may be redundant — the TODO added alongside integrations#3144 doubts that GitHub omits these fields for archived repositories at all — but that is unverified, and removing it would be a second, untested behaviour change on top of this one. The cost is that drift in these fields goes undetected while the token cannot read them, which is preferable to a permanent diff no apply can clear. Verified with a scoped installation token holding administration:read and contents:read against a configuration managing nine repositories: every one reported the diff on every plan before, none do after. Reverting to the released provider reproduces it, so the binary is the only variable. Refs integrations#3382
Resolves #3369
Before the change?
resourceGithubRepositoryVulnerabilityAlertsReadreturned a hard error when the underlying repository was archived or no longer existed:Because this surfaces during refresh, every subsequent
plan/applyfails until the resource is manually removed from both config and state — you can't eventerraform state rmcleanly while the read errors. Archiving a repo auto-disables its vulnerability alerts on GitHub's side, so the resource is genuinely unmanageable at that point.The existing 404 branch also set
d.SetId("")and then returneddiag.Errorf(...); since the SDK discards state changes when Read returns an error, that removal never actually took effect.After the change?
Read now follows the provider's documented convention (
.github/instructions/schema-and-state.instructions.md: "In Read, a 404 from GitHub means 'remove from state' — calld.SetId("")and return nil"):d.SetId("")+return nil(removed the straydiag.Errorf).d.SetId("")+return nil.d.SetId("")+return nil, with atflog.Warn.Also guards against a nil
*http.Responsebefore dereferencingStatusCode.Net effect: Terraform drops the resource from state gracefully. If the config still declares it on an archived repo, the follow-up plan is non-empty (recreate), and
Createstill returns the pre-existingcannot enable vulnerability alerts on archived repositoryerror — so users get a clear signal to remove the block, instead of a wedged refresh.Pull request checklist
errors_when_reading_archived_repository→removes_from_state_when_reading_archived_repository, asserting graceful removal + non-empty plan instead of an error)Does this introduce a breaking change?
Read no longer returns an error for archived/deleted repositories; instead the resource is removed from state. This is a behavior change but not a schema breaking change.