Skip to content

Commit ec995bc

Browse files
authored
fix: Fetch only teams required for environments (#17)
Saves fetching all of the teams in the org for every repository. Especially wasted API calls when there are no environments being defined. I've never used the `environments` input before, but the following *works* with this new code and fails with the old code. So... Was the old code broken as well? ``` module "blah { ... ... environments = { production = { reviewer_teams = [ "team-1", "team-2" ] reviewer_users = [ "user-1" ] deployment_branch_policy = { protected_branches = true custom_branch_policies = false } } } }
1 parent 9577e44 commit ec995bc

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ See [variables.tf] and [examples/] for details and use-cases.
120120
| [github_repository_webhook.repository_webhook](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_webhook) | resource |
121121
| [github_team_repository.team_repository](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_repository) | resource |
122122
| [github_team_repository.team_repository_by_slug](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_repository) | resource |
123-
| [github_organization_teams.all](https://registry.terraform.io/providers/integrations/github/latest/docs/data-sources/organization_teams) | data source |
123+
| [github_team.reviewers](https://registry.terraform.io/providers/integrations/github/latest/docs/data-sources/team) | data source |
124124
| [github_user.user](https://registry.terraform.io/providers/integrations/github/latest/docs/data-sources/user) | data source |
125125

126126
### Inputs

data.tf

Lines changed: 0 additions & 13 deletions
This file was deleted.

environments.tf

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ variable "environments" {
1414
default = {}
1515
}
1616

17+
locals {
18+
required_team_slugs = toset(flatten([
19+
for env_name, env_config in var.environments : env_config.reviewer_teams
20+
]))
21+
required_usernames = toset(flatten([
22+
for env_name, env_config in var.environments : env_config.reviewer_users
23+
]))
24+
team_ids_by_slug = { for t in data.github_team.reviewers : t.slug => t.id }
25+
user_ids_by_name = { for u in data.github_user.user : u.username => u.id }
26+
}
27+
28+
data "github_team" "reviewers" {
29+
for_each = local.required_team_slugs
30+
slug = each.key
31+
}
32+
33+
data "github_user" "user" {
34+
for_each = local.required_usernames
35+
username = each.key
36+
}
37+
1738
resource "github_repository_environment" "this" {
1839
for_each = var.environments
1940
repository = github_repository.repository.name
@@ -24,7 +45,7 @@ resource "github_repository_environment" "this" {
2445
dynamic "reviewers" {
2546
for_each = length(each.value.reviewer_teams) > 0 || length(each.value.reviewer_users) > 0 ? [true] : []
2647
content {
27-
teams = [for slug in each.value.reviewer_teams : try(local.team_ids_by_slug[slug], slug)]
48+
teams = [for slug in each.value.reviewer_teams : local.team_ids_by_slug[slug]]
2849
users = [for username in each.value.reviewer_users : local.user_ids_by_name[username]]
2950
}
3051
}

0 commit comments

Comments
 (0)