|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + gitlab = { |
| 4 | + source = "gitlabhq/gitlab" |
| 5 | + version = "~> 17.0" |
| 6 | + } |
| 7 | + null = { |
| 8 | + source = "hashicorp/null" |
| 9 | + } |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +provider "gitlab" { |
| 14 | + token = var.gitlab_token |
| 15 | + base_url = var.gitlab_base_url |
| 16 | +} |
| 17 | + |
| 18 | +locals { |
| 19 | + group_path = lower(replace(var.group_name, " ", "-")) |
| 20 | + |
| 21 | + project_map = { |
| 22 | + core = "Core" |
| 23 | + platform = "Platform" |
| 24 | + data = "Data" |
| 25 | + infra = "Infra" |
| 26 | + security = "Security" |
| 27 | + mobile = "Mobile" |
| 28 | + web = "Web" |
| 29 | + integrations = "Integrations" |
| 30 | + payments = "Payments" |
| 31 | + reliability = "Reliability" |
| 32 | + } |
| 33 | + |
| 34 | + dry_run_flag = var.enable_seed_creation ? "" : "--dry-run" |
| 35 | + comments_flag = var.enable_comments ? "--enable-comments" : "" |
| 36 | + pipelines_flag = var.enable_pipelines ? "" : "--disable-pipelines" |
| 37 | + merge_requests_flag = var.enable_merge_requests ? "" : "--disable-merge-requests" |
| 38 | + releases_flag = var.enable_releases ? "" : "--disable-releases" |
| 39 | + start_date_flag = var.provision_start_date != "" ? "--start-date ${var.provision_start_date}" : "" |
| 40 | + end_date_flag = var.provision_end_date != "" ? "--end-date ${var.provision_end_date}" : "" |
| 41 | + monthly_issue_flag = var.monthly_issue_count != 0 ? "--monthly-issue-count ${var.monthly_issue_count}" : "" |
| 42 | + reviewers_flag = length(var.reviewer_usernames) > 0 ? "--reviewers ${join(",", var.reviewer_usernames)}" : "" |
| 43 | + date_range_valid = var.provision_end_date == "" || var.provision_start_date != "" |
| 44 | +} |
| 45 | + |
| 46 | +module "gitlab_structure" { |
| 47 | + source = "./modules/gitlab_structure" |
| 48 | + |
| 49 | + group_name = var.group_name |
| 50 | + group_path = local.group_path |
| 51 | + project_map = local.project_map |
| 52 | + enable_group_creation = var.enable_group_creation |
| 53 | + enable_project_creation = var.enable_project_creation |
| 54 | + project_visibility = var.project_visibility |
| 55 | +} |
| 56 | + |
| 57 | +resource "null_resource" "seed" { |
| 58 | + depends_on = [module.gitlab_structure] |
| 59 | + |
| 60 | + lifecycle { |
| 61 | + precondition { |
| 62 | + condition = local.date_range_valid |
| 63 | + error_message = "provision_start_date is required when provision_end_date is set." |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + triggers = { |
| 68 | + story_map_hash = filesha256("${path.module}/seed/story_map.yaml") |
| 69 | + script_hash = filesha256("${path.module}/seed/seed_gitlab.py") |
| 70 | + seed_hash = sha256(var.seed_string) |
| 71 | + batch_size = tostring(var.batch_size) |
| 72 | + dry_run = tostring(var.enable_seed_creation) |
| 73 | + start_date = var.provision_start_date |
| 74 | + end_date = var.provision_end_date |
| 75 | + monthly_issues = tostring(var.monthly_issue_count) |
| 76 | + } |
| 77 | + |
| 78 | + provisioner "local-exec" { |
| 79 | + command = join(" ", compact([ |
| 80 | + "pip3 install -q -r ${path.module}/seed/requirements.txt &&", |
| 81 | + "mkdir -p ${path.module}/out &&", |
| 82 | + "python3", |
| 83 | + "${path.module}/seed/seed_gitlab.py", |
| 84 | + "--base-url", var.gitlab_base_url, |
| 85 | + "--group-path", module.gitlab_structure.group_path, |
| 86 | + "--story", "${path.module}/seed/story_map.yaml", |
| 87 | + "--manifest", "${path.module}/out/manifest.json", |
| 88 | + "--seed", var.seed_string, |
| 89 | + "--batch-size", tostring(var.batch_size), |
| 90 | + local.start_date_flag, |
| 91 | + local.end_date_flag, |
| 92 | + local.monthly_issue_flag, |
| 93 | + local.reviewers_flag, |
| 94 | + local.dry_run_flag, |
| 95 | + local.comments_flag, |
| 96 | + local.pipelines_flag, |
| 97 | + local.merge_requests_flag, |
| 98 | + local.releases_flag, |
| 99 | + ])) |
| 100 | + |
| 101 | + environment = { |
| 102 | + GITLAB_TOKEN = var.gitlab_token |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments