Skip to content

Commit d5f10a3

Browse files
vdemeestertekton-robot
authored andcommitted
Refactor terraform imports to use for_each
Replace individual import blocks with dynamic for_each loops based on the tektoncd_repos list in locals.tf. This: - Reduces ~75 lines to ~20 lines - Automatically stays in sync with tektoncd_repos list - Adds missing release-v* branch protection imports - Makes it easier to add new repos in the future Requires Terraform >= 1.5 (already specified in versions.tf).
1 parent c0721c5 commit d5f10a3

File tree

1 file changed

+8
-110
lines changed

1 file changed

+8
-110
lines changed

terraform/branch-protection/imports.tf

Lines changed: 8 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -12,123 +12,21 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Import existing main branch protections into Terraform state
15+
# Import existing branch protections into Terraform state
1616
# These protections already exist on GitHub and need to be imported
1717
# so Terraform can manage them going forward.
1818
#
19+
# Uses for_each to dynamically import based on tektoncd_repos list.
1920
# After successful import, these blocks can be removed.
2021

2122
import {
22-
to = github_branch_protection.main["dashboard"]
23-
id = "dashboard:main"
23+
for_each = toset(local.tektoncd_repos)
24+
to = github_branch_protection.main[each.key]
25+
id = "${each.key}:main"
2426
}
2527

2628
import {
27-
to = github_branch_protection.main["pipeline"]
28-
id = "pipeline:main"
29-
}
30-
31-
import {
32-
to = github_branch_protection.main["operator"]
33-
id = "operator:main"
34-
}
35-
36-
import {
37-
to = github_branch_protection.main["mcp-server"]
38-
id = "mcp-server:main"
39-
}
40-
41-
import {
42-
to = github_branch_protection.main["triggers"]
43-
id = "triggers:main"
44-
}
45-
46-
import {
47-
to = github_branch_protection.main["cli"]
48-
id = "cli:main"
49-
}
50-
51-
import {
52-
to = github_branch_protection.main["pruner"]
53-
id = "pruner:main"
54-
}
55-
56-
import {
57-
to = github_branch_protection.main["chains"]
58-
id = "chains:main"
59-
}
60-
61-
import {
62-
to = github_branch_protection.main["hub"]
63-
id = "hub:main"
64-
}
65-
66-
import {
67-
to = github_branch_protection.main["results"]
68-
id = "results:main"
69-
}
70-
71-
import {
72-
to = github_branch_protection.main["plumbing"]
73-
id = "plumbing:main"
74-
}
75-
76-
# Import existing release branch protections (release-v*)
77-
# These were created in the first terraform-branch-protection pipeline run.
78-
#
79-
# After successful import, these blocks can be removed.
80-
81-
import {
82-
to = github_branch_protection.releases["dashboard"]
83-
id = "dashboard:release-v*"
84-
}
85-
86-
import {
87-
to = github_branch_protection.releases["pipeline"]
88-
id = "pipeline:release-v*"
89-
}
90-
91-
import {
92-
to = github_branch_protection.releases["operator"]
93-
id = "operator:release-v*"
94-
}
95-
96-
import {
97-
to = github_branch_protection.releases["mcp-server"]
98-
id = "mcp-server:release-v*"
99-
}
100-
101-
import {
102-
to = github_branch_protection.releases["triggers"]
103-
id = "triggers:release-v*"
104-
}
105-
106-
import {
107-
to = github_branch_protection.releases["cli"]
108-
id = "cli:release-v*"
109-
}
110-
111-
import {
112-
to = github_branch_protection.releases["pruner"]
113-
id = "pruner:release-v*"
114-
}
115-
116-
import {
117-
to = github_branch_protection.releases["chains"]
118-
id = "chains:release-v*"
119-
}
120-
121-
import {
122-
to = github_branch_protection.releases["hub"]
123-
id = "hub:release-v*"
124-
}
125-
126-
import {
127-
to = github_branch_protection.releases["results"]
128-
id = "results:release-v*"
129-
}
130-
131-
import {
132-
to = github_branch_protection.releases["plumbing"]
133-
id = "plumbing:release-v*"
29+
for_each = toset(local.tektoncd_repos)
30+
to = github_branch_protection.releases[each.key]
31+
id = "${each.key}:release-v*"
13432
}

0 commit comments

Comments
 (0)