Skip to content

fix: Import-safety fixes for managed group lookups and gitlab_branch - #11

Merged
wiseelf merged 2 commits into
mainfrom
fix/import-safe-managed-group-paths
Jul 7, 2026
Merged

fix: Import-safety fixes for managed group lookups and gitlab_branch#11
wiseelf merged 2 commits into
mainfrom
fix/import-safe-managed-group-paths

Conversation

@wiseelf

@wiseelf wiseelf commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • local.managed_group_paths referenced keys(gitlab_group.parent_groups)/keys(gitlab_group.subgroups), which made data.gitlab_group.referenced's for_each depend on those resources being planned first. That's fine for plan/apply (which plan the whole graph), but breaks terraform import, which fails with Invalid for_each argument: ... known only after apply even though the underlying value is a pure function of var.gitlab_groups. Now computed directly from var.gitlab_groups, mirroring the same key expressions used by the gitlab_group.parent_groups/subgroups resources, with zero resource references.
  • gitlab_branch.this had no lifecycle { ignore_changes = [ref] }. Per the provider's own docs, ref is only populated in state at creation time and can't be read back for an existing branch — so importing a branch (or one that's diverged from its original ref) leaves ref unset in state while config still declares it, and since ref is ForceNew, the next plan destroys and recreates the branch, discarding its actual commit history.

Both surfaced from the same live import attempt against a real branch (dev/demo) that already has real commit history — recreating it would have wiped that history.

Test plan

  • terraform validate passes on the root module
  • terraform console smoke test confirms managed_group_paths/referenced_group_paths now resolve to fully known values (no (known after apply)) against synthetic var.gitlab_groups/var.gitlab_projects data
  • Confirmed against the provider's own gitlab_branch docs, which recommend this exact ignore_changes pattern for imports
  • Re-run the dev/mc-iris-demo import end-to-end to confirm both the for_each error and the destroy/recreate plan are gone

wiseelf added 2 commits July 7, 2026 17:35
…keys

managed_group_paths referenced keys(gitlab_group.parent_groups) and
keys(gitlab_group.subgroups), which feeds into referenced_group_paths and
therefore data.gitlab_group.referenced's for_each. Referencing a resource
address there makes the for_each depend on that resource being planned
first, which breaks operations that don't plan the whole graph up front --
notably terraform import, which fails with 'Invalid for_each argument:
known only after apply' even though the underlying value never actually
depends on anything computed.

Compute the same key set (group.name, or parent/name for subgroups) directly
from var.gitlab_groups instead, mirroring the gitlab_group.parent_groups/
subgroups for_each expressions without referencing those resources. Makes
managed_group_paths a pure function of input variables, safe for import,
plan, and apply alike. Verified via terraform console that referenced_group_
paths now resolves to a fully known set instead of known-after-apply.
ref is only populated in state at creation time; the provider has no way to
read it back for an existing branch. After import (or for a branch that has
since diverged from its original ref), state has ref unset while config
still declares it, and since ref is ForceNew, Terraform plans to destroy and
recreate the branch -- discarding its actual commit history. This is exactly
what the provider's own docs recommend guarding against.

Add lifecycle { ignore_changes = [ref] } to gitlab_branch.this.
@wiseelf wiseelf self-assigned this Jul 7, 2026
@wiseelf
wiseelf merged commit 0e8291b into main Jul 7, 2026
4 checks passed
@wiseelf
wiseelf deleted the fix/import-safe-managed-group-paths branch July 7, 2026 08:08
github-actions Bot pushed a commit that referenced this pull request Jul 7, 2026
## [2.0.1](v2.0.0...v2.0.1) (2026-07-07)

### Bug Fixes

* Import-safety fixes for managed group lookups and gitlab_branch ([#11](#11)) ([0e8291b](0e8291b))
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

This PR is included in version 2.0.1 🎉

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.

2 participants